Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse input type nodes when serializing signature parameter and return types #37444

Merged
merged 17 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
371 changes: 209 additions & 162 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2114,10 +2114,13 @@ namespace ts {
return isIdentifier(node) && node.escapedText === "exports";
}

export function isModuleIdentifier(node: Node) {
return isIdentifier(node) && node.escapedText === "module";
}

export function isModuleExportsAccessExpression(node: Node): node is LiteralLikeElementAccessExpression & { expression: Identifier } {
return (isPropertyAccessExpression(node) || isLiteralLikeElementAccess(node))
&& isIdentifier(node.expression)
&& node.expression.escapedText === "module"
&& isModuleIdentifier(node.expression)
&& getElementOrPropertyAccessName(node) === "exports";
}

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/1.0lib-noErrors.types
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,7 @@ interface Array<T> {
>n : number
}
declare var Array: {
>Array : { (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[]; }
>Array : { (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: Array<any>; }

new (arrayLength?: number): any[];
>arrayLength : number
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ArrowFunction1.types
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ArrowFunctions/ArrowFunction1.ts ===
var v = (a: ) => {
>v : (a: any) => void
>(a: ) => { } : (a: any) => void
>v : (a: ) => void
>(a: ) => { } : (a: ) => void
weswigham marked this conversation as resolved.
Show resolved Hide resolved
>a : any

};
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module clodule1 {
>clodule1 : typeof clodule1

function f(x: T) { }
>f : (x: any) => void
>f : (x: T) => void
weswigham marked this conversation as resolved.
Show resolved Hide resolved
>x : any
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module A {
}

export var UnitSquare : {
>UnitSquare : { top: { left: Point; right: Point; }; bottom: { left: Point; right: Point; }; }
>UnitSquare : { top: { left: Point; right: Point;}; bottom: { left: Point; right: Point;}; }

top: { left: Point, right: Point },
>top : { left: Point; right: Point; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module A {

=== tests/cases/conformance/internalModules/DeclarationMerging/test.ts ===
var fn: () => { x: number; y: number };
>fn : () => { x: number; y: number; }
>fn : () => { x: number; y: number;}
>x : number
>y : number

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module B {

=== tests/cases/conformance/internalModules/DeclarationMerging/test.ts ===
var fn: () => { x: number; y: number };
>fn : () => { x: number; y: number; }
>fn : () => { x: number; y: number;}
>x : number
>y : number

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var p = Geometry.Origin;
>Origin : A.Point

var line: { start: { x: number; y: number }; end: { x: number; y: number; } };
>line : { start: { x: number; y: number; }; end: { x: number; y: number; }; }
>line : { start: { x: number; y: number;}; end: { x: number; y: number;}; }
>start : { x: number; y: number; }
>x : number
>y : number
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/ParameterList5.types
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
=== tests/cases/compiler/ParameterList5.ts ===
function A(): (public B) => C {
>A : () => (B: any) => any
>A : () => (B: any) => C
>B : any
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/TypeGuardWithEnumUnion.types
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ enum Color { R, G, B }
>B : Color.B

function f1(x: Color | string) {
>f1 : (x: string | Color) => void
>f1 : (x: Color | string) => void
>x : string | Color

if (typeof x === "number") {
Expand All @@ -33,7 +33,7 @@ function f1(x: Color | string) {
}

function f2(x: Color | string | string[]) {
>f2 : (x: string | Color | string[]) => void
>f2 : (x: Color | string | string[]) => void
>x : string | Color | string[]

if (typeof x === "object") {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/aliasUsageInObjectLiteral.types
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var b: { x: IHasVisualizationModel } = { x: moduleA };
>moduleA : typeof moduleA

var c: { y: { z: IHasVisualizationModel } } = { y: { z: moduleA } };
>c : { y: { z: IHasVisualizationModel; }; }
>c : { y: { z: IHasVisualizationModel;}; }
>y : { z: IHasVisualizationModel; }
>z : IHasVisualizationModel
>{ y: { z: moduleA } } : { y: { z: typeof moduleA; }; }
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/ambientErrors.types
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare function fn(x: number): string;
>x : number

declare function fn(x: 'foo'): number;
>fn : { (x: number): string; (x: "foo"): number; }
>fn : { (x: number): string; (x: 'foo'): number; }
>x : "foo"

// Ambient functions with duplicate signatures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var r3 = foo3(a); // any
>a : any

declare function foo7(x: { bar: number }): { bar: number };
>foo7 : { (x: { bar: number; }): { bar: number; }; (x: any): any; }
>foo7 : { (x: { bar: number;}): { bar: number;}; (x: any): any; }
>x : { bar: number; }
>bar : number
>bar : number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var o = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
>3 : 3

var o1: { x: [string, number], y: { c: boolean, d: string, e: number } } = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
>o1 : { x: [string, number]; y: { c: boolean; d: string; e: number; }; }
>o1 : { x: [string, number]; y: { c: boolean; d: string; e: number;}; }
>x : [string, number]
>y : { c: boolean; d: string; e: number; }
>c : boolean
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/arguments.types
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ function f() {

interface I {
method(args: typeof arguments): void;
>method : (args: any) => void
>method : (args: typeof arguments) => void
>args : any
>arguments : any

fn: (args: typeof arguments) => void;
>fn : (args: any) => void
>fn : (args: typeof arguments) => void
>args : any
>arguments : any

Expand All @@ -41,7 +41,7 @@ interface I {
>arguments : any

construct: new (args: typeof arguments) => void;
>construct : new (args: any) => void
>construct : new (args: typeof arguments) => void
>args : any
>arguments : any
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/arrayConcat3.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Fn<T extends object> = <U extends T>(subj: U) => U
>subj : U

function doStuff<T extends object, T1 extends T>(a: Array<Fn<T>>, b: Array<Fn<T1>>) {
>doStuff : <T extends object, T1 extends T>(a: Fn<T>[], b: Fn<T1>[]) => void
>doStuff : <T extends object, T1 extends T>(a: Array<Fn<T>>, b: Array<Fn<T1>>) => void
>a : Fn<T>[]
>b : Fn<T1>[]

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayFlatMap.types
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ array.flatMap((): ReadonlyArray<number> => []); // ok
>array.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
>array : number[]
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
>(): ReadonlyArray<number> => [] : () => readonly number[]
>(): ReadonlyArray<number> => [] : () => ReadonlyArray<number>
>[] : undefined[]

readonlyArray.flatMap((): ReadonlyArray<number> => []); // ok
>readonlyArray.flatMap((): ReadonlyArray<number> => []) : number[]
>readonlyArray.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
>readonlyArray : readonly number[]
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
>(): ReadonlyArray<number> => [] : () => readonly number[]
>(): ReadonlyArray<number> => [] : () => ReadonlyArray<number>
>[] : undefined[]

Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,28 @@ var es = [(x: string) => 2, (x: Object) => 1]; // { (x:string) => number }[]
>1 : 1

var fs = [(a: { x: number; y?: number }) => 1, (b: { x: number; z?: number }) => 2]; // (a: { x: number; y?: number }) => number[]
>fs : (((a: { x: number; y?: number; }) => number) | ((b: { x: number; z?: number; }) => number))[]
>[(a: { x: number; y?: number }) => 1, (b: { x: number; z?: number }) => 2] : (((a: { x: number; y?: number; }) => number) | ((b: { x: number; z?: number; }) => number))[]
>(a: { x: number; y?: number }) => 1 : (a: { x: number; y?: number; }) => number
>fs : (((a: { x: number; y?: number;}) => number) | ((b: { x: number; z?: number;}) => number))[]
>[(a: { x: number; y?: number }) => 1, (b: { x: number; z?: number }) => 2] : (((a: { x: number; y?: number;}) => number) | ((b: { x: number; z?: number;}) => number))[]
>(a: { x: number; y?: number }) => 1 : (a: { x: number; y?: number;}) => number
>a : { x: number; y?: number; }
>x : number
>y : number
>1 : 1
>(b: { x: number; z?: number }) => 2 : (b: { x: number; z?: number; }) => number
>(b: { x: number; z?: number }) => 2 : (b: { x: number; z?: number;}) => number
>b : { x: number; z?: number; }
>x : number
>z : number
>2 : 2

var gs = [(b: { x: number; z?: number }) => 2, (a: { x: number; y?: number }) => 1]; // (b: { x: number; z?: number }) => number[]
>gs : (((b: { x: number; z?: number; }) => number) | ((a: { x: number; y?: number; }) => number))[]
>[(b: { x: number; z?: number }) => 2, (a: { x: number; y?: number }) => 1] : (((b: { x: number; z?: number; }) => number) | ((a: { x: number; y?: number; }) => number))[]
>(b: { x: number; z?: number }) => 2 : (b: { x: number; z?: number; }) => number
>gs : (((b: { x: number; z?: number;}) => number) | ((a: { x: number; y?: number;}) => number))[]
>[(b: { x: number; z?: number }) => 2, (a: { x: number; y?: number }) => 1] : (((b: { x: number; z?: number;}) => number) | ((a: { x: number; y?: number;}) => number))[]
>(b: { x: number; z?: number }) => 2 : (b: { x: number; z?: number;}) => number
>b : { x: number; z?: number; }
>x : number
>z : number
>2 : 2
>(a: { x: number; y?: number }) => 1 : (a: { x: number; y?: number; }) => number
>(a: { x: number; y?: number }) => 1 : (a: { x: number; y?: number;}) => number
>a : { x: number; y?: number; }
>x : number
>y : number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class X {
>X : X

public f(a: Array) { }
>f : (a: any) => void
>f : (a: Array) => void
>a : any
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/arraySigChecking.types
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ myArray = [[1, 2]];
>2 : 2

function isEmpty(l: { length: number }) {
>isEmpty : (l: { length: number; }) => boolean
>isEmpty : (l: { length: number;}) => boolean
>l : { length: number; }
>length : number

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/assignmentCompatBug5.types
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/compiler/assignmentCompatBug5.ts ===
function foo1(x: { a: number; }) { }
>foo1 : (x: { a: number; }) => void
>foo1 : (x: { a: number;}) => void
>x : { a: number; }
>a : number

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/compiler/assignmentCompatFunctionsWithOptionalArgs.ts ===
function foo(x: { id: number; name?: string; }): void;
>foo : (x: { id: number; name?: string; }) => void
>foo : (x: { id: number; name?: string;}) => void
>x : { id: number; name?: string; }
>id : number
>name : string
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/assignmentCompatOnNew.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Foo{};
>Foo : Foo

function bar(x: {new(): Foo;}){}
>bar : (x: new () => Foo) => void
>bar : (x: { new (): Foo;}) => void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see how the signature annotation changed but shouldnt it be { new (): Foo; } or {new(): Foo;} instead ? Why is there extra space before new

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure, it's because our default formatting for object literals which don't have any emit flags set is newline-separated with appropriate indentation. I could set some emit flags to try to make the objects single-line; but I imagine that default is actually better when we're not using the single line string writer that we use for .types baselines and error messages. (eg, quickinfo, declarations)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm even if its our default one wit wouldnt print it shouldnt start with these extra spaces no?

Copy link
Member Author

@weswigham weswigham Mar 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it actually emits as

{
    new (): Foo;
}

and the printer is simply eliding newlines (it is), it would be.

>x : new () => Foo

bar(Foo); // Error, but should be allowed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,25 @@ var a10: (...x: Derived[]) => Derived;
>x : Derived[]

var a11: (x: { foo: string }, y: { foo: string; bar: string }) => Base;
>a11 : (x: { foo: string; }, y: { foo: string; bar: string; }) => Base
>a11 : (x: { foo: string;}, y: { foo: string; bar: string;}) => Base
>x : { foo: string; }
>foo : string
>y : { foo: string; bar: string; }
>foo : string
>bar : string

var a12: (x: Array<Base>, y: Array<Derived2>) => Array<Derived>;
>a12 : (x: Base[], y: Derived2[]) => Derived[]
>a12 : (x: Array<Base>, y: Array<Derived2>) => Array<Derived>
>x : Base[]
>y : Derived2[]

var a13: (x: Array<Base>, y: Array<Derived>) => Array<Derived>;
>a13 : (x: Base[], y: Derived[]) => Derived[]
>a13 : (x: Array<Base>, y: Array<Derived>) => Array<Derived>
>x : Base[]
>y : Derived[]

var a14: (x: { a: string; b: number }) => Object;
>a14 : (x: { a: string; b: number; }) => Object
>a14 : (x: { a: string; b: number;}) => Object
>x : { a: string; b: number; }
>a : string
>b : number
Expand Down Expand Up @@ -274,10 +274,10 @@ b8 = a8; // ok
>a8 : (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived

var b9: <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: string; bing: number }) => U) => (r: T) => U;
>b9 : <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: string; bing: number; }) => U) => (r: T) => U
>b9 : <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: string; bing: number;}) => U) => (r: T) => U
>x : (arg: T) => U
>arg : T
>y : (arg2: { foo: string; bing: number; }) => U
>y : (arg2: { foo: string; bing: number;}) => U
>arg2 : { foo: string; bing: number; }
>foo : string
>bing : number
Expand Down Expand Up @@ -323,7 +323,7 @@ b11 = a11; // ok
>a11 : (x: { foo: string; }, y: { foo: string; bar: string; }) => Base

var b12: <T extends Array<Base>>(x: Array<Base>, y: T) => Array<Derived>;
>b12 : <T extends Base[]>(x: Base[], y: T) => Derived[]
>b12 : <T extends Base[]>(x: Array<Base>, y: T) => Array<Derived>
>x : Base[]
>y : T

Expand All @@ -338,7 +338,7 @@ b12 = a12; // ok
>a12 : (x: Base[], y: Derived2[]) => Derived[]

var b13: <T extends Array<Derived>>(x: Array<Base>, y: T) => T;
>b13 : <T extends Derived[]>(x: Base[], y: T) => T
>b13 : <T extends Derived[]>(x: Array<Base>, y: T) => T
>x : Base[]
>y : T

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ module Errors {
>x : Base[]

var a11: (x: { foo: string }, y: { foo: string; bar: string }) => Base;
>a11 : (x: { foo: string; }, y: { foo: string; bar: string; }) => Base
>a11 : (x: { foo: string;}, y: { foo: string; bar: string;}) => Base
>x : { foo: string; }
>foo : string
>y : { foo: string; bar: string; }
>foo : string
>bar : string

var a12: (x: Array<Base>, y: Array<Derived2>) => Array<Derived>;
>a12 : (x: Base[], y: Derived2[]) => Derived[]
>a12 : (x: Array<Base>, y: Array<Derived2>) => Array<Derived>
>x : Base[]
>y : Derived2[]

Expand All @@ -73,7 +73,7 @@ module Errors {

};
var a15: (x: { a: string; b: number }) => number;
>a15 : (x: { a: string; b: number; }) => number
>a15 : (x: { a: string; b: number;}) => number
>x : { a: string; b: number; }
>a : string
>b : number
Expand Down Expand Up @@ -158,10 +158,10 @@ module Errors {
>a7 : (x: (arg: Base) => Derived) => (r: Base) => Derived2

var b8: <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U;
>b8 : <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U
>b8 : <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number;}) => U) => (r: T) => U
>x : (arg: T) => U
>arg : T
>y : (arg2: { foo: number; }) => U
>y : (arg2: { foo: number;}) => U
>arg2 : { foo: number; }
>foo : number
>r : T
Expand Down Expand Up @@ -207,7 +207,7 @@ module Errors {
>a11 : (x: { foo: string; }, y: { foo: string; bar: string; }) => Base

var b12: <T extends Array<Derived2>>(x: Array<Base>, y: Array<Base>) => T;
>b12 : <T extends Derived2[]>(x: Base[], y: Base[]) => T
>b12 : <T extends Derived2[]>(x: Array<Base>, y: Array<Base>) => T
>x : Base[]
>y : Base[]

Expand Down
Loading