Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

For $ExpectType assertions, alphabetically sort unions and intersections #61

Closed
wants to merge 4 commits into from
Closed
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/rules/expectRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,12 @@ function getExpectTypeFailures(

const actual = checker.typeToString(type, /*enclosingDeclaration*/ undefined, ts.TypeFormatFlags.NoTruncation);
if (actual !== expected) {
unmetExpectations.push({ node, expected, actual });
console.log("!!!", actual);
Copy link

@fbartho fbartho Apr 11, 2018

Choose a reason for hiding this comment

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

Debug logs probably need to be cleaned up in this method/if-block?

const actualSorted = fixupType(actual);
if (actualSorted !== expected) {
console.log("???", actualSorted);
unmetExpectations.push({ node, expected, actual });
}
}

typeAssertions.delete(line);
Expand All @@ -308,6 +313,27 @@ function getExpectTypeFailures(
}
}

function fixupType(s: string): string {
// Don't mess up `{ a: number | string }` into `string } | { a: number`
if (/[\<\(\[\{]/.test(s)) {
return s;
}
return fixupUnions(s);
}

function fixupUnions(s: string): string {
const splitter = " | ";
return s.split(splitter).map(fixupIntersections).sort().join(splitter);
}

function fixupIntersections(s: string): string {
if (s.startsWith("(") && s.endsWith(")")) {
return `(${fixupIntersections(s.slice(1, s.length - 1))})`;
}
const splitter = " & ";
return s.split(splitter).sort().join(splitter);
}

function lineOfPosition(pos: number, sourceFile: SourceFile): number {
return sourceFile.getLineAndCharacterOfPosition(pos).line;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Type definitions for dt-header 1.0
// Project: https://github.com/bobby-headers/dt-header
// Definitions by: Jane Doe <https://github.com/jane doe>
~ [Error parsing header. Expected: '>']
~ [Error parsing header. Expected: /\<https\:\/\/github\.com\/([a-zA-Z\d\-]+)\>/]
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
2 changes: 1 addition & 1 deletion test/dt-header/wrong/types/bad-url/index.d.ts.lint
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Type definitions for dt-header 1.0
// Project: https://github.com/bobby-headers/dt-header
// Definitions by: Jane Doe <https://github.org/janedoe>
~ [Error parsing header. Expected: '<https://github.com/']
~ [Error parsing header. Expected: /\<https\:\/\/github\.com\/([a-zA-Z\d\-]+)\>/]
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
14 changes: 14 additions & 0 deletions test/expect/expectType.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@ declare function f(

// Test that we never truncate types.
f; // $ExpectType (one: number, two: [number, number], three: [number, number, number], four: [number, number, number, number]) => number

// Test that a union could be either exactly what tsc output, or a sorted version of that.
declare const x: "foo" | "bar";
x; // $ExpectType "foo" | "bar"
x; // $ExpectType "bar" | "foo"

type A = { a: number };
type B = { b: number };
type C = { c: number };

// Test that we don't try to sort nested unions.
declare class Generic<T> {}
declare const generic: Generic<string | number>;
generic; // $ExpectType Generic<string | number>