Skip to content

Commit 5e7343f

Browse files
committed
Merge pull request #2596 from Microsoft/parseClassInStrictMode
Parse class in strict mode
2 parents d56f2f6 + 09d037f commit 5e7343f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+920
-1206
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12445,7 +12445,16 @@ module ts {
1244512445
let identifier = <Identifier>name;
1244612446
if (contextNode && (contextNode.parserContextFlags & ParserContextFlags.StrictMode) && isEvalOrArgumentsIdentifier(identifier)) {
1244712447
let nameText = declarationNameToString(identifier);
12448-
return grammarErrorOnNode(identifier, Diagnostics.Invalid_use_of_0_in_strict_mode, nameText);
12448+
12449+
// We are checking if this name is inside class declaration or class expression (which are under class definitions inside ES6 spec.)
12450+
// if so, we would like to give more explicit invalid usage error.
12451+
// This will be particularly helpful in the case of "arguments" as such case is very common mistake.
12452+
if (getAncestor(name, SyntaxKind.ClassDeclaration) || getAncestor(name, SyntaxKind.ClassExpression)) {
12453+
return grammarErrorOnNode(identifier, Diagnostics.Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode, nameText);
12454+
}
12455+
else {
12456+
return grammarErrorOnNode(identifier, Diagnostics.Invalid_use_of_0_in_strict_mode, nameText);
12457+
}
1244912458
}
1245012459
}
1245112460
}

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ module ts {
167167
Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name: { code: 1207, category: DiagnosticCategory.Error, key: "Decorators cannot be applied to multiple get/set accessors of the same name." },
168168
Cannot_compile_non_external_modules_when_the_separateCompilation_flag_is_provided: { code: 1208, category: DiagnosticCategory.Error, key: "Cannot compile non-external modules when the '--separateCompilation' flag is provided." },
169169
Ambient_const_enums_are_not_allowed_when_the_separateCompilation_flag_is_provided: { code: 1209, category: DiagnosticCategory.Error, key: "Ambient const enums are not allowed when the '--separateCompilation' flag is provided." },
170-
A_class_declaration_without_the_default_modifier_must_have_a_name: { code: 1210, category: DiagnosticCategory.Error, key: "A class declaration without the 'default' modifier must have a name" },
170+
Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode: { code: 1210, category: DiagnosticCategory.Error, key: "Invalid use of '{0}'. Class definitions are automatically in strict mode." },
171+
A_class_declaration_without_the_default_modifier_must_have_a_name: { code: 1211, category: DiagnosticCategory.Error, key: "A class declaration without the 'default' modifier must have a name" },
171172
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
172173
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
173174
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,13 @@
659659
"category": "Error",
660660
"code": 1209
661661
},
662+
"Invalid use of '{0}'. Class definitions are automatically in strict mode.": {
663+
"category": "Error",
664+
"code": 1210
665+
},
662666
"A class declaration without the 'default' modifier must have a name": {
663667
"category": "Error",
664-
"code": 1210
668+
"code": 1211
665669
},
666670
"Duplicate identifier '{0}'.": {
667671
"category": "Error",

src/compiler/parser.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4756,9 +4756,7 @@ module ts {
47564756
function parseClassDeclarationOrExpression(fullStart: number, decorators: NodeArray<Decorator>, modifiers: ModifiersArray, kind: SyntaxKind): ClassLikeDeclaration {
47574757
// In ES6 specification, All parts of a ClassDeclaration or a ClassExpression are strict mode code
47584758
let savedStrictModeContext = inStrictModeContext();
4759-
if (languageVersion >= ScriptTarget.ES6) {
4760-
setStrictModeContext(true);
4761-
}
4759+
setStrictModeContext(true);
47624760

47634761
var node = <ClassLikeDeclaration>createNode(kind, fullStart);
47644762
node.decorators = decorators;

src/server/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ module ts.server {
6868
};
6969
}
7070

71-
private processRequest<T extends protocol.Request>(command: string, arguments?: any): T {
71+
private processRequest<T extends protocol.Request>(command: string, args?: any): T {
7272
var request: protocol.Request = {
7373
seq: this.sequence++,
7474
type: "request",
75-
command: command,
76-
arguments: arguments
75+
arguments: args,
76+
command
7777
};
7878

7979
this.writeMessage(JSON.stringify(request));
Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,203 @@
11
tests/cases/compiler/collisionArgumentsClassConstructor.ts(3,28): error TS2396: Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters.
2+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(3,31): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
3+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(4,13): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
4+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(8,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
25
tests/cases/compiler/collisionArgumentsClassConstructor.ts(8,17): error TS2396: Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters.
6+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(9,13): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
7+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(13,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
8+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(14,13): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
9+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(20,13): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
10+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(25,13): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
311
tests/cases/compiler/collisionArgumentsClassConstructor.ts(30,17): error TS2396: Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters.
12+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(30,24): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
13+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(31,13): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
14+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(35,24): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
15+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(36,13): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
16+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(41,31): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
17+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(44,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
18+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(47,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
19+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(51,31): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
20+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(52,31): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
421
tests/cases/compiler/collisionArgumentsClassConstructor.ts(53,25): error TS2396: Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters.
22+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(53,28): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
23+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(54,13): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
24+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(59,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
25+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(60,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
26+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(61,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
527
tests/cases/compiler/collisionArgumentsClassConstructor.ts(61,17): error TS2396: Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters.
28+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(62,13): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
29+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(67,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
30+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(68,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
31+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(69,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
32+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(70,13): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
33+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(75,31): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
34+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(76,31): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
35+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(79,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
36+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(80,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
37+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(84,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
38+
tests/cases/compiler/collisionArgumentsClassConstructor.ts(85,17): error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
639

740

8-
==== tests/cases/compiler/collisionArgumentsClassConstructor.ts (5 errors) ====
41+
==== tests/cases/compiler/collisionArgumentsClassConstructor.ts (38 errors) ====
942
// Constructors
1043
class c1 {
1144
constructor(i: number, ...arguments) { // error
1245
~~~~~~~~~~~~
1346
!!! error TS2396: Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters.
47+
~~~~~~~~~
48+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
1449
var arguments: any[]; // no error
50+
~~~~~~~~~
51+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
1552
}
1653
}
1754
class c12 {
1855
constructor(arguments: number, ...rest) { // error
56+
~~~~~~~~~
57+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
1958
~~~~~~~~~~~~~~~~~
2059
!!! error TS2396: Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters.
2160
var arguments = 10; // no error
61+
~~~~~~~~~
62+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
2263
}
2364
}
2465
class c1NoError {
2566
constructor(arguments: number) { // no error
67+
~~~~~~~~~
68+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
2669
var arguments = 10; // no error
70+
~~~~~~~~~
71+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
2772
}
2873
}
2974

3075
class c2 {
3176
constructor(...restParameters) {
3277
var arguments = 10; // no error
78+
~~~~~~~~~
79+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
3380
}
3481
}
3582
class c2NoError {
3683
constructor() {
3784
var arguments = 10; // no error
85+
~~~~~~~~~
86+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
3887
}
3988
}
4089

4190
class c3 {
4291
constructor(public arguments: number, ...restParameters) { //arguments is error
4392
~~~~~~~~~~~~~~~~~~~~~~~~
4493
!!! error TS2396: Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters.
94+
~~~~~~~~~
95+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
4596
var arguments = 10; // no error
97+
~~~~~~~~~
98+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
4699
}
47100
}
48101
class c3NoError {
49102
constructor(public arguments: number) { // no error
103+
~~~~~~~~~
104+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
50105
var arguments = 10; // no error
106+
~~~~~~~~~
107+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
51108
}
52109
}
53110

54111
declare class c4 {
55112
constructor(i: number, ...arguments); // No error - no code gen
113+
~~~~~~~~~
114+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
56115
}
57116
declare class c42 {
58117
constructor(arguments: number, ...rest); // No error - no code gen
118+
~~~~~~~~~
119+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
59120
}
60121
declare class c4NoError {
61122
constructor(arguments: number); // no error
123+
~~~~~~~~~
124+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
62125
}
63126

64127
class c5 {
65128
constructor(i: number, ...arguments); // no codegen no error
129+
~~~~~~~~~
130+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
66131
constructor(i: string, ...arguments); // no codegen no error
132+
~~~~~~~~~
133+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
67134
constructor(i: any, ...arguments) { // error
68135
~~~~~~~~~~~~
69136
!!! error TS2396: Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters.
137+
~~~~~~~~~
138+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
70139
var arguments: any[]; // no error
140+
~~~~~~~~~
141+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
71142
}
72143
}
73144

74145
class c52 {
75146
constructor(arguments: number, ...rest); // no codegen no error
147+
~~~~~~~~~
148+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
76149
constructor(arguments: string, ...rest); // no codegen no error
150+
~~~~~~~~~
151+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
77152
constructor(arguments: any, ...rest) { // error
153+
~~~~~~~~~
154+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
78155
~~~~~~~~~~~~~~
79156
!!! error TS2396: Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters.
80157
var arguments: any; // no error
158+
~~~~~~~~~
159+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
81160
}
82161
}
83162

84163
class c5NoError {
85164
constructor(arguments: number); // no error
165+
~~~~~~~~~
166+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
86167
constructor(arguments: string); // no error
168+
~~~~~~~~~
169+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
87170
constructor(arguments: any) { // no error
171+
~~~~~~~~~
172+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
88173
var arguments: any; // no error
174+
~~~~~~~~~
175+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
89176
}
90177
}
91178

92179
declare class c6 {
93180
constructor(i: number, ...arguments); // no codegen no error
181+
~~~~~~~~~
182+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
94183
constructor(i: string, ...arguments); // no codegen no error
184+
~~~~~~~~~
185+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
95186
}
96187
declare class c62 {
97188
constructor(arguments: number, ...rest); // no codegen no error
189+
~~~~~~~~~
190+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
98191
constructor(arguments: string, ...rest); // no codegen no error
192+
~~~~~~~~~
193+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
99194
}
100195

101196
declare class c6NoError {
102197
constructor(arguments: number); // no error
198+
~~~~~~~~~
199+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
103200
constructor(arguments: string); // no error
201+
~~~~~~~~~
202+
!!! error TS1210: Invalid use of 'arguments'. Class definitions are automatically in strict mode.
104203
}

0 commit comments

Comments
 (0)