[walker] Handle catch scopes more accurately in scopeVisitor
This commit is contained in:
parent
b1623b10c1
commit
e6ff440e9f
15
util/walk.js
15
util/walk.js
@ -272,8 +272,12 @@
|
||||
|
||||
// A custom walker that keeps track of the scope chain and the
|
||||
// variables defined in it.
|
||||
function makeScope(prev) {
|
||||
return {vars: Object.create(null), prev: prev};
|
||||
function makeScope(prev, isCatch) {
|
||||
return {vars: Object.create(null), prev: prev, isCatch: isCatch};
|
||||
}
|
||||
function normalScope(scope) {
|
||||
while (scope.isCatch) scope = scope.prev;
|
||||
return scope;
|
||||
}
|
||||
exports.scopeVisitor = exports.make({
|
||||
Function: function(node, scope, c) {
|
||||
@ -282,7 +286,7 @@
|
||||
inner.vars[node.params[i].name] = {type: "argument", node: node.params[i]};
|
||||
if (node.id) {
|
||||
var decl = node.type == "FunctionDeclaration";
|
||||
(decl ? scope : inner).vars[node.id.name] =
|
||||
(decl ? normalScope(scope) : inner).vars[node.id.name] =
|
||||
{type: decl ? "function" : "function name", node: node.id};
|
||||
}
|
||||
c(node.body, inner, "ScopeBody");
|
||||
@ -290,16 +294,17 @@
|
||||
TryStatement: function(node, scope, c) {
|
||||
c(node.block, scope, "Statement");
|
||||
if (node.handler) {
|
||||
var inner = makeScope(scope);
|
||||
var inner = makeScope(scope, true);
|
||||
inner.vars[node.handler.param.name] = {type: "catch clause", node: node.handler.param};
|
||||
c(node.handler.body, inner, "ScopeBody");
|
||||
}
|
||||
if (node.finalizer) c(node.finalizer, scope, "Statement");
|
||||
},
|
||||
VariableDeclaration: function(node, scope, c) {
|
||||
var target = normalScope(scope);
|
||||
for (var i = 0; i < node.declarations.length; ++i) {
|
||||
var decl = node.declarations[i];
|
||||
scope.vars[decl.id.name] = {type: "var", node: decl.id};
|
||||
target.vars[decl.id.name] = {type: "var", node: decl.id};
|
||||
if (decl.init) c(decl.init, scope, "Expression");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user