Skip to content

Commit 9e6603c

Browse files
committed
update isInPropertyInitializerOrClassStaticBlock
1 parent dc8f02a commit 9e6603c

6 files changed

Lines changed: 107 additions & 22 deletions

File tree

src/compiler/checker.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25347,7 +25347,7 @@ namespace ts {
2534725347
// To avoid that we will give an error to users if they use arguments objects in arrow function so that they
2534825348
// can explicitly bound arguments objects
2534925349
if (symbol === argumentsSymbol) {
25350-
if (isArgumentsInPropertyInitializerOrClassStaticBlock(node)) {
25350+
if (isInPropertyInitializerOrClassStaticBlock(node, /* ignoreArrowFunctions */true)) {
2535125351
error(node, Diagnostics.arguments_cannot_be_referenced_in_property_initializers_or_class_static_initialization_block);
2535225352
return errorType;
2535325353
}
@@ -28869,11 +28869,12 @@ namespace ts {
2886928869
}
2887028870
}
2887128871

28872-
function isInPropertyInitializerOrClassStaticBlock(node: Node): boolean {
28872+
function isInPropertyInitializerOrClassStaticBlock(node: Node, ignoreArrowFunctions?: boolean): boolean {
2887328873
return !!findAncestor(node, node => {
2887428874
switch (node.kind) {
2887528875
case SyntaxKind.PropertyDeclaration:
28876-
return true;
28876+
case SyntaxKind.ClassStaticBlockDeclaration:
28877+
return true;
2887728878
case SyntaxKind.PropertyAssignment:
2887828879
case SyntaxKind.MethodDeclaration:
2887928880
case SyntaxKind.GetAccessor:
@@ -28889,23 +28890,10 @@ namespace ts {
2888928890
case SyntaxKind.ExpressionWithTypeArguments:
2889028891
case SyntaxKind.HeritageClause:
2889128892
return false;
28893+
case SyntaxKind.JsxClosingElement: // already reported in JsxOpeningElement
28894+
return "quit";
2889228895
case SyntaxKind.ArrowFunction:
28893-
case SyntaxKind.ExpressionStatement:
28894-
return isBlock(node.parent) && isClassStaticBlockDeclaration(node.parent.parent) ? true : "quit";
28895-
default:
28896-
return isExpressionNode(node) ? false : "quit";
28897-
}
28898-
});
28899-
}
28900-
28901-
function isArgumentsInPropertyInitializerOrClassStaticBlock(node: Node): boolean {
28902-
return !!findAncestor(node, node => {
28903-
switch(node.kind) {
28904-
case SyntaxKind.PropertyDeclaration:
28905-
case SyntaxKind.ClassStaticBlockDeclaration:
28906-
return true;
28907-
case SyntaxKind.ArrowFunction:
28908-
return false;
28896+
return ignoreArrowFunctions ? false : "quit";
2890928897
default:
2891028898
return isFunctionLikeDeclaration(node) ? "quit" : false;
2891128899
}

tests/baselines/reference/classStaticBlock6.errors.txt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ tests/cases/conformance/classes/classStaticBlock/classStaticBlock6.ts(42,18): er
1313
tests/cases/conformance/classes/classStaticBlock/classStaticBlock6.ts(45,17): error TS2522: The 'arguments' object cannot be referenced in an async function or method in ES3 and ES5. Consider using a standard function or method.
1414
tests/cases/conformance/classes/classStaticBlock/classStaticBlock6.ts(46,22): error TS1109: Expression expected.
1515
tests/cases/conformance/classes/classStaticBlock/classStaticBlock6.ts(55,13): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
16+
tests/cases/conformance/classes/classStaticBlock/classStaticBlock6.ts(66,14): error TS2729: Property 'b' is used before its initialization.
17+
tests/cases/conformance/classes/classStaticBlock/classStaticBlock6.ts(68,18): error TS2729: Property 'b' is used before its initialization.
1618

1719

18-
==== tests/cases/conformance/classes/classStaticBlock/classStaticBlock6.ts (15 errors) ====
20+
==== tests/cases/conformance/classes/classStaticBlock/classStaticBlock6.ts (17 errors) ====
1921
class B {
2022
static a = 1;
2123
}
@@ -108,4 +110,20 @@ tests/cases/conformance/classes/classStaticBlock/classStaticBlock6.ts(55,13): er
108110
}
109111
}
110112
}
111-
113+
114+
class foo2 {
115+
static {
116+
this.b // should error
117+
~
118+
!!! error TS2729: Property 'b' is used before its initialization.
119+
!!! related TS2728 tests/cases/conformance/classes/classStaticBlock/classStaticBlock6.ts:72:12: 'b' is declared here.
120+
if (1) {
121+
this.b; // should error
122+
~
123+
!!! error TS2729: Property 'b' is used before its initialization.
124+
!!! related TS2728 tests/cases/conformance/classes/classStaticBlock/classStaticBlock6.ts:72:12: 'b' is declared here.
125+
}
126+
}
127+
128+
static b = 1;
129+
}

tests/baselines/reference/classStaticBlock6.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ function foo1 () {
6161
}
6262
}
6363
}
64-
64+
65+
class foo2 {
66+
static {
67+
this.b // should error
68+
if (1) {
69+
this.b; // should error
70+
}
71+
}
72+
73+
static b = 1;
74+
}
6575

6676
//// [classStaticBlock6.js]
6777
var __extends = (this && this.__extends) || (function () {
@@ -115,6 +125,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
115125
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
116126
}
117127
};
128+
var _this = this;
118129
var B = /** @class */ (function () {
119130
function B() {
120131
}
@@ -211,3 +222,17 @@ function foo1() {
211222
}
212223
})();
213224
}
225+
var foo2 = /** @class */ (function () {
226+
function foo2() {
227+
}
228+
var _a;
229+
_a = foo2;
230+
(function () {
231+
_a.b; // should error
232+
if (1) {
233+
_a.b; // should error
234+
}
235+
})();
236+
foo2.b = 1;
237+
return foo2;
238+
}());

tests/baselines/reference/classStaticBlock6.symbols

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,23 @@ function foo1 () {
102102
}
103103
}
104104

105+
class foo2 {
106+
>foo2 : Symbol(foo2, Decl(classStaticBlock6.ts, 61, 1))
107+
108+
static {
109+
this.b // should error
110+
>this.b : Symbol(foo2.b, Decl(classStaticBlock6.ts, 69, 5))
111+
>this : Symbol(foo2, Decl(classStaticBlock6.ts, 61, 1))
112+
>b : Symbol(foo2.b, Decl(classStaticBlock6.ts, 69, 5))
113+
114+
if (1) {
115+
this.b; // should error
116+
>this.b : Symbol(foo2.b, Decl(classStaticBlock6.ts, 69, 5))
117+
>this : Symbol(foo2, Decl(classStaticBlock6.ts, 61, 1))
118+
>b : Symbol(foo2.b, Decl(classStaticBlock6.ts, 69, 5))
119+
}
120+
}
121+
122+
static b = 1;
123+
>b : Symbol(foo2.b, Decl(classStaticBlock6.ts, 69, 5))
124+
}

tests/baselines/reference/classStaticBlock6.types

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,26 @@ function foo1 () {
123123
}
124124
}
125125

126+
class foo2 {
127+
>foo2 : foo2
128+
129+
static {
130+
this.b // should error
131+
>this.b : number
132+
>this : typeof foo2
133+
>b : number
134+
135+
if (1) {
136+
>1 : 1
137+
138+
this.b; // should error
139+
>this.b : number
140+
>this : typeof foo2
141+
>b : number
142+
}
143+
}
144+
145+
static b = 1;
146+
>b : number
147+
>1 : 1
148+
}

tests/cases/conformance/classes/classStaticBlock/classStaticBlock6.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,14 @@ function foo1 () {
6060
}
6161
}
6262
}
63+
64+
class foo2 {
65+
static {
66+
this.b // should error
67+
if (1) {
68+
this.b; // should error
69+
}
70+
}
71+
72+
static b = 1;
73+
}

0 commit comments

Comments
 (0)