Skip to content

Commit 4dbd04c

Browse files
committed
Merge pull request #5208 from Microsoft/capturedBlockScopedVars
support block scoped vars captured in closures inside loops
2 parents dababb1 + d2045a8 commit 4dbd04c

File tree

79 files changed

+32072
-64
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+32072
-64
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6609,7 +6609,7 @@ namespace ts {
66096609
while (current && !nodeStartsNewLexicalEnvironment(current)) {
66106610
if (isIterationStatement(current, /*lookInLabeledStatements*/ false)) {
66116611
if (inFunction) {
6612-
grammarErrorOnFirstToken(current, Diagnostics.Loop_contains_block_scoped_variable_0_referenced_by_a_function_in_the_loop_This_is_only_supported_in_ECMAScript_6_or_higher, declarationNameToString(node));
6612+
getNodeLinks(current).flags |= NodeCheckFlags.LoopWithBlockScopedBindingCapturedInFunction;
66136613
}
66146614
// mark value declaration so during emit they can have a special handling
66156615
getNodeLinks(<VariableDeclaration>symbol.valueDeclaration).flags |= NodeCheckFlags.BlockScopedBindingInLoop;
@@ -14580,6 +14580,10 @@ namespace ts {
1458014580

1458114581
// Emitter support
1458214582

14583+
function isArgumentsLocalBinding(node: Identifier): boolean {
14584+
return getReferencedValueSymbol(node) === argumentsSymbol;
14585+
}
14586+
1458314587
// When resolved as an expression identifier, if the given node references an exported entity, return the declaration
1458414588
// node of the exported entity's container. Otherwise, return undefined.
1458514589
function getReferencedExportContainer(node: Identifier): SourceFile | ModuleDeclaration | EnumDeclaration {
@@ -14884,7 +14888,8 @@ namespace ts {
1488414888
collectLinkedAliases,
1488514889
getReferencedValueDeclaration,
1488614890
getTypeReferenceSerializationKind,
14887-
isOptionalParameter
14891+
isOptionalParameter,
14892+
isArgumentsLocalBinding
1488814893
};
1488914894
}
1489014895

@@ -15711,21 +15716,6 @@ namespace ts {
1571115716
}
1571215717
}
1571315718

15714-
function isIterationStatement(node: Node, lookInLabeledStatements: boolean): boolean {
15715-
switch (node.kind) {
15716-
case SyntaxKind.ForStatement:
15717-
case SyntaxKind.ForInStatement:
15718-
case SyntaxKind.ForOfStatement:
15719-
case SyntaxKind.DoStatement:
15720-
case SyntaxKind.WhileStatement:
15721-
return true;
15722-
case SyntaxKind.LabeledStatement:
15723-
return lookInLabeledStatements && isIterationStatement((<LabeledStatement>node).statement, lookInLabeledStatements);
15724-
}
15725-
15726-
return false;
15727-
}
15728-
1572915719
function checkGrammarBreakOrContinueStatement(node: BreakOrContinueStatement): boolean {
1573015720
let current: Node = node;
1573115721
while (current) {

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,10 +2000,6 @@
20002000
"category": "Error",
20012001
"code": 4082
20022002
},
2003-
"Loop contains block-scoped variable '{0}' referenced by a function in the loop. This is only supported in ECMAScript 6 or higher.": {
2004-
"category": "Error",
2005-
"code": 4091
2006-
},
20072003
"The current host does not support the '{0}' option.": {
20082004
"category": "Error",
20092005
"code": 5001

0 commit comments

Comments
 (0)