i made the javascripts faster with a transformer prepass to check what transformers actually have to be ran

This commit is contained in:
Sebastian McKenzie
2015-02-04 12:56:34 +11:00
parent ffc9244f88
commit 7d950cd60a
30 changed files with 223 additions and 95 deletions

View File

@@ -10,11 +10,12 @@ var contains = require("lodash/collection/contains");
var flatten = require("lodash/array/flatten");
var compact = require("lodash/array/compact");
function TraversalContext() {
function TraversalContext(scope) {
this.shouldFlatten = false;
this.shouldRemove = false;
this.shouldSkip = false;
this.shouldStop = false;
this.scope = scope;
}
TraversalContext.prototype.flatten = function () {
@@ -44,20 +45,31 @@ TraversalContext.prototype.reset = function () {
TraversalContext.prototype.maybeRemove = function (obj, key) {
if (this.shouldRemove) {
obj[key] = null;
this.shouldFlatten = true;
this.flatten();
}
};
function replaceNode(obj, key, node, result) {
var isArray = Array.isArray(result);
TraversalContext.prototype.replaceNode = function (obj, key, node, replacement, scope) {
var isArray = Array.isArray(replacement);
// inherit comments from original node to the first replacement node
var inheritTo = result;
if (isArray) inheritTo = result[0];
var inheritTo = replacement;
if (isArray) inheritTo = replacement[0];
if (inheritTo) t.inheritsComments(inheritTo, node);
// replace the node
obj[key] = result;
obj[key] = replacement;
var file = this.scope && this.scope.file;
if (file) {
if (isArray) {
for (var i = 0; i < replacement.length; i++) {
file.checkNode(replacement[i], scope);
}
} else {
file.checkNode(replacement, scope);
}
}
// we're replacing a statement or block node with an array of statements so we better
// ensure that it's a block
@@ -66,39 +78,16 @@ function replaceNode(obj, key, node, result) {
}
if (isArray) {
return true;
this.flatten();
}
}
TraversalContext.prototype.enterNode = function (obj, key, node, enter, parent, scope, state) {
var result = enter(node, parent, scope, this, state);
var flatten = false;
if (result) {
flatten = replaceNode(obj, key, node, result);
node = result;
if (flatten) {
this.shouldFlatten = true;
}
}
this.maybeRemove(obj, key);
return node;
};
TraversalContext.prototype.exitNode = function (obj, key, node, exit, parent, scope, state) {
var result = exit(node, parent, scope, this, state);
var flatten = false;
TraversalContext.prototype.call = function (fn, obj, key, node, parent, scope, state) {
var replacement = fn(node, parent, scope, this, state);
if (result) {
flatten = replaceNode(obj, key, node, result);
node = result;
if (flatten) {
this.shouldFlatten = true;
}
if (replacement) {
this.replaceNode(obj, key, node, replacement, scope);
node = replacement;
}
this.maybeRemove(obj, key);
@@ -122,7 +111,7 @@ TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent,
ourScope = new Scope(node, parent, scope);
}
node = this.enterNode(obj, key, node, opts.enter, parent, ourScope, state);
node = this.call(opts.enter, obj, key, node, parent, ourScope, state);
if (this.shouldSkip) {
return this.shouldStop;
@@ -136,7 +125,7 @@ TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent,
}
} else {
traverseNode(node, opts, ourScope, state);
this.exitNode(obj, key, node, opts.exit, parent, ourScope, state);
this.call(opts.exit, obj, key, node, parent, ourScope, state);
}
return this.shouldStop;
@@ -174,7 +163,7 @@ function traverseNode(node, opts, scope, state) {
var keys = t.VISITOR_KEYS[node.type];
if (!keys) return;
var context = new TraversalContext();
var context = new TraversalContext(scope);
for (var i = 0; i < keys.length; i++) {
if (context.visit(node, keys[i], opts, scope, state)) {
return;

View File

@@ -35,6 +35,18 @@ function Scope(block, parentBlock, parent, file) {
Scope.defaultDeclarations = flatten([globals.builtin, globals.browser, globals.node].map(Object.keys));
/**
* Description
*
* @param {Object} node
* @param {Object} opts
* @param [state]
*/
Scope.prototype.traverse = function (node, opts, state) {
traverse(node, opts, this, state);
};
/**
* Description
*