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

Write more useful types in .types test outputs #48578

Merged
merged 1 commit into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
  •  
  •  
  •  
Write more useful types in .types test outputs
  • Loading branch information
rbuckton committed Apr 5, 2022
commit 07c949eaf9f306783ded377a60f15d8f3507436b
58 changes: 33 additions & 25 deletions src/harness/typeWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,31 +115,39 @@ namespace Harness {
// let type = this.checker.getTypeAtLocation(node);
let type = ts.isExpressionWithTypeArgumentsInClassExtendsClause(node.parent) ? this.checker.getTypeAtLocation(node.parent) : undefined;
if (!type || type.flags & ts.TypeFlags.Any) type = this.checker.getTypeAtLocation(node);
const typeString =
// Distinguish `errorType`s from `any`s; but only if the file has no errors.
// Additionally,
// * the LHS of a qualified name
// * a binding pattern name
// * labels
// * the "global" in "declare global"
// * the "target" in "new.target"
// * names in import statements
// * type-only names in export statements
// * and intrinsic jsx tag names
// return `error`s via `getTypeAtLocation`
// But this is generally expected, so we don't call those out, either
(!this.hadErrorBaseline &&
type.flags & ts.TypeFlags.Any &&
!ts.isBindingElement(node.parent) &&
!ts.isPropertyAccessOrQualifiedName(node.parent) &&
!ts.isLabelName(node) &&
!(ts.isModuleDeclaration(node.parent) && ts.isGlobalScopeAugmentation(node.parent)) &&
!ts.isMetaProperty(node.parent) &&
!this.isImportStatementName(node) &&
!this.isExportStatementName(node) &&
!this.isIntrinsicJsxTag(node)) ?
(type as ts.IntrinsicType).intrinsicName :
this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType);
// Distinguish `errorType`s from `any`s; but only if the file has no errors.
// Additionally,
// * the LHS of a qualified name
// * a binding pattern name
// * labels
// * the "global" in "declare global"
// * the "target" in "new.target"
// * names in import statements
// * type-only names in export statements
// * and intrinsic jsx tag names
// return `error`s via `getTypeAtLocation`
// But this is generally expected, so we don't call those out, either
let typeString: string;
if (!this.hadErrorBaseline &&
type.flags & ts.TypeFlags.Any &&
!ts.isBindingElement(node.parent) &&
!ts.isPropertyAccessOrQualifiedName(node.parent) &&
!ts.isLabelName(node) &&
!(ts.isModuleDeclaration(node.parent) && ts.isGlobalScopeAugmentation(node.parent)) &&
!ts.isMetaProperty(node.parent) &&
!this.isImportStatementName(node) &&
!this.isExportStatementName(node) &&
!this.isIntrinsicJsxTag(node)) {
typeString = (type as ts.IntrinsicType).intrinsicName;
}
else {
typeString = this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType);
if (ts.isIdentifier(node) && ts.isTypeAliasDeclaration(node.parent) && node.parent.name === node && typeString === ts.idText(node)) {
// for a complex type alias `type T = ...`, showing "T : T" isn't very helpful for type tests. When the type produced is the same as
// the name of the type alias, recreate the type string without reusing the alias name
typeString = this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType | ts.TypeFormatFlags.InTypeAlias);
}
}
return {
line: lineAndCharacter.line,
syntaxKind: node.kind,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/compiler/DeclarationErrorsNoEmitOnError.ts ===
type T = { x : number }
>T : T
>T : { x: number; }
>x : number

export interface I {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ abstract class AbstractB { b: string; }
>b : string

type Abstracts = typeof AbstractA | typeof AbstractB;
>Abstracts : Abstracts
>Abstracts : typeof AbstractA | typeof AbstractB
>AbstractA : typeof AbstractA
>AbstractB : typeof AbstractB

type Concretes = typeof ConcreteA | typeof ConcreteB;
>Concretes : Concretes
>Concretes : typeof ConcreteA | typeof ConcreteB
>ConcreteA : typeof ConcreteA
>ConcreteB : typeof ConcreteB

type ConcretesOrAbstracts = Concretes | Abstracts;
>ConcretesOrAbstracts : ConcretesOrAbstracts
>ConcretesOrAbstracts : Abstracts | Concretes

declare const cls1: ConcretesOrAbstracts;
>cls1 : ConcretesOrAbstracts
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/accessorBodyInTypeContext.types
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/compiler/accessorBodyInTypeContext.ts ===
type A = {
>A : A
>A : { readonly foo: number; }

get foo() { return 0 }
>foo : number
Expand All @@ -9,7 +9,7 @@ type A = {
};

type B = {
>B : B
>B : { foo: any; }

set foo(v: any) { }
>foo : any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ type ExtendedMapper<HandledInputT, OutputT, ArgsT extends any[]> = (name : strin
>args : ArgsT

type a = ExtendedMapper<any, any, [any]>;
>a : a
>a : (name: string, mixed: any, args_0: any) => any

type b = ExtendedMapper<any, any, any[]>;
>b : b
>b : (name: string, mixed: any, ...args: any[]) => any

type test = a extends b ? "y" : "n"
>test : "y"
Expand All @@ -32,10 +32,10 @@ type ExtendedMapper1<HandledInputT, OutputT, ArgsT extends any[]> = (
);

type a1 = ExtendedMapper1<any, any, [any]>;
>a1 : a1
>a1 : (name: string, mixed: any, args_0: any) => any

type b1 = ExtendedMapper1<any, any, any[]>;
>b1 : b1
>b1 : (name: string, mixed: any, ...args: any[]) => any

type test1 = a1 extends b1 ? "y" : "n"
>test1 : "y"
Expand All @@ -56,10 +56,10 @@ type ExtendedMapper2<HandledInputT, OutputT, ArgsT extends any[]> = (
);

type a2 = ExtendedMapper2<any, any, [any]>;
>a2 : a2
>a2 : (name: string, mixed: any, args_0: any) => any

type b2 = ExtendedMapper2<any, any, any[]>;
>b2 : b2
>b2 : (name: string, mixed: any, ...args: any[]) => any

type test2 = a2 extends b2 ? "y" : "n"
>test2 : "y"
Expand All @@ -69,13 +69,13 @@ let check2: test2 = "y";
>"y" : "y"

type a3 = (name: string, mixed: any, args_0: any) => any
>a3 : a3
>a3 : (name: string, mixed: any, args_0: any) => any
>name : string
>mixed : any
>args_0 : any

type b3 = (name: string, mixed: any, ...args: any[]) => any
>b3 : b3
>b3 : (name: string, mixed: any, ...args: any[]) => any
>name : string
>mixed : any
>args : any[]
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/anyMappedTypesError.types
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
=== tests/cases/compiler/anyMappedTypesError.ts ===
type Foo = {[P in "bar"]};
>Foo : Foo
>Foo : { bar: any; }

2 changes: 1 addition & 1 deletion tests/baselines/reference/argumentsAsPropertyName.types
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== tests/cases/compiler/argumentsAsPropertyName.ts ===
// target: es5
type MyType = {
>MyType : MyType
>MyType : { arguments: Array<string>; }

arguments: Array<string>
>arguments : string[]
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayDestructuringInSwitch1.types
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
=== tests/cases/compiler/arrayDestructuringInSwitch1.ts ===
export type Expression = BooleanLogicExpression | 'true' | 'false';
>Expression : Expression
>Expression : BooleanLogicExpression | "true" | "false"

export type BooleanLogicExpression = ['and', ...Expression[]] | ['not', Expression];
>BooleanLogicExpression : BooleanLogicExpression
>BooleanLogicExpression : ["and", ...Expression[]] | ["not", Expression]

export function evaluate(expression: Expression): boolean {
>evaluate : (expression: Expression) => boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/compiler/arrayDestructuringInSwitch2.ts ===
type X = { kind: "a", a: [1] } | { kind: "b", a: [] }
>X : X
>X : { kind: "a"; a: [1]; } | { kind: "b"; a: []; }
>kind : "a"
>a : [1]
>kind : "b"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface Dog {
}

type Animal = Cat | Dog;
>Animal : Animal
>Animal : Cat | Dog

declare function assertEqual<T>(value: any, type: T): asserts value is T;
>assertEqual : <T>(value: any, type: T) => asserts value is T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace Example1 {
>Example1 : typeof Example1

type S = { done: boolean, value: number };
>S : S
>S : { done: boolean; value: number; }
>done : boolean
>value : number

type T =
>T : T
>T : { done: true; value: number; } | { done: false; value: number; }

| { done: true, value: number } // T0
>done : true
Expand Down Expand Up @@ -42,12 +42,12 @@ namespace Example2 {
>Example2 : typeof Example2

type S = { a: 0 | 2, b: 4 };
>S : S
>S : { a: 0 | 2; b: 4; }
>a : 0 | 2
>b : 4

type T = { a: 0, b: 1 | 4 } // T0
>T : T
>T : { a: 0; b: 1 | 4; } | { a: 1; b: 2; } | { a: 2; b: 3 | 4; }
>a : 0
>b : 4 | 1

Expand Down Expand Up @@ -78,12 +78,12 @@ namespace Example3 {
>Example3 : typeof Example3

type S = { a: 0 | 2, b: 4 };
>S : S
>S : { a: 0 | 2; b: 4; }
>a : 0 | 2
>b : 4

type T = { a: 0, b: 1 | 4 } // T0
>T : T
>T : { a: 0; b: 1 | 4; } | { a: 1; b: 2 | 4; } | { a: 2; b: 3; }
>a : 0
>b : 4 | 1

Expand Down Expand Up @@ -115,12 +115,12 @@ namespace Example4 {
>Example4 : typeof Example4

type S = { a: 0 | 2, b: 4 };
>S : S
>S : { a: 0 | 2; b: 4; }
>a : 0 | 2
>b : 4

type T = { a: 0, b: 1 | 4 } // T0
>T : T
>T : { a: 0; b: 1 | 4; } | { a: 1; b: 2; } | { a: 2; b: 3 | 4; c: string; }
>a : 0
>b : 4 | 1

Expand Down Expand Up @@ -155,16 +155,16 @@ namespace Example5 {
// 3 discriminant properties with 3 types a piece
// is 27 possible combinations.
type N = 0 | 1 | 2;
>N : N
>N : 0 | 2 | 1

type S = { a: N, b: N, c: N };
>S : S
>S : { a: N; b: N; c: N; }
>a : N
>b : N
>c : N

type T = { a: 0, b: N, c: N }
>T : T
>T : { a: 0; b: N; c: N; } | { a: 1; b: N; c: N; } | { a: 2; b: N; c: N; } | { a: N; b: 0; c: N; } | { a: N; b: 1; c: N; } | { a: N; b: 2; c: N; } | { a: N; b: N; c: 0; } | { a: N; b: N; c: 1; } | { a: N; b: N; c: 2; }
>a : 0
>b : N
>c : N
Expand Down Expand Up @@ -228,7 +228,7 @@ namespace GH14865 {
>GH14865 : typeof GH14865

type Style1 = {
>Style1 : Style1
>Style1 : { type: "A"; data: string; } | { type: "B"; data: string; }

type: "A";
>type : "A"
Expand All @@ -246,7 +246,7 @@ namespace GH14865 {
};

type Style2 = {
>Style2 : Style2
>Style2 : { type: "A" | "B"; data: string; }

type: "A" | "B";
>type : "A" | "B"
Expand Down Expand Up @@ -322,10 +322,10 @@ namespace GH12052 {
>type : "categorical"

type IAxis = ILinearAxis | ICategoricalAxis;
>IAxis : IAxis
>IAxis : ILinearAxis | ICategoricalAxis

type IAxisType = "linear" | "categorical";
>IAxisType : IAxisType
>IAxisType : "linear" | "categorical"

function getAxisType(): IAxisType {
>getAxisType : () => IAxisType
Expand Down Expand Up @@ -381,10 +381,10 @@ namespace GH18421 {
}

type ThingType = 'one' | 'two';
>ThingType : ThingType
>ThingType : "one" | "two"

type Thing = ThingTypeOne | ThingTypeTwo;
>Thing : Thing
>Thing : ThingTypeOne | ThingTypeTwo

function makeNewThing(thingType: ThingType): Thing {
>makeNewThing : (thingType: ThingType) => Thing
Expand All @@ -406,7 +406,7 @@ namespace GH15907 {
>GH15907 : typeof GH15907

type Action = { type: 'activate' } | { type: 'disactivate' };
>Action : Action
>Action : { type: 'activate'; } | { type: 'disactivate'; }
>type : "activate"
>type : "disactivate"

Expand Down Expand Up @@ -445,7 +445,7 @@ namespace GH20889 {
>type : "A2"
}
type AU = A1 | A2;
>AU : AU
>AU : A1 | A2

function foo(obj1: AU) {
>foo : (obj1: AU) => void
Expand All @@ -470,10 +470,10 @@ namespace GH39357 {
>GH39357 : typeof GH39357

type A = ["a", number] | ["b", number] | ["c", string];
>A : A
>A : ["a", number] | ["b", number] | ["c", string]

type B = "a" | "b" | "c";
>B : B
>B : "a" | "b" | "c"

declare const b: B;
>b : B
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ g(async v => v ? "contextuallyTypable" : Promise.reject());
>reject : <T = never>(reason?: any) => Promise<T>

type MyCallback = (thing: string) => void;
>MyCallback : MyCallback
>MyCallback : (thing: string) => void
>thing : string

declare function h(cb: (v: boolean) => MyCallback | PromiseLike<MyCallback>): void;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/awaitedType.types
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type T3 = Awaited<number | Promise<number>>;
>T3 : number

type T4 = Awaited<number | Promise<string>>;
>T4 : T4
>T4 : string | number

type T5 = Awaited<{ then: number }>;
>T5 : { then: number; }
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/awaitedTypeStrictNull.types
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type T3 = Awaited<number | Promise<number>>;
>T3 : number

type T4 = Awaited<number | Promise<string>>;
>T4 : T4
>T4 : string | number

type T5 = Awaited<{ then: number }>;
>T5 : { then: number; }
Expand Down
Loading