Skip to content

Commit 99d2435

Browse files
authored
fix(42220): Missing 'used before declaration' for class expression used in own computed property name (#56514)
1 parent 8da01f3 commit 99d2435

6 files changed

+199
-68
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2837,7 +2837,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
28372837
// still might be illegal if usage is in the initializer of the variable declaration (eg var a = a)
28382838
return !isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration as VariableDeclaration, usage);
28392839
}
2840-
else if (isClassDeclaration(declaration)) {
2840+
else if (isClassLike(declaration)) {
28412841
// still might be illegal if the usage is within a computed property name in the class (eg class A { static p = "a"; [A.p]() {} })
28422842
return !findAncestor(usage, n => isComputedPropertyName(n) && n.parent.parent === declaration);
28432843
}
Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,49 @@
1-
computedPropertyNamesWithStaticProperty.ts(3,10): error TS2449: Class 'C' used before its declaration.
2-
computedPropertyNamesWithStaticProperty.ts(6,10): error TS2449: Class 'C' used before its declaration.
3-
computedPropertyNamesWithStaticProperty.ts(9,6): error TS2449: Class 'C' used before its declaration.
1+
computedPropertyNamesWithStaticProperty.ts(3,10): error TS2449: Class 'C1' used before its declaration.
2+
computedPropertyNamesWithStaticProperty.ts(6,10): error TS2449: Class 'C1' used before its declaration.
3+
computedPropertyNamesWithStaticProperty.ts(9,6): error TS2449: Class 'C1' used before its declaration.
4+
computedPropertyNamesWithStaticProperty.ts(14,10): error TS2449: Class 'C2' used before its declaration.
5+
computedPropertyNamesWithStaticProperty.ts(17,10): error TS2449: Class 'C2' used before its declaration.
6+
computedPropertyNamesWithStaticProperty.ts(20,6): error TS2449: Class 'C2' used before its declaration.
47

58

6-
==== computedPropertyNamesWithStaticProperty.ts (3 errors) ====
7-
class C {
9+
==== computedPropertyNamesWithStaticProperty.ts (6 errors) ====
10+
class C1 {
811
static staticProp = 10;
9-
get [C.staticProp]() {
10-
~
11-
!!! error TS2449: Class 'C' used before its declaration.
12-
!!! related TS2728 computedPropertyNamesWithStaticProperty.ts:1:7: 'C' is declared here.
12+
get [C1.staticProp]() {
13+
~~
14+
!!! error TS2449: Class 'C1' used before its declaration.
15+
!!! related TS2728 computedPropertyNamesWithStaticProperty.ts:1:7: 'C1' is declared here.
1316
return "hello";
1417
}
15-
set [C.staticProp](x: string) {
16-
~
17-
!!! error TS2449: Class 'C' used before its declaration.
18-
!!! related TS2728 computedPropertyNamesWithStaticProperty.ts:1:7: 'C' is declared here.
18+
set [C1.staticProp](x: string) {
19+
~~
20+
!!! error TS2449: Class 'C1' used before its declaration.
21+
!!! related TS2728 computedPropertyNamesWithStaticProperty.ts:1:7: 'C1' is declared here.
1922
var y = x;
2023
}
21-
[C.staticProp]() { }
22-
~
23-
!!! error TS2449: Class 'C' used before its declaration.
24-
!!! related TS2728 computedPropertyNamesWithStaticProperty.ts:1:7: 'C' is declared here.
25-
}
24+
[C1.staticProp]() { }
25+
~~
26+
!!! error TS2449: Class 'C1' used before its declaration.
27+
!!! related TS2728 computedPropertyNamesWithStaticProperty.ts:1:7: 'C1' is declared here.
28+
}
29+
30+
(class C2 {
31+
static staticProp = 10;
32+
get [C2.staticProp]() {
33+
~~
34+
!!! error TS2449: Class 'C2' used before its declaration.
35+
!!! related TS2728 computedPropertyNamesWithStaticProperty.ts:12:8: 'C2' is declared here.
36+
return "hello";
37+
}
38+
set [C2.staticProp](x: string) {
39+
~~
40+
!!! error TS2449: Class 'C2' used before its declaration.
41+
!!! related TS2728 computedPropertyNamesWithStaticProperty.ts:12:8: 'C2' is declared here.
42+
var y = x;
43+
}
44+
[C2.staticProp]() { }
45+
~~
46+
!!! error TS2449: Class 'C2' used before its declaration.
47+
!!! related TS2728 computedPropertyNamesWithStaticProperty.ts:12:8: 'C2' is declared here.
48+
})
49+
Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,49 @@
11
//// [tests/cases/conformance/es6/computedProperties/computedPropertyNamesWithStaticProperty.ts] ////
22

33
//// [computedPropertyNamesWithStaticProperty.ts]
4-
class C {
4+
class C1 {
55
static staticProp = 10;
6-
get [C.staticProp]() {
6+
get [C1.staticProp]() {
77
return "hello";
88
}
9-
set [C.staticProp](x: string) {
9+
set [C1.staticProp](x: string) {
1010
var y = x;
1111
}
12-
[C.staticProp]() { }
13-
}
12+
[C1.staticProp]() { }
13+
}
14+
15+
(class C2 {
16+
static staticProp = 10;
17+
get [C2.staticProp]() {
18+
return "hello";
19+
}
20+
set [C2.staticProp](x: string) {
21+
var y = x;
22+
}
23+
[C2.staticProp]() { }
24+
})
25+
1426

1527
//// [computedPropertyNamesWithStaticProperty.js]
16-
class C {
17-
get [C.staticProp]() {
28+
var _a;
29+
class C1 {
30+
get [C1.staticProp]() {
1831
return "hello";
1932
}
20-
set [C.staticProp](x) {
33+
set [C1.staticProp](x) {
2134
var y = x;
2235
}
23-
[C.staticProp]() { }
36+
[C1.staticProp]() { }
2437
}
25-
C.staticProp = 10;
38+
C1.staticProp = 10;
39+
(_a = class C2 {
40+
get [C2.staticProp]() {
41+
return "hello";
42+
}
43+
set [C2.staticProp](x) {
44+
var y = x;
45+
}
46+
[C2.staticProp]() { }
47+
},
48+
_a.staticProp = 10,
49+
_a);
Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,68 @@
11
//// [tests/cases/conformance/es6/computedProperties/computedPropertyNamesWithStaticProperty.ts] ////
22

33
=== computedPropertyNamesWithStaticProperty.ts ===
4-
class C {
5-
>C : Symbol(C, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 0))
4+
class C1 {
5+
>C1 : Symbol(C1, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 0))
66

77
static staticProp = 10;
8-
>staticProp : Symbol(C.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 9))
8+
>staticProp : Symbol(C1.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 10))
99

10-
get [C.staticProp]() {
11-
>[C.staticProp] : Symbol(C[C.staticProp], Decl(computedPropertyNamesWithStaticProperty.ts, 1, 27))
12-
>C.staticProp : Symbol(C.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 9))
13-
>C : Symbol(C, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 0))
14-
>staticProp : Symbol(C.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 9))
10+
get [C1.staticProp]() {
11+
>[C1.staticProp] : Symbol(C1[C1.staticProp], Decl(computedPropertyNamesWithStaticProperty.ts, 1, 27))
12+
>C1.staticProp : Symbol(C1.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 10))
13+
>C1 : Symbol(C1, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 0))
14+
>staticProp : Symbol(C1.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 10))
1515

1616
return "hello";
1717
}
18-
set [C.staticProp](x: string) {
19-
>[C.staticProp] : Symbol(C[C.staticProp], Decl(computedPropertyNamesWithStaticProperty.ts, 4, 5))
20-
>C.staticProp : Symbol(C.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 9))
21-
>C : Symbol(C, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 0))
22-
>staticProp : Symbol(C.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 9))
23-
>x : Symbol(x, Decl(computedPropertyNamesWithStaticProperty.ts, 5, 23))
18+
set [C1.staticProp](x: string) {
19+
>[C1.staticProp] : Symbol(C1[C1.staticProp], Decl(computedPropertyNamesWithStaticProperty.ts, 4, 5))
20+
>C1.staticProp : Symbol(C1.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 10))
21+
>C1 : Symbol(C1, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 0))
22+
>staticProp : Symbol(C1.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 10))
23+
>x : Symbol(x, Decl(computedPropertyNamesWithStaticProperty.ts, 5, 24))
2424

2525
var y = x;
2626
>y : Symbol(y, Decl(computedPropertyNamesWithStaticProperty.ts, 6, 11))
27-
>x : Symbol(x, Decl(computedPropertyNamesWithStaticProperty.ts, 5, 23))
27+
>x : Symbol(x, Decl(computedPropertyNamesWithStaticProperty.ts, 5, 24))
2828
}
29-
[C.staticProp]() { }
30-
>[C.staticProp] : Symbol(C[C.staticProp], Decl(computedPropertyNamesWithStaticProperty.ts, 7, 5))
31-
>C.staticProp : Symbol(C.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 9))
32-
>C : Symbol(C, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 0))
33-
>staticProp : Symbol(C.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 9))
29+
[C1.staticProp]() { }
30+
>[C1.staticProp] : Symbol(C1[C1.staticProp], Decl(computedPropertyNamesWithStaticProperty.ts, 7, 5))
31+
>C1.staticProp : Symbol(C1.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 10))
32+
>C1 : Symbol(C1, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 0))
33+
>staticProp : Symbol(C1.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 0, 10))
3434
}
35+
36+
(class C2 {
37+
>C2 : Symbol(C2, Decl(computedPropertyNamesWithStaticProperty.ts, 11, 1))
38+
39+
static staticProp = 10;
40+
>staticProp : Symbol(C2.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 11, 11))
41+
42+
get [C2.staticProp]() {
43+
>[C2.staticProp] : Symbol(C2[C2.staticProp], Decl(computedPropertyNamesWithStaticProperty.ts, 12, 27))
44+
>C2.staticProp : Symbol(C2.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 11, 11))
45+
>C2 : Symbol(C2, Decl(computedPropertyNamesWithStaticProperty.ts, 11, 1))
46+
>staticProp : Symbol(C2.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 11, 11))
47+
48+
return "hello";
49+
}
50+
set [C2.staticProp](x: string) {
51+
>[C2.staticProp] : Symbol(C2[C2.staticProp], Decl(computedPropertyNamesWithStaticProperty.ts, 15, 5))
52+
>C2.staticProp : Symbol(C2.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 11, 11))
53+
>C2 : Symbol(C2, Decl(computedPropertyNamesWithStaticProperty.ts, 11, 1))
54+
>staticProp : Symbol(C2.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 11, 11))
55+
>x : Symbol(x, Decl(computedPropertyNamesWithStaticProperty.ts, 16, 24))
56+
57+
var y = x;
58+
>y : Symbol(y, Decl(computedPropertyNamesWithStaticProperty.ts, 17, 11))
59+
>x : Symbol(x, Decl(computedPropertyNamesWithStaticProperty.ts, 16, 24))
60+
}
61+
[C2.staticProp]() { }
62+
>[C2.staticProp] : Symbol(C2[C2.staticProp], Decl(computedPropertyNamesWithStaticProperty.ts, 18, 5))
63+
>C2.staticProp : Symbol(C2.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 11, 11))
64+
>C2 : Symbol(C2, Decl(computedPropertyNamesWithStaticProperty.ts, 11, 1))
65+
>staticProp : Symbol(C2.staticProp, Decl(computedPropertyNamesWithStaticProperty.ts, 11, 11))
66+
67+
})
68+
Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,74 @@
11
//// [tests/cases/conformance/es6/computedProperties/computedPropertyNamesWithStaticProperty.ts] ////
22

33
=== computedPropertyNamesWithStaticProperty.ts ===
4-
class C {
5-
>C : C
4+
class C1 {
5+
>C1 : C1
66

77
static staticProp = 10;
88
>staticProp : number
99
>10 : 10
1010

11-
get [C.staticProp]() {
12-
>[C.staticProp] : string
13-
>C.staticProp : number
14-
>C : typeof C
11+
get [C1.staticProp]() {
12+
>[C1.staticProp] : string
13+
>C1.staticProp : number
14+
>C1 : typeof C1
1515
>staticProp : number
1616

1717
return "hello";
1818
>"hello" : "hello"
1919
}
20-
set [C.staticProp](x: string) {
21-
>[C.staticProp] : string
22-
>C.staticProp : number
23-
>C : typeof C
20+
set [C1.staticProp](x: string) {
21+
>[C1.staticProp] : string
22+
>C1.staticProp : number
23+
>C1 : typeof C1
2424
>staticProp : number
2525
>x : string
2626

2727
var y = x;
2828
>y : string
2929
>x : string
3030
}
31-
[C.staticProp]() { }
32-
>[C.staticProp] : () => void
33-
>C.staticProp : number
34-
>C : typeof C
31+
[C1.staticProp]() { }
32+
>[C1.staticProp] : () => void
33+
>C1.staticProp : number
34+
>C1 : typeof C1
3535
>staticProp : number
3636
}
37+
38+
(class C2 {
39+
>(class C2 { static staticProp = 10; get [C2.staticProp]() { return "hello"; } set [C2.staticProp](x: string) { var y = x; } [C2.staticProp]() { }}) : typeof C2
40+
>class C2 { static staticProp = 10; get [C2.staticProp]() { return "hello"; } set [C2.staticProp](x: string) { var y = x; } [C2.staticProp]() { }} : typeof C2
41+
>C2 : typeof C2
42+
43+
static staticProp = 10;
44+
>staticProp : number
45+
>10 : 10
46+
47+
get [C2.staticProp]() {
48+
>[C2.staticProp] : string
49+
>C2.staticProp : number
50+
>C2 : typeof C2
51+
>staticProp : number
52+
53+
return "hello";
54+
>"hello" : "hello"
55+
}
56+
set [C2.staticProp](x: string) {
57+
>[C2.staticProp] : string
58+
>C2.staticProp : number
59+
>C2 : typeof C2
60+
>staticProp : number
61+
>x : string
62+
63+
var y = x;
64+
>y : string
65+
>x : string
66+
}
67+
[C2.staticProp]() { }
68+
>[C2.staticProp] : () => void
69+
>C2.staticProp : number
70+
>C2 : typeof C2
71+
>staticProp : number
72+
73+
})
74+
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
// @target: es6
2-
class C {
2+
class C1 {
33
static staticProp = 10;
4-
get [C.staticProp]() {
4+
get [C1.staticProp]() {
55
return "hello";
66
}
7-
set [C.staticProp](x: string) {
7+
set [C1.staticProp](x: string) {
88
var y = x;
99
}
10-
[C.staticProp]() { }
11-
}
10+
[C1.staticProp]() { }
11+
}
12+
13+
(class C2 {
14+
static staticProp = 10;
15+
get [C2.staticProp]() {
16+
return "hello";
17+
}
18+
set [C2.staticProp](x: string) {
19+
var y = x;
20+
}
21+
[C2.staticProp]() { }
22+
})

0 commit comments

Comments
 (0)