Skip to content

Commit 0c244d8

Browse files
authored
Add used-before-declaration errors for class refs inside computed names (microsoft#23784)
1 parent 96b2cf8 commit 0c244d8

7 files changed

+170
-0
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,10 @@ namespace ts {
11111111
// still might be illegal if usage is in the initializer of the variable declaration (eg var a = a)
11121112
return !isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration as VariableDeclaration, usage);
11131113
}
1114+
else if (isClassDeclaration(declaration)) {
1115+
// still might be illegal if the usage is within a computed property name in the class (eg class A { static p = "a"; [A.p]() {} })
1116+
return !findAncestor(usage, n => isComputedPropertyName(n) && n.parent.parent === declaration);
1117+
}
11141118
return true;
11151119
}
11161120

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
tests/cases/compiler/classDeclarationShouldBeOutOfScopeInComputedNames.ts(5,22): error TS2449: Class 'A' used before its declaration.
2+
tests/cases/compiler/classDeclarationShouldBeOutOfScopeInComputedNames.ts(6,13): error TS2449: Class 'A' used before its declaration.
3+
tests/cases/compiler/classDeclarationShouldBeOutOfScopeInComputedNames.ts(7,6): error TS2449: Class 'A' used before its declaration.
4+
tests/cases/compiler/classDeclarationShouldBeOutOfScopeInComputedNames.ts(8,6): error TS2449: Class 'A' used before its declaration.
5+
6+
7+
==== tests/cases/compiler/classDeclarationShouldBeOutOfScopeInComputedNames.ts (4 errors) ====
8+
class A {
9+
static readonly p1 = Symbol();
10+
static readonly p2 = Symbol();
11+
// All of the below should be out of scope or TDZ - `A` has not finished being constructed as they are executed
12+
static readonly [A.p1] = 0;
13+
~
14+
!!! error TS2449: Class 'A' used before its declaration.
15+
static [A.p2]() { return 0 };
16+
~
17+
!!! error TS2449: Class 'A' used before its declaration.
18+
[A.p1]() { }
19+
~
20+
!!! error TS2449: Class 'A' used before its declaration.
21+
[A.p2] = 0
22+
~
23+
!!! error TS2449: Class 'A' used before its declaration.
24+
}
25+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [classDeclarationShouldBeOutOfScopeInComputedNames.ts]
2+
class A {
3+
static readonly p1 = Symbol();
4+
static readonly p2 = Symbol();
5+
// All of the below should be out of scope or TDZ - `A` has not finished being constructed as they are executed
6+
static readonly [A.p1] = 0;
7+
static [A.p2]() { return 0 };
8+
[A.p1]() { }
9+
[A.p2] = 0
10+
}
11+
12+
13+
//// [classDeclarationShouldBeOutOfScopeInComputedNames.js]
14+
var A = /** @class */ (function () {
15+
function A() {
16+
this[_a] = 0;
17+
}
18+
A[(_b = A.p1, A.p2)] = function () { return 0; };
19+
;
20+
A.prototype[A.p1] = function () { };
21+
_a = A.p2;
22+
A.p1 = Symbol();
23+
A.p2 = Symbol();
24+
// All of the below should be out of scope or TDZ - `A` has not finished being constructed as they are executed
25+
A[_b] = 0;
26+
return A;
27+
var _b, _a;
28+
}());
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
=== tests/cases/compiler/classDeclarationShouldBeOutOfScopeInComputedNames.ts ===
2+
class A {
3+
>A : Symbol(A, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 0, 0))
4+
5+
static readonly p1 = Symbol();
6+
>p1 : Symbol(A.p1, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 0, 9))
7+
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
8+
9+
static readonly p2 = Symbol();
10+
>p2 : Symbol(A.p2, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 1, 34))
11+
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
12+
13+
// All of the below should be out of scope or TDZ - `A` has not finished being constructed as they are executed
14+
static readonly [A.p1] = 0;
15+
>[A.p1] : Symbol(A[A.p1], Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 2, 34))
16+
>A.p1 : Symbol(A.p1, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 0, 9))
17+
>A : Symbol(A, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 0, 0))
18+
>p1 : Symbol(A.p1, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 0, 9))
19+
20+
static [A.p2]() { return 0 };
21+
>[A.p2] : Symbol(A[A.p2], Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 4, 31))
22+
>A.p2 : Symbol(A.p2, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 1, 34))
23+
>A : Symbol(A, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 0, 0))
24+
>p2 : Symbol(A.p2, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 1, 34))
25+
26+
[A.p1]() { }
27+
>[A.p1] : Symbol(A[A.p1], Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 5, 33))
28+
>A.p1 : Symbol(A.p1, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 0, 9))
29+
>A : Symbol(A, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 0, 0))
30+
>p1 : Symbol(A.p1, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 0, 9))
31+
32+
[A.p2] = 0
33+
>[A.p2] : Symbol(A[A.p2], Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 6, 16))
34+
>A.p2 : Symbol(A.p2, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 1, 34))
35+
>A : Symbol(A, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 0, 0))
36+
>p2 : Symbol(A.p2, Decl(classDeclarationShouldBeOutOfScopeInComputedNames.ts, 1, 34))
37+
}
38+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
=== tests/cases/compiler/classDeclarationShouldBeOutOfScopeInComputedNames.ts ===
2+
class A {
3+
>A : A
4+
5+
static readonly p1 = Symbol();
6+
>p1 : unique symbol
7+
>Symbol() : unique symbol
8+
>Symbol : SymbolConstructor
9+
10+
static readonly p2 = Symbol();
11+
>p2 : unique symbol
12+
>Symbol() : unique symbol
13+
>Symbol : SymbolConstructor
14+
15+
// All of the below should be out of scope or TDZ - `A` has not finished being constructed as they are executed
16+
static readonly [A.p1] = 0;
17+
>[A.p1] : 0
18+
>A.p1 : unique symbol
19+
>A : typeof A
20+
>p1 : unique symbol
21+
>0 : 0
22+
23+
static [A.p2]() { return 0 };
24+
>[A.p2] : () => number
25+
>A.p2 : unique symbol
26+
>A : typeof A
27+
>p2 : unique symbol
28+
>0 : 0
29+
30+
[A.p1]() { }
31+
>[A.p1] : () => void
32+
>A.p1 : unique symbol
33+
>A : typeof A
34+
>p1 : unique symbol
35+
36+
[A.p2] = 0
37+
>[A.p2] : number
38+
>A.p2 : unique symbol
39+
>A : typeof A
40+
>p2 : unique symbol
41+
>0 : 0
42+
}
43+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tests/cases/conformance/es6/computedProperties/computedPropertyNamesWithStaticProperty.ts(3,10): error TS2449: Class 'C' used before its declaration.
2+
tests/cases/conformance/es6/computedProperties/computedPropertyNamesWithStaticProperty.ts(6,10): error TS2449: Class 'C' used before its declaration.
3+
tests/cases/conformance/es6/computedProperties/computedPropertyNamesWithStaticProperty.ts(9,6): error TS2449: Class 'C' used before its declaration.
4+
5+
6+
==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesWithStaticProperty.ts (3 errors) ====
7+
class C {
8+
static staticProp = 10;
9+
get [C.staticProp]() {
10+
~
11+
!!! error TS2449: Class 'C' used before its declaration.
12+
return "hello";
13+
}
14+
set [C.staticProp](x: string) {
15+
~
16+
!!! error TS2449: Class 'C' used before its declaration.
17+
var y = x;
18+
}
19+
[C.staticProp]() { }
20+
~
21+
!!! error TS2449: Class 'C' used before its declaration.
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @lib: es6
2+
class A {
3+
static readonly p1 = Symbol();
4+
static readonly p2 = Symbol();
5+
// All of the below should be out of scope or TDZ - `A` has not finished being constructed as they are executed
6+
static readonly [A.p1] = 0;
7+
static [A.p2]() { return 0 };
8+
[A.p1]() { }
9+
[A.p2] = 0
10+
}

0 commit comments

Comments
 (0)