Skip to content

Commit 7f5fb8b

Browse files
committed
drop locals in block-scope container nodes during binding
1 parent ba52d60 commit 7f5fb8b

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/compiler/binder.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ module ts {
7878

7979
if (!file.locals) {
8080
file.locals = {};
81-
container = blockScopeContainer = file;
81+
container = file;
82+
setBlockScopeContainer(file, /*cleanLocals*/ false);
8283
bind(file);
8384
file.symbolCount = symbolCount;
8485
}
@@ -88,6 +89,13 @@ module ts {
8889
return new Symbol(flags, name);
8990
}
9091

92+
function setBlockScopeContainer(node: Node, cleanLocals: boolean) {
93+
blockScopeContainer = node;
94+
if (cleanLocals) {
95+
blockScopeContainer.locals = undefined;
96+
}
97+
}
98+
9199
function addDeclarationToSymbol(symbol: Symbol, node: Declaration, symbolKind: SymbolFlags) {
92100
symbol.flags |= symbolKind;
93101
if (!symbol.declarations) symbol.declarations = [];
@@ -244,7 +252,10 @@ module ts {
244252
}
245253

246254
if (isBlockScopeContainer) {
247-
blockScopeContainer = node;
255+
// clean locals in block scope container if
256+
// - current node does not have local variables
257+
// - current node is not source file (source file always have locals)
258+
setBlockScopeContainer(node, /*cleanLocals*/ (symbolKind & SymbolFlags.HasLocals) == 0 && node.kind !== SyntaxKind.SourceFile);
248259
}
249260

250261
forEachChild(node, bind);
@@ -347,7 +358,8 @@ module ts {
347358
addDeclarationToSymbol(symbol, node, SymbolFlags.FunctionScopedVariable);
348359
var saveParent = parent;
349360
var savedBlockScopeContainer = blockScopeContainer;
350-
parent = blockScopeContainer = node;
361+
parent = node;
362+
setBlockScopeContainer(node, /*cleanLocals*/ true);
351363
forEachChild(node, bind);
352364
parent = saveParent;
353365
blockScopeContainer = savedBlockScopeContainer;

0 commit comments

Comments
 (0)