Skip to content

Commit

Permalink
Condense all error spans to just the name for variable, class, interf…
Browse files Browse the repository at this point in the history
…ace, module, enum and enum member
  • Loading branch information
JsonFreeman committed Jul 21, 2014
1 parent 3580a99 commit 585d9bd
Show file tree
Hide file tree
Showing 149 changed files with 436 additions and 660 deletions.
26 changes: 21 additions & 5 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module ts {
}

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

export function createDiagnosticForNodeFromMessageChain(node: Node, messageChain: DiagnosticMessageChain): Diagnostic {
node = getErrorSpanForNode(node);
var file = getSourceFileOfNode(node);
var start = skipTrivia(file.text, node.pos);
var length = node.end - start;
return flattenDiagnosticChain(file, start, length, messageChain);
}

export function getErrorSpanForNode(node: Node): TextRange {
var errorSpan: TextRange;
export function getErrorSpanForNode(node: Node): Node {
var errorSpan: Node;
switch (node.kind) {
// This list is a work in progress. Add missing node kinds to improve their error
// spans.
case SyntaxKind.VariableDeclaration:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.EnumMember:
errorSpan = (<Declaration>node).name;
break;
}

// We now have the ideal error span, but it may be a node that is optional and absent
// (e.g. the name of a function expression), in which case errorSpan will be undefined.
// Alternatively, it might be required and missing (e.g. the name of a module), in which
// case its pos will equal its end (length 0). In either of these cases, we should fall
// back to the original node that the error was issued on.
return errorSpan && errorSpan.pos < errorSpan.end ? errorSpan : node;
}

Expand Down Expand Up @@ -3150,9 +3166,9 @@ module ts {
var firstExternalModule = forEach(files, f => isExternalModule(f) ? f : undefined);
if (firstExternalModule && options.module === ModuleKind.None) {
// We cannot use createDiagnosticFromNode because nodes do not have parents yet
var externalModuleIndicatorNode = firstExternalModule.externalModuleIndicator;
var errorStart = skipTrivia(firstExternalModule.text, externalModuleIndicatorNode.pos);
var errorLength = externalModuleIndicatorNode.end - errorStart;
var externalModuleErrorSpan = getErrorSpanForNode(firstExternalModule.externalModuleIndicator);
var errorStart = skipTrivia(firstExternalModule.text, externalModuleErrorSpan.pos);
var errorLength = externalModuleErrorSpan.end - errorStart;
errors.push(createFileDiagnostic(firstExternalModule, errorStart, errorLength, Diagnostics.Cannot_compile_external_modules_unless_the_module_flag_is_provided));
}

Expand Down
5 changes: 2 additions & 3 deletions tests/baselines/reference/ExportAssignment7.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
==== tests/cases/compiler/ExportAssignment7.ts (3 errors) ====
export class C {
~~~~~~~~~~~~~~~~
}
~
~
!!! Cannot compile external modules unless the '--module' flag is provided.
}

export = B;
~~~~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
==== tests/cases/conformance/internalModules/DeclarationMerging/part1.ts (1 errors) ====
export module A {
~~~~~~~~~~~~~~~~~
~
!!! Cannot compile external modules unless the '--module' flag is provided.
export interface Point {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x: number;
~~~~~~~~~~~~~~~~~~
y: number;
~~~~~~~~~~~~~~~~~~
}
~~~~~


export module Utils {
~~~~~~~~~~~~~~~~~~~~~~~~~
export function mirror<T extends Point>(p: T) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
return { x: p.y, y: p.x };
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
~~~~~~~~~
}
~~~~~


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

==== tests/cases/conformance/internalModules/DeclarationMerging/part2.ts (3 errors) ====
export module A {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ambiguousOverload.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
function foof(bar: any): any { return bar };
var x: number = foof("s", null);
var y: string = foof("s", null);
~~~~~~~~~~~~~~~~~~~~~~~~~~~
~
!!! Type 'number' is not assignable to type 'string'.

function foof2(bar: string, x): string;
function foof2(bar: string, y): number;
function foof2(bar: any): any { return bar };
var x2: string = foof2("s", null);
var y2: number = foof2("s", null);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
!!! Type 'string' is not assignable to type 'number'.
8 changes: 4 additions & 4 deletions tests/baselines/reference/arrayAssignmentTest1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@
var arr_c3: C3[] = [];

var i1_error: I1 = []; // should be an error - is
~~~~~~~~~~~~~~~~~
~~~~~~~~
!!! Type 'undefined[]' is not assignable to type 'I1':
!!! Property 'IM1' is missing in type 'undefined[]'.
var c1_error: C1 = []; // should be an error - is
~~~~~~~~~~~~~~~~~
~~~~~~~~
!!! Type 'undefined[]' is not assignable to type 'C1':
!!! Property 'IM1' is missing in type 'undefined[]'.
var c2_error: C2 = []; // should be an error - is
~~~~~~~~~~~~~~~~~
~~~~~~~~
!!! Type 'undefined[]' is not assignable to type 'C2':
!!! Property 'C2M1' is missing in type 'undefined[]'.
var c3_error: C3 = []; // should be an error - is
~~~~~~~~~~~~~~~~~
~~~~~~~~
!!! Type 'undefined[]' is not assignable to type 'C3':
!!! Property 'CM3M1' is missing in type 'undefined[]'.

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/arrayAssignmentTest5.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public onEnter(line:string, state:IState, offset:number):IAction {
var lineTokens:ILineTokens= this.tokenize(line, state, true);
var tokens:IStateToken[]= lineTokens.tokens;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~
!!! Type 'IToken[]' is not assignable to type 'IStateToken[]':
!!! Type 'IToken' is not assignable to type 'IStateToken':
!!! Property 'state' is missing in type 'IToken'.
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/arraySigChecking.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
var myVar: myInt;
var strArray: string[] = [myVar.voidFn()];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~
!!! Type 'void[]' is not assignable to type 'string[]':
!!! Type 'void' is not assignable to type 'string'.

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayTypeOfTypeOf.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
!!! '=' expected.
~
!!! Expression expected.
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
!!! 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[]; }':
!!! Property 'isArray' is missing in type 'Number'.
var xs4: typeof Array<typeof x>;
~
!!! '=' expected.
~
!!! Expression expected.
~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
!!! 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[]; }'.
2 changes: 1 addition & 1 deletion tests/baselines/reference/assignmentCompatBug2.errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
==== tests/cases/compiler/assignmentCompatBug2.ts (5 errors) ====
var b2: { b: number;} = { a: 0 }; // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
!!! Type '{ a: number; }' is not assignable to type '{ b: number; }':
!!! Property 'b' is missing in type '{ a: number; }'.

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/assignmentToObject.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var a = { toString: 5 };
var b: {} = a; // ok
var c: Object = a; // should be error
~~~~~~~~~~~~~
~
!!! Type '{ toString: number; }' is not assignable to type 'Object':
!!! Types of property 'toString' are incompatible:
!!! Type 'number' is not assignable to type '() => string'.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
==== tests/cases/compiler/assignmentToObjectAndFunction.ts (3 errors) ====
var errObj: Object = { toString: 0 }; // Error, incompatible toString
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~
!!! Type '{ toString: number; }' is not assignable to type 'Object':
!!! Types of property 'toString' are incompatible:
!!! Type 'number' is not assignable to type '() => string'.
Expand All @@ -11,7 +11,7 @@
}; // Ok, because toString is a subtype of Object's toString

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

Expand All @@ -35,7 +35,7 @@
}

var badFundule: Function = bad; // error
~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~
!!! Type 'typeof bad' is not assignable to type 'Function':
!!! Types of property 'apply' are incompatible:
!!! Type 'number' is not assignable to type '(thisArg: any, argArray?: any) => any'.
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@
var f = () => { };

var v1: {
~~~~~
[n: number]: Foo
~~~~~~~~~~~~~~~~~~~~
} = o; // Should be allowed
~~~~~
~~
!!! Type '{}' is not assignable to type '{ [x: number]: Foo; }':
!!! Index signature is missing in type '{}'.
[n: number]: Foo
} = o; // Should be allowed

var v2: {
~~~~~
[n: number]: Bar
~~~~~~~~~~~~~~~~~~~~
} = f; // Should be allowed
~~~~~
~~
!!! Type '() => void' is not assignable to type '{ [x: number]: Bar; }':
!!! Index signature is missing in type '() => void'.
!!! Index signature is missing in type '() => void'.
[n: number]: Bar
} = f; // Should be allowed
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

var c = new C();
var r: string = c['foo'](1);
~~~~~~~~~~~~~~~~~~~~~~~
~
!!! Type 'number' is not assignable to type 'string'.
var r2: number = c['foo']('');
~~~~~~~~~~~~~~~~~~~~~~~~~
~~
!!! Type 'string' is not assignable to type 'number'.
6 changes: 3 additions & 3 deletions tests/baselines/reference/classExtendsItself.errors.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
==== tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsItself.ts (3 errors) ====
class C extends C { } // error
~~~~~~~~~~~~~~~~~~~~~
~
!!! Type 'C' recursively references itself as a base type.

class D<T> extends D<T> { } // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~
~
!!! Type 'D<T>' recursively references itself as a base type.

class E<T> extends E<string> { } // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~
!!! Type 'E<T>' recursively references itself as a base type.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
==== tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsItselfIndirectly.ts (2 errors) ====
class C extends E { foo: string; } // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~
!!! Type 'C' recursively references itself as a base type.

class D extends C { bar: string; }

class E extends D { baz: number; }

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

class D2<T> extends C2<T> { bar: T; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
==== tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsItselfIndirectly2.ts (2 errors) ====
class C extends N.E { foo: string; } // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~
!!! Type 'C' recursively references itself as a base type.

module M {
Expand All @@ -14,7 +14,7 @@

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

module P {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
==== tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsItselfIndirectly_file1.ts (1 errors) ====
class C extends E { foo: string; } // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~
!!! Type 'C' recursively references itself as a base type.

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

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

==== tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendsItselfIndirectly_file5.ts (0 errors) ====
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/classInheritence.errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
==== tests/cases/compiler/classInheritence.ts (1 errors) ====
class B extends A { }
class A extends A { }
~~~~~~~~~~~~~~~~~~~~~
~
!!! Type 'A' recursively references itself as a base type.
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@
log(msg?: any): void;
};
export class Test1 {
~~~~~~~~~~~~~~~~~~~~
~~~~~
!!! Cannot compile external modules unless the '--module' flag is provided.
constructor(private field1: string) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
~~~~~
messageHandler = () => {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
console.log(field1); // But this should be error as the field1 will resolve to var field1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~
!!! Initializer of instance member variable 'messageHandler' cannot reference identifier 'field1' declared in the constructor.
// but since this code would be generated inside constructor, in generated js
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// it would resolve to private field1 and thats not what user intended here.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
};
~~~~~~
}
~
!!! Cannot compile external modules unless the '--module' flag is provided.
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/classSideInheritance3.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
}

var r1: typeof A = B; // error
~~~~~~~~~~~~~~~~
~~
!!! Type 'typeof B' is not assignable to type 'typeof A'.
var r2: new (x: string) => A = B; // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
!!! Type 'typeof B' is not assignable to type 'new (x: string) => A'.
var r3: typeof A = C; // ok
Loading

0 comments on commit 585d9bd

Please sign in to comment.