fix SymbolDef.global
properly compute for top-level block-variables
This commit is contained in:
40
lib/scope.js
40
lib/scope.js
@@ -105,7 +105,8 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
|
|||||||
var labels = new Dictionary();
|
var labels = new Dictionary();
|
||||||
var defun = null;
|
var defun = null;
|
||||||
var in_destructuring = null;
|
var in_destructuring = null;
|
||||||
var in_export;
|
var in_export = false;
|
||||||
|
var in_block = 0;
|
||||||
var tw = new TreeWalker(function(node, descend){
|
var tw = new TreeWalker(function(node, descend){
|
||||||
if (node.is_block_scope()) {
|
if (node.is_block_scope()) {
|
||||||
var save_scope = scope;
|
var save_scope = scope;
|
||||||
@@ -146,6 +147,16 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
|
|||||||
in_export = false;
|
in_export = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (node instanceof AST_BlockStatement
|
||||||
|
|| node instanceof AST_Switch
|
||||||
|
|| node instanceof AST_Try
|
||||||
|
|| node instanceof AST_Catch
|
||||||
|
|| node instanceof AST_Finally) {
|
||||||
|
in_block++;
|
||||||
|
descend();
|
||||||
|
in_block--;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if (node instanceof AST_LabeledStatement) {
|
if (node instanceof AST_LabeledStatement) {
|
||||||
var l = node.label;
|
var l = node.label;
|
||||||
if (labels.has(l.name)) {
|
if (labels.has(l.name)) {
|
||||||
@@ -172,7 +183,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
|
|||||||
node.references = [];
|
node.references = [];
|
||||||
}
|
}
|
||||||
if (node instanceof AST_SymbolLambda) {
|
if (node instanceof AST_SymbolLambda) {
|
||||||
defun.def_function(node, in_export);
|
defun.def_function(node, in_export, in_block);
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_SymbolDefun) {
|
else if (node instanceof AST_SymbolDefun) {
|
||||||
// Careful here, the scope where this should be defined is
|
// Careful here, the scope where this should be defined is
|
||||||
@@ -184,29 +195,28 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
|
|||||||
while (parent_lambda.is_block_scope()) {
|
while (parent_lambda.is_block_scope()) {
|
||||||
parent_lambda = parent_lambda.parent_scope;
|
parent_lambda = parent_lambda.parent_scope;
|
||||||
}
|
}
|
||||||
(node.scope = parent_lambda).def_function(node, in_export);
|
(node.scope = parent_lambda).def_function(node, in_export, in_block);
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_SymbolClass) {
|
else if (node instanceof AST_SymbolClass) {
|
||||||
defun.def_variable(node, in_export);
|
defun.def_variable(node, in_export, in_block);
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_SymbolImport) {
|
else if (node instanceof AST_SymbolImport) {
|
||||||
scope.def_variable(node, in_export);
|
scope.def_variable(node, in_export, in_block);
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_SymbolDefClass) {
|
else if (node instanceof AST_SymbolDefClass) {
|
||||||
// This deals with the name of the class being available
|
// This deals with the name of the class being available
|
||||||
// inside the class.
|
// inside the class.
|
||||||
(node.scope = defun.parent_scope).def_function(node, in_export);
|
(node.scope = defun.parent_scope).def_function(node, in_export, in_block);
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_SymbolVar
|
else if (node instanceof AST_SymbolVar
|
||||||
|| node instanceof AST_SymbolConst
|
|| node instanceof AST_SymbolConst
|
||||||
|| node instanceof AST_SymbolLet) {
|
|| node instanceof AST_SymbolLet) {
|
||||||
var def = ((node instanceof AST_SymbolBlockDeclaration) ? scope : defun).def_variable(node, in_export);
|
var def = ((node instanceof AST_SymbolBlockDeclaration) ? scope : defun).def_variable(node, in_export, in_block);
|
||||||
def.destructuring = in_destructuring;
|
def.destructuring = in_destructuring;
|
||||||
def.init = tw.parent().value;
|
def.init = tw.parent().value;
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_SymbolCatch) {
|
else if (node instanceof AST_SymbolCatch) {
|
||||||
(options.screw_ie8 ? scope : defun)
|
(options.screw_ie8 ? scope : defun).def_variable(node, in_export, in_block);
|
||||||
.def_variable(node);
|
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_LabelRef) {
|
else if (node instanceof AST_LabelRef) {
|
||||||
var sym = labels.get(node.name);
|
var sym = labels.get(node.name);
|
||||||
@@ -336,11 +346,11 @@ AST_Scope.DEFMETHOD("find_variable", function(name){
|
|||||||
|| (this.parent_scope && this.parent_scope.find_variable(name));
|
|| (this.parent_scope && this.parent_scope.find_variable(name));
|
||||||
});
|
});
|
||||||
|
|
||||||
AST_Scope.DEFMETHOD("def_function", function(symbol, in_export){
|
AST_Scope.DEFMETHOD("def_function", function(symbol, in_export, in_block){
|
||||||
this.functions.set(symbol.name, this.def_variable(symbol, in_export));
|
this.functions.set(symbol.name, this.def_variable(symbol, in_export, in_block));
|
||||||
});
|
});
|
||||||
|
|
||||||
AST_Scope.DEFMETHOD("def_variable", function(symbol, in_export){
|
AST_Scope.DEFMETHOD("def_variable", function(symbol, in_export, in_block){
|
||||||
var def;
|
var def;
|
||||||
if (!this.variables.has(symbol.name)) {
|
if (!this.variables.has(symbol.name)) {
|
||||||
def = new SymbolDef(this, this.variables.size(), symbol);
|
def = new SymbolDef(this, this.variables.size(), symbol);
|
||||||
@@ -349,7 +359,11 @@ AST_Scope.DEFMETHOD("def_variable", function(symbol, in_export){
|
|||||||
if (in_export) {
|
if (in_export) {
|
||||||
def.export = true;
|
def.export = true;
|
||||||
}
|
}
|
||||||
def.global = !this.parent_scope && !(symbol instanceof AST_SymbolBlockDeclaration);
|
if (in_block && symbol instanceof AST_SymbolBlockDeclaration) {
|
||||||
|
def.global = false;
|
||||||
|
} else {
|
||||||
|
def.global = !this.parent_scope;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
def = this.variables.get(symbol.name);
|
def = this.variables.get(symbol.name);
|
||||||
def.orig.push(symbol);
|
def.orig.push(symbol);
|
||||||
|
|||||||
Reference in New Issue
Block a user