diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 406a919b1f..ba4ed2faaf 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -9,6 +9,7 @@ var transform = require("./transformation"); var generate = require("./generation"); var defaults = require("lodash/object/defaults"); var contains = require("lodash/collection/contains"); +var traverse = require("./traverse"); var clone = require("./helpers/clone"); var Scope = require("./traverse/scope"); var util = require("./util"); @@ -352,6 +353,8 @@ File.prototype.transform = function (ast) { modFormatter.init(); } + this.checkNode(ast); + var astRun = function (key) { each(self.transformerStack, function (pass) { pass.astRun(key); @@ -367,6 +370,30 @@ File.prototype.transform = function (ast) { 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 () { var opts = this.opts; var ast = this.ast; diff --git a/lib/6to5/generation/generators/types.js b/lib/6to5/generation/generators/types.js index 117af92ea1..999cd4ae2d 100644 --- a/lib/6to5/generation/generators/types.js +++ b/lib/6to5/generation/generators/types.js @@ -94,8 +94,10 @@ exports.Literal = function (node) { }); this.push(val); - } else if (type === "boolean" || type === "number") { - this.push(JSON.stringify(val)); + } else if (type === "number") { + this.push(val + ""); + } else if (type === "boolean" ) { + this.push(val ? "true" : "false"); } else if (node.regex) { this.push("/" + node.regex.pattern + "/" + node.regex.flags); } else if (val === null) { diff --git a/lib/6to5/transformation/transformer-pass.js b/lib/6to5/transformation/transformer-pass.js index 95a013f68f..5c02d6e215 100644 --- a/lib/6to5/transformation/transformer-pass.js +++ b/lib/6to5/transformation/transformer-pass.js @@ -11,11 +11,14 @@ var contains = require("lodash/collection/contains"); function TransformerPass(file, transformer) { this.transformer = transformer; + this.shouldRun = !transformer.check; this.handlers = transformer.handlers; this.file = file; } TransformerPass.prototype.astRun = function (key) { + if (!this.shouldRun) return; + var handlers = this.handlers; var file = this.file; @@ -29,23 +32,39 @@ TransformerPass.prototype.canRun = function () { var opts = this.file.opts; var key = transformer.key; + + // internal if (key[0] === "_") return true; + // blacklist var blacklist = opts.blacklist; if (blacklist.length && contains(blacklist, key)) return false; + // whitelist var whitelist = opts.whitelist; if (whitelist.length && !contains(whitelist, key)) return false; + // optional if (transformer.optional && !contains(opts.optional, key)) return false; + // experimental if (transformer.experimental && !opts.experimental) return false; + // playground if (transformer.playground && !opts.playground) return false; return true; }; +TransformerPass.prototype.checkNode = function (node) { + var check = this.transformer.check; + if (check) { + return this.shouldRun = check(node); + } else { + return true; + } +}; + var transformVisitor = { enter: function (node, parent, scope, context, state) { var fns = state.handlers[node.type]; @@ -61,6 +80,8 @@ var transformVisitor = { }; TransformerPass.prototype.transform = function () { + if (!this.shouldRun) return; + var file = this.file; util.debug(file.opts.filename + ": Running transformer " + this.transformer.key); diff --git a/lib/6to5/transformation/transformer.js b/lib/6to5/transformation/transformer.js index 02efb7396e..ce4a87e3d9 100644 --- a/lib/6to5/transformation/transformer.js +++ b/lib/6to5/transformation/transformer.js @@ -16,13 +16,16 @@ var each = require("lodash/collection/each"); function Transformer(key, transformer, opts) { this.manipulateOptions = transformer.manipulateOptions; - this.experimental = !!transformer.experimental; - this.playground = !!transformer.playground; - this.secondPass = !!transformer.secondPass; - this.optional = !!transformer.optional; - this.handlers = this.normalize(transformer); - this.opts = opts || {}; - this.key = key; + this.check = transformer.check; + + this.experimental = !!transformer.experimental; + this.playground = !!transformer.playground; + this.secondPass = !!transformer.secondPass; + this.optional = !!transformer.optional; + + this.handlers = this.normalize(transformer); + this.opts = opts || {}; + this.key = key; } Transformer.prototype.normalize = function (transformer) { diff --git a/lib/6to5/transformation/transformers/es5/properties.mutators.js b/lib/6to5/transformation/transformers/es5/properties.mutators.js index 40b87e6fe4..b939b160da 100644 --- a/lib/6to5/transformation/transformers/es5/properties.mutators.js +++ b/lib/6to5/transformation/transformers/es5/properties.mutators.js @@ -3,6 +3,10 @@ var defineMap = require("../../helpers/define-map"); var t = require("../../../types"); +exports.check = function (node) { + return t.isProperty(node) && (node.kind === "get" || node.kind === "set"); +}; + exports.ObjectExpression = function (node) { var mutatorMap = {}; var hasAny = false; diff --git a/lib/6to5/transformation/transformers/es6/arrow-functions.js b/lib/6to5/transformation/transformers/es6/arrow-functions.js index 32dec4e25c..aa62f9ef21 100644 --- a/lib/6to5/transformation/transformers/es6/arrow-functions.js +++ b/lib/6to5/transformation/transformers/es6/arrow-functions.js @@ -2,6 +2,8 @@ var t = require("../../../types"); +exports.check = t.isArrowFunctionExpression; + exports.ArrowFunctionExpression = function (node) { t.ensureBlock(node); diff --git a/lib/6to5/transformation/transformers/es6/block-scoping.js b/lib/6to5/transformation/transformers/es6/block-scoping.js index def499aa33..40daed0dae 100644 --- a/lib/6to5/transformation/transformers/es6/block-scoping.js +++ b/lib/6to5/transformation/transformers/es6/block-scoping.js @@ -7,6 +7,10 @@ var t = require("../../../types"); var values = require("lodash/object/values"); 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) { if (!t.isVariableDeclaration(node)) return false; if (node._let) return true; diff --git a/lib/6to5/transformation/transformers/es6/classes.js b/lib/6to5/transformation/transformers/es6/classes.js index 2ede1861a8..61e906ca62 100644 --- a/lib/6to5/transformation/transformers/es6/classes.js +++ b/lib/6to5/transformation/transformers/es6/classes.js @@ -6,6 +6,8 @@ var defineMap = require("../../helpers/define-map"); var util = require("../../../util"); var t = require("../../../types"); +exports.check = t.isClass; + exports.ClassDeclaration = function (node, parent, scope, context, file) { return new Class(node, file, scope, true).run(); }; diff --git a/lib/6to5/transformation/transformers/es6/constants.js b/lib/6to5/transformation/transformers/es6/constants.js index dcedaa222f..cb7101faf2 100644 --- a/lib/6to5/transformation/transformers/es6/constants.js +++ b/lib/6to5/transformation/transformers/es6/constants.js @@ -3,6 +3,10 @@ var traverse = require("../../../traverse"); var t = require("../../../types"); +exports.check = function (node) { + return t.isVariableDeclaration(node, { kind: "const" }); +}; + var visitor = { enter: function (node, parent, scope, context, state) { if (t.isAssignmentExpression(node) || t.isUpdateExpression(node)) { diff --git a/lib/6to5/transformation/transformers/es6/destructuring.js b/lib/6to5/transformation/transformers/es6/destructuring.js index 246edf31ee..6dc61bfd6e 100644 --- a/lib/6to5/transformation/transformers/es6/destructuring.js +++ b/lib/6to5/transformation/transformers/es6/destructuring.js @@ -4,6 +4,8 @@ var t = require("../../../types"); +exports.check = t.isPattern; + var buildVariableAssign = function (opts, id, init) { var op = opts.operator; if (t.isMemberExpression(id)) op = "="; diff --git a/lib/6to5/transformation/transformers/es6/for-of.js b/lib/6to5/transformation/transformers/es6/for-of.js index 0bbfd9fc83..938ab0df42 100644 --- a/lib/6to5/transformation/transformers/es6/for-of.js +++ b/lib/6to5/transformation/transformers/es6/for-of.js @@ -3,6 +3,8 @@ var util = require("../../../util"); var t = require("../../../types"); +exports.check = t.isForOfStatement; + exports.ForOfStatement = function (node, parent, scope, context, file) { var callback = spec; if (file.isLoose("es6.forOf")) callback = loose; diff --git a/lib/6to5/transformation/transformers/es6/modules.js b/lib/6to5/transformation/transformers/es6/modules.js index abb911f0b5..9249b3da64 100644 --- a/lib/6to5/transformation/transformers/es6/modules.js +++ b/lib/6to5/transformation/transformers/es6/modules.js @@ -2,6 +2,8 @@ var t = require("../../../types"); +exports.check = require("../internal/modules").check; + exports.ImportDeclaration = function (node, parent, scope, context, file) { var nodes = []; diff --git a/lib/6to5/transformation/transformers/es6/parameters.default.js b/lib/6to5/transformation/transformers/es6/parameters.default.js index 20184cfee9..2854e572d7 100644 --- a/lib/6to5/transformation/transformers/es6/parameters.default.js +++ b/lib/6to5/transformation/transformers/es6/parameters.default.js @@ -4,6 +4,10 @@ var traverse = require("../../../traverse"); var util = require("../../../util"); var t = require("../../../types"); +exports.check = function (node) { + return t.isFunction(node) && hasDefaults(node); +}; + var hasDefaults = function (node) { for (var i = 0; i < node.params.length; i++) { if (t.isAssignmentPattern(node.params[i])) return true; diff --git a/lib/6to5/transformation/transformers/es6/parameters.rest.js b/lib/6to5/transformation/transformers/es6/parameters.rest.js index b885ed6e1d..60a8aca8b2 100644 --- a/lib/6to5/transformation/transformers/es6/parameters.rest.js +++ b/lib/6to5/transformation/transformers/es6/parameters.rest.js @@ -3,6 +3,8 @@ var util = require("../../../util"); var t = require("../../../types"); +exports.check = t.isRestElement; + var hasRest = function (node) { return t.isRestElement(node.params[node.params.length - 1]); }; diff --git a/lib/6to5/transformation/transformers/es6/properties.computed.js b/lib/6to5/transformation/transformers/es6/properties.computed.js index 5091488ca7..1b557a6d35 100644 --- a/lib/6to5/transformation/transformers/es6/properties.computed.js +++ b/lib/6to5/transformation/transformers/es6/properties.computed.js @@ -2,6 +2,10 @@ var t = require("../../../types"); +exports.check = function (node) { + return t.isProperty(node) && node.computed; +}; + exports.ObjectExpression = function (node, parent, scope, context, file) { var hasComputed = false; diff --git a/lib/6to5/transformation/transformers/es6/properties.shorthand.js b/lib/6to5/transformation/transformers/es6/properties.shorthand.js index 9c4230fdeb..81fd7c40cb 100644 --- a/lib/6to5/transformation/transformers/es6/properties.shorthand.js +++ b/lib/6to5/transformation/transformers/es6/properties.shorthand.js @@ -4,6 +4,10 @@ var nameMethod = require("../../helpers/name-method"); var t = require("../../../types"); 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) { if (node.method) { node.method = false; diff --git a/lib/6to5/transformation/transformers/es6/spread.js b/lib/6to5/transformation/transformers/es6/spread.js index c37e1905b3..46ffecfcae 100644 --- a/lib/6to5/transformation/transformers/es6/spread.js +++ b/lib/6to5/transformation/transformers/es6/spread.js @@ -1,7 +1,9 @@ "use strict"; -var t = require("../../../types"); var contains = require("lodash/collection/contains"); +var t = require("../../../types"); + +exports.check = t.isSpreadElement; var getSpreadLiteral = function (spread, file) { return file.toArray(spread.argument); diff --git a/lib/6to5/transformation/transformers/es6/template-literals.js b/lib/6to5/transformation/transformers/es6/template-literals.js index 1f5cd55014..4fcb9243b3 100644 --- a/lib/6to5/transformation/transformers/es6/template-literals.js +++ b/lib/6to5/transformation/transformers/es6/template-literals.js @@ -6,6 +6,10 @@ var buildBinaryExpression = function (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) { var args = []; var quasi = node.quasi; diff --git a/lib/6to5/transformation/transformers/es6/unicode-regex.js b/lib/6to5/transformation/transformers/es6/unicode-regex.js index bccb76303d..0f4964d243 100644 --- a/lib/6to5/transformation/transformers/es6/unicode-regex.js +++ b/lib/6to5/transformation/transformers/es6/unicode-regex.js @@ -2,6 +2,11 @@ var rewritePattern = require("regexpu/rewrite-pattern"); 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) { var regex = node.regex; diff --git a/lib/6to5/transformation/transformers/internal/modules.js b/lib/6to5/transformation/transformers/internal/modules.js index 1477c94b0f..5c0a82f2cc 100644 --- a/lib/6to5/transformation/transformers/internal/modules.js +++ b/lib/6to5/transformation/transformers/internal/modules.js @@ -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.ExportDeclaration = function (node, parent, scope) { diff --git a/lib/6to5/transformation/transformers/other/react.js b/lib/6to5/transformation/transformers/other/react.js index a246c682db..e265fb7b1e 100644 --- a/lib/6to5/transformation/transformers/other/react.js +++ b/lib/6to5/transformation/transformers/other/react.js @@ -10,6 +10,12 @@ var esutils = require("esutils"); var react = require("../../helpers/react"); 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) { if (node.name === "this" && t.isReferenced(node, parent)) { return t.thisExpression(); diff --git a/lib/6to5/transformation/transformers/other/regenerator.js b/lib/6to5/transformation/transformers/other/regenerator.js index 7199c70fb5..d4a9119e7a 100644 --- a/lib/6to5/transformation/transformers/other/regenerator.js +++ b/lib/6to5/transformation/transformers/other/regenerator.js @@ -1,6 +1,11 @@ "use strict"; var regenerator = require("regenerator-6to5"); +var t = require("../../../types"); + +exports.check = function (node) { + return t.isFunction(node) && (node.async || node.generator); +}; exports.ast = { before: function (ast, file) { diff --git a/lib/6to5/transformation/transformers/playground/object-getter-memoization.js b/lib/6to5/transformation/transformers/playground/object-getter-memoization.js index 07ee0dc18a..e27da96702 100644 --- a/lib/6to5/transformation/transformers/playground/object-getter-memoization.js +++ b/lib/6to5/transformation/transformers/playground/object-getter-memoization.js @@ -23,6 +23,7 @@ exports.Property = exports.MethodDefinition = function (node, parent, scope, context, file) { if (node.kind !== "memo") return; node.kind = "get"; + file.checkNode(node, scope); var value = node.value; t.ensureBlock(value); diff --git a/lib/6to5/traverse/index.js b/lib/6to5/traverse/index.js index 7279bad14d..e376908fe1 100644 --- a/lib/6to5/traverse/index.js +++ b/lib/6to5/traverse/index.js @@ -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; diff --git a/lib/6to5/traverse/scope.js b/lib/6to5/traverse/scope.js index ca04afd3b3..1f05f05b86 100644 --- a/lib/6to5/traverse/scope.js +++ b/lib/6to5/traverse/scope.js @@ -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 * diff --git a/lib/6to5/types/alias-keys.json b/lib/6to5/types/alias-keys.json index 5fae4406da..52ae624e72 100644 --- a/lib/6to5/types/alias-keys.json +++ b/lib/6to5/types/alias-keys.json @@ -44,28 +44,39 @@ "ArrayPattern": ["Pattern"], "AssignmentPattern": ["Pattern"], - "Property": ["UserWhitespacable"], + "Property": ["UserWhitespacable"], "JSXElement": ["UserWhitespacable", "Expression"], - "ArrayExpression": ["Expression"], - "AssignmentExpression": ["Expression"], - "AwaitExpression": ["Expression"], - "BindFunctionExpression": ["Expression"], - "BindMemberExpression": ["Expression"], - "CallExpression": ["Expression"], - "ComprehensionExpression": ["Expression", "Scope"], - "ConditionalExpression": ["Expression"], - "Identifier": ["Expression"], - "Literal": ["Expression"], - "MemberExpression": ["Expression"], - "NewExpression": ["Expression"], - "ObjectExpression": ["Expression"], - "SequenceExpression": ["Expression"], - "TaggedTemplateExpression": ["Expression"], - "ThisExpression": ["Expression"], - "UpdateExpression": ["Expression"], + "ArrayExpression": ["Expression"], + "AssignmentExpression": ["Expression"], + "AwaitExpression": ["Expression"], + "BindFunctionExpression": ["Expression"], + "BindMemberExpression": ["Expression"], + "CallExpression": ["Expression"], + "ComprehensionExpression": ["Expression", "Scope"], + "ConditionalExpression": ["Expression"], + "Identifier": ["Expression"], + "Literal": ["Expression"], + "MemberExpression": ["Expression"], + "NewExpression": ["Expression"], + "ObjectExpression": ["Expression"], + "SequenceExpression": ["Expression"], + "TaggedTemplateExpression": ["Expression"], + "ThisExpression": ["Expression"], + "UpdateExpression": ["Expression"], "VirtualPropertyExpression": ["Expression"], - "JSXEmptyExpression": ["Expression"], - "JSXMemberExpression": ["Expression"], - "YieldExpression": ["Expression"] + "JSXEmptyExpression": ["Expression"], + "JSXMemberExpression": ["Expression"], + "YieldExpression": ["Expression"], + + "JSXAttribute": ["JSX"], + "JSXClosingElement": ["JSX"], + "JSXElement": ["JSX"], + "JSXEmptyExpression": ["JSX"], + "JSXExpressionContainer": ["JSX"], + "JSXIdentifier": ["JSX"], + "JSXMemberExpression": ["JSX"], + "JSXNamespacedName": ["JSX"], + "JSXOpeningElement": ["JSX"], + "JSXSpreadAttribute": ["JSX"] } diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 49b381ef29..8cf3aa3fd7 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -138,7 +138,7 @@ exports.parse = function (opts, code, callback) { var ast = acorn.parse(code, { allowImportExportEverywhere: opts.allowImportExportEverywhere, - allowReturnOutsideFunction: true, + allowReturnOutsideFunction: !opts._anal, ecmaVersion: opts.experimental ? 7 : 6, playground: opts.playground, strictMode: opts.strictMode, diff --git a/test/_helper.js b/test/_helper.js index bb8c7c3341..c44d9f672f 100644 --- a/test/_helper.js +++ b/test/_helper.js @@ -1,7 +1,8 @@ -var util = require("../lib/6to5/util"); -var path = require("path"); -var fs = require("fs"); -var _ = require("lodash"); +var esvalid = require("esvalid"); +var util = require("../lib/6to5/util"); +var path = require("path"); +var fs = require("fs"); +var _ = require("lodash"); var humanize = function (val, noext) { 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) { if (!fs.existsSync(__dirname + "/../vendor/" + name)) { console.error("No vendor/" + name + " - run `make bootstrap`"); diff --git a/test/_transformation-helper.js b/test/_transformation-helper.js index 09535e04f3..3d6fb56166 100644 --- a/test/_transformation-helper.js +++ b/test/_transformation-helper.js @@ -2,7 +2,6 @@ var genHelpers = require("./_generator-helpers"); var transform = require("../lib/6to5/transformation"); var sourceMap = require("source-map"); var codeFrame = require("../lib/6to5/helpers/code-frame"); -var esvalid = require("esvalid"); var Module = require("module"); var helper = require("./_helper"); var assert = require("assert"); @@ -55,15 +54,7 @@ var run = function (task, done) { var checkAst = function (result, opts) { if (noCheckAst) return; - - 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(". ")); - } + helper.esvalid(result.ast.program, opts.loc); }; if (execCode) { diff --git a/test/test262.js b/test/test262.js index 11c96abcfe..e77d04f638 100644 --- a/test/test262.js +++ b/test/test262.js @@ -4,6 +4,7 @@ require("./_helper").assertVendor("test262"); var transform = require("../lib/6to5/transformation"); var readdir = require("fs-readdir-recursive"); +var helper = require("./_helper"); var path = require("path"); var fs = require("fs"); var _ = require("lodash"); @@ -16,7 +17,7 @@ var read = function (loc) { }); }; -var exec = function (loc) { +var check = function (loc) { try { var file = fs.readFileSync(loc, "utf8"); @@ -26,25 +27,21 @@ var exec = function (loc) { // ReferenceError: 1++; (runtime) var lazyError = /negative: (\S+)/.test(file); - var compiled = transform(file, { + transform(file, { filename: loc, - blacklist: ["useStrict"] + blacklist: ["useStrict"], + _anal: true }); - - global.eval(compiled); } catch (err) { if (err && lazyError && err instanceof SyntaxError) { return; } else { + err.stack = loc + ": " + err.stack; throw err; } } }; -// harness -var harness = read(test262Loc + "/harness"); -_.each(harness, exec); - // tests! var tests = read(test262Loc + "/test"); _.each(tests, function (loc) { @@ -52,6 +49,6 @@ _.each(tests, function (loc) { alias = alias.replace(/\.([^\.]+)$/g, ""); test(alias, function () { this.timeout(0); - exec(loc); + check(loc); }); });