From c5e6913edea3da168929732c8ee0ba75de0f661c Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 25 Apr 2019 15:44:23 -0700 Subject: [PATCH] Add grammar error on quoted constructors for TS 3.5 --- src/compiler/checker.ts | 3 +++ src/compiler/diagnosticMessages.json | 6 ++++- .../quotedConstructors.ts | 24 +++++++------------ 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ce56db4b962f4..da002607ed82c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -31468,6 +31468,9 @@ namespace ts { return grammarErrorAtPos(node, node.end - 1, ";".length, Diagnostics._0_expected, "{"); } } + else if (isClassLike(node.parent) && isStringLiteral(node.name) && node.name.text === "constructor" && (!compilerOptions.target || compilerOptions.target < ScriptTarget.ES5)) { + 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); + } if (checkGrammarForGenerator(node)) { return true; } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index a78e89f42beb3..34d093caea23a 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2959,7 +2959,7 @@ "category": "Error", "code": 4104 }, - + "The current host does not support the '{0}' option.": { "category": "Error", "code": 5001 @@ -4954,5 +4954,9 @@ "No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer." :{ "category": "Error", "code": 18004 + }, + "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.": { + "category": "Error", + "code": 96000 } } diff --git a/tests/cases/conformance/classes/constructorDeclarations/quotedConstructors.ts b/tests/cases/conformance/classes/constructorDeclarations/quotedConstructors.ts index e0564ba73ec54..3ab45c72810cd 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/quotedConstructors.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/quotedConstructors.ts @@ -1,23 +1,17 @@ class C { - x: number; - "constructor"() { - this.x = 0; - } + "constructor"() {} // Error in 3.5 } -(new C).constructor(); // Error class D { - x: number; - 'constructor'() { - this.x = 0; - } + 'constructor'() {} // Error in 3.5 } -(new C).constructor(); // Error class E { - x: number; - ['constructor']() { - this.x = 0; - } + ['constructor']() {} } -(new E).constructor(); + +new class { + "constructor"() {} // Error in 3.5 +}; + +var o = { "constructor"() {} };