From 9336886fc2340079e38751e8b088f4a8ef9eea2e Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Tue, 6 Dec 2016 10:57:52 -0800 Subject: [PATCH] Added more specific diagnostic message --- src/compiler/checker.ts | 8 ++++---- src/compiler/diagnosticMessages.json | 4 ++++ tests/baselines/reference/superAccess2.errors.txt | 12 ++++++------ .../reference/superInConstructorParam1.errors.txt | 4 ++-- tests/baselines/reference/superNewCall1.errors.txt | 4 ++-- ...erPropertyInConstructorBeforeSuperCall.errors.txt | 8 ++++---- .../reference/superWithTypeArgument.errors.txt | 4 ++-- .../reference/superWithTypeArgument2.errors.txt | 4 ++-- .../reference/superWithTypeArgument3.errors.txt | 4 ++-- 9 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8c08e9b121b..b7b3043e661 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10405,7 +10405,7 @@ namespace ts { return baseConstructorType === nullWideningType; } - function checkThisBeforeSuper(node: Node, container: Node) { + function checkThisBeforeSuper(node: Node, container: Node, diagnosticMessage: DiagnosticMessage) { const containingClassDecl = container.parent; const baseTypeNode = getClassExtendsHeritageClauseElement(containingClassDecl); @@ -10423,7 +10423,7 @@ namespace ts { // "this" is not accessed as a parameter of the super-call. if (!superCall || superCall.end > node.pos) { // In ES6, super inside constructor of class-declaration has to precede "this" accessing - error(node, Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class); + error(node, diagnosticMessage); } } } @@ -10435,7 +10435,7 @@ namespace ts { let needToCaptureLexicalThis = false; if (container.kind === SyntaxKind.Constructor) { - checkThisBeforeSuper(node, container); + checkThisBeforeSuper(node, container, Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class); } // Now skip arrow functions to get the "real" owner of 'this'. @@ -10584,7 +10584,7 @@ namespace ts { } if (!isCallExpression && container.kind === SyntaxKind.Constructor) { - checkThisBeforeSuper(node, container); + checkThisBeforeSuper(node, container, Diagnostics.super_must_be_called_before_accessing_a_property_of_super_in_the_constructor_of_a_derived_class); } if ((getModifierFlags(container) & ModifierFlags.Static) || isCallExpression) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 4d42c77e566..b59921c2948 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3165,6 +3165,10 @@ "category": "Error", "code": 17010 }, + "'super' must be called before accessing a property of 'super' in the constructor of a derived class.": { + "category": "Error", + "code": 17011 + }, "Circularity detected while resolving configuration: {0}": { "category": "Error", diff --git a/tests/baselines/reference/superAccess2.errors.txt b/tests/baselines/reference/superAccess2.errors.txt index c427115574f..edcd4031c47 100644 --- a/tests/baselines/reference/superAccess2.errors.txt +++ b/tests/baselines/reference/superAccess2.errors.txt @@ -2,13 +2,13 @@ tests/cases/compiler/superAccess2.ts(7,15): error TS1034: 'super' must be follow tests/cases/compiler/superAccess2.ts(8,17): error TS2338: 'super' property access is permitted only in a constructor, member function, or member accessor of a derived class. tests/cases/compiler/superAccess2.ts(8,22): error TS1034: 'super' must be followed by an argument list or member access. tests/cases/compiler/superAccess2.ts(11,28): error TS2336: 'super' cannot be referenced in constructor arguments. -tests/cases/compiler/superAccess2.ts(11,28): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/compiler/superAccess2.ts(11,28): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. tests/cases/compiler/superAccess2.ts(11,33): error TS1034: 'super' must be followed by an argument list or member access. tests/cases/compiler/superAccess2.ts(11,40): error TS2336: 'super' cannot be referenced in constructor arguments. -tests/cases/compiler/superAccess2.ts(11,40): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/compiler/superAccess2.ts(11,40): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. tests/cases/compiler/superAccess2.ts(11,45): error TS1034: 'super' must be followed by an argument list or member access. tests/cases/compiler/superAccess2.ts(11,59): error TS2336: 'super' cannot be referenced in constructor arguments. -tests/cases/compiler/superAccess2.ts(11,59): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/compiler/superAccess2.ts(11,59): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. tests/cases/compiler/superAccess2.ts(11,64): error TS1034: 'super' must be followed by an argument list or member access. tests/cases/compiler/superAccess2.ts(15,19): error TS1034: 'super' must be followed by an argument list or member access. tests/cases/compiler/superAccess2.ts(17,15): error TS2339: Property 'y' does not exist on type 'P'. @@ -37,19 +37,19 @@ tests/cases/compiler/superAccess2.ts(21,15): error TS2339: Property 'x' does not ~~~~~ !!! error TS2336: 'super' cannot be referenced in constructor arguments. ~~~~~ -!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. ~ !!! error TS1034: 'super' must be followed by an argument list or member access. ~~~~~ !!! error TS2336: 'super' cannot be referenced in constructor arguments. ~~~~~ -!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. ~ !!! error TS1034: 'super' must be followed by an argument list or member access. ~~~~~ !!! error TS2336: 'super' cannot be referenced in constructor arguments. ~~~~~ -!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. ~ !!! error TS1034: 'super' must be followed by an argument list or member access. super(); diff --git a/tests/baselines/reference/superInConstructorParam1.errors.txt b/tests/baselines/reference/superInConstructorParam1.errors.txt index 1eac79769d8..4220fcb9b0e 100644 --- a/tests/baselines/reference/superInConstructorParam1.errors.txt +++ b/tests/baselines/reference/superInConstructorParam1.errors.txt @@ -1,6 +1,6 @@ tests/cases/compiler/superInConstructorParam1.ts(8,3): error TS2377: Constructors for derived classes must contain a 'super' call. tests/cases/compiler/superInConstructorParam1.ts(8,19): error TS2336: 'super' cannot be referenced in constructor arguments. -tests/cases/compiler/superInConstructorParam1.ts(8,19): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/compiler/superInConstructorParam1.ts(8,19): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. ==== tests/cases/compiler/superInConstructorParam1.ts (3 errors) ==== @@ -16,7 +16,7 @@ tests/cases/compiler/superInConstructorParam1.ts(8,19): error TS17009: 'super' m ~~~~~ !!! error TS2336: 'super' cannot be referenced in constructor arguments. ~~~~~ -!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. } ~~~ !!! error TS2377: Constructors for derived classes must contain a 'super' call. diff --git a/tests/baselines/reference/superNewCall1.errors.txt b/tests/baselines/reference/superNewCall1.errors.txt index 3dd7435e5c4..c8fd5c471fb 100644 --- a/tests/baselines/reference/superNewCall1.errors.txt +++ b/tests/baselines/reference/superNewCall1.errors.txt @@ -1,6 +1,6 @@ tests/cases/compiler/superNewCall1.ts(9,5): error TS2377: Constructors for derived classes must contain a 'super' call. tests/cases/compiler/superNewCall1.ts(10,9): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. -tests/cases/compiler/superNewCall1.ts(10,13): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/compiler/superNewCall1.ts(10,13): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. ==== tests/cases/compiler/superNewCall1.ts (3 errors) ==== @@ -19,7 +19,7 @@ tests/cases/compiler/superNewCall1.ts(10,13): error TS17009: 'super' must be cal ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. ~~~~~ -!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. } ~~~~~ !!! error TS2377: Constructors for derived classes must contain a 'super' call. diff --git a/tests/baselines/reference/superPropertyInConstructorBeforeSuperCall.errors.txt b/tests/baselines/reference/superPropertyInConstructorBeforeSuperCall.errors.txt index 029154e3318..407e984c5d1 100644 --- a/tests/baselines/reference/superPropertyInConstructorBeforeSuperCall.errors.txt +++ b/tests/baselines/reference/superPropertyInConstructorBeforeSuperCall.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/classes/constructorDeclarations/superCalls/superPropertyInConstructorBeforeSuperCall.ts(7,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/superPropertyInConstructorBeforeSuperCall.ts(13,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/superPropertyInConstructorBeforeSuperCall.ts(7,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/superPropertyInConstructorBeforeSuperCall.ts(13,15): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. ==== tests/cases/conformance/classes/constructorDeclarations/superCalls/superPropertyInConstructorBeforeSuperCall.ts (2 errors) ==== @@ -11,7 +11,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/superProperty constructor() { super.x(); ~~~~~ -!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. super(); } } @@ -19,6 +19,6 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/superProperty constructor() { super(super.x()); ~~~~~ -!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. } } \ No newline at end of file diff --git a/tests/baselines/reference/superWithTypeArgument.errors.txt b/tests/baselines/reference/superWithTypeArgument.errors.txt index bef02357d35..5860cb466d9 100644 --- a/tests/baselines/reference/superWithTypeArgument.errors.txt +++ b/tests/baselines/reference/superWithTypeArgument.errors.txt @@ -1,5 +1,5 @@ tests/cases/compiler/superWithTypeArgument.ts(6,5): error TS2377: Constructors for derived classes must contain a 'super' call. -tests/cases/compiler/superWithTypeArgument.ts(7,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/compiler/superWithTypeArgument.ts(7,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. tests/cases/compiler/superWithTypeArgument.ts(7,14): error TS1034: 'super' must be followed by an argument list or member access. @@ -14,7 +14,7 @@ tests/cases/compiler/superWithTypeArgument.ts(7,14): error TS1034: 'super' must super(); ~~~~~~~~~~~~~~~~~~~ ~~~~~ -!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. ~ !!! error TS1034: 'super' must be followed by an argument list or member access. } diff --git a/tests/baselines/reference/superWithTypeArgument2.errors.txt b/tests/baselines/reference/superWithTypeArgument2.errors.txt index 4bd7e960858..473e7f3b7ad 100644 --- a/tests/baselines/reference/superWithTypeArgument2.errors.txt +++ b/tests/baselines/reference/superWithTypeArgument2.errors.txt @@ -1,5 +1,5 @@ tests/cases/compiler/superWithTypeArgument2.ts(6,5): error TS2377: Constructors for derived classes must contain a 'super' call. -tests/cases/compiler/superWithTypeArgument2.ts(7,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/compiler/superWithTypeArgument2.ts(7,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. tests/cases/compiler/superWithTypeArgument2.ts(7,14): error TS1034: 'super' must be followed by an argument list or member access. @@ -14,7 +14,7 @@ tests/cases/compiler/superWithTypeArgument2.ts(7,14): error TS1034: 'super' must super(x); ~~~~~~~~~~~~~~~~~~~~ ~~~~~ -!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. ~ !!! error TS1034: 'super' must be followed by an argument list or member access. } diff --git a/tests/baselines/reference/superWithTypeArgument3.errors.txt b/tests/baselines/reference/superWithTypeArgument3.errors.txt index a6a59f92a00..37e6681d770 100644 --- a/tests/baselines/reference/superWithTypeArgument3.errors.txt +++ b/tests/baselines/reference/superWithTypeArgument3.errors.txt @@ -1,5 +1,5 @@ tests/cases/compiler/superWithTypeArgument3.ts(7,5): error TS2377: Constructors for derived classes must contain a 'super' call. -tests/cases/compiler/superWithTypeArgument3.ts(8,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/compiler/superWithTypeArgument3.ts(8,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. tests/cases/compiler/superWithTypeArgument3.ts(8,14): error TS1034: 'super' must be followed by an argument list or member access. @@ -15,7 +15,7 @@ tests/cases/compiler/superWithTypeArgument3.ts(8,14): error TS1034: 'super' must super(); ~~~~~~~~~~~~~~~~~~~ ~~~~~ -!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. ~ !!! error TS1034: 'super' must be followed by an argument list or member access. }