Skip to content

Commit 8cf32b4

Browse files
AndaristZzzen
andauthored
Port "arguments should not be allowed in class static block" (#1828)
Co-authored-by: Zzzen <[email protected]>
1 parent 5595106 commit 8cf32b4

9 files changed

+132
-157
lines changed

internal/checker/checker.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10583,7 +10583,7 @@ func (c *Checker) checkIdentifier(node *ast.Node, checkMode CheckMode) *Type {
1058310583
return c.errorType
1058410584
}
1058510585
if symbol == c.argumentsSymbol {
10586-
if c.isInPropertyInitializerOrClassStaticBlock(node) {
10586+
if c.isInPropertyInitializerOrClassStaticBlock(node, true /*ignoreArrowFunctions*/) {
1058710587
c.error(node, diagnostics.X_arguments_cannot_be_referenced_in_property_initializers_or_class_static_initialization_blocks)
1058810588
return c.errorType
1058910589
}
@@ -11215,7 +11215,7 @@ func (c *Checker) checkPropertyNotUsedBeforeDeclaration(prop *ast.Symbol, node *
1121511215
}
1121611216
var diagnostic *ast.Diagnostic
1121711217
declarationName := right.Text()
11218-
if c.isInPropertyInitializerOrClassStaticBlock(node) &&
11218+
if c.isInPropertyInitializerOrClassStaticBlock(node, false /*ignoreArrowFunctions*/) &&
1121911219
!c.isOptionalPropertyDeclaration(valueDeclaration) &&
1122011220
!(ast.IsAccessExpression(node) && ast.IsAccessExpression(node.Expression())) &&
1122111221
!c.isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right) &&
@@ -13151,25 +13151,19 @@ func (c *Checker) checkShorthandPropertyAssignment(node *ast.Node, inDestructuri
1315113151
return expressionType
1315213152
}
1315313153

13154-
func (c *Checker) isInPropertyInitializerOrClassStaticBlock(node *ast.Node) bool {
13154+
func (c *Checker) isInPropertyInitializerOrClassStaticBlock(node *ast.Node, ignoreArrowFunctions bool) bool {
1315513155
return ast.FindAncestorOrQuit(node, func(node *ast.Node) ast.FindAncestorResult {
1315613156
switch node.Kind {
13157-
case ast.KindPropertyDeclaration:
13157+
case ast.KindPropertyDeclaration, ast.KindClassStaticBlockDeclaration:
1315813158
return ast.FindAncestorTrue
13159-
case ast.KindPropertyAssignment, ast.KindMethodDeclaration, ast.KindGetAccessor, ast.KindSetAccessor, ast.KindSpreadAssignment,
13160-
ast.KindComputedPropertyName, ast.KindTemplateSpan, ast.KindJsxExpression, ast.KindJsxAttribute, ast.KindJsxAttributes,
13161-
ast.KindJsxSpreadAttribute, ast.KindJsxOpeningElement, ast.KindExpressionWithTypeArguments, ast.KindHeritageClause:
13162-
return ast.FindAncestorFalse
13163-
case ast.KindArrowFunction, ast.KindExpressionStatement:
13164-
if ast.IsBlock(node.Parent) && ast.IsClassStaticBlockDeclaration(node.Parent.Parent) {
13165-
return ast.FindAncestorTrue
13166-
}
13159+
case ast.KindTypeQuery, ast.KindJsxClosingElement:
1316713160
return ast.FindAncestorQuit
13161+
case ast.KindArrowFunction:
13162+
return core.IfElse(ignoreArrowFunctions, ast.FindAncestorFalse, ast.FindAncestorQuit)
13163+
case ast.KindBlock:
13164+
return core.IfElse(ast.IsFunctionLikeDeclaration(node.Parent) && node.Parent.Kind != ast.KindArrowFunction, ast.FindAncestorQuit, ast.FindAncestorFalse)
1316813165
default:
13169-
if ast.IsExpressionNode(node) {
13170-
return ast.FindAncestorFalse
13171-
}
13172-
return ast.FindAncestorQuit
13166+
return ast.FindAncestorFalse
1317313167
}
1317413168
}) != nil
1317513169
}

testdata/baselines/reference/submodule/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.errors.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(3,10): error
22
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(9,10): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
33
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(15,15): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
44
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(21,15): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
5+
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(33,16): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
6+
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(40,7): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
7+
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(42,16): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
58
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(66,6): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later.
69
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(75,7): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
10+
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(77,9): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
11+
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(96,26): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
12+
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(102,15): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
713

814

9-
==== argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts (6 errors) ====
15+
==== argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts (12 errors) ====
1016
function A() {
1117
return class T {
1218
a = arguments
@@ -48,15 +54,21 @@ argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(75,7): error
4854
function D() {
4955
return class T {
5056
a = () => arguments // should error
57+
~~~~~~~~~
58+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
5159
}
5260
}
5361

5462
function D1() {
5563
return class T {
5664
a = () => {
5765
arguments; // should error
66+
~~~~~~~~~
67+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
5868
const b = () => {
5969
return arguments; // should error
70+
~~~~~~~~~
71+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
6072
}
6173

6274
function f() {
@@ -96,6 +108,8 @@ argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(75,7): error
96108
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
97109
while(1) {
98110
arguments // should error
111+
~~~~~~~~~
112+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
99113
}
100114
}
101115
}
@@ -115,12 +129,16 @@ argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(75,7): error
115129
function D5() {
116130
return class T {
117131
a = (() => { return arguments; })() // should error
132+
~~~~~~~~~
133+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
118134
}
119135
}
120136

121137
function D6() {
122138
return class T {
123139
a = (x = arguments) => {} // should error
140+
~~~~~~~~~
141+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
124142
}
125143
}
126144

testdata/baselines/reference/submodule/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.errors.txt.diff

Lines changed: 0 additions & 71 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.types

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ function D() {
8181
>T : typeof T
8282

8383
a = () => arguments // should error
84-
>a : () => IArguments
85-
>() => arguments : () => IArguments
86-
>arguments : IArguments
84+
>a : () => any
85+
>() => arguments : () => any
86+
>arguments : any
8787
}
8888
}
8989

@@ -99,14 +99,14 @@ function D1() {
9999
>() => { arguments; // should error const b = () => { return arguments; // should error } function f() { return arguments; // ok } } : () => void
100100

101101
arguments; // should error
102-
>arguments : IArguments
102+
>arguments : any
103103

104104
const b = () => {
105-
>b : () => IArguments
106-
>() => { return arguments; // should error } : () => IArguments
105+
>b : () => any
106+
>() => { return arguments; // should error } : () => any
107107

108108
return arguments; // should error
109-
>arguments : IArguments
109+
>arguments : any
110110
}
111111

112112
function f() {
@@ -175,7 +175,7 @@ function D3() {
175175
>1 : 1
176176

177177
arguments // should error
178-
>arguments : IArguments
178+
>arguments : any
179179
}
180180
}
181181
}
@@ -208,11 +208,11 @@ function D5() {
208208
>T : typeof T
209209

210210
a = (() => { return arguments; })() // should error
211-
>a : IArguments
212-
>(() => { return arguments; })() : IArguments
213-
>(() => { return arguments; }) : () => IArguments
214-
>() => { return arguments; } : () => IArguments
215-
>arguments : IArguments
211+
>a : any
212+
>(() => { return arguments; })() : any
213+
>(() => { return arguments; }) : () => any
214+
>() => { return arguments; } : () => any
215+
>arguments : any
216216
}
217217
}
218218

@@ -224,10 +224,10 @@ function D6() {
224224
>T : typeof T
225225

226226
a = (x = arguments) => {} // should error
227-
>a : (x?: IArguments) => void
228-
>(x = arguments) => {} : (x?: IArguments) => void
229-
>x : IArguments
230-
>arguments : IArguments
227+
>a : (x?: any) => void
228+
>(x = arguments) => {} : (x?: any) => void
229+
>x : any
230+
>arguments : any
231231
}
232232
}
233233

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,65 @@
11
--- old.argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.types
22
+++ new.argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.types
3-
@@= skipped -174, +174 lines =@@
4-
>1 : 1
3+
@@= skipped -80, +80 lines =@@
4+
>T : typeof T
55

6-
arguments // should error
7-
->arguments : any
8-
+>arguments : IArguments
6+
a = () => arguments // should error
7+
->a : () => IArguments
8+
->() => arguments : () => IArguments
9+
->arguments : IArguments
10+
+>a : () => any
11+
+>() => arguments : () => any
12+
+>arguments : any
13+
}
14+
}
15+
16+
@@= skipped -18, +18 lines =@@
17+
>() => { arguments; // should error const b = () => { return arguments; // should error } function f() { return arguments; // ok } } : () => void
18+
19+
arguments; // should error
20+
->arguments : IArguments
21+
+>arguments : any
22+
23+
const b = () => {
24+
->b : () => IArguments
25+
->() => { return arguments; // should error } : () => IArguments
26+
+>b : () => any
27+
+>() => { return arguments; // should error } : () => any
28+
29+
return arguments; // should error
30+
->arguments : IArguments
31+
+>arguments : any
932
}
10-
}
11-
}
33+
34+
function f() {
35+
@@= skipped -109, +109 lines =@@
36+
>T : typeof T
37+
38+
a = (() => { return arguments; })() // should error
39+
->a : IArguments
40+
->(() => { return arguments; })() : IArguments
41+
->(() => { return arguments; }) : () => IArguments
42+
->() => { return arguments; } : () => IArguments
43+
->arguments : IArguments
44+
+>a : any
45+
+>(() => { return arguments; })() : any
46+
+>(() => { return arguments; }) : () => any
47+
+>() => { return arguments; } : () => any
48+
+>arguments : any
49+
}
50+
}
51+
52+
@@= skipped -16, +16 lines =@@
53+
>T : typeof T
54+
55+
a = (x = arguments) => {} // should error
56+
->a : (x?: IArguments) => void
57+
->(x = arguments) => {} : (x?: IArguments) => void
58+
->x : IArguments
59+
->arguments : IArguments
60+
+>a : (x?: any) => void
61+
+>(x = arguments) => {} : (x?: any) => void
62+
+>x : any
63+
+>arguments : any
64+
}
65+
}

testdata/baselines/reference/submodule/compiler/decoratorUsedBeforeDeclaration.errors.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ decoratorUsedBeforeDeclaration.ts(1,9): error TS2450: Enum 'Enum' used before it
33
decoratorUsedBeforeDeclaration.ts(2,7): error TS2450: Enum 'Enum' used before its declaration.
44
decoratorUsedBeforeDeclaration.ts(4,4): error TS2448: Block-scoped variable 'lambda' used before its declaration.
55
decoratorUsedBeforeDeclaration.ts(4,11): error TS2450: Enum 'Enum' used before its declaration.
6+
decoratorUsedBeforeDeclaration.ts(4,16): error TS2729: Property 'No' is used before its initialization.
67
decoratorUsedBeforeDeclaration.ts(5,9): error TS2450: Enum 'Enum' used before its declaration.
8+
decoratorUsedBeforeDeclaration.ts(5,14): error TS2729: Property 'No' is used before its initialization.
79

810

9-
==== decoratorUsedBeforeDeclaration.ts (6 errors) ====
11+
==== decoratorUsedBeforeDeclaration.ts (8 errors) ====
1012
@lambda(Enum.No)
1113
~~~~~~
1214
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
@@ -26,10 +28,16 @@ decoratorUsedBeforeDeclaration.ts(5,9): error TS2450: Enum 'Enum' used before it
2628
~~~~
2729
!!! error TS2450: Enum 'Enum' used before its declaration.
2830
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
31+
~~
32+
!!! error TS2729: Property 'No' is used before its initialization.
33+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:36:3: 'No' is declared here.
2934
@deco(Enum.No)
3035
~~~~
3136
!!! error TS2450: Enum 'Enum' used before its declaration.
3237
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
38+
~~
39+
!!! error TS2729: Property 'No' is used before its initialization.
40+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:36:3: 'No' is declared here.
3341
greeting: string;
3442

3543
constructor(message: string) {

0 commit comments

Comments
 (0)