i made the javascripts faster with a transformer prepass to check what transformers actually have to be ran
This commit is contained in:
@@ -9,6 +9,7 @@ var transform = require("./transformation");
|
|||||||
var generate = require("./generation");
|
var generate = require("./generation");
|
||||||
var defaults = require("lodash/object/defaults");
|
var defaults = require("lodash/object/defaults");
|
||||||
var contains = require("lodash/collection/contains");
|
var contains = require("lodash/collection/contains");
|
||||||
|
var traverse = require("./traverse");
|
||||||
var clone = require("./helpers/clone");
|
var clone = require("./helpers/clone");
|
||||||
var Scope = require("./traverse/scope");
|
var Scope = require("./traverse/scope");
|
||||||
var util = require("./util");
|
var util = require("./util");
|
||||||
@@ -352,6 +353,8 @@ File.prototype.transform = function (ast) {
|
|||||||
modFormatter.init();
|
modFormatter.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.checkNode(ast);
|
||||||
|
|
||||||
var astRun = function (key) {
|
var astRun = function (key) {
|
||||||
each(self.transformerStack, function (pass) {
|
each(self.transformerStack, function (pass) {
|
||||||
pass.astRun(key);
|
pass.astRun(key);
|
||||||
@@ -367,6 +370,30 @@ File.prototype.transform = function (ast) {
|
|||||||
astRun("exit");
|
astRun("exit");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var checkTransformerVisitor = {
|
||||||
|
enter: function (node, parent, scope, context, state) {
|
||||||
|
state.check(node, scope);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
File.prototype.checkNode = function (node, scope) {
|
||||||
|
var self = this;
|
||||||
|
scope = scope || this.scope;
|
||||||
|
|
||||||
|
var check = function (node, scope) {
|
||||||
|
each(self.transformerStack, function (pass) {
|
||||||
|
if (pass.shouldRun) return;
|
||||||
|
pass.checkNode(node, scope);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
check(node, scope);
|
||||||
|
|
||||||
|
traverse(node, checkTransformerVisitor, scope, {
|
||||||
|
check: check
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
File.prototype.generate = function () {
|
File.prototype.generate = function () {
|
||||||
var opts = this.opts;
|
var opts = this.opts;
|
||||||
var ast = this.ast;
|
var ast = this.ast;
|
||||||
|
|||||||
@@ -94,8 +94,10 @@ exports.Literal = function (node) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.push(val);
|
this.push(val);
|
||||||
} else if (type === "boolean" || type === "number") {
|
} else if (type === "number") {
|
||||||
this.push(JSON.stringify(val));
|
this.push(val + "");
|
||||||
|
} else if (type === "boolean" ) {
|
||||||
|
this.push(val ? "true" : "false");
|
||||||
} else if (node.regex) {
|
} else if (node.regex) {
|
||||||
this.push("/" + node.regex.pattern + "/" + node.regex.flags);
|
this.push("/" + node.regex.pattern + "/" + node.regex.flags);
|
||||||
} else if (val === null) {
|
} else if (val === null) {
|
||||||
|
|||||||
@@ -11,11 +11,14 @@ var contains = require("lodash/collection/contains");
|
|||||||
|
|
||||||
function TransformerPass(file, transformer) {
|
function TransformerPass(file, transformer) {
|
||||||
this.transformer = transformer;
|
this.transformer = transformer;
|
||||||
|
this.shouldRun = !transformer.check;
|
||||||
this.handlers = transformer.handlers;
|
this.handlers = transformer.handlers;
|
||||||
this.file = file;
|
this.file = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
TransformerPass.prototype.astRun = function (key) {
|
TransformerPass.prototype.astRun = function (key) {
|
||||||
|
if (!this.shouldRun) return;
|
||||||
|
|
||||||
var handlers = this.handlers;
|
var handlers = this.handlers;
|
||||||
var file = this.file;
|
var file = this.file;
|
||||||
|
|
||||||
@@ -29,23 +32,39 @@ TransformerPass.prototype.canRun = function () {
|
|||||||
|
|
||||||
var opts = this.file.opts;
|
var opts = this.file.opts;
|
||||||
var key = transformer.key;
|
var key = transformer.key;
|
||||||
|
|
||||||
|
// internal
|
||||||
if (key[0] === "_") return true;
|
if (key[0] === "_") return true;
|
||||||
|
|
||||||
|
// blacklist
|
||||||
var blacklist = opts.blacklist;
|
var blacklist = opts.blacklist;
|
||||||
if (blacklist.length && contains(blacklist, key)) return false;
|
if (blacklist.length && contains(blacklist, key)) return false;
|
||||||
|
|
||||||
|
// whitelist
|
||||||
var whitelist = opts.whitelist;
|
var whitelist = opts.whitelist;
|
||||||
if (whitelist.length && !contains(whitelist, key)) return false;
|
if (whitelist.length && !contains(whitelist, key)) return false;
|
||||||
|
|
||||||
|
// optional
|
||||||
if (transformer.optional && !contains(opts.optional, key)) return false;
|
if (transformer.optional && !contains(opts.optional, key)) return false;
|
||||||
|
|
||||||
|
// experimental
|
||||||
if (transformer.experimental && !opts.experimental) return false;
|
if (transformer.experimental && !opts.experimental) return false;
|
||||||
|
|
||||||
|
// playground
|
||||||
if (transformer.playground && !opts.playground) return false;
|
if (transformer.playground && !opts.playground) return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
TransformerPass.prototype.checkNode = function (node) {
|
||||||
|
var check = this.transformer.check;
|
||||||
|
if (check) {
|
||||||
|
return this.shouldRun = check(node);
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
var transformVisitor = {
|
var transformVisitor = {
|
||||||
enter: function (node, parent, scope, context, state) {
|
enter: function (node, parent, scope, context, state) {
|
||||||
var fns = state.handlers[node.type];
|
var fns = state.handlers[node.type];
|
||||||
@@ -61,6 +80,8 @@ var transformVisitor = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
TransformerPass.prototype.transform = function () {
|
TransformerPass.prototype.transform = function () {
|
||||||
|
if (!this.shouldRun) return;
|
||||||
|
|
||||||
var file = this.file;
|
var file = this.file;
|
||||||
|
|
||||||
util.debug(file.opts.filename + ": Running transformer " + this.transformer.key);
|
util.debug(file.opts.filename + ": Running transformer " + this.transformer.key);
|
||||||
|
|||||||
@@ -16,13 +16,16 @@ var each = require("lodash/collection/each");
|
|||||||
|
|
||||||
function Transformer(key, transformer, opts) {
|
function Transformer(key, transformer, opts) {
|
||||||
this.manipulateOptions = transformer.manipulateOptions;
|
this.manipulateOptions = transformer.manipulateOptions;
|
||||||
this.experimental = !!transformer.experimental;
|
this.check = transformer.check;
|
||||||
this.playground = !!transformer.playground;
|
|
||||||
this.secondPass = !!transformer.secondPass;
|
this.experimental = !!transformer.experimental;
|
||||||
this.optional = !!transformer.optional;
|
this.playground = !!transformer.playground;
|
||||||
this.handlers = this.normalize(transformer);
|
this.secondPass = !!transformer.secondPass;
|
||||||
this.opts = opts || {};
|
this.optional = !!transformer.optional;
|
||||||
this.key = key;
|
|
||||||
|
this.handlers = this.normalize(transformer);
|
||||||
|
this.opts = opts || {};
|
||||||
|
this.key = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
Transformer.prototype.normalize = function (transformer) {
|
Transformer.prototype.normalize = function (transformer) {
|
||||||
|
|||||||
@@ -3,6 +3,10 @@
|
|||||||
var defineMap = require("../../helpers/define-map");
|
var defineMap = require("../../helpers/define-map");
|
||||||
var t = require("../../../types");
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = function (node) {
|
||||||
|
return t.isProperty(node) && (node.kind === "get" || node.kind === "set");
|
||||||
|
};
|
||||||
|
|
||||||
exports.ObjectExpression = function (node) {
|
exports.ObjectExpression = function (node) {
|
||||||
var mutatorMap = {};
|
var mutatorMap = {};
|
||||||
var hasAny = false;
|
var hasAny = false;
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
var t = require("../../../types");
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = t.isArrowFunctionExpression;
|
||||||
|
|
||||||
exports.ArrowFunctionExpression = function (node) {
|
exports.ArrowFunctionExpression = function (node) {
|
||||||
t.ensureBlock(node);
|
t.ensureBlock(node);
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ var t = require("../../../types");
|
|||||||
var values = require("lodash/object/values");
|
var values = require("lodash/object/values");
|
||||||
var extend = require("lodash/object/extend");
|
var extend = require("lodash/object/extend");
|
||||||
|
|
||||||
|
exports.check = function (node) {
|
||||||
|
return t.isVariableDeclaration(node) && (node.kind === "let" || node.kind === "const");
|
||||||
|
};
|
||||||
|
|
||||||
var isLet = function (node, parent) {
|
var isLet = function (node, parent) {
|
||||||
if (!t.isVariableDeclaration(node)) return false;
|
if (!t.isVariableDeclaration(node)) return false;
|
||||||
if (node._let) return true;
|
if (node._let) return true;
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ var defineMap = require("../../helpers/define-map");
|
|||||||
var util = require("../../../util");
|
var util = require("../../../util");
|
||||||
var t = require("../../../types");
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = t.isClass;
|
||||||
|
|
||||||
exports.ClassDeclaration = function (node, parent, scope, context, file) {
|
exports.ClassDeclaration = function (node, parent, scope, context, file) {
|
||||||
return new Class(node, file, scope, true).run();
|
return new Class(node, file, scope, true).run();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,6 +3,10 @@
|
|||||||
var traverse = require("../../../traverse");
|
var traverse = require("../../../traverse");
|
||||||
var t = require("../../../types");
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = function (node) {
|
||||||
|
return t.isVariableDeclaration(node, { kind: "const" });
|
||||||
|
};
|
||||||
|
|
||||||
var visitor = {
|
var visitor = {
|
||||||
enter: function (node, parent, scope, context, state) {
|
enter: function (node, parent, scope, context, state) {
|
||||||
if (t.isAssignmentExpression(node) || t.isUpdateExpression(node)) {
|
if (t.isAssignmentExpression(node) || t.isUpdateExpression(node)) {
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
var t = require("../../../types");
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = t.isPattern;
|
||||||
|
|
||||||
var buildVariableAssign = function (opts, id, init) {
|
var buildVariableAssign = function (opts, id, init) {
|
||||||
var op = opts.operator;
|
var op = opts.operator;
|
||||||
if (t.isMemberExpression(id)) op = "=";
|
if (t.isMemberExpression(id)) op = "=";
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
var util = require("../../../util");
|
var util = require("../../../util");
|
||||||
var t = require("../../../types");
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = t.isForOfStatement;
|
||||||
|
|
||||||
exports.ForOfStatement = function (node, parent, scope, context, file) {
|
exports.ForOfStatement = function (node, parent, scope, context, file) {
|
||||||
var callback = spec;
|
var callback = spec;
|
||||||
if (file.isLoose("es6.forOf")) callback = loose;
|
if (file.isLoose("es6.forOf")) callback = loose;
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
var t = require("../../../types");
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = require("../internal/modules").check;
|
||||||
|
|
||||||
exports.ImportDeclaration = function (node, parent, scope, context, file) {
|
exports.ImportDeclaration = function (node, parent, scope, context, file) {
|
||||||
var nodes = [];
|
var nodes = [];
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,10 @@ var traverse = require("../../../traverse");
|
|||||||
var util = require("../../../util");
|
var util = require("../../../util");
|
||||||
var t = require("../../../types");
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = function (node) {
|
||||||
|
return t.isFunction(node) && hasDefaults(node);
|
||||||
|
};
|
||||||
|
|
||||||
var hasDefaults = function (node) {
|
var hasDefaults = function (node) {
|
||||||
for (var i = 0; i < node.params.length; i++) {
|
for (var i = 0; i < node.params.length; i++) {
|
||||||
if (t.isAssignmentPattern(node.params[i])) return true;
|
if (t.isAssignmentPattern(node.params[i])) return true;
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
var util = require("../../../util");
|
var util = require("../../../util");
|
||||||
var t = require("../../../types");
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = t.isRestElement;
|
||||||
|
|
||||||
var hasRest = function (node) {
|
var hasRest = function (node) {
|
||||||
return t.isRestElement(node.params[node.params.length - 1]);
|
return t.isRestElement(node.params[node.params.length - 1]);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
var t = require("../../../types");
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = function (node) {
|
||||||
|
return t.isProperty(node) && node.computed;
|
||||||
|
};
|
||||||
|
|
||||||
exports.ObjectExpression = function (node, parent, scope, context, file) {
|
exports.ObjectExpression = function (node, parent, scope, context, file) {
|
||||||
var hasComputed = false;
|
var hasComputed = false;
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,10 @@ var nameMethod = require("../../helpers/name-method");
|
|||||||
var t = require("../../../types");
|
var t = require("../../../types");
|
||||||
var clone = require("lodash/lang/clone");
|
var clone = require("lodash/lang/clone");
|
||||||
|
|
||||||
|
exports.check = function (node) {
|
||||||
|
return t.isProperty(node) && (node.method || node.shorthand);
|
||||||
|
};
|
||||||
|
|
||||||
exports.Property = function (node, parent, scope, context, file) {
|
exports.Property = function (node, parent, scope, context, file) {
|
||||||
if (node.method) {
|
if (node.method) {
|
||||||
node.method = false;
|
node.method = false;
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var t = require("../../../types");
|
|
||||||
var contains = require("lodash/collection/contains");
|
var contains = require("lodash/collection/contains");
|
||||||
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = t.isSpreadElement;
|
||||||
|
|
||||||
var getSpreadLiteral = function (spread, file) {
|
var getSpreadLiteral = function (spread, file) {
|
||||||
return file.toArray(spread.argument);
|
return file.toArray(spread.argument);
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ var buildBinaryExpression = function (left, right) {
|
|||||||
return t.binaryExpression("+", left, right);
|
return t.binaryExpression("+", left, right);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.check = function (node) {
|
||||||
|
return t.isTemplateLiteral(node) || t.isTaggedTemplateExpression(node);
|
||||||
|
};
|
||||||
|
|
||||||
exports.TaggedTemplateExpression = function (node, parent, scope, context, file) {
|
exports.TaggedTemplateExpression = function (node, parent, scope, context, file) {
|
||||||
var args = [];
|
var args = [];
|
||||||
var quasi = node.quasi;
|
var quasi = node.quasi;
|
||||||
|
|||||||
@@ -2,6 +2,11 @@
|
|||||||
|
|
||||||
var rewritePattern = require("regexpu/rewrite-pattern");
|
var rewritePattern = require("regexpu/rewrite-pattern");
|
||||||
var pull = require("lodash/array/pull");
|
var pull = require("lodash/array/pull");
|
||||||
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = function (node) {
|
||||||
|
return t.isLiteral(node) && node.regex && node.regex.flags.indexOf("u") >= 0;
|
||||||
|
};
|
||||||
|
|
||||||
exports.Literal = function (node) {
|
exports.Literal = function (node) {
|
||||||
var regex = node.regex;
|
var regex = node.regex;
|
||||||
|
|||||||
@@ -15,6 +15,10 @@ var resolveModuleSource = function (node, parent, scope, context, file) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.check = function (node) {
|
||||||
|
return t.isImportDeclaration(node) || t.isExportDeclaration(node);
|
||||||
|
};
|
||||||
|
|
||||||
exports.ImportDeclaration = resolveModuleSource;
|
exports.ImportDeclaration = resolveModuleSource;
|
||||||
|
|
||||||
exports.ExportDeclaration = function (node, parent, scope) {
|
exports.ExportDeclaration = function (node, parent, scope) {
|
||||||
|
|||||||
@@ -10,6 +10,12 @@ var esutils = require("esutils");
|
|||||||
var react = require("../../helpers/react");
|
var react = require("../../helpers/react");
|
||||||
var t = require("../../../types");
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = function (node) {
|
||||||
|
if (t.isJSX(node)) return true;
|
||||||
|
if (react.isCreateClass(node)) return true;
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
exports.JSXIdentifier = function (node, parent) {
|
exports.JSXIdentifier = function (node, parent) {
|
||||||
if (node.name === "this" && t.isReferenced(node, parent)) {
|
if (node.name === "this" && t.isReferenced(node, parent)) {
|
||||||
return t.thisExpression();
|
return t.thisExpression();
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var regenerator = require("regenerator-6to5");
|
var regenerator = require("regenerator-6to5");
|
||||||
|
var t = require("../../../types");
|
||||||
|
|
||||||
|
exports.check = function (node) {
|
||||||
|
return t.isFunction(node) && (node.async || node.generator);
|
||||||
|
};
|
||||||
|
|
||||||
exports.ast = {
|
exports.ast = {
|
||||||
before: function (ast, file) {
|
before: function (ast, file) {
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ exports.Property =
|
|||||||
exports.MethodDefinition = function (node, parent, scope, context, file) {
|
exports.MethodDefinition = function (node, parent, scope, context, file) {
|
||||||
if (node.kind !== "memo") return;
|
if (node.kind !== "memo") return;
|
||||||
node.kind = "get";
|
node.kind = "get";
|
||||||
|
file.checkNode(node, scope);
|
||||||
|
|
||||||
var value = node.value;
|
var value = node.value;
|
||||||
t.ensureBlock(value);
|
t.ensureBlock(value);
|
||||||
|
|||||||
@@ -10,11 +10,12 @@ var contains = require("lodash/collection/contains");
|
|||||||
var flatten = require("lodash/array/flatten");
|
var flatten = require("lodash/array/flatten");
|
||||||
var compact = require("lodash/array/compact");
|
var compact = require("lodash/array/compact");
|
||||||
|
|
||||||
function TraversalContext() {
|
function TraversalContext(scope) {
|
||||||
this.shouldFlatten = false;
|
this.shouldFlatten = false;
|
||||||
this.shouldRemove = false;
|
this.shouldRemove = false;
|
||||||
this.shouldSkip = false;
|
this.shouldSkip = false;
|
||||||
this.shouldStop = false;
|
this.shouldStop = false;
|
||||||
|
this.scope = scope;
|
||||||
}
|
}
|
||||||
|
|
||||||
TraversalContext.prototype.flatten = function () {
|
TraversalContext.prototype.flatten = function () {
|
||||||
@@ -44,20 +45,31 @@ TraversalContext.prototype.reset = function () {
|
|||||||
TraversalContext.prototype.maybeRemove = function (obj, key) {
|
TraversalContext.prototype.maybeRemove = function (obj, key) {
|
||||||
if (this.shouldRemove) {
|
if (this.shouldRemove) {
|
||||||
obj[key] = null;
|
obj[key] = null;
|
||||||
this.shouldFlatten = true;
|
this.flatten();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function replaceNode(obj, key, node, result) {
|
TraversalContext.prototype.replaceNode = function (obj, key, node, replacement, scope) {
|
||||||
var isArray = Array.isArray(result);
|
var isArray = Array.isArray(replacement);
|
||||||
|
|
||||||
// inherit comments from original node to the first replacement node
|
// inherit comments from original node to the first replacement node
|
||||||
var inheritTo = result;
|
var inheritTo = replacement;
|
||||||
if (isArray) inheritTo = result[0];
|
if (isArray) inheritTo = replacement[0];
|
||||||
if (inheritTo) t.inheritsComments(inheritTo, node);
|
if (inheritTo) t.inheritsComments(inheritTo, node);
|
||||||
|
|
||||||
// replace the 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
|
// we're replacing a statement or block node with an array of statements so we better
|
||||||
// ensure that it's a block
|
// ensure that it's a block
|
||||||
@@ -66,39 +78,16 @@ function replaceNode(obj, key, node, result) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isArray) {
|
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) {
|
TraversalContext.prototype.call = function (fn, obj, key, node, parent, scope, state) {
|
||||||
var result = exit(node, parent, scope, this, state);
|
var replacement = fn(node, parent, scope, this, state);
|
||||||
var flatten = false;
|
|
||||||
|
|
||||||
if (result) {
|
if (replacement) {
|
||||||
flatten = replaceNode(obj, key, node, result);
|
this.replaceNode(obj, key, node, replacement, scope);
|
||||||
node = result;
|
node = replacement;
|
||||||
|
|
||||||
if (flatten) {
|
|
||||||
this.shouldFlatten = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.maybeRemove(obj, key);
|
this.maybeRemove(obj, key);
|
||||||
@@ -122,7 +111,7 @@ TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent,
|
|||||||
ourScope = new Scope(node, parent, scope);
|
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) {
|
if (this.shouldSkip) {
|
||||||
return this.shouldStop;
|
return this.shouldStop;
|
||||||
@@ -136,7 +125,7 @@ TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent,
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
traverseNode(node, opts, ourScope, state);
|
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;
|
return this.shouldStop;
|
||||||
@@ -174,7 +163,7 @@ function traverseNode(node, opts, scope, state) {
|
|||||||
var keys = t.VISITOR_KEYS[node.type];
|
var keys = t.VISITOR_KEYS[node.type];
|
||||||
if (!keys) return;
|
if (!keys) return;
|
||||||
|
|
||||||
var context = new TraversalContext();
|
var context = new TraversalContext(scope);
|
||||||
for (var i = 0; i < keys.length; i++) {
|
for (var i = 0; i < keys.length; i++) {
|
||||||
if (context.visit(node, keys[i], opts, scope, state)) {
|
if (context.visit(node, keys[i], opts, scope, state)) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -35,6 +35,18 @@ function Scope(block, parentBlock, parent, file) {
|
|||||||
|
|
||||||
Scope.defaultDeclarations = flatten([globals.builtin, globals.browser, globals.node].map(Object.keys));
|
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
|
* Description
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -44,28 +44,39 @@
|
|||||||
"ArrayPattern": ["Pattern"],
|
"ArrayPattern": ["Pattern"],
|
||||||
"AssignmentPattern": ["Pattern"],
|
"AssignmentPattern": ["Pattern"],
|
||||||
|
|
||||||
"Property": ["UserWhitespacable"],
|
"Property": ["UserWhitespacable"],
|
||||||
"JSXElement": ["UserWhitespacable", "Expression"],
|
"JSXElement": ["UserWhitespacable", "Expression"],
|
||||||
|
|
||||||
"ArrayExpression": ["Expression"],
|
"ArrayExpression": ["Expression"],
|
||||||
"AssignmentExpression": ["Expression"],
|
"AssignmentExpression": ["Expression"],
|
||||||
"AwaitExpression": ["Expression"],
|
"AwaitExpression": ["Expression"],
|
||||||
"BindFunctionExpression": ["Expression"],
|
"BindFunctionExpression": ["Expression"],
|
||||||
"BindMemberExpression": ["Expression"],
|
"BindMemberExpression": ["Expression"],
|
||||||
"CallExpression": ["Expression"],
|
"CallExpression": ["Expression"],
|
||||||
"ComprehensionExpression": ["Expression", "Scope"],
|
"ComprehensionExpression": ["Expression", "Scope"],
|
||||||
"ConditionalExpression": ["Expression"],
|
"ConditionalExpression": ["Expression"],
|
||||||
"Identifier": ["Expression"],
|
"Identifier": ["Expression"],
|
||||||
"Literal": ["Expression"],
|
"Literal": ["Expression"],
|
||||||
"MemberExpression": ["Expression"],
|
"MemberExpression": ["Expression"],
|
||||||
"NewExpression": ["Expression"],
|
"NewExpression": ["Expression"],
|
||||||
"ObjectExpression": ["Expression"],
|
"ObjectExpression": ["Expression"],
|
||||||
"SequenceExpression": ["Expression"],
|
"SequenceExpression": ["Expression"],
|
||||||
"TaggedTemplateExpression": ["Expression"],
|
"TaggedTemplateExpression": ["Expression"],
|
||||||
"ThisExpression": ["Expression"],
|
"ThisExpression": ["Expression"],
|
||||||
"UpdateExpression": ["Expression"],
|
"UpdateExpression": ["Expression"],
|
||||||
"VirtualPropertyExpression": ["Expression"],
|
"VirtualPropertyExpression": ["Expression"],
|
||||||
"JSXEmptyExpression": ["Expression"],
|
"JSXEmptyExpression": ["Expression"],
|
||||||
"JSXMemberExpression": ["Expression"],
|
"JSXMemberExpression": ["Expression"],
|
||||||
"YieldExpression": ["Expression"]
|
"YieldExpression": ["Expression"],
|
||||||
|
|
||||||
|
"JSXAttribute": ["JSX"],
|
||||||
|
"JSXClosingElement": ["JSX"],
|
||||||
|
"JSXElement": ["JSX"],
|
||||||
|
"JSXEmptyExpression": ["JSX"],
|
||||||
|
"JSXExpressionContainer": ["JSX"],
|
||||||
|
"JSXIdentifier": ["JSX"],
|
||||||
|
"JSXMemberExpression": ["JSX"],
|
||||||
|
"JSXNamespacedName": ["JSX"],
|
||||||
|
"JSXOpeningElement": ["JSX"],
|
||||||
|
"JSXSpreadAttribute": ["JSX"]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ exports.parse = function (opts, code, callback) {
|
|||||||
|
|
||||||
var ast = acorn.parse(code, {
|
var ast = acorn.parse(code, {
|
||||||
allowImportExportEverywhere: opts.allowImportExportEverywhere,
|
allowImportExportEverywhere: opts.allowImportExportEverywhere,
|
||||||
allowReturnOutsideFunction: true,
|
allowReturnOutsideFunction: !opts._anal,
|
||||||
ecmaVersion: opts.experimental ? 7 : 6,
|
ecmaVersion: opts.experimental ? 7 : 6,
|
||||||
playground: opts.playground,
|
playground: opts.playground,
|
||||||
strictMode: opts.strictMode,
|
strictMode: opts.strictMode,
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
var util = require("../lib/6to5/util");
|
var esvalid = require("esvalid");
|
||||||
var path = require("path");
|
var util = require("../lib/6to5/util");
|
||||||
var fs = require("fs");
|
var path = require("path");
|
||||||
var _ = require("lodash");
|
var fs = require("fs");
|
||||||
|
var _ = require("lodash");
|
||||||
|
|
||||||
var humanize = function (val, noext) {
|
var humanize = function (val, noext) {
|
||||||
if (noext) val = path.basename(val, path.extname(val));
|
if (noext) val = path.basename(val, path.extname(val));
|
||||||
@@ -18,6 +19,17 @@ var readFile = exports.readFile = function (filename) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.esvalid = function (ast, loc) {
|
||||||
|
var errors = esvalid.errors(ast);
|
||||||
|
if (errors.length) {
|
||||||
|
var msg = [];
|
||||||
|
_.each(errors, function (err) {
|
||||||
|
msg.push(err.message + " - " + JSON.stringify(err.node));
|
||||||
|
});
|
||||||
|
throw new Error(loc + ": " + msg.join(". "));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
exports.assertVendor = function (name) {
|
exports.assertVendor = function (name) {
|
||||||
if (!fs.existsSync(__dirname + "/../vendor/" + name)) {
|
if (!fs.existsSync(__dirname + "/../vendor/" + name)) {
|
||||||
console.error("No vendor/" + name + " - run `make bootstrap`");
|
console.error("No vendor/" + name + " - run `make bootstrap`");
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ var genHelpers = require("./_generator-helpers");
|
|||||||
var transform = require("../lib/6to5/transformation");
|
var transform = require("../lib/6to5/transformation");
|
||||||
var sourceMap = require("source-map");
|
var sourceMap = require("source-map");
|
||||||
var codeFrame = require("../lib/6to5/helpers/code-frame");
|
var codeFrame = require("../lib/6to5/helpers/code-frame");
|
||||||
var esvalid = require("esvalid");
|
|
||||||
var Module = require("module");
|
var Module = require("module");
|
||||||
var helper = require("./_helper");
|
var helper = require("./_helper");
|
||||||
var assert = require("assert");
|
var assert = require("assert");
|
||||||
@@ -55,15 +54,7 @@ var run = function (task, done) {
|
|||||||
|
|
||||||
var checkAst = function (result, opts) {
|
var checkAst = function (result, opts) {
|
||||||
if (noCheckAst) return;
|
if (noCheckAst) return;
|
||||||
|
helper.esvalid(result.ast.program, opts.loc);
|
||||||
var errors = esvalid.errors(result.ast.program);
|
|
||||||
if (errors.length) {
|
|
||||||
var msg = [];
|
|
||||||
_.each(errors, function (err) {
|
|
||||||
msg.push(err.message + " - " + JSON.stringify(err.node));
|
|
||||||
});
|
|
||||||
throw new Error(opts.loc + ": " + msg.join(". "));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (execCode) {
|
if (execCode) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ require("./_helper").assertVendor("test262");
|
|||||||
|
|
||||||
var transform = require("../lib/6to5/transformation");
|
var transform = require("../lib/6to5/transformation");
|
||||||
var readdir = require("fs-readdir-recursive");
|
var readdir = require("fs-readdir-recursive");
|
||||||
|
var helper = require("./_helper");
|
||||||
var path = require("path");
|
var path = require("path");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
@@ -16,7 +17,7 @@ var read = function (loc) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var exec = function (loc) {
|
var check = function (loc) {
|
||||||
try {
|
try {
|
||||||
var file = fs.readFileSync(loc, "utf8");
|
var file = fs.readFileSync(loc, "utf8");
|
||||||
|
|
||||||
@@ -26,25 +27,21 @@ var exec = function (loc) {
|
|||||||
// ReferenceError: 1++; (runtime)
|
// ReferenceError: 1++; (runtime)
|
||||||
var lazyError = /negative: (\S+)/.test(file);
|
var lazyError = /negative: (\S+)/.test(file);
|
||||||
|
|
||||||
var compiled = transform(file, {
|
transform(file, {
|
||||||
filename: loc,
|
filename: loc,
|
||||||
blacklist: ["useStrict"]
|
blacklist: ["useStrict"],
|
||||||
|
_anal: true
|
||||||
});
|
});
|
||||||
|
|
||||||
global.eval(compiled);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err && lazyError && err instanceof SyntaxError) {
|
if (err && lazyError && err instanceof SyntaxError) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
|
err.stack = loc + ": " + err.stack;
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// harness
|
|
||||||
var harness = read(test262Loc + "/harness");
|
|
||||||
_.each(harness, exec);
|
|
||||||
|
|
||||||
// tests!
|
// tests!
|
||||||
var tests = read(test262Loc + "/test");
|
var tests = read(test262Loc + "/test");
|
||||||
_.each(tests, function (loc) {
|
_.each(tests, function (loc) {
|
||||||
@@ -52,6 +49,6 @@ _.each(tests, function (loc) {
|
|||||||
alias = alias.replace(/\.([^\.]+)$/g, "");
|
alias = alias.replace(/\.([^\.]+)$/g, "");
|
||||||
test(alias, function () {
|
test(alias, function () {
|
||||||
this.timeout(0);
|
this.timeout(0);
|
||||||
exec(loc);
|
check(loc);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user