diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9dabafb0cde..6888a6fc25d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5086,10 +5086,6 @@ module ts { return getNarrowedTypeOfSymbol(getExportSymbolOfValueSymbolIfExported(symbol), node); } - function isNameScopeBoundary(n: Node): boolean { - return isAnyFunction(n) || n.kind === SyntaxKind.ModuleDeclaration || n.kind === SyntaxKind.SourceFile; - } - function checkBlockScopedBindingCapturedInLoop(node: Identifier, symbol: Symbol): void { if (languageVersion >= ScriptTarget.ES6 || (symbol.flags & SymbolFlags.BlockScopedVariable) === 0) { return; @@ -5116,7 +5112,7 @@ module ts { } var current: Node = container; - while (current && !isNameScopeBoundary(current)) { + while (current && !nodeStartsNewLexicalEnvironment(current)) { if (isIterationStatement(current, /*lookInLabeledStatements*/ false)) { if (inFunction) { grammarErrorOnFirstToken(current, Diagnostics.Code_in_the_loop_captures_block_scoped_variable_0_in_closure_This_is_natively_supported_in_ECMAScript_6_or_higher, declarationNameToString(node)); diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index fbe341ccd28..1cfbbd20096 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3832,21 +3832,20 @@ module ts { return current; } switch (current.kind) { + case SyntaxKind.SourceFile: + case SyntaxKind.SwitchKeyword: case SyntaxKind.CatchClause: + case SyntaxKind.ModuleDeclaration: case SyntaxKind.ForStatement: case SyntaxKind.ForInStatement: case SyntaxKind.ForOfStatement: - case SyntaxKind.SwitchKeyword: return current; case SyntaxKind.Block: - if (isAnyFunction(current.parent)) { - return current.parent; - } - else { + // function block is not considered block-scope container + // see comment in binder.ts: bind(...), case for SyntaxKind.Block + if (!isAnyFunction(current.parent)) { return current; } - case SyntaxKind.SourceFile: - return current; } current = current.parent; diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 50a8de9671d..bf8a9dd1097 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1117,6 +1117,11 @@ module ts { return createTextChangeRange(createTextSpanFromBounds(oldStartN, oldEndN), /*newLength: */newEndN - oldStartN); } + // @internal + export function nodeStartsNewLexicalEnvironment(n: Node): boolean { + return isAnyFunction(n) || n.kind === SyntaxKind.ModuleDeclaration || n.kind === SyntaxKind.SourceFile; + } + // @internal export function nodeIsSynthesized(node: Node): boolean { return node.pos === -1 && node.end === -1;