From 9f3d83adebe5c5700b72aa2581debe2d2f7edc73 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 8 Sep 2014 16:14:43 -0700 Subject: [PATCH] Truncate long types in error messages --- src/compiler/checker.ts | 28 +++++++++++++++---- src/compiler/types.ts | 10 +++---- src/harness/typeWriter.ts | 2 +- .../reference/arrayTypeOfTypeOf.errors.txt | 4 +-- ...gnmentCompatWithCallSignatures4.errors.txt | 4 +-- ...tCompatWithConstructSignatures4.errors.txt | 12 ++++---- ...tureAssignabilityInInheritance3.errors.txt | 2 +- ...tureAssignabilityInInheritance3.errors.txt | 2 +- ...errorMessageOnObjectLiteralType.errors.txt | 2 +- ...rConstrainsPropertyDeclarations.errors.txt | 2 +- .../reference/propertyAccess.errors.txt | 2 +- .../reference/redefineArray.errors.txt | 2 +- ...rConstrainsPropertyDeclarations.errors.txt | 2 +- .../baselines/reference/typeName1.errors.txt | 4 +-- 14 files changed, 46 insertions(+), 32 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ab8e3655541..007a935a4ae 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -943,20 +943,36 @@ module ts { writer.write(symbolToString(symbol, enclosingDeclaration, meaning)); } - function createSingleLineTextWriter() { + function createSingleLineTextWriter(maxLength?: number) { var result = ""; + var overflow = false; + function write(s: string) { + if (!overflow) { + result += s; + if (result.length > maxLength) { + result = result.substr(0, maxLength - 3) + "..."; + overflow = true; + } + } + } return { - write(s: string) { result += s; }, - writeSymbol(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags) { writeSymbolToTextWriter(symbol, enclosingDeclaration, meaning, this); }, - writeLine() { result += " "; }, + write: write, + writeSymbol(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags) { + writeSymbolToTextWriter(symbol, enclosingDeclaration, meaning, this); + }, + writeLine() { + write(" "); + }, increaseIndent() { }, decreaseIndent() { }, - getText() { return result; } + getText() { + return result; + } }; } function typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string { - var stringWriter = createSingleLineTextWriter(); + var stringWriter = createSingleLineTextWriter(flags & TypeFormatFlags.NoTruncation ? undefined : 100); // TODO(shkamat): typeToString should take enclosingDeclaration as input, once we have implemented enclosingDeclaration writeTypeToTextWriter(type, enclosingDeclaration, flags, stringWriter); return stringWriter.getText(); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 922db40cdec..c156e3181c1 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -631,12 +631,10 @@ module ts { } export enum TypeFormatFlags { - None = 0x00000000, - - /** writes Array instead T[] */ - WriteArrayAsGenericType = 0x00000001, // Declarations - - UseTypeOfFunction = 0x00000002, // instead of writing signature type of function use typeof + None = 0x00000000, + WriteArrayAsGenericType = 0x00000001, // Write Array instead T[] + UseTypeOfFunction = 0x00000002, // Write typeof instead of function type literal + NoTruncation = 0x00000004, // Don't truncate typeToString result } export enum SymbolAccessibility { diff --git a/src/harness/typeWriter.ts b/src/harness/typeWriter.ts index a3b23d10f14..8fac5e45dab 100644 --- a/src/harness/typeWriter.ts +++ b/src/harness/typeWriter.ts @@ -86,7 +86,7 @@ class TypeWriterWalker { column: lineAndCharacter.character, syntaxKind: ts.SyntaxKind[node.kind], sourceText: sourceText, - type: this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.None) + type: this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation) }); } diff --git a/tests/baselines/reference/arrayTypeOfTypeOf.errors.txt b/tests/baselines/reference/arrayTypeOfTypeOf.errors.txt index cb877f5e57b..c0a826e06a0 100644 --- a/tests/baselines/reference/arrayTypeOfTypeOf.errors.txt +++ b/tests/baselines/reference/arrayTypeOfTypeOf.errors.txt @@ -10,7 +10,7 @@ ~ !!! Expression expected. ~~~ -!!! Type 'number' is not assignable to type '{ (arrayLength?: number): any[]; (arrayLength: number): T[]; (...items: T[]): T[]; new (arrayLength?: number): any[]; new (arrayLength: number): T[]; new (...items: T[]): T[]; isArray(arg: any): boolean; prototype: any[]; }': +!!! Type 'number' is not assignable to type '{ (arrayLength?: number): any[]; (arrayLength: number): T[]; (...items: T[]): T[]; new (arr...': !!! Property 'isArray' is missing in type 'Number'. var xs4: typeof Array; ~ @@ -18,4 +18,4 @@ ~ !!! Expression expected. ~~~ -!!! Type 'number' is not assignable to type '{ (arrayLength?: number): any[]; (arrayLength: number): T[]; (...items: T[]): T[]; new (arrayLength?: number): any[]; new (arrayLength: number): T[]; new (...items: T[]): T[]; isArray(arg: any): boolean; prototype: any[]; }'. \ No newline at end of file +!!! Type 'number' is not assignable to type '{ (arrayLength?: number): any[]; (arrayLength: number): T[]; (...items: T[]): T[]; new (arr...'. \ No newline at end of file diff --git a/tests/baselines/reference/assignmentCompatWithCallSignatures4.errors.txt b/tests/baselines/reference/assignmentCompatWithCallSignatures4.errors.txt index 973dfe92ca2..5fc04ea8d90 100644 --- a/tests/baselines/reference/assignmentCompatWithCallSignatures4.errors.txt +++ b/tests/baselines/reference/assignmentCompatWithCallSignatures4.errors.txt @@ -52,7 +52,7 @@ var b8: (x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U; a8 = b8; // error, { foo: number } and Base are incompatible ~~ -!!! Type '(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U' is not assignable to type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived': +!!! Type '(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T)...' is not assignable to type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived': !!! Types of parameters 'y' and 'y' are incompatible: !!! Type '(arg2: { foo: number; }) => any' is not assignable to type '(arg2: Base) => Derived': !!! Types of parameters 'arg2' and 'arg2' are incompatible: @@ -61,7 +61,7 @@ !!! Type 'number' is not assignable to type 'string'. b8 = a8; // error, { foo: number } and Base are incompatible ~~ -!!! Type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type '(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U': +!!! Type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type '(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T)...': !!! Types of parameters 'y' and 'y' are incompatible: !!! Type '(arg2: Base) => Derived' is not assignable to type '(arg2: { foo: number; }) => any': !!! Types of parameters 'arg2' and 'arg2' are incompatible: diff --git a/tests/baselines/reference/assignmentCompatWithConstructSignatures4.errors.txt b/tests/baselines/reference/assignmentCompatWithConstructSignatures4.errors.txt index c5055b16e89..9c895e57f08 100644 --- a/tests/baselines/reference/assignmentCompatWithConstructSignatures4.errors.txt +++ b/tests/baselines/reference/assignmentCompatWithConstructSignatures4.errors.txt @@ -52,7 +52,7 @@ var b8: new (x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U; a8 = b8; // error, type mismatch ~~ -!!! Type 'new (x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U' is not assignable to type 'new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived': +!!! Type 'new (x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r...' is not assignable to type 'new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived': !!! Types of parameters 'y' and 'y' are incompatible: !!! Type '(arg2: { foo: number; }) => any' is not assignable to type '(arg2: Base) => Derived': !!! Types of parameters 'arg2' and 'arg2' are incompatible: @@ -61,7 +61,7 @@ !!! Type 'number' is not assignable to type 'string'. b8 = a8; // error ~~ -!!! Type 'new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type 'new (x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U': +!!! Type 'new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type 'new (x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r...': !!! Types of parameters 'y' and 'y' are incompatible: !!! Type '(arg2: Base) => Derived' is not assignable to type '(arg2: { foo: number; }) => any': !!! Types of parameters 'arg2' and 'arg2' are incompatible: @@ -93,24 +93,24 @@ var b16: new (x: (a: T) => T) => T[]; a16 = b16; // error ~~~ -!!! Type 'new (x: (a: T) => T) => T[]' is not assignable to type '{ new (x: { new (a: number): number; new (a?: number): number; }): number[]; new (x: { new (a: boolean): boolean; new (a?: boolean): boolean; }): boolean[]; }': +!!! Type 'new (x: (a: T) => T) => T[]' is not assignable to type '{ new (x: { new (a: number): number; new (a?: number): number; }): number[]; new (x: { new (a: bo...': !!! Types of parameters 'x' and 'x' are incompatible: !!! Type '(a: any) => any' is not assignable to type '{ new (a: number): number; new (a?: number): number; }'. b16 = a16; // error ~~~ -!!! Type '{ new (x: { new (a: number): number; new (a?: number): number; }): number[]; new (x: { new (a: boolean): boolean; new (a?: boolean): boolean; }): boolean[]; }' is not assignable to type 'new (x: (a: T) => T) => T[]': +!!! Type '{ new (x: { new (a: number): number; new (a?: number): number; }): number[]; new (x: { new (a: bo...' is not assignable to type 'new (x: (a: T) => T) => T[]': !!! Types of parameters 'x' and 'x' are incompatible: !!! Type '{ new (a: number): number; new (a?: number): number; }' is not assignable to type '(a: any) => any'. var b17: new (x: (a: T) => T) => any[]; a17 = b17; // error ~~~ -!!! Type 'new (x: (a: T) => T) => any[]' is not assignable to type '{ new (x: { new (a: T): T; new (a: T): T; }): any[]; new (x: { new (a: T): T; new (a: T): T; }): any[]; }': +!!! Type 'new (x: (a: T) => T) => any[]' is not assignable to type '{ new (x: { new (a: T): T; new (a: T): T; }): any[]; new (x: {...': !!! Types of parameters 'x' and 'x' are incompatible: !!! Type '(a: any) => any' is not assignable to type '{ new (a: T): T; new (a: T): T; }'. b17 = a17; // error ~~~ -!!! Type '{ new (x: { new (a: T): T; new (a: T): T; }): any[]; new (x: { new (a: T): T; new (a: T): T; }): any[]; }' is not assignable to type 'new (x: (a: T) => T) => any[]': +!!! Type '{ new (x: { new (a: T): T; new (a: T): T; }): any[]; new (x: {...' is not assignable to type 'new (x: (a: T) => T) => any[]': !!! Types of parameters 'x' and 'x' are incompatible: !!! Type '{ new (a: T): T; new (a: T): T; }' is not assignable to type '(a: any) => any'. } diff --git a/tests/baselines/reference/callSignatureAssignabilityInInheritance3.errors.txt b/tests/baselines/reference/callSignatureAssignabilityInInheritance3.errors.txt index 81cd8b924f0..f7798de7d1d 100644 --- a/tests/baselines/reference/callSignatureAssignabilityInInheritance3.errors.txt +++ b/tests/baselines/reference/callSignatureAssignabilityInInheritance3.errors.txt @@ -68,7 +68,7 @@ ~~ !!! Interface 'I4' incorrectly extends interface 'A': !!! Types of property 'a8' are incompatible: -!!! Type '(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U' is not assignable to type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived': +!!! Type '(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T)...' is not assignable to type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived': !!! Types of parameters 'y' and 'y' are incompatible: !!! Type '(arg2: { foo: number; }) => any' is not assignable to type '(arg2: Base) => Derived': !!! Types of parameters 'arg2' and 'arg2' are incompatible: diff --git a/tests/baselines/reference/constructSignatureAssignabilityInInheritance3.errors.txt b/tests/baselines/reference/constructSignatureAssignabilityInInheritance3.errors.txt index 2d267d5a260..29bc90a6bdf 100644 --- a/tests/baselines/reference/constructSignatureAssignabilityInInheritance3.errors.txt +++ b/tests/baselines/reference/constructSignatureAssignabilityInInheritance3.errors.txt @@ -58,7 +58,7 @@ ~~ !!! Interface 'I4' incorrectly extends interface 'A': !!! Types of property 'a8' are incompatible: -!!! Type 'new (x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U' is not assignable to type 'new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived': +!!! Type 'new (x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r...' is not assignable to type 'new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived': !!! Types of parameters 'y' and 'y' are incompatible: !!! Type '(arg2: { foo: number; }) => any' is not assignable to type '(arg2: Base) => Derived': !!! Types of parameters 'arg2' and 'arg2' are incompatible: diff --git a/tests/baselines/reference/errorMessageOnObjectLiteralType.errors.txt b/tests/baselines/reference/errorMessageOnObjectLiteralType.errors.txt index 9b19634265d..5172fb47ec3 100644 --- a/tests/baselines/reference/errorMessageOnObjectLiteralType.errors.txt +++ b/tests/baselines/reference/errorMessageOnObjectLiteralType.errors.txt @@ -8,4 +8,4 @@ !!! Property 'getOwnPropertyNamess' does not exist on type '{ a: string; b: number; }'. Object.getOwnPropertyNamess(null); ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'getOwnPropertyNamess' does not exist on type '{ (): any; (value: any): any; new (value?: any): Object; prototype: Object; getPrototypeOf(o: any): any; getOwnPropertyDescriptor(o: any, p: string): PropertyDescriptor; getOwnPropertyNames(o: any): string[]; create(o: any, properties?: PropertyDescriptorMap): any; defineProperty(o: any, p: string, attributes: PropertyDescriptor): any; defineProperties(o: any, properties: PropertyDescriptorMap): any; seal(o: any): any; freeze(o: any): any; preventExtensions(o: any): any; isSealed(o: any): boolean; isFrozen(o: any): boolean; isExtensible(o: any): boolean; keys(o: any): string[]; }'. \ No newline at end of file +!!! Property 'getOwnPropertyNamess' does not exist on type '{ (): any; (value: any): any; new (value?: any): Object; prototype: Object; getPrototypeOf(o: any...'. \ No newline at end of file diff --git a/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt b/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt index 1a74ee37809..ed088598bab 100644 --- a/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt +++ b/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt @@ -98,7 +98,7 @@ // error var b: { [x: number]: string; } = { ~ -!!! Type '{ [x: number]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: unknown; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }': +!!! Type '{ [x: number]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e...' is not assignable to type '{ [x: number]: string; }': !!! Index signatures are incompatible: !!! Type '{}' is not assignable to type 'string'. a: '', diff --git a/tests/baselines/reference/propertyAccess.errors.txt b/tests/baselines/reference/propertyAccess.errors.txt index acb32459571..29a62c3fe8c 100644 --- a/tests/baselines/reference/propertyAccess.errors.txt +++ b/tests/baselines/reference/propertyAccess.errors.txt @@ -41,7 +41,7 @@ // Dotted property access of property that doesn't exist on value's apparent type var cc = obj.qqq; // error ~~~ -!!! Property 'qqq' does not exist on type '{ 10: string; x: string; y: number; z: { n: string; m: number; o: () => boolean; }; 'literal property': number; }'. +!!! Property 'qqq' does not exist on type '{ 10: string; x: string; y: number; z: { n: string; m: number; o: () => boolean; }; 'literal prop...'. // Bracket notation property access using string literal value on type with property of that literal name var dd = obj['literal property']; diff --git a/tests/baselines/reference/redefineArray.errors.txt b/tests/baselines/reference/redefineArray.errors.txt index 5ac77d01f63..35d118de6ff 100644 --- a/tests/baselines/reference/redefineArray.errors.txt +++ b/tests/baselines/reference/redefineArray.errors.txt @@ -1,5 +1,5 @@ ==== tests/cases/compiler/redefineArray.ts (1 errors) ==== Array = function (n:number, s:string) {return n;}; ~~~~~ -!!! Type '(n: number, s: string) => number' is not assignable to type '{ (arrayLength?: number): any[]; (arrayLength: number): T[]; (...items: T[]): T[]; new (arrayLength?: number): any[]; new (arrayLength: number): T[]; new (...items: T[]): T[]; isArray(arg: any): boolean; prototype: any[]; }': +!!! Type '(n: number, s: string) => number' is not assignable to type '{ (arrayLength?: number): any[]; (arrayLength: number): T[]; (...items: T[]): T[]; new (arr...': !!! Property 'isArray' is missing in type '(n: number, s: string) => number'. \ No newline at end of file diff --git a/tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations.errors.txt b/tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations.errors.txt index 5ccca110ce8..894a83fc88a 100644 --- a/tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations.errors.txt +++ b/tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations.errors.txt @@ -128,7 +128,7 @@ // error var b: { [x: string]: string; } = { ~ -!!! Type '{ [x: string]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: MyString; X: string; foo: () => string; }' is not assignable to type '{ [x: string]: string; }': +!!! Type '{ [x: string]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e...' is not assignable to type '{ [x: string]: string; }': !!! Index signatures are incompatible: !!! Type '{}' is not assignable to type 'string'. a: '', diff --git a/tests/baselines/reference/typeName1.errors.txt b/tests/baselines/reference/typeName1.errors.txt index 92b3279ab81..8deb7dc43d6 100644 --- a/tests/baselines/reference/typeName1.errors.txt +++ b/tests/baselines/reference/typeName1.errors.txt @@ -24,7 +24,7 @@ !!! Property 'x' is missing in type 'Number'. var x5:{ (s:string):number;(n:number):string;x;y;z:number;f(n:number):string;f(s:string):number; }=3; ~~ -!!! Type 'number' is not assignable to type '{ (s: string): number; (n: number): string; x: any; y: any; z: number; f(n: number): string; f(s: string): number; }': +!!! Type 'number' is not assignable to type '{ (s: string): number; (n: number): string; x: any; y: any; z: number; f(n: number): string; f(s:...': !!! Property 'x' is missing in type 'Number'. var x6:{ z:number;f:{(n:number):string;(s:string):number;}; }=3; ~~ @@ -53,7 +53,7 @@ !!! Property 'length' is missing in type 'Number'. var x12:{z:I;x:boolean;y:(s:string)=>boolean;w:{ z:I;[s:string]:{ x; y; };[n:number]:{x; y;};():boolean; };}[][]=3; ~~~ -!!! Type 'number' is not assignable to type '{ z: I; x: boolean; y: (s: string) => boolean; w: { (): boolean; [x: string]: { x: any; y: any; }; [x: number]: { x: any; y: any; }; z: I; }; }[][]': +!!! Type 'number' is not assignable to type '{ z: I; x: boolean; y: (s: string) => boolean; w: { (): boolean; [x: string]: { x: any; y: any; }...': !!! Property 'length' is missing in type 'Number'. var x13:{ new(): number; new(n:number):number; x: string; w: {y: number;}; (): {}; } = 3; ~~~