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

src/compiler/utilities.ts

Lines changed: 11 additions & 0 deletions
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) {

tests/baselines/reference/ClassDeclaration10.errors.txt

Lines changed: 1 addition & 1 deletion
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
~~~

tests/baselines/reference/ClassDeclaration11.errors.txt

Lines changed: 1 addition & 1 deletion
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
}

tests/baselines/reference/ClassDeclaration14.errors.txt

Lines changed: 1 addition & 1 deletion
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
}

tests/baselines/reference/ClassDeclaration8.errors.txt

Lines changed: 1 addition & 1 deletion
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
}

tests/baselines/reference/bases.errors.txt

Lines changed: 2 additions & 4 deletions
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;

tests/baselines/reference/classConstructorOverloadsAccessibility.errors.txt

Lines changed: 3 additions & 3 deletions
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() {

tests/baselines/reference/classUpdateTests.errors.txt

Lines changed: 1 addition & 1 deletion
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

tests/baselines/reference/classWithTwoConstructorDefinitions.errors.txt

Lines changed: 4 additions & 4 deletions
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
}

tests/baselines/reference/constructorOverloads1.errors.txt

Lines changed: 6 additions & 10 deletions
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
}

0 commit comments

Comments
 (0)