Skip to content

Commit c5e6913

Browse files
committed
Add grammar error on quoted constructors for TS 3.5
1 parent 72f30a8 commit c5e6913

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31468,6 +31468,9 @@ namespace ts {
3146831468
return grammarErrorAtPos(node, node.end - 1, ";".length, Diagnostics._0_expected, "{");
3146931469
}
3147031470
}
31471+
else if (isClassLike(node.parent) && isStringLiteral(node.name) && node.name.text === "constructor" && (!compilerOptions.target || compilerOptions.target < ScriptTarget.ES5)) {
31472+
return grammarErrorOnNode(node.name, Diagnostics.Quoted_constructors_have_previously_been_interpreted_as_methods_which_is_incorrect_In_TypeScript_3_6_they_will_be_correctly_parsed_as_constructors_In_the_meantime_consider_using_constructor_to_write_a_constructor_or_constructor_to_write_a_method);
31473+
}
3147131474
if (checkGrammarForGenerator(node)) {
3147231475
return true;
3147331476
}

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2959,7 +2959,7 @@
29592959
"category": "Error",
29602960
"code": 4104
29612961
},
2962-
2962+
29632963
"The current host does not support the '{0}' option.": {
29642964
"category": "Error",
29652965
"code": 5001
@@ -4954,5 +4954,9 @@
49544954
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer." :{
49554955
"category": "Error",
49564956
"code": 18004
4957+
},
4958+
"Quoted constructors have previously been interpreted as methods, which is incorrect. In TypeScript 3.6, they will be correctly parsed as constructors. In the meantime, consider using 'constructor()' to write a constructor, or '[\"constructor\"]()' to write a method.": {
4959+
"category": "Error",
4960+
"code": 96000
49574961
}
49584962
}
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
class C {
2-
x: number;
3-
"constructor"() {
4-
this.x = 0;
5-
}
2+
"constructor"() {} // Error in 3.5
63
}
7-
(new C).constructor(); // Error
84

95
class D {
10-
x: number;
11-
'constructor'() {
12-
this.x = 0;
13-
}
6+
'constructor'() {} // Error in 3.5
147
}
15-
(new C).constructor(); // Error
168

179
class E {
18-
x: number;
19-
['constructor']() {
20-
this.x = 0;
21-
}
10+
['constructor']() {}
2211
}
23-
(new E).constructor();
12+
13+
new class {
14+
"constructor"() {} // Error in 3.5
15+
};
16+
17+
var o = { "constructor"() {} };

0 commit comments

Comments
 (0)