more reliable scope construction

This commit is contained in:
Sebastian McKenzie
2015-02-11 00:38:59 +11:00
parent 4df1b6700b
commit 897566ccb3
5 changed files with 40 additions and 27 deletions

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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)) {

View File

@@ -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"],

View File

@@ -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);