Skip to content

Commit

Permalink
Shorten error spans for errors reported on constructor declarations (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored May 31, 2024
1 parent 40583ff commit 7f7ff92
Show file tree
Hide file tree
Showing 37 changed files with 111 additions and 229 deletions.
11 changes: 11 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2309,6 +2309,17 @@ export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpa
const pos = skipTrivia(sourceFile.text, (node as JSDocSatisfiesTag).tagName.pos);
return getSpanOfTokenAtPosition(sourceFile, pos);
}
case SyntaxKind.Constructor: {
const constructorDeclaration = node as ConstructorDeclaration;
const start = skipTrivia(sourceFile.text, constructorDeclaration.pos);
const scanner = createScanner(sourceFile.languageVersion, /*skipTrivia*/ true, sourceFile.languageVariant, sourceFile.text, /*onError*/ undefined, start);
let token = scanner.scan();
while (token !== SyntaxKind.ConstructorKeyword && token !== SyntaxKind.EndOfFileToken) {
token = scanner.scan();
}
const end = scanner.getTokenEnd();
return createTextSpanFromBounds(start, end);
}
}

if (errorNode === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/ClassDeclaration10.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ClassDeclaration10.ts(3,4): error TS2391: Function implementation is missing or
==== ClassDeclaration10.ts (2 errors) ====
class C {
constructor();
~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2390: Constructor implementation is missing.
foo();
~~~
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/ClassDeclaration11.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ClassDeclaration11.ts(2,4): error TS2390: Constructor implementation is missing.
==== ClassDeclaration11.ts (1 errors) ====
class C {
constructor();
~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2390: Constructor implementation is missing.
foo() { }
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/ClassDeclaration14.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ ClassDeclaration14.ts(3,4): error TS2390: Constructor implementation is missing.
~~~
!!! error TS2391: Function implementation is missing or not immediately following the declaration.
constructor();
~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2390: Constructor implementation is missing.
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/ClassDeclaration8.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ ClassDeclaration8.ts(2,3): error TS2390: Constructor implementation is missing.
==== ClassDeclaration8.ts (1 errors) ====
class C {
constructor();
~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2390: Constructor implementation is missing.
}
6 changes: 2 additions & 4 deletions tests/baselines/reference/bases.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ bases.ts(18,9): error TS2339: Property 'y' does not exist on type 'C'.
!!! error TS2420: Property 'x' is missing in type 'C' but required in type 'I'.
!!! related TS2728 bases.ts:2:5: 'x' is declared here.
constructor() {
~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
this.x: any;
~~~~~~~~~~~~~~~~~~~~
~~~~
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
~
Expand All @@ -47,8 +47,6 @@ bases.ts(18,9): error TS2339: Property 'y' does not exist on type 'C'.
~~~
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
}
~~~~~
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
}

new C().x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ classConstructorOverloadsAccessibility.ts(11,2): error TS2385: Overload signatur
==== classConstructorOverloadsAccessibility.ts (3 errors) ====
class A {
public constructor(a: boolean) // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~
!!! error TS2385: Overload signatures must all be public, private or protected.
protected constructor(a: number) // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2385: Overload signatures must all be public, private or protected.
private constructor(a: string)
private constructor() {
Expand All @@ -19,7 +19,7 @@ classConstructorOverloadsAccessibility.ts(11,2): error TS2385: Overload signatur

class B {
protected constructor(a: number) // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2385: Overload signatures must all be public, private or protected.
constructor(a: string)
constructor() {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/classUpdateTests.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ classUpdateTests.ts(113,1): error TS1128: Declaration or statement expected.

class F extends E {
constructor() {} // ERROR - super call required
~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ classWithTwoConstructorDefinitions.ts(8,5): error TS2392: Multiple constructor i
==== classWithTwoConstructorDefinitions.ts (4 errors) ====
class C {
constructor() { } // error
~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2392: Multiple constructor implementations are not allowed.
constructor(x) { } // error
~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2392: Multiple constructor implementations are not allowed.
}

class D<T> {
constructor(x: T) { } // error
~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2392: Multiple constructor implementations are not allowed.
constructor(x: T, y: T) { } // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2392: Multiple constructor implementations are not allowed.
}
16 changes: 6 additions & 10 deletions tests/baselines/reference/constructorOverloads1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,21 @@ constructorOverloads1.ts(17,18): error TS2769: No overload matches this call.
==== constructorOverloads1.ts (6 errors) ====
class Foo {
constructor(s: string);
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2392: Multiple constructor implementations are not allowed.
constructor(n: number);
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2392: Multiple constructor implementations are not allowed.
constructor(x: any) {
~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2392: Multiple constructor implementations are not allowed.

}
~~~~~
!!! error TS2392: Multiple constructor implementations are not allowed.
constructor(x: any) {
~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2392: Multiple constructor implementations are not allowed.

}
~~~~~
!!! error TS2392: Multiple constructor implementations are not allowed.
bar1() { /*WScript.Echo("bar1");*/ }
bar2() { /*WScript.Echo("bar1");*/ }
}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/constructorOverloads3.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ constructorOverloads3.ts(12,5): error TS2377: Constructors for derived classes m
constructor(n: number);
constructor(a: any);
constructor(x: any, y?: any) { }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
bar1() { /*WScript.Echo("Yo");*/}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/constructorOverloads8.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ constructorOverloads8.ts(3,5): error TS2392: Multiple constructor implementation
==== constructorOverloads8.ts (2 errors) ====
class C {
constructor(x) { }
~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2392: Multiple constructor implementations are not allowed.
constructor(y, x) { } // illegal, 2 constructor implementations
~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2392: Multiple constructor implementations are not allowed.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ constructorWithIncompleteTypeAnnotation.ts(261,1): error TS1128: Declaration or
private otherValue = 42;

constructor(private value: number, public name: string) : }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2390: Constructor implementation is missing.
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2369: A parameter property is only allowed in a constructor implementation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ constructorsWithSpecializedSignatures.ts(26,5): error TS2394: This overload sign
class D {
constructor(x: "hi");
constructor(x: "foo");
~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2394: This overload signature is not compatible with its implementation signature.
!!! related TS2750 constructorsWithSpecializedSignatures.ts:20:5: The implementation signature is declared here.
constructor(x: number);
Expand All @@ -32,7 +32,7 @@ constructorsWithSpecializedSignatures.ts(26,5): error TS2394: This overload sign
class D2 {
constructor(x: "hi");
constructor(x: "foo");
~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2394: This overload signature is not compatible with its implementation signature.
!!! related TS2750 constructorsWithSpecializedSignatures.ts:28:5: The implementation signature is declared here.
constructor(x: string);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ derivedClassConstructorWithoutSuperCall.ts(24,31): error TS2337: Super calls are

class Derived extends Base {
constructor() { // error
~~~~~~~~~~~~~~~~~~~~~~~~
}
~~~~~
~~~~~~~~~~~
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
}
}

class Base2<T> {
Expand All @@ -26,26 +25,22 @@ derivedClassConstructorWithoutSuperCall.ts(24,31): error TS2337: Super calls are

class Derived2<T> extends Base2<T> {
constructor() { // error for no super call (nested scopes don't count)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
var r2 = () => super(); // error for misplaced super call (nested function)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~
!!! error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors.
}
~~~~~
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
}

class Derived3<T> extends Base2<T> {
constructor() { // error
~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
var r = function () { super() } // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~
!!! error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors.
}
~~~~~
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
}

class Derived4<T> extends Base2<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,16 @@ derivedClassParameterProperties.ts(81,9): error TS17009: 'super' must be called
a = 1;
b: number;
constructor(y: string) {
~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers.
this.a = 3;
~~~~~~~~~~~~~~~~~~~
~~~~
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
this.b = 3;
~~~~~~~~~~~~~~~~~~~
~~~~
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
super();
~~~~~~~~~~~~~~~~
}
~~~~~
!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers.
}

class Derived8 extends Base {
Expand All @@ -99,20 +95,16 @@ derivedClassParameterProperties.ts(81,9): error TS17009: 'super' must be called
a = 1;
b: number;
constructor(y: string) {
~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~
!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers.
this.a = 3;
~~~~~~~~~~~~~~~~~~~
~~~~
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
this.b = 3;
~~~~~~~~~~~~~~~~~~~
~~~~
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
super();
~~~~~~~~~~~~~~~~
}
~~~~~
!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers.
}

class Derived10<T> extends Base2<T> {
Expand Down
Loading

0 comments on commit 7f7ff92

Please sign in to comment.