From 897566ccb3097a75a01c2aa8ab63d6631ceaf80f Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 11 Feb 2015 00:38:59 +1100 Subject: [PATCH] more reliable scope construction --- .../transformers/es6/constants.js | 4 ++-- lib/6to5/traversal/path.js | 2 +- lib/6to5/traversal/scope.js | 15 +++--------- lib/6to5/types/alias-keys.json | 24 +++++++++---------- lib/6to5/types/index.js | 22 +++++++++++++++++ 5 files changed, 40 insertions(+), 27 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6/constants.js b/lib/6to5/transformation/transformers/es6/constants.js index 47b257afdc..286cdb8ff4 100644 --- a/lib/6to5/transformation/transformers/es6/constants.js +++ b/lib/6to5/transformation/transformers/es6/constants.js @@ -31,13 +31,13 @@ var visitor = { throw state.file.errorWithNode(id, name + " is read-only"); } - } else if (t.isScope(node)) { + } else if (t.isScope(node, parent)) { this.skip(); } } }; -exports.Scope = function (node, parent, scope, file) { +exports.Scopable = function (node, parent, scope, file) { scope.traverse(node, visitor, { constants: scope.getAllBindingsOfKind("const"), file: file diff --git a/lib/6to5/traversal/path.js b/lib/6to5/traversal/path.js index 473474038f..24a009d37c 100644 --- a/lib/6to5/traversal/path.js +++ b/lib/6to5/traversal/path.js @@ -49,7 +49,7 @@ TraversalPath.getScope = function (node, parent, scope) { var ourScope = scope; // we're entering a new scope so let's construct it! - if (t.isScope(node)) { + if (t.isScope(node, parent)) { ourScope = new Scope(node, parent, scope); } diff --git a/lib/6to5/traversal/scope.js b/lib/6to5/traversal/scope.js index 0731d4ac82..f6d9c4eab2 100644 --- a/lib/6to5/traversal/scope.js +++ b/lib/6to5/traversal/scope.js @@ -163,8 +163,8 @@ Scope.prototype.rename = function (oldName, newName) { enter: function (node, parent, scope) { if (t.isReferencedIdentifier(node, parent) && node.name === oldName) { node.name = newName; - } else if (t.isScope(node)) { - if (scope.bindingIdentifierEquals(oldName, binding)) { + } else if (t.isScope(node, parent)) { + if (!scope.bindingIdentifierEquals(oldName, binding)) { this.skip(); } } @@ -310,7 +310,7 @@ var blockVariableVisitor = { enter: function (node, parent, scope, state) { if (t.isFunctionDeclaration(node) || t.isBlockScoped(node)) { state.registerDeclaration(node); - } else if (t.isScope(node)) { + } else if (t.isScope(node, parent)) { this.skip(); } } @@ -336,15 +336,6 @@ Scope.prototype.crawl = function () { extend(this, info); - // - - if (parent && t.isBlockStatement(block)) { - if (t.isLoop(parent.block, { body: block }) || - t.isFunction(parent.block, { body: block })) { - return; - } - } - // ForStatement - left, init if (t.isLoop(block)) { diff --git a/lib/6to5/types/alias-keys.json b/lib/6to5/types/alias-keys.json index 52ae624e72..2dfddb2885 100644 --- a/lib/6to5/types/alias-keys.json +++ b/lib/6to5/types/alias-keys.json @@ -3,13 +3,13 @@ "BreakStatement": ["Statement"], "ContinueStatement": ["Statement"], "DebuggerStatement": ["Statement"], - "DoWhileStatement": ["Statement", "Loop", "While", "Scope"], + "DoWhileStatement": ["Statement", "Loop", "While", "Scopable"], "IfStatement": ["Statement"], "ReturnStatement": ["Statement"], "SwitchStatement": ["Statement"], "ThrowStatement": ["Statement"], "TryStatement": ["Statement"], - "WhileStatement": ["Statement", "Loop", "While", "Scope"], + "WhileStatement": ["Statement", "Loop", "While", "Scopable"], "WithStatement": ["Statement"], "EmptyStatement": ["Statement"], "LabeledStatement": ["Statement"], @@ -18,13 +18,13 @@ "ImportDeclaration": ["Statement", "Declaration"], "PrivateDeclaration": ["Statement", "Declaration"], - "ArrowFunctionExpression": ["Scope", "Function", "Expression"], - "FunctionDeclaration": ["Statement", "Declaration", "Scope", "Function"], - "FunctionExpression": ["Scope", "Function", "Expression"], + "ArrowFunctionExpression": ["Scopable", "Function", "Expression"], + "FunctionDeclaration": ["Statement", "Declaration", "Scopable", "Function"], + "FunctionExpression": ["Scopable", "Function", "Expression"], - "BlockStatement": ["Statement", "Scope"], - "Program": ["Scope"], - "CatchClause": ["Scope"], + "BlockStatement": ["Statement", "Scopable"], + "Program": ["Scopable"], + "CatchClause": ["Scopable"], "LogicalExpression": ["Binary", "Expression"], "BinaryExpression": ["Binary", "Expression"], @@ -36,9 +36,9 @@ "ClassDeclaration": ["Statement", "Declaration", "Class"], "ClassExpression": ["Class", "Expression"], - "ForOfStatement": ["Statement", "For", "Scope", "Loop"], - "ForInStatement": ["Statement", "For", "Scope", "Loop"], - "ForStatement": ["Statement", "For", "Scope", "Loop"], + "ForOfStatement": ["Statement", "For", "Scopable", "Loop"], + "ForInStatement": ["Statement", "For", "Scopable", "Loop"], + "ForStatement": ["Statement", "For", "Scopable", "Loop"], "ObjectPattern": ["Pattern"], "ArrayPattern": ["Pattern"], @@ -53,7 +53,7 @@ "BindFunctionExpression": ["Expression"], "BindMemberExpression": ["Expression"], "CallExpression": ["Expression"], - "ComprehensionExpression": ["Expression", "Scope"], + "ComprehensionExpression": ["Expression", "Scopable"], "ConditionalExpression": ["Expression"], "Identifier": ["Expression"], "Literal": ["Expression"], diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 58ffdda7fb..63a59db517 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -780,5 +780,27 @@ t.isSpecifierDefault = function (specifier) { return specifier.default || t.isIdentifier(specifier.id) && specifier.id.name === "default"; }; +/** + * Description + * + * @param {Node} node + * @param {Node} parent + * @returns {Boolean} + */ + +t.isScope = function (node, parent) { + if (t.isBlockStatement(node)) { + if (t.isLoop(parent.block, { body: node })) { + return false; + } + + if (t.isFunction(parent.block, { body: node })) { + return false; + } + } + + return t.isScopable(node); +}; + toFastProperties(t); toFastProperties(t.VISITOR_KEYS);