Skip to content

Commit dc8f02a

Browse files
committed
arguments should not be allowed in class static block
1 parent 20c93d3 commit dc8f02a

8 files changed

Lines changed: 624 additions & 19 deletions

src/compiler/checker.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25347,8 +25347,8 @@ 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 (isInPropertyInitializerOrClassStaticBlock(node)) {
25351-
error(node, Diagnostics.arguments_cannot_be_referenced_in_property_initializers);
25350+
if (isArgumentsInPropertyInitializerOrClassStaticBlock(node)) {
25351+
error(node, Diagnostics.arguments_cannot_be_referenced_in_property_initializers_or_class_static_initialization_block);
2535225352
return errorType;
2535325353
}
2535425354

@@ -28898,6 +28898,20 @@ namespace ts {
2889828898
});
2889928899
}
2890028900

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;
28909+
default:
28910+
return isFunctionLikeDeclaration(node) ? "quit" : false;
28911+
}
28912+
});
28913+
}
28914+
2890128915
/**
2890228916
* It's possible that "prop.valueDeclaration" is a local declaration, but the property was also declared in a superclass.
2890328917
* In that case we won't consider it used before its declaration, because it gets its value from the superclass' declaration.

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3353,7 +3353,7 @@
33533353
"category": "Error",
33543354
"code": 2814
33553355
},
3356-
"'arguments' cannot be referenced in property initializers.": {
3356+
"'arguments' cannot be referenced in property initializers or class static initialization block.": {
33573357
"category": "Error",
33583358
"code": 2815
33593359
},
Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,141 @@
1-
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(3,10): error TS2815: 'arguments' cannot be referenced in property initializers.
2-
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(9,10): error TS2815: 'arguments' cannot be referenced in property initializers.
3-
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(15,15): error TS2815: 'arguments' cannot be referenced in property initializers.
4-
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(21,15): error TS2815: 'arguments' cannot be referenced in property initializers.
1+
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(3,10): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
2+
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(9,10): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
3+
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(15,15): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
4+
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(21,15): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
5+
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(33,16): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
6+
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(40,7): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
7+
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(42,16): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
8+
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(57,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
9+
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(60,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
10+
tests/cases/compiler/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.
11+
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(75,7): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
12+
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(77,9): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
13+
tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(96,26): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
514

615

7-
==== tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts (4 errors) ====
16+
==== tests/cases/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts (13 errors) ====
817
function A() {
918
return class T {
1019
a = arguments
1120
~~~~~~~~~
12-
!!! error TS2815: 'arguments' cannot be referenced in property initializers.
21+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
1322
}
1423
}
1524

1625
function A1() {
1726
return new class T {
1827
a = arguments
1928
~~~~~~~~~
20-
!!! error TS2815: 'arguments' cannot be referenced in property initializers.
29+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
2130
}
2231
}
2332

2433
function B() {
2534
return class T {
2635
a = { b: arguments }
2736
~~~~~~~~~
28-
!!! error TS2815: 'arguments' cannot be referenced in property initializers.
37+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
2938
}
3039
}
3140

3241
function B1() {
3342
return new class T {
3443
a = { b: arguments }
3544
~~~~~~~~~
36-
!!! error TS2815: 'arguments' cannot be referenced in property initializers.
45+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
3746
}
3847
}
3948

4049
function C() {
4150
return class T {
4251
a = function () { arguments }
4352
}
44-
}
53+
}
54+
55+
function D() {
56+
return class T {
57+
a = () => arguments // should error
58+
~~~~~~~~~
59+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
60+
}
61+
}
62+
63+
function D1() {
64+
return class T {
65+
a = () => {
66+
arguments; // should error
67+
~~~~~~~~~
68+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
69+
const b = () => {
70+
return arguments; // should error
71+
~~~~~~~~~
72+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
73+
}
74+
75+
function f() {
76+
return arguments; // ok
77+
}
78+
}
79+
}
80+
}
81+
82+
function D2() {
83+
return class {
84+
constructor() {
85+
arguments; // ok
86+
}
87+
get foo() {
88+
~~~
89+
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
90+
return arguments; // ok
91+
}
92+
set foo(foo: any) {
93+
~~~
94+
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
95+
arguments; // ok
96+
}
97+
bar() {
98+
arguments; // ok
99+
}
100+
[Symbol.iterator]() {
101+
~~~~~~
102+
!!! 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.
103+
arguments; // ok
104+
}
105+
}
106+
}
107+
108+
function D3() {
109+
return class T {
110+
static {
111+
arguments; // should error
112+
~~~~~~~~~
113+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
114+
while(1) {
115+
arguments // should error
116+
~~~~~~~~~
117+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
118+
}
119+
}
120+
}
121+
}
122+
123+
function D4() {
124+
return class T {
125+
static {
126+
function f() {
127+
arguments; // ok
128+
}
129+
}
130+
}
131+
}
132+
133+
134+
function D5() {
135+
return class T {
136+
a = (() => { return arguments; })() // should error
137+
~~~~~~~~~
138+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization block.
139+
}
140+
}
141+

tests/baselines/reference/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.js

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,77 @@ function C() {
2727
return class T {
2828
a = function () { arguments }
2929
}
30-
}
30+
}
31+
32+
function D() {
33+
return class T {
34+
a = () => arguments // should error
35+
}
36+
}
37+
38+
function D1() {
39+
return class T {
40+
a = () => {
41+
arguments; // should error
42+
const b = () => {
43+
return arguments; // should error
44+
}
45+
46+
function f() {
47+
return arguments; // ok
48+
}
49+
}
50+
}
51+
}
52+
53+
function D2() {
54+
return class {
55+
constructor() {
56+
arguments; // ok
57+
}
58+
get foo() {
59+
return arguments; // ok
60+
}
61+
set foo(foo: any) {
62+
arguments; // ok
63+
}
64+
bar() {
65+
arguments; // ok
66+
}
67+
[Symbol.iterator]() {
68+
arguments; // ok
69+
}
70+
}
71+
}
72+
73+
function D3() {
74+
return class T {
75+
static {
76+
arguments; // should error
77+
while(1) {
78+
arguments // should error
79+
}
80+
}
81+
}
82+
}
83+
84+
function D4() {
85+
return class T {
86+
static {
87+
function f() {
88+
arguments; // ok
89+
}
90+
}
91+
}
92+
}
93+
94+
95+
function D5() {
96+
return class T {
97+
a = (() => { return arguments; })() // should error
98+
}
99+
}
100+
31101

32102
//// [argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.js]
33103
function A() {
@@ -70,3 +140,88 @@ function C() {
70140
return T;
71141
}());
72142
}
143+
function D() {
144+
return /** @class */ (function () {
145+
function T() {
146+
this.a = function () { return arguments; }; // should error
147+
}
148+
return T;
149+
}());
150+
}
151+
function D1() {
152+
return /** @class */ (function () {
153+
function T() {
154+
this.a = function () {
155+
arguments; // should error
156+
var b = function () {
157+
return arguments; // should error
158+
};
159+
function f() {
160+
return arguments; // ok
161+
}
162+
};
163+
}
164+
return T;
165+
}());
166+
}
167+
function D2() {
168+
return /** @class */ (function () {
169+
function class_1() {
170+
arguments; // ok
171+
}
172+
Object.defineProperty(class_1.prototype, "foo", {
173+
get: function () {
174+
return arguments; // ok
175+
},
176+
set: function (foo) {
177+
arguments; // ok
178+
},
179+
enumerable: false,
180+
configurable: true
181+
});
182+
class_1.prototype.bar = function () {
183+
arguments; // ok
184+
};
185+
class_1.prototype[Symbol.iterator] = function () {
186+
arguments; // ok
187+
};
188+
return class_1;
189+
}());
190+
}
191+
function D3() {
192+
var _a;
193+
return _a = /** @class */ (function () {
194+
function T() {
195+
}
196+
return T;
197+
}()),
198+
(function () {
199+
arguments; // should error
200+
while (1) {
201+
arguments; // should error
202+
}
203+
})(),
204+
_a;
205+
}
206+
function D4() {
207+
var _a;
208+
return _a = /** @class */ (function () {
209+
function T() {
210+
}
211+
return T;
212+
}()),
213+
(function () {
214+
function f() {
215+
arguments; // ok
216+
}
217+
})(),
218+
_a;
219+
}
220+
function D5() {
221+
return /** @class */ (function () {
222+
function T() {
223+
this.a = (function () { return arguments; })(); // should error
224+
}
225+
return T;
226+
}());
227+
}

0 commit comments

Comments
 (0)