diff --git a/lib/6to5/transformation/file.js b/lib/6to5/transformation/file.js index c23360d713..e93a3bd07d 100644 --- a/lib/6to5/transformation/file.js +++ b/lib/6to5/transformation/file.js @@ -160,8 +160,8 @@ File.prototype.normalizeOptions = function (opts) { opts.blacklist = transform._ensureTransformerNames("blacklist", opts.blacklist); opts.whitelist = transform._ensureTransformerNames("whitelist", opts.whitelist); - opts.optional = transform._ensureTransformerNames("optional", opts.optional); - opts.loose = transform._ensureTransformerNames("loose", opts.loose); + opts.optional = transform._ensureTransformerNames("optional", opts.optional); + opts.loose = transform._ensureTransformerNames("loose", opts.loose); return opts; }; @@ -198,6 +198,12 @@ File.prototype.buildTransformers = function () { this.transformers = transformers; }; +File.prototype.debug = function (msg) { + var parts = this.opts.filename; + if (msg) parts += ": " + msg; + util.debug(parts); +}; + File.prototype.toArray = function (node, i) { if (t.isArrayExpression(node)) { return node; @@ -344,7 +350,7 @@ File.prototype.parse = function (code) { File.prototype.transform = function (ast) { var self = this; - util.debug(this.opts.filename); + this.debug(); this.ast = ast; this.lastStatements = t.getLastStatements(ast.program); @@ -357,23 +363,27 @@ File.prototype.transform = function (ast) { this.checkNode(ast); - var astRun = function (key) { - each(self.transformerStack, function (pass) { - pass.astRun(key); - }); - }; - - astRun("enter"); + this.call("pre"); each(this.transformerStack, function (pass) { pass.transform(); }); - astRun("exit"); + this.call("post"); +}; + +File.prototype.call = function (key) { + var stack = this.transformerStack; + for (var i = 0; i < stack.length; i++) { + var transformer = stack[i].transformer; + if (transformer[key]) { + transformer[key](this); + } + } }; var checkTransformerVisitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { checkNode(state.stack, node, scope); } }; diff --git a/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js b/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js index 7a4011a79e..636c9b284b 100644 --- a/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js +++ b/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js @@ -12,7 +12,7 @@ module.exports = function (exports, opts) { return t.assignmentExpression("=", left, right); }; - exports.ExpressionStatement = function (node, parent, scope, context, file) { + exports.ExpressionStatement = function (node, parent, scope, file) { // hit the `AssignmentExpression` one below if (file.isConsequenceExpressionStatement(node)) return; @@ -29,7 +29,7 @@ module.exports = function (exports, opts) { return nodes; }; - exports.AssignmentExpression = function (node, parent, scope, context, file) { + exports.AssignmentExpression = function (node, parent, scope, file) { if (!isAssignment(node)) return; var nodes = []; diff --git a/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js b/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js index f72126415f..ce68e5c49f 100644 --- a/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js +++ b/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js @@ -8,7 +8,7 @@ module.exports = function (exports, opts) { return t.assignmentExpression("=", left, right); }; - exports.ExpressionStatement = function (node, parent, scope, context, file) { + exports.ExpressionStatement = function (node, parent, scope, file) { // hit the `AssignmentExpression` one below if (file.isConsequenceExpressionStatement(node)) return; @@ -27,7 +27,7 @@ module.exports = function (exports, opts) { return nodes; }; - exports.AssignmentExpression = function (node, parent, scope, context, file) { + exports.AssignmentExpression = function (node, parent, scope, file) { if (!opts.is(node, file)) return; var nodes = []; diff --git a/lib/6to5/transformation/helpers/name-method.js b/lib/6to5/transformation/helpers/name-method.js index ae86c91523..8df977dafb 100644 --- a/lib/6to5/transformation/helpers/name-method.js +++ b/lib/6to5/transformation/helpers/name-method.js @@ -4,7 +4,7 @@ var util = require("../../util"); var t = require("../../types"); var visitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { // check if this node is an identifier that matches the same as our function id if (!t.isIdentifier(node, { name: state.id })) return; @@ -17,7 +17,7 @@ var visitor = { if (localDeclar !== state.outerDeclar) return; state.selfReference = true; - context.stop(); + this.stop(); } }; diff --git a/lib/6to5/transformation/helpers/remap-async-to-generator.js b/lib/6to5/transformation/helpers/remap-async-to-generator.js index 05924787da..1161832439 100644 --- a/lib/6to5/transformation/helpers/remap-async-to-generator.js +++ b/lib/6to5/transformation/helpers/remap-async-to-generator.js @@ -3,8 +3,8 @@ var t = require("../../types"); var visitor = { - enter: function (node, parent, scope, context) { - if (t.isFunction(node)) context.skip(); + enter: function (node, parent, scope) { + if (t.isFunction(node)) this.skip(); if (t.isAwaitExpression(node)) { node.type = "YieldExpression"; diff --git a/lib/6to5/transformation/helpers/replace-supers.js b/lib/6to5/transformation/helpers/replace-supers.js index d4d59b056c..66048ab4d9 100644 --- a/lib/6to5/transformation/helpers/replace-supers.js +++ b/lib/6to5/transformation/helpers/replace-supers.js @@ -79,19 +79,19 @@ ReplaceSupers.prototype.replace = function () { }; var visitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { var topLevel = state.topLevel; var self = state.self; if (t.isFunction(node) && !t.isArrowFunctionExpression(node)) { // we need to call traverseLevel again so we're context aware self.traverseLevel(node, false); - return context.skip(); + return this.skip(); } if (t.isProperty(node, { method: true }) || t.isMethodDefinition(node)) { // break on object methods - return context.skip(); + return this.skip(); } var getThisReference = topLevel ? diff --git a/lib/6to5/transformation/modules/_default.js b/lib/6to5/transformation/modules/_default.js index d72f4bdb9c..ad36cbeed7 100644 --- a/lib/6to5/transformation/modules/_default.js +++ b/lib/6to5/transformation/modules/_default.js @@ -38,7 +38,7 @@ DefaultFormatter.prototype.bumpImportOccurences = function (node) { }; var exportsVisitor = { - enter: function (node, parent, scope, context, formatter) { + enter: function (node, parent, scope, formatter) { var declar = node && node.declaration; if (t.isExportDeclaration(node)) { formatter.hasLocalImports = true; @@ -63,7 +63,7 @@ DefaultFormatter.prototype.getLocalExports = function () { }; var importsVisitor = { - enter: function (node, parent, scope, context, formatter) { + enter: function (node, parent, scope, formatter) { if (t.isImportDeclaration(node)) { formatter.hasLocalImports = true; extend(formatter.localImports, t.getBindingIdentifiers(node)); @@ -77,9 +77,9 @@ DefaultFormatter.prototype.getLocalImports = function () { }; var remapVisitor = { - enter: function (node, parent, scope, context, formatter) { + enter: function (node, parent, scope, formatter) { if (t.isUpdateExpression(node) && formatter.isLocalReference(node.argument, scope)) { - context.skip(); + this.skip(); // expand to long file assignment expression var assign = t.assignmentExpression(node.operator[0] + "=", node.argument, t.literal(1)); @@ -107,7 +107,7 @@ var remapVisitor = { } if (t.isAssignmentExpression(node) && formatter.isLocalReference(node.left, scope)) { - context.skip(); + this.skip(); return formatter.remapExportAssignment(node); } } diff --git a/lib/6to5/transformation/modules/system.js b/lib/6to5/transformation/modules/system.js index 4f0f3f0208..0d333037d5 100644 --- a/lib/6to5/transformation/modules/system.js +++ b/lib/6to5/transformation/modules/system.js @@ -68,7 +68,7 @@ SystemFormatter.prototype.importSpecifier = function (specifier, node, nodes) { }; var runnerSettersVisitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { if (node._importSource === state.source) { if (t.isVariableDeclaration(node)) { each(node.declarations, function (declar) { @@ -81,7 +81,7 @@ var runnerSettersVisitor = { state.nodes.push(node); } - context.remove(); + this.remove(); } } }; @@ -103,10 +103,10 @@ SystemFormatter.prototype.buildRunnerSetters = function (block, hoistDeclarators }; var hoistVariablesVisitor = { - enter: function (node, parent, scope, context, hoistDeclarators) { + enter: function (node, parent, scope, hoistDeclarators) { if (t.isFunction(node)) { // nothing inside is accessible - return context.skip(); + return this.skip(); } if (t.isVariableDeclaration(node)) { @@ -148,12 +148,12 @@ var hoistVariablesVisitor = { }; var hoistFunctionsVisitor = { - enter: function (node, parent, scope, context, handlerBody) { - if (t.isFunction(node)) context.skip(); + enter: function (node, parent, scope, handlerBody) { + if (t.isFunction(node)) this.skip(); if (t.isFunctionDeclaration(node) || node._blockHoist) { handlerBody.push(node); - context.remove(); + this.remove(); } } }; diff --git a/lib/6to5/transformation/transformer-pass.js b/lib/6to5/transformation/transformer-pass.js index 84ce74350a..9b16df92f2 100644 --- a/lib/6to5/transformation/transformer-pass.js +++ b/lib/6to5/transformation/transformer-pass.js @@ -15,17 +15,6 @@ function TransformerPass(file, transformer) { this.file = file; } -TransformerPass.prototype.astRun = function (key) { - if (!this.shouldRun) return; - - var handlers = this.handlers; - var file = this.file; - - if (handlers.ast && handlers.ast[key]) { - handlers.ast[key](file.ast, file); - } -}; - TransformerPass.prototype.canRun = function () { var transformer = this.transformer; @@ -64,31 +53,12 @@ TransformerPass.prototype.checkNode = function (node) { } }; -var transformVisitor = { - enter: function (node, parent, scope, context, state) { - var fns = state.handlers[node.type]; - if (!fns) return; - return fns.enter(node, parent, scope, context, state.file, state.pass); - }, - - exit: function (node, parent, scope, context, state) { - var fns = state.handlers[node.type]; - if (!fns) return; - return fns.exit(node, parent, scope, context, state.file, state.pass); - } -}; - TransformerPass.prototype.transform = function () { if (!this.shouldRun) return; var file = this.file; - util.debug(file.opts.filename + ": Running transformer " + this.transformer.key); + file.debug("Running transformer " + this.transformer.key); - this.astRun("before"); - - var state = { file: file, handlers: this.handlers, pass: this }; - file.scope.traverse(file.ast, transformVisitor, state); - - this.astRun("after"); + file.scope.traverse(file.ast, this.handlers, file); }; diff --git a/lib/6to5/transformation/transformer.js b/lib/6to5/transformation/transformer.js index 2f94ff3f69..31bf351efb 100644 --- a/lib/6to5/transformation/transformer.js +++ b/lib/6to5/transformation/transformer.js @@ -6,6 +6,7 @@ var TransformerPass = require("./transformer-pass"); var isFunction = require("lodash/lang/isFunction"); var traverse = require("../traversal"); var isObject = require("lodash/lang/isObject"); +var clone = require("../helpers/clone"); var each = require("lodash/collection/each"); /** @@ -15,13 +16,23 @@ var each = require("lodash/collection/each"); */ function Transformer(key, transformer, opts) { - this.manipulateOptions = transformer.manipulateOptions; - this.check = transformer.check; + transformer = clone(transformer); - this.experimental = !!transformer.experimental; - this.playground = !!transformer.playground; - this.secondPass = !!transformer.secondPass; - this.optional = !!transformer.optional; + var take = function (key) { + var val = transformer[key]; + delete transformer[key]; + return val; + }; + + this.manipulateOptions = take("manipulateOptions"); + this.check = take("check"); + this.post = take("post"); + this.pre = take("pre"); + + this.experimental = !!take("experimental"); + this.playground = !!take("playground"); + this.secondPass = !!take("secondPass"); + this.optional = !!take("optional"); this.handlers = this.normalize(transformer); this.opts = opts || {}; diff --git a/lib/6to5/transformation/transformers/es6/block-scoping-tdz.js b/lib/6to5/transformation/transformers/es6/block-scoping-tdz.js index fcb38ec11b..df09f17e01 100644 --- a/lib/6to5/transformation/transformers/es6/block-scoping-tdz.js +++ b/lib/6to5/transformation/transformers/es6/block-scoping-tdz.js @@ -3,7 +3,7 @@ var t = require("../../../types"); var visitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { if (!t.isReferencedIdentifier(node, parent)) return; var declared = state.letRefs[node.name]; @@ -36,7 +36,7 @@ exports.optional = true; exports.Loop = exports.Program = -exports.BlockStatement = function (node, parent, scope, context, file) { +exports.BlockStatement = function (node, parent, scope, file) { var letRefs = node._letReferences; if (!letRefs) return; diff --git a/lib/6to5/transformation/transformers/es6/block-scoping.js b/lib/6to5/transformation/transformers/es6/block-scoping.js index 2a6edc14fd..b52a39f8bc 100644 --- a/lib/6to5/transformation/transformers/es6/block-scoping.js +++ b/lib/6to5/transformation/transformers/es6/block-scoping.js @@ -43,7 +43,7 @@ exports.VariableDeclaration = function (node, parent) { isLet(node, parent); }; -exports.Loop = function (node, parent, scope, context, file) { +exports.Loop = function (node, parent, scope, file) { var init = node.left || node.init; if (isLet(init, node)) { t.ensureBlock(node); @@ -54,7 +54,7 @@ exports.Loop = function (node, parent, scope, context, file) { }; exports.Program = -exports.BlockStatement = function (block, parent, scope, context, file) { +exports.BlockStatement = function (block, parent, scope, file) { if (!t.isLoop(parent)) { var blockScoping = new BlockScoping(false, block, parent, scope, file); blockScoping.run(); @@ -108,7 +108,7 @@ BlockScoping.prototype.run = function () { } }; -function replace(node, parent, scope, context, remaps) { +function replace(node, parent, scope, remaps) { if (!t.isReferencedIdentifier(node, parent)) return; var remap = remaps[node.name]; @@ -120,7 +120,7 @@ function replace(node, parent, scope, context, remaps) { } else { // scope already has it's own binding that doesn't // match the one we have a stored replacement for - if (context) context.skip(); + if (this) this.skip(); } } @@ -129,7 +129,7 @@ var replaceVisitor = { }; function traverseReplace(node, parent, scope, remaps) { - replace(node, parent, scope, null, remaps); + replace(node, parent, scope, remaps); scope.traverse(node, replaceVisitor, remaps); } @@ -225,7 +225,7 @@ BlockScoping.prototype.needsClosure = function () { }; var letReferenceFunctionVisitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { // not a direct reference if (!t.isReferencedIdentifier(node, parent)) return; @@ -241,10 +241,10 @@ var letReferenceFunctionVisitor = { }; var letReferenceBlockVisitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { if (t.isFunction(node)) { scope.traverse(node, letReferenceFunctionVisitor, state); - return context.skip(); + return this.skip(); } } }; @@ -310,7 +310,7 @@ var loopNodeTo = function (node) { }; var loopVisitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { var replace; if (t.isLoop(node)) { @@ -320,7 +320,7 @@ var loopVisitor = { } if (t.isFunction(node) || t.isLoop(node)) { - return context.skip(); + return this.skip(); } var loopText = loopNodeTo(node); @@ -362,7 +362,7 @@ var loopVisitor = { }; var loopLabelVisitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { if (t.isLabeledStatement(node)) { state.innerLabels.push(node.label.name); } @@ -395,7 +395,7 @@ BlockScoping.prototype.checkLoop = function () { }; var hoistVarDeclarationsVisitor = { - enter: function (node, parent, scope, context, self) { + enter: function (node, parent, scope, self) { if (t.isForStatement(node)) { if (isVar(node.init, node)) { node.init = t.sequenceExpression(self.pushDeclar(node.init)); @@ -407,7 +407,7 @@ var hoistVarDeclarationsVisitor = { } else if (isVar(node, parent)) { return self.pushDeclar(node).map(t.expressionStatement); } else if (t.isFunction(node)) { - return context.skip(); + return this.skip(); } } }; diff --git a/lib/6to5/transformation/transformers/es6/classes.js b/lib/6to5/transformation/transformers/es6/classes.js index d45297f4d9..9372e17c06 100644 --- a/lib/6to5/transformation/transformers/es6/classes.js +++ b/lib/6to5/transformation/transformers/es6/classes.js @@ -8,11 +8,11 @@ var t = require("../../../types"); exports.check = t.isClass; -exports.ClassDeclaration = function (node, parent, scope, context, file) { +exports.ClassDeclaration = function (node, parent, scope, file) { return new Class(node, file, scope, true).run(); }; -exports.ClassExpression = function (node, parent, scope, context, file) { +exports.ClassExpression = function (node, parent, scope, file) { if (!node.id) { if (t.isProperty(parent) && parent.value === node && !parent.computed && t.isIdentifier(parent.key)) { // var o = { foo: class {} }; diff --git a/lib/6to5/transformation/transformers/es6/constants.js b/lib/6to5/transformation/transformers/es6/constants.js index d41de48b88..ce888a17bd 100644 --- a/lib/6to5/transformation/transformers/es6/constants.js +++ b/lib/6to5/transformation/transformers/es6/constants.js @@ -7,7 +7,7 @@ exports.check = function (node) { }; var visitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { if (t.isAssignmentExpression(node) || t.isUpdateExpression(node)) { var ids = t.getBindingIdentifiers(node); @@ -30,12 +30,12 @@ var visitor = { throw state.file.errorWithNode(id, key + " is read-only"); } } else if (t.isScope(node)) { - context.skip(); + this.skip(); } } }; -exports.Scope = function (node, parent, scope, context, file) { +exports.Scope = function (node, parent, scope, file) { scope.traverse(node, visitor, { constants: scope.getAllDeclarationsOfKind("const"), file: file diff --git a/lib/6to5/transformation/transformers/es6/destructuring.js b/lib/6to5/transformation/transformers/es6/destructuring.js index 9a08a475b3..7e26902dd3 100644 --- a/lib/6to5/transformation/transformers/es6/destructuring.js +++ b/lib/6to5/transformation/transformers/es6/destructuring.js @@ -178,7 +178,7 @@ Destructuring.prototype.init = function (pattern, parentId) { }; exports.ForInStatement = -exports.ForOfStatement = function (node, parent, scope, context, file) { +exports.ForOfStatement = function (node, parent, scope, file) { var declar = node.left; if (!t.isVariableDeclaration(declar)) return; @@ -207,7 +207,7 @@ exports.ForOfStatement = function (node, parent, scope, context, file) { block.body = nodes.concat(block.body); }; -exports.Function = function (node, parent, scope, context, file) { +exports.Function = function (node, parent, scope, file) { var nodes = []; var hasDestructuring = false; @@ -238,7 +238,7 @@ exports.Function = function (node, parent, scope, context, file) { block.body = nodes.concat(block.body); }; -exports.CatchClause = function (node, parent, scope, context, file) { +exports.CatchClause = function (node, parent, scope, file) { var pattern = node.param; if (!t.isPattern(pattern)) return; @@ -258,7 +258,7 @@ exports.CatchClause = function (node, parent, scope, context, file) { node.body.body = nodes.concat(node.body.body); }; -exports.ExpressionStatement = function (node, parent, scope, context, file) { +exports.ExpressionStatement = function (node, parent, scope, file) { var expr = node.expression; if (expr.type !== "AssignmentExpression") return; if (!t.isPattern(expr.left)) return; @@ -282,7 +282,7 @@ exports.ExpressionStatement = function (node, parent, scope, context, file) { return nodes; }; -exports.AssignmentExpression = function (node, parent, scope, context, file) { +exports.AssignmentExpression = function (node, parent, scope, file) { if (!t.isPattern(node.left)) return; var ref = scope.generateUidIdentifier("temp"); @@ -316,7 +316,7 @@ var variableDeclarationhasPattern = function (node) { return false; }; -exports.VariableDeclaration = function (node, parent, scope, context, file) { +exports.VariableDeclaration = function (node, parent, scope, file) { if (t.isForInStatement(parent) || t.isForOfStatement(parent)) return; if (!variableDeclarationhasPattern(node)) return; diff --git a/lib/6to5/transformation/transformers/es6/for-of.js b/lib/6to5/transformation/transformers/es6/for-of.js index 938ab0df42..85b01dd4bf 100644 --- a/lib/6to5/transformation/transformers/es6/for-of.js +++ b/lib/6to5/transformation/transformers/es6/for-of.js @@ -5,11 +5,11 @@ var t = require("../../../types"); exports.check = t.isForOfStatement; -exports.ForOfStatement = function (node, parent, scope, context, file) { +exports.ForOfStatement = function (node, parent, scope, file) { var callback = spec; if (file.isLoose("es6.forOf")) callback = loose; - var build = callback(node, parent, scope, context, file); + var build = callback(node, parent, scope, file); var declar = build.declar; var loop = build.loop; var block = loop.body; @@ -34,7 +34,7 @@ exports.ForOfStatement = function (node, parent, scope, context, file) { return loop; }; -var loose = function (node, parent, scope, context, file) { +var loose = function (node, parent, scope, file) { var left = node.left; var declar, id; @@ -71,7 +71,7 @@ var loose = function (node, parent, scope, context, file) { }; }; -var spec = function (node, parent, scope, context, file) { +var spec = function (node, parent, scope, file) { var left = node.left; var declar; diff --git a/lib/6to5/transformation/transformers/es6/modules.js b/lib/6to5/transformation/transformers/es6/modules.js index 9249b3da64..23b8a106c2 100644 --- a/lib/6to5/transformation/transformers/es6/modules.js +++ b/lib/6to5/transformation/transformers/es6/modules.js @@ -4,7 +4,7 @@ var t = require("../../../types"); exports.check = require("../internal/modules").check; -exports.ImportDeclaration = function (node, parent, scope, context, file) { +exports.ImportDeclaration = function (node, parent, scope, file) { var nodes = []; if (node.specifiers.length) { @@ -23,7 +23,7 @@ exports.ImportDeclaration = function (node, parent, scope, context, file) { return nodes; }; -exports.ExportDeclaration = function (node, parent, scope, context, file) { +exports.ExportDeclaration = function (node, parent, scope, file) { var nodes = []; var i; diff --git a/lib/6to5/transformation/transformers/es6/parameters.default.js b/lib/6to5/transformation/transformers/es6/parameters.default.js index 204e8fb46e..b56b6ee34f 100644 --- a/lib/6to5/transformation/transformers/es6/parameters.default.js +++ b/lib/6to5/transformation/transformers/es6/parameters.default.js @@ -15,10 +15,10 @@ var hasDefaults = function (node) { }; var iifeVisitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { if (t.isReferencedIdentifier(node, parent) && state.scope.hasOwnReference(node.name)) { state.iife = true; - context.stop(); + this.stop(); } } }; diff --git a/lib/6to5/transformation/transformers/es6/properties.computed.js b/lib/6to5/transformation/transformers/es6/properties.computed.js index 1b557a6d35..194f30e663 100644 --- a/lib/6to5/transformation/transformers/es6/properties.computed.js +++ b/lib/6to5/transformation/transformers/es6/properties.computed.js @@ -6,7 +6,7 @@ exports.check = function (node) { return t.isProperty(node) && node.computed; }; -exports.ObjectExpression = function (node, parent, scope, context, file) { +exports.ObjectExpression = function (node, parent, scope, file) { var hasComputed = false; for (var i = 0; i < node.properties.length; i++) { diff --git a/lib/6to5/transformation/transformers/es6/properties.shorthand.js b/lib/6to5/transformation/transformers/es6/properties.shorthand.js index 81fd7c40cb..fff70cef38 100644 --- a/lib/6to5/transformation/transformers/es6/properties.shorthand.js +++ b/lib/6to5/transformation/transformers/es6/properties.shorthand.js @@ -8,7 +8,7 @@ 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, file) { if (node.method) { node.method = false; nameMethod.property(node, file, scope); diff --git a/lib/6to5/transformation/transformers/es6/spread.js b/lib/6to5/transformation/transformers/es6/spread.js index 46ffecfcae..bea2ce9ee2 100644 --- a/lib/6to5/transformation/transformers/es6/spread.js +++ b/lib/6to5/transformation/transformers/es6/spread.js @@ -44,7 +44,7 @@ var build = function (props, file) { return nodes; }; -exports.ArrayExpression = function (node, parent, scope, context, file) { +exports.ArrayExpression = function (node, parent, scope, file) { var elements = node.elements; if (!hasSpread(elements)) return; @@ -59,7 +59,7 @@ exports.ArrayExpression = function (node, parent, scope, context, file) { return t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes); }; -exports.CallExpression = function (node, parent, scope, context, file) { +exports.CallExpression = function (node, parent, scope, file) { var args = node.arguments; if (!hasSpread(args)) return; @@ -99,7 +99,7 @@ exports.CallExpression = function (node, parent, scope, context, file) { node.arguments.unshift(contextLiteral); }; -exports.NewExpression = function (node, parent, scope, context, file) { +exports.NewExpression = function (node, parent, scope, file) { var args = node.arguments; if (!hasSpread(args)) return; diff --git a/lib/6to5/transformation/transformers/es6/template-literals.js b/lib/6to5/transformation/transformers/es6/template-literals.js index 4fcb9243b3..7124c53401 100644 --- a/lib/6to5/transformation/transformers/es6/template-literals.js +++ b/lib/6to5/transformation/transformers/es6/template-literals.js @@ -10,7 +10,7 @@ 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, file) { var args = []; var quasi = node.quasi; diff --git a/lib/6to5/transformation/transformers/es7/abstract-references.js b/lib/6to5/transformation/transformers/es7/abstract-references.js index fa1ed60dfa..d86cbaa7fc 100644 --- a/lib/6to5/transformation/transformers/es7/abstract-references.js +++ b/lib/6to5/transformation/transformers/es7/abstract-references.js @@ -23,7 +23,7 @@ var container = function (parent, call, ret, file) { } }; -exports.AssignmentExpression = function (node, parent, scope, context, file) { +exports.AssignmentExpression = function (node, parent, scope, file) { var left = node.left; if (!t.isVirtualPropertyExpression(left)) return; @@ -63,7 +63,7 @@ exports.AssignmentExpression = function (node, parent, scope, context, file) { return container(parent, call, value, file); }; -exports.UnaryExpression = function (node, parent, scope, context, file) { +exports.UnaryExpression = function (node, parent, scope, file) { var arg = node.argument; if (!t.isVirtualPropertyExpression(arg)) return; if (node.operator !== "delete") return; diff --git a/lib/6to5/transformation/transformers/es7/comprehensions.js b/lib/6to5/transformation/transformers/es7/comprehensions.js index a7b613ce69..98e7075d41 100644 --- a/lib/6to5/transformation/transformers/es7/comprehensions.js +++ b/lib/6to5/transformation/transformers/es7/comprehensions.js @@ -7,7 +7,7 @@ var t = require("../../../types"); exports.experimental = true; -exports.ComprehensionExpression = function (node, parent, scope, context, file) { +exports.ComprehensionExpression = function (node, parent, scope, file) { var callback = array; if (node.generator) callback = generator; return callback(node, parent, scope, file); diff --git a/lib/6to5/transformation/transformers/es7/object-rest-spread.js b/lib/6to5/transformation/transformers/es7/object-rest-spread.js index f70d9572e7..9337b95871 100644 --- a/lib/6to5/transformation/transformers/es7/object-rest-spread.js +++ b/lib/6to5/transformation/transformers/es7/object-rest-spread.js @@ -19,7 +19,7 @@ var hasSpread = function (node) { return false; }; -exports.ObjectExpression = function (node, parent, scope, context, file) { +exports.ObjectExpression = function (node, parent, scope, file) { if (!hasSpread(node)) return; var args = []; diff --git a/lib/6to5/transformation/transformers/internal/alias-functions.js b/lib/6to5/transformation/transformers/internal/alias-functions.js index 1290c17669..10f16d49d7 100644 --- a/lib/6to5/transformation/transformers/internal/alias-functions.js +++ b/lib/6to5/transformation/transformers/internal/alias-functions.js @@ -3,12 +3,12 @@ var t = require("../../../types"); var functionChildrenVisitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { if (t.isFunction(node) && !node._aliasFunction) { - return context.skip(); + return this.skip(); } - if (node._ignoreAliasFunctions) return context.skip(); + if (node._ignoreAliasFunctions) return this.skip(); var getId; @@ -25,11 +25,11 @@ var functionChildrenVisitor = { }; var functionVisitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { if (!node._aliasFunction) { if (t.isFunction(node)) { // stop traversal of this node as it'll be hit again by this transformer - return context.skip(); + return this.skip(); } else { return; } @@ -38,7 +38,7 @@ var functionVisitor = { // traverse all child nodes of this function and find `arguments` and `this` scope.traverse(node, functionChildrenVisitor, state); - return context.skip(); + return this.skip(); } }; diff --git a/lib/6to5/transformation/transformers/internal/module-formatter.js b/lib/6to5/transformation/transformers/internal/module-formatter.js index 3bacf0a1dd..da60d60a4b 100644 --- a/lib/6to5/transformation/transformers/internal/module-formatter.js +++ b/lib/6to5/transformation/transformers/internal/module-formatter.js @@ -2,16 +2,16 @@ var useStrict = require("../../helpers/use-strict"); -exports.Program = { - exit: function (program, parent, scope, context, file) { - if (!file.transformers["es6.modules"].canRun()) return; +exports.post = function (file) { + if (!file.transformers["es6.modules"].canRun()) return; - useStrict.wrap(program, function () { - program.body = file.dynamicImports.concat(program.body); - }); + var program = file.ast.program; - if (file.moduleFormatter.transform) { - file.moduleFormatter.transform(program); - } + useStrict.wrap(program, function () { + program.body = file.dynamicImports.concat(program.body); + }); + + if (file.moduleFormatter.transform) { + file.moduleFormatter.transform(program); } }; diff --git a/lib/6to5/transformation/transformers/internal/modules.js b/lib/6to5/transformation/transformers/internal/modules.js index 5c0a82f2cc..fabf23c62a 100644 --- a/lib/6to5/transformation/transformers/internal/modules.js +++ b/lib/6to5/transformation/transformers/internal/modules.js @@ -8,7 +8,7 @@ var t = require("../../../types"); -var resolveModuleSource = function (node, parent, scope, context, file) { +var resolveModuleSource = function (node, parent, scope, file) { var resolveModuleSource = file.opts.resolveModuleSource; if (node.source && resolveModuleSource) { node.source.value = resolveModuleSource(node.source.value); diff --git a/lib/6to5/transformation/transformers/minification/dead-code-elimination.js b/lib/6to5/transformation/transformers/minification/dead-code-elimination.js index 5668829823..b5ded93996 100644 --- a/lib/6to5/transformation/transformers/minification/dead-code-elimination.js +++ b/lib/6to5/transformation/transformers/minification/dead-code-elimination.js @@ -2,7 +2,7 @@ var t = require("../../../types"); exports.optional = true; -exports.ExpressionStatement = function (node, parent, scope, context) { +exports.ExpressionStatement = function (node, parent, scope) { // remove consequenceless expressions such as local variables and literals // // var foo = true; foo; -> var foo = true; @@ -11,12 +11,12 @@ exports.ExpressionStatement = function (node, parent, scope, context) { var expr = node.expression; if (t.isLiteral(expr) || (t.isIdentifier(node) && t.hasBinding(node.name))) { - context.remove(); + this.remove(); } }; exports.IfStatement = { - exit: function (node, parent, scope, context) { + exit: function (node, parent, scope) { // todo: in scenarios where we can just return the consequent or // alternate we should drop the block statement if it contains no // block scoped variables @@ -47,7 +47,7 @@ exports.IfStatement = { if (alternate) { return alternate; } else { - return context.remove(); + return this.remove(); } } diff --git a/lib/6to5/transformation/transformers/minification/remove-console-calls.js b/lib/6to5/transformation/transformers/minification/remove-console-calls.js index 09ff326e15..bac1650af9 100644 --- a/lib/6to5/transformation/transformers/minification/remove-console-calls.js +++ b/lib/6to5/transformation/transformers/minification/remove-console-calls.js @@ -6,8 +6,8 @@ var isConsole = t.buildMatchMemberExpression("console", true); exports.optional = true; -exports.CallExpression = function (node, parent, scope, context) { +exports.CallExpression = function (node, parent, scope) { if (isConsole(node.callee)) { - context.remove(); + this.remove(); } }; diff --git a/lib/6to5/transformation/transformers/minification/remove-debugger.js b/lib/6to5/transformation/transformers/minification/remove-debugger.js index 5df8930923..b8950c888a 100644 --- a/lib/6to5/transformation/transformers/minification/remove-debugger.js +++ b/lib/6to5/transformation/transformers/minification/remove-debugger.js @@ -2,8 +2,8 @@ var t = require("../../../types"); exports.optional = true; -exports.ExpressionStatement = function (node, parent, scope, context) { +exports.ExpressionStatement = function (node, parent, scope) { if (t.isIdentifier(node.expression, { name: "debugger" })) { - context.remove(); + this.remove(); } }; diff --git a/lib/6to5/transformation/transformers/other/async-to-generator.js b/lib/6to5/transformation/transformers/other/async-to-generator.js index 790078275a..a9085e887b 100644 --- a/lib/6to5/transformation/transformers/other/async-to-generator.js +++ b/lib/6to5/transformation/transformers/other/async-to-generator.js @@ -7,7 +7,7 @@ exports.optional = true; exports.manipulateOptions = bluebirdCoroutines.manipulateOptions; -exports.Function = function (node, parent, scope, context, file) { +exports.Function = function (node, parent, scope, file) { if (!node.async || node.generator) return; return remapAsyncToGenerator(node, file.addHelper("async-to-generator"), scope); diff --git a/lib/6to5/transformation/transformers/other/bluebird-coroutines.js b/lib/6to5/transformation/transformers/other/bluebird-coroutines.js index 4d51fdda16..b32a5f14bb 100644 --- a/lib/6to5/transformation/transformers/other/bluebird-coroutines.js +++ b/lib/6to5/transformation/transformers/other/bluebird-coroutines.js @@ -10,7 +10,7 @@ exports.manipulateOptions = function (opts) { exports.optional = true; -exports.Function = function (node, parent, scope, context, file) { +exports.Function = function (node, parent, scope, file) { if (!node.async || node.generator) return; return remapAsyncToGenerator( diff --git a/lib/6to5/transformation/transformers/other/react.js b/lib/6to5/transformation/transformers/other/react.js index e265fb7b1e..e20a4fc85d 100644 --- a/lib/6to5/transformation/transformers/other/react.js +++ b/lib/6to5/transformation/transformers/other/react.js @@ -26,7 +26,7 @@ exports.JSXIdentifier = function (node, parent) { } }; -exports.JSXNamespacedName = function (node, parent, scope, context, file) { +exports.JSXNamespacedName = function (node, parent, scope, file) { throw file.errorWithNode(node, "Namespace tags are not supported. ReactJSX is not XML."); }; @@ -53,7 +53,7 @@ var isCompatTag = function (tagName) { }; exports.JSXOpeningElement = { - exit: function (node, parent, scope, context, file) { + exit: function (node, parent, scope, file) { var reactCompat = file.opts.reactCompat; var tagExpr = node.name; var args = []; @@ -259,7 +259,7 @@ var addDisplayName = function (id, call) { } }; -exports.ExportDeclaration = function (node, parent, scope, context, file) { +exports.ExportDeclaration = function (node, parent, scope, file) { if (node.default && react.isCreateClass(node.declaration)) { addDisplayName(file.opts.basename, node.declaration); } diff --git a/lib/6to5/transformation/transformers/other/regenerator.js b/lib/6to5/transformation/transformers/other/regenerator.js index 766956e95d..2f2724d553 100644 --- a/lib/6to5/transformation/transformers/other/regenerator.js +++ b/lib/6to5/transformation/transformers/other/regenerator.js @@ -1,6 +1,7 @@ "use strict"; var regenerator = require("regenerator-6to5"); +var t = require("../../../types"); exports.check = function (node) { return t.isFunction(node) && (node.async || node.generator); diff --git a/lib/6to5/transformation/transformers/other/self-contained.js b/lib/6to5/transformation/transformers/other/self-contained.js index 3ff1c8d9da..5cfb06f95c 100644 --- a/lib/6to5/transformation/transformers/other/self-contained.js +++ b/lib/6to5/transformation/transformers/other/self-contained.js @@ -20,7 +20,7 @@ var ALIASABLE_CONSTRUCTORS = [ ]; var astVisitor = { - enter: function (node, parent, scope, context, file) { + enter: function (node, parent, scope, file) { var prop; if (t.isMemberExpression(node) && t.isReferenced(node, parent)) { @@ -31,7 +31,7 @@ var astVisitor = { if (!t.isReferenced(obj, node)) return; if (!node.computed && coreHas(obj) && has(core[obj.name], prop.name) && !scope.getBinding(obj.name)) { - context.skip(); + this.skip(); return t.prependToMemberExpression(node, file.get("coreIdentifier")); } } else if (t.isReferencedIdentifier(node, parent) && !t.isMemberExpression(parent) && contains(ALIASABLE_CONSTRUCTORS, node.name) && !scope.getBinding(node.name)) { @@ -64,27 +64,25 @@ exports.manipulateOptions = function (opts) { if (opts.whitelist.length) opts.whitelist.push("es6.modules"); }; -exports.Program = { - enter: function (node, parent, scope, context, file) { - file.setDynamic("runtimeIdentifier", function () { - return file.addImport("6to5-runtime/helpers", "to5Helpers"); - }); - - file.setDynamic("coreIdentifier", function () { - return file.addImport("6to5-runtime/core-js", "core"); - }); - - file.setDynamic("regeneratorIdentifier", function () { - return file.addImport("6to5-runtime/regenerator", "regeneratorRuntime"); - }); - }, - - exit: function (node, parent, scope, context, file) { - file.scope.traverse(node, astVisitor, file); - } +exports.post = function (file) { + file.scope.traverse(file.ast, astVisitor, file); }; -exports.Identifier = function (node, parent, scope, context, file) { +exports.pre = function ( file) { + file.setDynamic("runtimeIdentifier", function () { + return file.addImport("6to5-runtime/helpers", "to5Helpers"); + }); + + file.setDynamic("coreIdentifier", function () { + return file.addImport("6to5-runtime/core-js", "core"); + }); + + file.setDynamic("regeneratorIdentifier", function () { + return file.addImport("6to5-runtime/regenerator", "regeneratorRuntime"); + }); +}; + +exports.Identifier = function (node, parent, scope, file) { if (node.name === "regeneratorRuntime" && t.isReferenced(node, parent)) { return file.get("regeneratorIdentifier"); } diff --git a/lib/6to5/transformation/transformers/other/use-strict.js b/lib/6to5/transformation/transformers/other/use-strict.js index 87d9672cc8..3a7db8257e 100644 --- a/lib/6to5/transformation/transformers/other/use-strict.js +++ b/lib/6to5/transformation/transformers/other/use-strict.js @@ -3,17 +3,16 @@ var useStrict = require("../../helpers/use-strict"); var t = require("../../../types"); -exports.Program = { - exit: function (program) { - if (!useStrict.has(program)) { - program.body.unshift(t.expressionStatement(t.literal("use strict"))); - } +exports.post = function (file) { + var program = file.ast.program; + if (!useStrict.has(program)) { + program.body.unshift(t.expressionStatement(t.literal("use strict"))); } }; exports.FunctionDeclaration = -exports.FunctionExpression = function (node, parent, scope, context) { - context.skip(); +exports.FunctionExpression = function (node, parent, scope) { + this.skip(); }; exports.ThisExpression = function () { diff --git a/lib/6to5/transformation/transformers/playground/method-binding.js b/lib/6to5/transformation/transformers/playground/method-binding.js index 47e08b86ef..085496a46b 100644 --- a/lib/6to5/transformation/transformers/playground/method-binding.js +++ b/lib/6to5/transformation/transformers/playground/method-binding.js @@ -26,7 +26,7 @@ exports.BindMemberExpression = function (node, parent, scope) { } }; -exports.BindFunctionExpression = function (node, parent, scope, context, file) { +exports.BindFunctionExpression = function (node, parent, scope, file) { var buildCall = function (args) { var param = scope.generateUidIdentifier("val"); return t.functionExpression(null, [param], t.blockStatement([ diff --git a/lib/6to5/transformation/transformers/playground/object-getter-memoization.js b/lib/6to5/transformation/transformers/playground/object-getter-memoization.js index 5f49b46dc2..7ceaee2943 100644 --- a/lib/6to5/transformation/transformers/playground/object-getter-memoization.js +++ b/lib/6to5/transformation/transformers/playground/object-getter-memoization.js @@ -5,8 +5,8 @@ var t = require("../../../types"); exports.playground = true; var visitor = { - enter: function (node, parent, scope, context, state) { - if (t.isFunction(node)) return context.skip(); + enter: function (node, parent, scope, state) { + if (t.isFunction(node)) return this.skip(); if (t.isReturnStatement(node) && node.argument) { node.argument = t.memberExpression(t.callExpression(state.file.addHelper("define-property"), [ @@ -19,7 +19,7 @@ var visitor = { }; exports.Property = -exports.MethodDefinition = function (node, parent, scope, context, file) { +exports.MethodDefinition = function (node, parent, scope, file) { if (node.kind !== "memo") return; node.kind = "get"; diff --git a/lib/6to5/transformation/transformers/spec/proto-to-assign.js b/lib/6to5/transformation/transformers/spec/proto-to-assign.js index e63e03696c..dac84dfecc 100644 --- a/lib/6to5/transformation/transformers/spec/proto-to-assign.js +++ b/lib/6to5/transformation/transformers/spec/proto-to-assign.js @@ -19,7 +19,7 @@ var buildDefaultsCallExpression = function (expr, ref, file) { exports.optional = true; exports.secondPass = true; -exports.AssignmentExpression = function (node, parent, scope, context, file) { +exports.AssignmentExpression = function (node, parent, scope, file) { if (!isProtoAssignmentExpression(node)) return; var nodes = []; @@ -33,7 +33,7 @@ exports.AssignmentExpression = function (node, parent, scope, context, file) { return t.toSequenceExpression(nodes); }; -exports.ExpressionStatement = function (node, parent, scope, context, file) { +exports.ExpressionStatement = function (node, parent, scope, file) { var expr = node.expression; if (!t.isAssignmentExpression(expr, { operator: "=" })) return; @@ -42,7 +42,7 @@ exports.ExpressionStatement = function (node, parent, scope, context, file) { } }; -exports.ObjectExpression = function (node, parent, scope, context, file) { +exports.ObjectExpression = function (node, parent, scope, file) { var proto; for (var i = 0; i < node.properties.length; i++) { diff --git a/lib/6to5/transformation/transformers/spec/typeof-symbol.js b/lib/6to5/transformation/transformers/spec/typeof-symbol.js index c4a10c6fa1..ce3e6016b5 100644 --- a/lib/6to5/transformation/transformers/spec/typeof-symbol.js +++ b/lib/6to5/transformation/transformers/spec/typeof-symbol.js @@ -4,8 +4,8 @@ var t = require("../../../types"); exports.optional = true; -exports.UnaryExpression = function (node, parent, scope, context, file) { - context.skip(); +exports.UnaryExpression = function (node, parent, scope, file) { + this.skip(); if (node.operator === "typeof") { var call = t.callExpression(file.addHelper("typeof"), [node.argument]); diff --git a/lib/6to5/transformation/transformers/validation/no-for-in-of-assignment.js b/lib/6to5/transformation/transformers/validation/no-for-in-of-assignment.js index 165b888533..e00e74d8ab 100644 --- a/lib/6to5/transformation/transformers/validation/no-for-in-of-assignment.js +++ b/lib/6to5/transformation/transformers/validation/no-for-in-of-assignment.js @@ -3,7 +3,7 @@ var t = require("../../../types"); exports.ForInStatement = -exports.ForOfStatement = function (node, parent, scope, context, file) { +exports.ForOfStatement = function (node, parent, scope, file) { var left = node.left; if (t.isVariableDeclaration(left)) { var declar = left.declarations[0]; diff --git a/lib/6to5/transformation/transformers/validation/setters.js b/lib/6to5/transformation/transformers/validation/setters.js index e104022bbc..bb4e7e1ab8 100644 --- a/lib/6to5/transformation/transformers/validation/setters.js +++ b/lib/6to5/transformation/transformers/validation/setters.js @@ -1,7 +1,7 @@ "use strict"; exports.MethodDefinition = -exports.Property = function (node, parent, scope, context, file) { +exports.Property = function (node, parent, scope, file) { if (node.kind === "set" && node.value.params.length !== 1) { throw file.errorWithNode(node.value, "Setters must have only one parameter"); } diff --git a/lib/6to5/transformation/transformers/validation/undeclared-variable-check.js b/lib/6to5/transformation/transformers/validation/undeclared-variable-check.js index 919d417f2f..ebf66d15fc 100644 --- a/lib/6to5/transformation/transformers/validation/undeclared-variable-check.js +++ b/lib/6to5/transformation/transformers/validation/undeclared-variable-check.js @@ -5,7 +5,7 @@ var t = require("../../../types"); exports.optional = true; -exports.Identifier = function (node, parent, scope, context, file) { +exports.Identifier = function (node, parent, scope, file) { if (!t.isReferenced(node, parent)) return; if (scope.hasBinding(node.name)) return; diff --git a/lib/6to5/traversal/context.js b/lib/6to5/traversal/context.js index 995e7f6475..43db07f7a2 100644 --- a/lib/6to5/traversal/context.js +++ b/lib/6to5/traversal/context.js @@ -4,9 +4,9 @@ module.exports = TraversalContext; -var TraversalIteration = require("./iteration"); -var flatten = require("lodash/array/flatten"); -var compact = require("lodash/array/compact"); +var TraversalPath = require("./path"); +var flatten = require("lodash/array/flatten"); +var compact = require("lodash/array/compact"); function TraversalContext(scope, opts, state) { this.shouldFlatten = false; @@ -44,7 +44,7 @@ TraversalContext.prototype.reset = function () { TraversalContext.prototype.visitNode = function (node, obj, key) { this.reset(); - var iteration = new TraversalIteration(this, node, obj, key); + var iteration = new TraversalPath(this, node, obj, key); return iteration.visit(); }; diff --git a/lib/6to5/traversal/index.js b/lib/6to5/traversal/index.js index b5f475d78f..8ebb8fbcd5 100644 --- a/lib/6to5/traversal/index.js +++ b/lib/6to5/traversal/index.js @@ -93,7 +93,7 @@ traverse.explode = function (obj) { return obj; }; -function hasBlacklistedType(node, parent, scope, context, state) { +function hasBlacklistedType(node, parent, scope, state) { if (node.type === state.type) { state.has = true; context.skip(); diff --git a/lib/6to5/traversal/iteration.js b/lib/6to5/traversal/path.js similarity index 56% rename from lib/6to5/traversal/iteration.js rename to lib/6to5/traversal/path.js index 884cec41c8..19cac99043 100644 --- a/lib/6to5/traversal/iteration.js +++ b/lib/6to5/traversal/path.js @@ -1,6 +1,6 @@ "use strict"; -module.exports = TraversalIteration; +module.exports = TraversalPath; /* jshint maxparams:7 */ @@ -9,16 +9,42 @@ var contains = require("lodash/collection/contains"); var Scope = require("./scope"); var t = require("../types"); -function TraversalIteration(context, parent, obj, key) { +function TraversalPath(context, parent, obj, key) { + this.shouldRemove = false; + this.shouldSkip = false; + this.shouldStop = false; + + this.context = context; + this.state = this.context.state; + this.opts = this.context.opts; + this.key = key; this.obj = obj; - this.context = context; + this.parent = parent; - this.scope = TraversalIteration.getScope(this.getNode(), parent, context.scope); + this.scope = TraversalPath.getScope(this.getNode(), parent, context.scope); this.state = context.state; } -TraversalIteration.getScope = function (node, parent, scope) { +TraversalPath.prototype.remove = function () { + this.shouldRemove = true; + this.shouldSkip = true; +}; + +TraversalPath.prototype.skip = function () { + this.shouldSkip = true; +}; + +TraversalPath.prototype.stop = function () { + this.shouldStop = true; + this.shouldSkip = true; +}; + +TraversalPath.prototype.flatten = function () { + this.context.flatten(); +}; + +TraversalPath.getScope = function (node, parent, scope) { var ourScope = scope; // we're entering a new scope so let's construct it! @@ -29,22 +55,22 @@ TraversalIteration.getScope = function (node, parent, scope) { return ourScope; }; -TraversalIteration.prototype.maybeRemove = function () { - if (this.context.shouldRemove) { +TraversalPath.prototype.maybeRemove = function () { + if (this.shouldRemove) { this.setNode(null); - this.context.flatten(); + this.flatten(); } }; -TraversalIteration.prototype.setNode = function (val) { +TraversalPath.prototype.setNode = function (val) { return this.obj[this.key] = val; }; -TraversalIteration.prototype.getNode = function () { +TraversalPath.prototype.getNode = function () { return this.obj[this.key]; }; -TraversalIteration.prototype.replaceNode = function (replacement, scope) { +TraversalPath.prototype.replaceNode = function (replacement, scope) { var isArray = Array.isArray(replacement); // inherit comments from original node to the first replacement node @@ -73,13 +99,19 @@ TraversalIteration.prototype.replaceNode = function (replacement, scope) { t.ensureBlock(this.obj, this.key); } - this.context.flatten(); + this.flatten(); } }; -TraversalIteration.prototype.call = function (fn) { +TraversalPath.prototype.call = function (key) { var node = this.getNode(); - var replacement = fn.call(this, node, this.parent, this.scope, this.context, this.state); + if (!node) return; + + var opts = this.opts; + var fn = opts[key]; + if (opts[node.type]) fn = opts[node.type][key] || fn; + + var replacement = fn.call(this, node, this.parent, this.scope, this.state); if (replacement) { this.replaceNode(replacement); @@ -91,22 +123,19 @@ TraversalIteration.prototype.call = function (fn) { return node; }; -TraversalIteration.prototype.visit = function () { - this.context.reset(); - - var state = this.context.state; - var opts = this.context.opts; - var node = this.getNode(); +TraversalPath.prototype.visit = function () { + var opts = this.opts; + var node = this.getNode(); // type is blacklisted if (opts.blacklist && opts.blacklist.indexOf(node.type) > -1) { return; } - this.call(opts.enter); + this.call("enter"); - if (this.context.shouldSkip) { - return this.context.shouldStop; + if (this.shouldSkip) { + return this.shouldStop; } node = this.getNode(); @@ -115,12 +144,12 @@ TraversalIteration.prototype.visit = function () { // traverse over these replacement nodes we purposely don't call exitNode // as the original node has been destroyed for (var i = 0; i < node.length; i++) { - traverse.node(node[i], opts, this.scope, state); + traverse.node(node[i], opts, this.scope, this.state); } } else { - traverse.node(node, opts, this.scope, state); - this.call(opts.exit); + traverse.node(node, opts, this.scope, this.state); + this.call("exit"); } - return this.context.shouldStop; + return this.shouldStop; }; diff --git a/lib/6to5/traversal/scope.js b/lib/6to5/traversal/scope.js index 064aae2290..f752081409 100644 --- a/lib/6to5/traversal/scope.js +++ b/lib/6to5/traversal/scope.js @@ -213,7 +213,7 @@ Scope.prototype.registerVariableDeclaration = function (declar) { }; var functionVariableVisitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { if (t.isFor(node)) { each(t.FOR_INIT_KEYS, function (key) { var declar = node[key]; @@ -223,7 +223,7 @@ var functionVariableVisitor = { // this block is a function so we'll stop since none of the variables // declared within are accessible - if (t.isFunction(node)) return context.skip(); + if (t.isFunction(node)) return this.skip(); // function identifier doesn't belong to this scope if (state.blockId && node === state.blockId) return; @@ -240,7 +240,7 @@ var functionVariableVisitor = { }; var programReferenceVisitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { if (t.isReferencedIdentifier(node, parent) && !scope.hasReference(node.name)) { state.register(node, true); } @@ -248,11 +248,11 @@ var programReferenceVisitor = { }; var blockVariableVisitor = { - enter: function (node, parent, scope, context, state) { + enter: function (node, parent, scope, state) { if (t.isBlockScoped(node)) { state.register(node); } else if (t.isScope(node)) { - context.skip(); + this.skip(); } } }; @@ -339,12 +339,6 @@ Scope.prototype.crawl = function () { }); } - if (t.isFunctionExpression(block) && block.id) { - if (!t.isProperty(this.parentBlock, { method: true })) { - this.register(block.id); - } - } - // Program if (t.isProgram(block)) { diff --git a/lib/6to5/util.js b/lib/6to5/util.js index b9be071c03..501315617f 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -79,7 +79,7 @@ exports.sourceMapToComment = function (map) { }; var templateVisitor = { - enter: function (node, parent, scope, context, nodes) { + enter: function (node, parent, scope, nodes) { if (t.isIdentifier(node) && has(nodes, node.name)) { return nodes[node.name]; } diff --git a/test/fixtures/transformation/self-contained/full/expected.js b/test/fixtures/transformation/self-contained/full/expected.js index 2d01238c30..6e3064b562 100644 --- a/test/fixtures/transformation/self-contained/full/expected.js +++ b/test/fixtures/transformation/self-contained/full/expected.js @@ -2,10 +2,10 @@ var _regeneratorRuntime = require("6to5-runtime/regenerator"); -var _core = require("6to5-runtime/core-js"); - var _to5Helpers = require("6to5-runtime/helpers"); +var _core = require("6to5-runtime/core-js"); + var giveWord = _regeneratorRuntime.mark(function giveWord() { return _regeneratorRuntime.wrap(function giveWord$(context$1$0) { while (1) switch (context$1$0.prev = context$1$0.next) {