Skip to content

Commit 585d9bd

Browse files
committed
Condense all error spans to just the name for variable, class, interface, module, enum and enum member
1 parent 3580a99 commit 585d9bd

File tree

149 files changed

+436
-660
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+436
-660
lines changed

src/compiler/parser.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ module ts {
7171
}
7272

7373
export function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): Diagnostic {
74+
node = getErrorSpanForNode(node);
7475
var file = getSourceFileOfNode(node);
7576
var start = skipTrivia(file.text, node.pos);
7677
var length = node.end - start;
@@ -79,18 +80,33 @@ module ts {
7980
}
8081

8182
export function createDiagnosticForNodeFromMessageChain(node: Node, messageChain: DiagnosticMessageChain): Diagnostic {
83+
node = getErrorSpanForNode(node);
8284
var file = getSourceFileOfNode(node);
8385
var start = skipTrivia(file.text, node.pos);
8486
var length = node.end - start;
8587
return flattenDiagnosticChain(file, start, length, messageChain);
8688
}
8789

88-
export function getErrorSpanForNode(node: Node): TextRange {
89-
var errorSpan: TextRange;
90+
export function getErrorSpanForNode(node: Node): Node {
91+
var errorSpan: Node;
9092
switch (node.kind) {
9193
// This list is a work in progress. Add missing node kinds to improve their error
9294
// spans.
95+
case SyntaxKind.VariableDeclaration:
96+
case SyntaxKind.ClassDeclaration:
97+
case SyntaxKind.InterfaceDeclaration:
98+
case SyntaxKind.ModuleDeclaration:
99+
case SyntaxKind.EnumDeclaration:
100+
case SyntaxKind.EnumMember:
101+
errorSpan = (<Declaration>node).name;
102+
break;
93103
}
104+
105+
// We now have the ideal error span, but it may be a node that is optional and absent
106+
// (e.g. the name of a function expression), in which case errorSpan will be undefined.
107+
// Alternatively, it might be required and missing (e.g. the name of a module), in which
108+
// case its pos will equal its end (length 0). In either of these cases, we should fall
109+
// back to the original node that the error was issued on.
94110
return errorSpan && errorSpan.pos < errorSpan.end ? errorSpan : node;
95111
}
96112

@@ -3150,9 +3166,9 @@ module ts {
31503166
var firstExternalModule = forEach(files, f => isExternalModule(f) ? f : undefined);
31513167
if (firstExternalModule && options.module === ModuleKind.None) {
31523168
// We cannot use createDiagnosticFromNode because nodes do not have parents yet
3153-
var externalModuleIndicatorNode = firstExternalModule.externalModuleIndicator;
3154-
var errorStart = skipTrivia(firstExternalModule.text, externalModuleIndicatorNode.pos);
3155-
var errorLength = externalModuleIndicatorNode.end - errorStart;
3169+
var externalModuleErrorSpan = getErrorSpanForNode(firstExternalModule.externalModuleIndicator);
3170+
var errorStart = skipTrivia(firstExternalModule.text, externalModuleErrorSpan.pos);
3171+
var errorLength = externalModuleErrorSpan.end - errorStart;
31563172
errors.push(createFileDiagnostic(firstExternalModule, errorStart, errorLength, Diagnostics.Cannot_compile_external_modules_unless_the_module_flag_is_provided));
31573173
}
31583174

tests/baselines/reference/ExportAssignment7.errors.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
==== tests/cases/compiler/ExportAssignment7.ts (3 errors) ====
22
export class C {
3-
~~~~~~~~~~~~~~~~
4-
}
5-
~
3+
~
64
!!! Cannot compile external modules unless the '--module' flag is provided.
5+
}
76

87
export = B;
98
~~~~~~~~~~~

tests/baselines/reference/TwoInternalModulesThatMergeEachWithExportedLocalVarsOfTheSameName.errors.txt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,20 @@
11
==== tests/cases/conformance/internalModules/DeclarationMerging/part1.ts (1 errors) ====
22
export module A {
3-
~~~~~~~~~~~~~~~~~
3+
~
4+
!!! Cannot compile external modules unless the '--module' flag is provided.
45
export interface Point {
5-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66
x: number;
7-
~~~~~~~~~~~~~~~~~~
87
y: number;
9-
~~~~~~~~~~~~~~~~~~
108
}
11-
~~~~~
12-
139

1410
export module Utils {
15-
~~~~~~~~~~~~~~~~~~~~~~~~~
1611
export function mirror<T extends Point>(p: T) {
17-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1812
return { x: p.y, y: p.x };
19-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2013
}
21-
~~~~~~~~~
2214
}
23-
~~~~~
24-
2515

2616
export var Origin: Point = { x: 0, y: 0 };
27-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2817
}
29-
~
30-
!!! Cannot compile external modules unless the '--module' flag is provided.
3118

3219
==== tests/cases/conformance/internalModules/DeclarationMerging/part2.ts (3 errors) ====
3320
export module A {

tests/baselines/reference/ambiguousOverload.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
function foof(bar: any): any { return bar };
55
var x: number = foof("s", null);
66
var y: string = foof("s", null);
7-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
7+
~
88
!!! Type 'number' is not assignable to type 'string'.
99

1010
function foof2(bar: string, x): string;
1111
function foof2(bar: string, y): number;
1212
function foof2(bar: any): any { return bar };
1313
var x2: string = foof2("s", null);
1414
var y2: number = foof2("s", null);
15-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15+
~~
1616
!!! Type 'string' is not assignable to type 'number'.

tests/baselines/reference/arrayAssignmentTest1.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@
4545
var arr_c3: C3[] = [];
4646

4747
var i1_error: I1 = []; // should be an error - is
48-
~~~~~~~~~~~~~~~~~
48+
~~~~~~~~
4949
!!! Type 'undefined[]' is not assignable to type 'I1':
5050
!!! Property 'IM1' is missing in type 'undefined[]'.
5151
var c1_error: C1 = []; // should be an error - is
52-
~~~~~~~~~~~~~~~~~
52+
~~~~~~~~
5353
!!! Type 'undefined[]' is not assignable to type 'C1':
5454
!!! Property 'IM1' is missing in type 'undefined[]'.
5555
var c2_error: C2 = []; // should be an error - is
56-
~~~~~~~~~~~~~~~~~
56+
~~~~~~~~
5757
!!! Type 'undefined[]' is not assignable to type 'C2':
5858
!!! Property 'C2M1' is missing in type 'undefined[]'.
5959
var c3_error: C3 = []; // should be an error - is
60-
~~~~~~~~~~~~~~~~~
60+
~~~~~~~~
6161
!!! Type 'undefined[]' is not assignable to type 'C3':
6262
!!! Property 'CM3M1' is missing in type 'undefined[]'.
6363

tests/baselines/reference/arrayAssignmentTest5.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
public onEnter(line:string, state:IState, offset:number):IAction {
2323
var lineTokens:ILineTokens= this.tokenize(line, state, true);
2424
var tokens:IStateToken[]= lineTokens.tokens;
25-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25+
~~~~~~
2626
!!! Type 'IToken[]' is not assignable to type 'IStateToken[]':
2727
!!! Type 'IToken' is not assignable to type 'IStateToken':
2828
!!! Property 'state' is missing in type 'IToken'.

tests/baselines/reference/arraySigChecking.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
}
2020
var myVar: myInt;
2121
var strArray: string[] = [myVar.voidFn()];
22-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22+
~~~~~~~~
2323
!!! Type 'void[]' is not assignable to type 'string[]':
2424
!!! Type 'void' is not assignable to type 'string'.
2525

tests/baselines/reference/arrayTypeOfTypeOf.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
!!! '=' expected.
1010
~
1111
!!! Expression expected.
12-
~~~~~~~~~~~~~~~~~~~~~~~~~
12+
~~~
1313
!!! Type 'number' is not assignable to type '{ (arrayLength?: number): any[]; <T>(arrayLength: number): T[]; <T>(...items: T[]): T[]; new (arrayLength?: number): any[]; new <T>(arrayLength: number): T[]; new <T>(...items: T[]): T[]; isArray(arg: any): boolean; prototype: any[]; }':
1414
!!! Property 'isArray' is missing in type 'Number'.
1515
var xs4: typeof Array<typeof x>;
1616
~
1717
!!! '=' expected.
1818
~
1919
!!! Expression expected.
20-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
20+
~~~
2121
!!! Type 'number' is not assignable to type '{ (arrayLength?: number): any[]; <T>(arrayLength: number): T[]; <T>(...items: T[]): T[]; new (arrayLength?: number): any[]; new <T>(arrayLength: number): T[]; new <T>(...items: T[]): T[]; isArray(arg: any): boolean; prototype: any[]; }'.

tests/baselines/reference/assignmentCompatBug2.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
==== tests/cases/compiler/assignmentCompatBug2.ts (5 errors) ====
22
var b2: { b: number;} = { a: 0 }; // error
3-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
~~
44
!!! Type '{ a: number; }' is not assignable to type '{ b: number; }':
55
!!! Property 'b' is missing in type '{ a: number; }'.
66

tests/baselines/reference/assignmentToObject.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var a = { toString: 5 };
33
var b: {} = a; // ok
44
var c: Object = a; // should be error
5-
~~~~~~~~~~~~~
5+
~
66
!!! Type '{ toString: number; }' is not assignable to type 'Object':
77
!!! Types of property 'toString' are incompatible:
88
!!! Type 'number' is not assignable to type '() => string'.

tests/baselines/reference/assignmentToObjectAndFunction.errors.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
==== tests/cases/compiler/assignmentToObjectAndFunction.ts (3 errors) ====
22
var errObj: Object = { toString: 0 }; // Error, incompatible toString
3-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
~~~~~~
44
!!! Type '{ toString: number; }' is not assignable to type 'Object':
55
!!! Types of property 'toString' are incompatible:
66
!!! Type 'number' is not assignable to type '() => string'.
@@ -11,7 +11,7 @@
1111
}; // Ok, because toString is a subtype of Object's toString
1212

1313
var errFun: Function = {}; // Error for no call signature
14-
~~~~~~~~~~~~~~~~~~~~~
14+
~~~~~~
1515
!!! Type '{}' is not assignable to type 'Function':
1616
!!! Property 'apply' is missing in type '{}'.
1717

@@ -35,7 +35,7 @@
3535
}
3636

3737
var badFundule: Function = bad; // error
38-
~~~~~~~~~~~~~~~~~~~~~~~~~~
38+
~~~~~~~~~~
3939
!!! Type 'typeof bad' is not assignable to type 'Function':
4040
!!! Types of property 'apply' are incompatible:
4141
!!! Type 'number' is not assignable to type '(thisArg: any, argArray?: any) => any'.

tests/baselines/reference/augmentedTypeAssignmentCompatIndexSignature.errors.txt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,15 @@
1414
var f = () => { };
1515

1616
var v1: {
17-
~~~~~
18-
[n: number]: Foo
19-
~~~~~~~~~~~~~~~~~~~~
20-
} = o; // Should be allowed
21-
~~~~~
17+
~~
2218
!!! Type '{}' is not assignable to type '{ [x: number]: Foo; }':
2319
!!! Index signature is missing in type '{}'.
20+
[n: number]: Foo
21+
} = o; // Should be allowed
2422

2523
var v2: {
26-
~~~~~
27-
[n: number]: Bar
28-
~~~~~~~~~~~~~~~~~~~~
29-
} = f; // Should be allowed
30-
~~~~~
24+
~~
3125
!!! Type '() => void' is not assignable to type '{ [x: number]: Bar; }':
32-
!!! Index signature is missing in type '() => void'.
26+
!!! Index signature is missing in type '() => void'.
27+
[n: number]: Bar
28+
} = f; // Should be allowed

tests/baselines/reference/callOverloadViaElementAccessExpression.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
var c = new C();
1111
var r: string = c['foo'](1);
12-
~~~~~~~~~~~~~~~~~~~~~~~
12+
~
1313
!!! Type 'number' is not assignable to type 'string'.
1414
var r2: number = c['foo']('');
15-
~~~~~~~~~~~~~~~~~~~~~~~~~
15+
~~
1616
!!! Type 'string' is not assignable to type 'number'.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
==== tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsItself.ts (3 errors) ====
22
class C extends C { } // error
3-
~~~~~~~~~~~~~~~~~~~~~
3+
~
44
!!! Type 'C' recursively references itself as a base type.
55

66
class D<T> extends D<T> { } // error
7-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
7+
~
88
!!! Type 'D<T>' recursively references itself as a base type.
99

1010
class E<T> extends E<string> { } // error
11-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
~
1212
!!! Type 'E<T>' recursively references itself as a base type.

tests/baselines/reference/classExtendsItselfIndirectly.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
==== tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsItselfIndirectly.ts (2 errors) ====
22
class C extends E { foo: string; } // error
3-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
~
44
!!! Type 'C' recursively references itself as a base type.
55

66
class D extends C { bar: string; }
77

88
class E extends D { baz: number; }
99

1010
class C2<T> extends E2<T> { foo: T; } // error
11-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
~~
1212
!!! Type 'C2<T>' recursively references itself as a base type.
1313

1414
class D2<T> extends C2<T> { bar: T; }

tests/baselines/reference/classExtendsItselfIndirectly2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
==== tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsItselfIndirectly2.ts (2 errors) ====
22
class C extends N.E { foo: string; } // error
3-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
~
44
!!! Type 'C' recursively references itself as a base type.
55

66
module M {
@@ -14,7 +14,7 @@
1414

1515
module O {
1616
class C2<T> extends Q.E2<T> { foo: T; } // error
17-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17+
~~
1818
!!! Type 'C2<T>' recursively references itself as a base type.
1919

2020
module P {

tests/baselines/reference/classExtendsItselfIndirectly3.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
==== tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsItselfIndirectly_file1.ts (1 errors) ====
22
class C extends E { foo: string; } // error
3-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
~
44
!!! Type 'C' recursively references itself as a base type.
55

66
==== tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsItselfIndirectly_file2.ts (0 errors) ====
@@ -11,7 +11,7 @@
1111

1212
==== tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsItselfIndirectly_file4.ts (1 errors) ====
1313
class C2<T> extends E2<T> { foo: T; } // error
14-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
~~
1515
!!! Type 'C2<T>' recursively references itself as a base type.
1616

1717
==== tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsItselfIndirectly_file5.ts (0 errors) ====
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
==== tests/cases/compiler/classInheritence.ts (1 errors) ====
22
class B extends A { }
33
class A extends A { }
4-
~~~~~~~~~~~~~~~~~~~~~
4+
~
55
!!! Type 'A' recursively references itself as a base type.

tests/baselines/reference/classMemberInitializerWithLamdaScoping3.errors.txt

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,15 @@
66
log(msg?: any): void;
77
};
88
export class Test1 {
9-
~~~~~~~~~~~~~~~~~~~~
9+
~~~~~
10+
!!! Cannot compile external modules unless the '--module' flag is provided.
1011
constructor(private field1: string) {
11-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1212
}
13-
~~~~~
1413
messageHandler = () => {
15-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1614
console.log(field1); // But this should be error as the field1 will resolve to var field1
17-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1815
~~~~~~
1916
!!! Initializer of instance member variable 'messageHandler' cannot reference identifier 'field1' declared in the constructor.
2017
// but since this code would be generated inside constructor, in generated js
21-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2218
// it would resolve to private field1 and thats not what user intended here.
23-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2419
};
25-
~~~~~~
26-
}
27-
~
28-
!!! Cannot compile external modules unless the '--module' flag is provided.
20+
}

tests/baselines/reference/classSideInheritance3.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
}
1616

1717
var r1: typeof A = B; // error
18-
~~~~~~~~~~~~~~~~
18+
~~
1919
!!! Type 'typeof B' is not assignable to type 'typeof A'.
2020
var r2: new (x: string) => A = B; // error
21-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21+
~~
2222
!!! Type 'typeof B' is not assignable to type 'new (x: string) => A'.
2323
var r3: typeof A = C; // ok

0 commit comments

Comments
 (0)