Skip to content

Commit 7f7ff92

Browse files
authored
Shorten error spans for errors reported on constructor declarations (#58061)
1 parent 40583ff commit 7f7ff92

37 files changed

+111
-229
lines changed

Diff for: src/compiler/utilities.ts

+11
Original file line numberDiff line numberDiff line change
@@ -2309,6 +2309,17 @@ export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpa
23092309
const pos = skipTrivia(sourceFile.text, (node as JSDocSatisfiesTag).tagName.pos);
23102310
return getSpanOfTokenAtPosition(sourceFile, pos);
23112311
}
2312+
case SyntaxKind.Constructor: {
2313+
const constructorDeclaration = node as ConstructorDeclaration;
2314+
const start = skipTrivia(sourceFile.text, constructorDeclaration.pos);
2315+
const scanner = createScanner(sourceFile.languageVersion, /*skipTrivia*/ true, sourceFile.languageVariant, sourceFile.text, /*onError*/ undefined, start);
2316+
let token = scanner.scan();
2317+
while (token !== SyntaxKind.ConstructorKeyword && token !== SyntaxKind.EndOfFileToken) {
2318+
token = scanner.scan();
2319+
}
2320+
const end = scanner.getTokenEnd();
2321+
return createTextSpanFromBounds(start, end);
2322+
}
23122323
}
23132324

23142325
if (errorNode === undefined) {

Diff for: tests/baselines/reference/ClassDeclaration10.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ClassDeclaration10.ts(3,4): error TS2391: Function implementation is missing or
55
==== ClassDeclaration10.ts (2 errors) ====
66
class C {
77
constructor();
8-
~~~~~~~~~~~~~~
8+
~~~~~~~~~~~
99
!!! error TS2390: Constructor implementation is missing.
1010
foo();
1111
~~~

Diff for: tests/baselines/reference/ClassDeclaration11.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ClassDeclaration11.ts(2,4): error TS2390: Constructor implementation is missing.
44
==== ClassDeclaration11.ts (1 errors) ====
55
class C {
66
constructor();
7-
~~~~~~~~~~~~~~
7+
~~~~~~~~~~~
88
!!! error TS2390: Constructor implementation is missing.
99
foo() { }
1010
}

Diff for: tests/baselines/reference/ClassDeclaration14.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ ClassDeclaration14.ts(3,4): error TS2390: Constructor implementation is missing.
88
~~~
99
!!! error TS2391: Function implementation is missing or not immediately following the declaration.
1010
constructor();
11-
~~~~~~~~~~~~~~
11+
~~~~~~~~~~~
1212
!!! error TS2390: Constructor implementation is missing.
1313
}

Diff for: tests/baselines/reference/ClassDeclaration8.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ ClassDeclaration8.ts(2,3): error TS2390: Constructor implementation is missing.
44
==== ClassDeclaration8.ts (1 errors) ====
55
class C {
66
constructor();
7-
~~~~~~~~~~~~~~
7+
~~~~~~~~~~~
88
!!! error TS2390: Constructor implementation is missing.
99
}

Diff for: tests/baselines/reference/bases.errors.txt

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ bases.ts(18,9): error TS2339: Property 'y' does not exist on type 'C'.
3535
!!! error TS2420: Property 'x' is missing in type 'C' but required in type 'I'.
3636
!!! related TS2728 bases.ts:2:5: 'x' is declared here.
3737
constructor() {
38-
~~~~~~~~~~~~~~~
38+
~~~~~~~~~~~
39+
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
3940
this.x: any;
40-
~~~~~~~~~~~~~~~~~~~~
4141
~~~~
4242
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
4343
~
@@ -47,8 +47,6 @@ bases.ts(18,9): error TS2339: Property 'y' does not exist on type 'C'.
4747
~~~
4848
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
4949
}
50-
~~~~~
51-
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
5250
}
5351

5452
new C().x;

Diff for: tests/baselines/reference/classConstructorOverloadsAccessibility.errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ classConstructorOverloadsAccessibility.ts(11,2): error TS2385: Overload signatur
66
==== classConstructorOverloadsAccessibility.ts (3 errors) ====
77
class A {
88
public constructor(a: boolean) // error
9-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
~~~~~~~~~~~~~~~~~~
1010
!!! error TS2385: Overload signatures must all be public, private or protected.
1111
protected constructor(a: number) // error
12-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
~~~~~~~~~~~~~~~~~~~~~
1313
!!! error TS2385: Overload signatures must all be public, private or protected.
1414
private constructor(a: string)
1515
private constructor() {
@@ -19,7 +19,7 @@ classConstructorOverloadsAccessibility.ts(11,2): error TS2385: Overload signatur
1919

2020
class B {
2121
protected constructor(a: number) // error
22-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22+
~~~~~~~~~~~~~~~~~~~~~
2323
!!! error TS2385: Overload signatures must all be public, private or protected.
2424
constructor(a: string)
2525
constructor() {

Diff for: tests/baselines/reference/classUpdateTests.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ classUpdateTests.ts(113,1): error TS1128: Declaration or statement expected.
5353

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

Diff for: tests/baselines/reference/classWithTwoConstructorDefinitions.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ classWithTwoConstructorDefinitions.ts(8,5): error TS2392: Multiple constructor i
77
==== classWithTwoConstructorDefinitions.ts (4 errors) ====
88
class C {
99
constructor() { } // error
10-
~~~~~~~~~~~~~~~~~
10+
~~~~~~~~~~~
1111
!!! error TS2392: Multiple constructor implementations are not allowed.
1212
constructor(x) { } // error
13-
~~~~~~~~~~~~~~~~~~
13+
~~~~~~~~~~~
1414
!!! error TS2392: Multiple constructor implementations are not allowed.
1515
}
1616

1717
class D<T> {
1818
constructor(x: T) { } // error
19-
~~~~~~~~~~~~~~~~~~~~~
19+
~~~~~~~~~~~
2020
!!! error TS2392: Multiple constructor implementations are not allowed.
2121
constructor(x: T, y: T) { } // error
22-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
22+
~~~~~~~~~~~
2323
!!! error TS2392: Multiple constructor implementations are not allowed.
2424
}

Diff for: tests/baselines/reference/constructorOverloads1.errors.txt

+6-10
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,21 @@ constructorOverloads1.ts(17,18): error TS2769: No overload matches this call.
1717
==== constructorOverloads1.ts (6 errors) ====
1818
class Foo {
1919
constructor(s: string);
20-
~~~~~~~~~~~~~~~~~~~~~~~
20+
~~~~~~~~~~~
2121
!!! error TS2392: Multiple constructor implementations are not allowed.
2222
constructor(n: number);
23-
~~~~~~~~~~~~~~~~~~~~~~~
23+
~~~~~~~~~~~
2424
!!! error TS2392: Multiple constructor implementations are not allowed.
2525
constructor(x: any) {
26-
~~~~~~~~~~~~~~~~~~~~~
27-
26+
~~~~~~~~~~~
27+
!!! error TS2392: Multiple constructor implementations are not allowed.
2828

2929
}
30-
~~~~~
31-
!!! error TS2392: Multiple constructor implementations are not allowed.
3230
constructor(x: any) {
33-
~~~~~~~~~~~~~~~~~~~~~
34-
31+
~~~~~~~~~~~
32+
!!! error TS2392: Multiple constructor implementations are not allowed.
3533

3634
}
37-
~~~~~
38-
!!! error TS2392: Multiple constructor implementations are not allowed.
3935
bar1() { /*WScript.Echo("bar1");*/ }
4036
bar2() { /*WScript.Echo("bar1");*/ }
4137
}

Diff for: tests/baselines/reference/constructorOverloads3.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ constructorOverloads3.ts(12,5): error TS2377: Constructors for derived classes m
1414
constructor(n: number);
1515
constructor(a: any);
1616
constructor(x: any, y?: any) { }
17-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17+
~~~~~~~~~~~
1818
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
1919
bar1() { /*WScript.Echo("Yo");*/}
2020
}

Diff for: tests/baselines/reference/constructorOverloads8.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ constructorOverloads8.ts(3,5): error TS2392: Multiple constructor implementation
55
==== constructorOverloads8.ts (2 errors) ====
66
class C {
77
constructor(x) { }
8-
~~~~~~~~~~~~~~~~~~
8+
~~~~~~~~~~~
99
!!! error TS2392: Multiple constructor implementations are not allowed.
1010
constructor(y, x) { } // illegal, 2 constructor implementations
11-
~~~~~~~~~~~~~~~~~~~~~
11+
~~~~~~~~~~~
1212
!!! error TS2392: Multiple constructor implementations are not allowed.
1313
}
1414

Diff for: tests/baselines/reference/constructorWithIncompleteTypeAnnotation.errors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ constructorWithIncompleteTypeAnnotation.ts(261,1): error TS1128: Declaration or
476476
private otherValue = 42;
477477

478478
constructor(private value: number, public name: string) : }
479-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
479+
~~~~~~~~~~~
480480
!!! error TS2390: Constructor implementation is missing.
481481
~~~~~~~~~~~~~~~~~~~~~
482482
!!! error TS2369: A parameter property is only allowed in a constructor implementation.

Diff for: tests/baselines/reference/constructorsWithSpecializedSignatures.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ constructorsWithSpecializedSignatures.ts(26,5): error TS2394: This overload sign
2121
class D {
2222
constructor(x: "hi");
2323
constructor(x: "foo");
24-
~~~~~~~~~~~~~~~~~~~~~~
24+
~~~~~~~~~~~
2525
!!! error TS2394: This overload signature is not compatible with its implementation signature.
2626
!!! related TS2750 constructorsWithSpecializedSignatures.ts:20:5: The implementation signature is declared here.
2727
constructor(x: number);
@@ -32,7 +32,7 @@ constructorsWithSpecializedSignatures.ts(26,5): error TS2394: This overload sign
3232
class D2 {
3333
constructor(x: "hi");
3434
constructor(x: "foo");
35-
~~~~~~~~~~~~~~~~~~~~~~
35+
~~~~~~~~~~~
3636
!!! error TS2394: This overload signature is not compatible with its implementation signature.
3737
!!! related TS2750 constructorsWithSpecializedSignatures.ts:28:5: The implementation signature is declared here.
3838
constructor(x: string);

Diff for: tests/baselines/reference/derivedClassConstructorWithoutSuperCall.errors.txt

+6-11
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ derivedClassConstructorWithoutSuperCall.ts(24,31): error TS2337: Super calls are
1414

1515
class Derived extends Base {
1616
constructor() { // error
17-
~~~~~~~~~~~~~~~~~~~~~~~~
18-
}
19-
~~~~~
17+
~~~~~~~~~~~
2018
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
19+
}
2120
}
2221

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

2726
class Derived2<T> extends Base2<T> {
2827
constructor() { // error for no super call (nested scopes don't count)
29-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28+
~~~~~~~~~~~
29+
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
3030
var r2 = () => super(); // error for misplaced super call (nested function)
31-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3231
~~~~~
3332
!!! error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors.
3433
}
35-
~~~~~
36-
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
3734
}
3835

3936
class Derived3<T> extends Base2<T> {
4037
constructor() { // error
41-
~~~~~~~~~~~~~~~~~~~~~~~~
38+
~~~~~~~~~~~
39+
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
4240
var r = function () { super() } // error
43-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4441
~~~~~
4542
!!! error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors.
4643
}
47-
~~~~~
48-
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
4944
}
5045

5146
class Derived4<T> extends Base2<T> {

Diff for: tests/baselines/reference/derivedClassParameterProperties.errors.txt

+4-12
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,16 @@ derivedClassParameterProperties.ts(81,9): error TS17009: 'super' must be called
6666
a = 1;
6767
b: number;
6868
constructor(y: string) {
69-
~~~~~~~~~~~~~~~~~~~~~~~~
69+
~~~~~~~~~~~
70+
!!! 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.
7071
this.a = 3;
71-
~~~~~~~~~~~~~~~~~~~
7272
~~~~
7373
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
7474
this.b = 3;
75-
~~~~~~~~~~~~~~~~~~~
7675
~~~~
7776
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
7877
super();
79-
~~~~~~~~~~~~~~~~
8078
}
81-
~~~~~
82-
!!! 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.
8379
}
8480

8581
class Derived8 extends Base {
@@ -99,20 +95,16 @@ derivedClassParameterProperties.ts(81,9): error TS17009: 'super' must be called
9995
a = 1;
10096
b: number;
10197
constructor(y: string) {
102-
~~~~~~~~~~~~~~~~~~~~~~~~
98+
~~~~~~~~~~~
99+
!!! 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.
103100
this.a = 3;
104-
~~~~~~~~~~~~~~~~~~~
105101
~~~~
106102
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
107103
this.b = 3;
108-
~~~~~~~~~~~~~~~~~~~
109104
~~~~
110105
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
111106
super();
112-
~~~~~~~~~~~~~~~~
113107
}
114-
~~~~~
115-
!!! 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.
116108
}
117109

118110
class Derived10<T> extends Base2<T> {

0 commit comments

Comments
 (0)