Adding noErrorTruncation compiler option

This commit is contained in:
Anders Hejlsberg 2014-09-09 12:25:03 -07:00
parent 9f3d83adeb
commit aa58dcbee0
17 changed files with 86 additions and 33 deletions

View File

@ -37,7 +37,9 @@ module ts {
var emptyArray: any[] = [];
var emptySymbols: SymbolTable = {};
var compilerOptions = program.getCompilerOptions();
var checker: TypeChecker = {
getProgram: () => program,
getDiagnostics: getDiagnostics,
@ -972,7 +974,8 @@ module ts {
}
function typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string {
var stringWriter = createSingleLineTextWriter(flags & TypeFormatFlags.NoTruncation ? undefined : 100);
var maxLength = compilerOptions.noErrorTruncation || flags & TypeFormatFlags.NoTruncation ? undefined : 100;
var stringWriter = createSingleLineTextWriter(maxLength);
// TODO(shkamat): typeToString should take enclosingDeclaration as input, once we have implemented enclosingDeclaration
writeTypeToTextWriter(type, enclosingDeclaration, flags, stringWriter);
return stringWriter.getText();
@ -1364,7 +1367,7 @@ module ts {
return type;
function checkImplicitAny(type: Type) {
if (!fullTypeCheck || !program.getCompilerOptions().noImplicitAny) {
if (!fullTypeCheck || !compilerOptions.noImplicitAny) {
return;
}
// We need to have ended up with 'any', 'any[]', 'any[][]', etc.
@ -1467,7 +1470,7 @@ module ts {
}
// Otherwise, fall back to 'any'.
else {
if (program.getCompilerOptions().noImplicitAny) {
if (compilerOptions.noImplicitAny) {
error(setter, Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_type_annotation, symbol.name);
}
@ -3159,7 +3162,7 @@ module ts {
if (propType !== widenedType) {
propTypeWasWidened = true;
if (program.getCompilerOptions().noImplicitAny && getInnermostTypeOfNestedArrayTypes(widenedType) === anyType) {
if (compilerOptions.noImplicitAny && getInnermostTypeOfNestedArrayTypes(widenedType) === anyType) {
error(p.valueDeclaration, Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, p.name, typeToString(widenedType));
}
}
@ -3987,7 +3990,7 @@ module ts {
}
// Fall back to any.
if (program.getCompilerOptions().noImplicitAny && objectType !== anyType) {
if (compilerOptions.noImplicitAny && objectType !== anyType) {
error(node, Diagnostics.Index_signature_of_object_type_implicitly_has_an_any_type);
}
@ -4332,7 +4335,7 @@ module ts {
var declaration = signature.declaration;
if (declaration && (declaration.kind !== SyntaxKind.Constructor && declaration.kind !== SyntaxKind.ConstructSignature)) {
// When resolved signature is a call signature (and not a construct signature) the result type is any
if (program.getCompilerOptions().noImplicitAny) {
if (compilerOptions.noImplicitAny) {
error(node, Diagnostics.new_expression_whose_target_lacks_a_construct_signature_implicitly_has_an_any_type);
}
return anyType;
@ -4378,7 +4381,7 @@ module ts {
var unwidenedType = checkAndMarkExpression(func.body, contextualMapper);
var widenedType = getWidenedType(unwidenedType);
if (fullTypeCheck && program.getCompilerOptions().noImplicitAny && widenedType !== unwidenedType && getInnermostTypeOfNestedArrayTypes(widenedType) === anyType) {
if (fullTypeCheck && compilerOptions.noImplicitAny && widenedType !== unwidenedType && getInnermostTypeOfNestedArrayTypes(widenedType) === anyType) {
error(func, Diagnostics.Function_expression_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeToString(widenedType));
}
@ -4400,7 +4403,7 @@ module ts {
var widenedType = getWidenedType(commonType);
// Check and report for noImplicitAny if the best common type implicitly gets widened to an 'any'/arrays-of-'any' type.
if (fullTypeCheck && program.getCompilerOptions().noImplicitAny && widenedType !== commonType && getInnermostTypeOfNestedArrayTypes(widenedType) === anyType) {
if (fullTypeCheck && compilerOptions.noImplicitAny && widenedType !== commonType && getInnermostTypeOfNestedArrayTypes(widenedType) === anyType) {
var typeName = typeToString(widenedType);
if (func.name) {
@ -4976,7 +4979,7 @@ module ts {
checkCollisionWithCapturedThisVariable(node, node.name);
checkCollistionWithRequireExportsInGeneratedCode(node, node.name);
checkCollisionWithArgumentsInGeneratedCode(node);
if (program.getCompilerOptions().noImplicitAny && !node.type) {
if (compilerOptions.noImplicitAny && !node.type) {
switch (node.kind) {
case SyntaxKind.ConstructSignature:
error(node, Diagnostics.Construct_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type);
@ -5532,7 +5535,7 @@ module ts {
}
// If there is no body and no explicit return type, then report an error.
if (fullTypeCheck && program.getCompilerOptions().noImplicitAny && !node.body && !node.type) {
if (fullTypeCheck && compilerOptions.noImplicitAny && !node.body && !node.type) {
// Ignore privates within ambient contexts; they exist purely for documentative purposes to avoid name clashing.
// (e.g. privates within .d.ts files do not expose type information)
if (!isPrivateWithinAmbient(node)) {
@ -7177,7 +7180,7 @@ module ts {
function shouldEmitDeclarations() {
// If the declaration emit and there are no errors being reported in program or by checker
// declarations can be emitted
return program.getCompilerOptions().declaration &&
return compilerOptions.declaration &&
!program.getDiagnostics().length &&
!getDiagnostics().length;
}

View File

@ -955,6 +955,7 @@ module ts {
locale?: string;
mapRoot?: string;
module?: ModuleKind;
noErrorTruncation?: boolean;
noImplicitAny?: boolean;
noLib?: boolean;
noLibCheck?: boolean;
@ -967,7 +968,6 @@ module ts {
target?: ScriptTarget;
version?: boolean;
watch?: boolean;
[option: string]: any;
}

View File

@ -622,6 +622,7 @@ module Harness {
options = options || { noResolve: false };
options.target = options.target || ts.ScriptTarget.ES3;
options.module = options.module || ts.ModuleKind.None;
options.noErrorTruncation = true;
if (settingsCallback) {
settingsCallback(null);
@ -725,6 +726,10 @@ module Harness {
options.emitBOM = !!setting.value;
break;
case 'errortruncation':
options.noErrorTruncation = setting.value === 'false';
break;
default:
throw new Error('Unsupported compiler setting ' + setting.flag);
}
@ -1030,7 +1035,7 @@ module Harness {
var optionRegex = /^[\/]{2}\s*@(\w+)\s*:\s*(\S*)/gm; // multiple matches on multiple lines
// List of allowed metadata names
var fileMetadataNames = ["filename", "comments", "declaration", "module", "nolib", "sourcemap", "target", "out", "outDir", "noimplicitany", "noresolve", "newline", "newlines", "emitbom"];
var fileMetadataNames = ["filename", "comments", "declaration", "module", "nolib", "sourcemap", "target", "out", "outDir", "noimplicitany", "noresolve", "newline", "newlines", "emitbom", "errortruncation"];
function extractCompilerSettings(content: string): CompilerSetting[] {

View File

@ -10,7 +10,7 @@
~
!!! Expression expected.
~~~
!!! Type 'number' is not assignable to type '{ (arrayLength?: number): any[]; <T>(arrayLength: number): T[]; <T>(...items: T[]): T[]; new (arr...':
!!! 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>;
~
@ -18,4 +18,4 @@
~
!!! Expression expected.
~~~
!!! Type 'number' is not assignable to type '{ (arrayLength?: number): any[]; <T>(arrayLength: number): T[]; <T>(...items: T[]): T[]; new (arr...'.
!!! 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[]; }'.

View File

@ -52,7 +52,7 @@
var b8: <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U;
a8 = b8; // error, { foo: number } and Base are incompatible
~~
!!! Type '<T extends Base, U extends Derived>(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':
!!! Type '<T extends Base, U extends Derived>(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':
!!! 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 '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T)...':
!!! Type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U':
!!! 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:

View File

@ -52,7 +52,7 @@
var b8: new <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U;
a8 = b8; // error, type mismatch
~~
!!! Type 'new <T extends Base, U extends Derived>(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':
!!! Type 'new <T extends Base, U extends Derived>(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':
!!! 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 <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r...':
!!! Type 'new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type 'new <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U':
!!! 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 <T>(x: (a: T) => T) => T[];
a16 = b16; // error
~~~
!!! Type 'new <T>(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...':
!!! Type 'new <T>(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[]; }':
!!! 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: bo...' is not assignable to type 'new <T>(x: (a: T) => T) => T[]':
!!! 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 <T>(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 <T>(x: (a: T) => T) => any[];
a17 = b17; // error
~~~
!!! Type 'new <T>(x: (a: T) => T) => any[]' is not assignable to type '{ new (x: { new <T extends Derived>(a: T): T; new <T extends Base>(a: T): T; }): any[]; new (x: {...':
!!! Type 'new <T>(x: (a: T) => T) => any[]' is not assignable to type '{ new (x: { new <T extends Derived>(a: T): T; new <T extends Base>(a: T): T; }): any[]; new (x: { new <T extends Derived2>(a: T): T; new <T extends Base>(a: T): T; }): any[]; }':
!!! Types of parameters 'x' and 'x' are incompatible:
!!! Type '(a: any) => any' is not assignable to type '{ new <T extends Derived>(a: T): T; new <T extends Base>(a: T): T; }'.
b17 = a17; // error
~~~
!!! Type '{ new (x: { new <T extends Derived>(a: T): T; new <T extends Base>(a: T): T; }): any[]; new (x: {...' is not assignable to type 'new <T>(x: (a: T) => T) => any[]':
!!! Type '{ new (x: { new <T extends Derived>(a: T): T; new <T extends Base>(a: T): T; }): any[]; new (x: { new <T extends Derived2>(a: T): T; new <T extends Base>(a: T): T; }): any[]; }' is not assignable to type 'new <T>(x: (a: T) => T) => any[]':
!!! Types of parameters 'x' and 'x' are incompatible:
!!! Type '{ new <T extends Derived>(a: T): T; new <T extends Base>(a: T): T; }' is not assignable to type '(a: any) => any'.
}

View File

@ -68,7 +68,7 @@
~~
!!! Interface 'I4' incorrectly extends interface 'A':
!!! Types of property 'a8' are incompatible:
!!! Type '<T extends Base, U extends Derived>(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':
!!! Type '<T extends Base, U extends Derived>(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':
!!! 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:

View File

@ -58,7 +58,7 @@
~~
!!! Interface 'I4' incorrectly extends interface 'A':
!!! Types of property 'a8' are incompatible:
!!! Type 'new <T extends Base, U extends Derived>(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':
!!! Type 'new <T extends Base, U extends Derived>(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':
!!! 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:

View File

@ -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...'.
!!! 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[]; }'.

View File

@ -0,0 +1,15 @@
==== tests/cases/compiler/errorWithTruncatedType.ts (1 errors) ====
var x: {
propertyWithAnExceedinglyLongName1: string;
propertyWithAnExceedinglyLongName2: string;
propertyWithAnExceedinglyLongName3: string;
propertyWithAnExceedinglyLongName4: string;
propertyWithAnExceedinglyLongName5: string;
};
// String representation of type of 'x' should be truncated in error message
var s: string = x;
~
!!! Type '{ propertyWithAnExceedinglyLongName1: string; propertyWithAnExceedinglyLongName2: string; propert...' is not assignable to type 'string'.

View File

@ -0,0 +1,18 @@
//// [errorWithTruncatedType.ts]
var x: {
propertyWithAnExceedinglyLongName1: string;
propertyWithAnExceedinglyLongName2: string;
propertyWithAnExceedinglyLongName3: string;
propertyWithAnExceedinglyLongName4: string;
propertyWithAnExceedinglyLongName5: string;
};
// String representation of type of 'x' should be truncated in error message
var s: string = x;
//// [errorWithTruncatedType.js]
var x;
// String representation of type of 'x' should be truncated in error message
var s = x;

View File

@ -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...' 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": number; "3.0": string; "4.0": number; f: unknown; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }':
!!! Index signatures are incompatible:
!!! Type '{}' is not assignable to type 'string'.
a: '',

View File

@ -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 prop...'.
!!! Property 'qqq' does not exist on type '{ 10: string; x: string; y: number; z: { n: string; m: number; o: () => boolean; }; 'literal property': number; }'.
// Bracket notation property access using string literal value on type with property of that literal name
var dd = obj['literal property'];

View File

@ -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[]; <T>(arrayLength: number): T[]; <T>(...items: T[]): T[]; new (arr...':
!!! Type '(n: number, s: string) => 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 '(n: number, s: string) => number'.

View File

@ -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...' 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": number; "3.0": string; "4.0": number; f: MyString; X: string; foo: () => string; }' is not assignable to type '{ [x: string]: string; }':
!!! Index signatures are incompatible:
!!! Type '{}' is not assignable to type 'string'.
a: '',

View File

@ -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:...':
!!! 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; }':
!!! 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; }...':
!!! 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; }; }[][]':
!!! Property 'length' is missing in type 'Number'.
var x13:{ new(): number; new(n:number):number; x: string; w: {y: number;}; (): {}; } = 3;
~~~

View File

@ -0,0 +1,12 @@
// @errortruncation: true
var x: {
propertyWithAnExceedinglyLongName1: string;
propertyWithAnExceedinglyLongName2: string;
propertyWithAnExceedinglyLongName3: string;
propertyWithAnExceedinglyLongName4: string;
propertyWithAnExceedinglyLongName5: string;
};
// String representation of type of 'x' should be truncated in error message
var s: string = x;