Skip to content

Commit 751b1ae

Browse files
committed
disallow recursive references for block-scoped bindings
1 parent 76dcfb6 commit 751b1ae

File tree

8 files changed

+159
-41
lines changed

8 files changed

+159
-41
lines changed

src/compiler/checker.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,56 @@ module ts {
439439
var declaration = forEach(result.declarations, d => isBlockOrCatchScoped(d) ? d : undefined);
440440

441441
Debug.assert(declaration !== undefined, "Block-scoped variable declaration is undefined");
442-
if (!isDefinedBefore(declaration, errorLocation)) {
442+
443+
// first check if usage is lexically located after the declaration
444+
var isUsedBeforeDeclaration = !isDefinedBefore(declaration, errorLocation);
445+
if (!isUsedBeforeDeclaration) {
446+
// lexical check succedded however code still can be illegal.
447+
// - block scoped variables cannot be used in its initializers
448+
// let x = x; // illegal but usage is lexically after definition
449+
// - in ForIn/ForOf statements variable cannot be contained in expression part
450+
// for (let x in x)
451+
// for (let x of x)
452+
453+
// climb up to the variable declaration skipping binding patterns
454+
var variableDeclaration = <VariableDeclaration>getAncestor(declaration, SyntaxKind.VariableDeclaration);
455+
var container = getEnclosingBlockScopeContainer(variableDeclaration);
456+
457+
if (variableDeclaration.parent.parent.kind === SyntaxKind.VariableStatement ||
458+
variableDeclaration.parent.parent.kind === SyntaxKind.ForStatement) {
459+
// variable statement/for statement case, use site should not be inside initializer
460+
isUsedBeforeDeclaration = isChildNode(errorLocation, variableDeclaration.initializer, container);
461+
}
462+
else if (variableDeclaration.parent.parent.kind === SyntaxKind.ForOfStatement ||
463+
variableDeclaration.parent.parent.kind === SyntaxKind.ForInStatement) {
464+
// ForIn/ForOf case - use site should not be used in expression part
465+
isUsedBeforeDeclaration = isChildNode(errorLocation, (<ForInStatement>variableDeclaration.parent.parent).expression, container);
466+
}
467+
}
468+
if (isUsedBeforeDeclaration) {
443469
error(errorLocation, Diagnostics.Block_scoped_variable_0_used_before_its_declaration, declarationNameToString(declaration.name));
444470
}
445471
}
446472
}
447473
return result;
448474
}
449475

476+
/* Starting from 'initial' node walk up the parent chain until 'stopAt' node is reached.
477+
* If at any point current node is equal to 'parent' node - return true.
478+
* Return false if 'stopAt' node is reached.
479+
*/
480+
function isChildNode(initial: Node, parent: Node, stopAt: Node): boolean {
481+
if (!parent) {
482+
return false;
483+
}
484+
for (var current = initial; current && current !== stopAt; current = current.parent) {
485+
if (current === parent) {
486+
return true;
487+
}
488+
}
489+
return false;
490+
}
491+
450492
// An alias symbol is created by one of the following declarations:
451493
// import <symbol> = ...
452494
// import <symbol> from ...

src/compiler/emitter.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4116,34 +4116,6 @@ module ts {
41164116
}
41174117
}
41184118

4119-
function getEnclosingBlockScopeContainer(node: Node): Node {
4120-
var current = node;
4121-
while (current) {
4122-
if (isFunctionLike(current)) {
4123-
return current;
4124-
}
4125-
switch (current.kind) {
4126-
case SyntaxKind.SourceFile:
4127-
case SyntaxKind.CaseBlock:
4128-
case SyntaxKind.CatchClause:
4129-
case SyntaxKind.ModuleDeclaration:
4130-
case SyntaxKind.ForStatement:
4131-
case SyntaxKind.ForInStatement:
4132-
case SyntaxKind.ForOfStatement:
4133-
return current;
4134-
case SyntaxKind.Block:
4135-
// function block is not considered block-scope container
4136-
// see comment in binder.ts: bind(...), case for SyntaxKind.Block
4137-
if (!isFunctionLike(current.parent)) {
4138-
return current;
4139-
}
4140-
}
4141-
4142-
current = current.parent;
4143-
}
4144-
}
4145-
4146-
41474119
function getCombinedFlagsForIdentifier(node: Identifier): NodeFlags {
41484120
if (!node.parent || (node.parent.kind !== SyntaxKind.VariableDeclaration && node.parent.kind !== SyntaxKind.BindingElement)) {
41494121
return 0;

src/compiler/utilities.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,33 @@ module ts {
203203
isCatchClauseVariableDeclaration(declaration);
204204
}
205205

206+
export function getEnclosingBlockScopeContainer(node: Node): Node {
207+
var current = node;
208+
while (current) {
209+
if (isFunctionLike(current)) {
210+
return current;
211+
}
212+
switch (current.kind) {
213+
case SyntaxKind.SourceFile:
214+
case SyntaxKind.CaseBlock:
215+
case SyntaxKind.CatchClause:
216+
case SyntaxKind.ModuleDeclaration:
217+
case SyntaxKind.ForStatement:
218+
case SyntaxKind.ForInStatement:
219+
case SyntaxKind.ForOfStatement:
220+
return current;
221+
case SyntaxKind.Block:
222+
// function block is not considered block-scope container
223+
// see comment in binder.ts: bind(...), case for SyntaxKind.Block
224+
if (!isFunctionLike(current.parent)) {
225+
return current;
226+
}
227+
}
228+
229+
current = current.parent;
230+
}
231+
}
232+
206233
export function isCatchClauseVariableDeclaration(declaration: Declaration) {
207234
return declaration &&
208235
declaration.kind === SyntaxKind.VariableDeclaration &&
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/conformance/es6/for-ofStatements/for-of55.ts(2,15): error TS2448: Block-scoped variable 'v' used before its declaration.
2+
3+
4+
==== tests/cases/conformance/es6/for-ofStatements/for-of55.ts (1 errors) ====
5+
let v = [1];
6+
for (let v of v) {
7+
~
8+
!!! error TS2448: Block-scoped variable 'v' used before its declaration.
9+
v;
10+
}

tests/baselines/reference/for-of55.types

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
tests/cases/compiler/recursiveLetConst.ts(2,9): error TS2448: Block-scoped variable 'x' used before its declaration.
2+
tests/cases/compiler/recursiveLetConst.ts(3,12): error TS2448: Block-scoped variable 'x1' used before its declaration.
3+
tests/cases/compiler/recursiveLetConst.ts(4,11): error TS2448: Block-scoped variable 'y' used before its declaration.
4+
tests/cases/compiler/recursiveLetConst.ts(5,14): error TS2448: Block-scoped variable 'y1' used before its declaration.
5+
tests/cases/compiler/recursiveLetConst.ts(6,14): error TS2448: Block-scoped variable 'v' used before its declaration.
6+
tests/cases/compiler/recursiveLetConst.ts(7,16): error TS2448: Block-scoped variable 'v' used before its declaration.
7+
tests/cases/compiler/recursiveLetConst.ts(8,15): error TS2448: Block-scoped variable 'v' used before its declaration.
8+
tests/cases/compiler/recursiveLetConst.ts(9,15): error TS2448: Block-scoped variable 'v' used before its declaration.
9+
tests/cases/compiler/recursiveLetConst.ts(10,17): error TS2448: Block-scoped variable 'v' used before its declaration.
10+
11+
12+
==== tests/cases/compiler/recursiveLetConst.ts (9 errors) ====
13+
'use strict'
14+
let x = x + 1;
15+
~
16+
!!! error TS2448: Block-scoped variable 'x' used before its declaration.
17+
let [x1] = x1 + 1;
18+
~~
19+
!!! error TS2448: Block-scoped variable 'x1' used before its declaration.
20+
const y = y + 2;
21+
~
22+
!!! error TS2448: Block-scoped variable 'y' used before its declaration.
23+
const [y1] = y1 + 1;
24+
~~
25+
!!! error TS2448: Block-scoped variable 'y1' used before its declaration.
26+
for (let v = v; ; ) { }
27+
~
28+
!!! error TS2448: Block-scoped variable 'v' used before its declaration.
29+
for (let [v] = v; ;) { }
30+
~
31+
!!! error TS2448: Block-scoped variable 'v' used before its declaration.
32+
for (let v in v) { }
33+
~
34+
!!! error TS2448: Block-scoped variable 'v' used before its declaration.
35+
for (let v of v) { }
36+
~
37+
!!! error TS2448: Block-scoped variable 'v' used before its declaration.
38+
for (let [v] of v) { }
39+
~
40+
!!! error TS2448: Block-scoped variable 'v' used before its declaration.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [recursiveLetConst.ts]
2+
'use strict'
3+
let x = x + 1;
4+
let [x1] = x1 + 1;
5+
const y = y + 2;
6+
const [y1] = y1 + 1;
7+
for (let v = v; ; ) { }
8+
for (let [v] = v; ;) { }
9+
for (let v in v) { }
10+
for (let v of v) { }
11+
for (let [v] of v) { }
12+
13+
//// [recursiveLetConst.js]
14+
'use strict';
15+
let x = x + 1;
16+
let [x1] = x1 + 1;
17+
const y = y + 2;
18+
const [y1] = y1 + 1;
19+
for (let v = v;;) {
20+
}
21+
for (let [v] = v;;) {
22+
}
23+
for (let v in v) {
24+
}
25+
for (let v of v) {
26+
}
27+
for (let [v] of v) {
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @target:es6
2+
'use strict'
3+
let x = x + 1;
4+
let [x1] = x1 + 1;
5+
const y = y + 2;
6+
const [y1] = y1 + 1;
7+
for (let v = v; ; ) { }
8+
for (let [v] = v; ;) { }
9+
for (let v in v) { }
10+
for (let v of v) { }
11+
for (let [v] of v) { }

0 commit comments

Comments
 (0)