Skip to content

Commit

Permalink
Re-fix ExpectType (#1016)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Jun 24, 2024
1 parent 488f41d commit 6dd1778
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-dolls-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@definitelytyped/eslint-plugin": patch
---

Re-fix ExpectType
15 changes: 8 additions & 7 deletions packages/eslint-plugin/src/rules/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ const zeroSourceLocation: Readonly<TSESTree.SourceLocation> = {
end: { line: 0, column: 0 },
};

const expectTypeToken = "$ExpectType"

function walk(
getLocFromIndex: (index: number) => Readonly<TSESTree.Position>,
report: Reporter,
Expand Down Expand Up @@ -285,7 +287,7 @@ function walk(
}
}

if (sourceFile.isDeclarationFile || !sourceFile.text.includes("$ExpectType")) {
if (sourceFile.isDeclarationFile || !sourceFile.text.includes(expectTypeToken)) {
// Normal file.
return;
}
Expand Down Expand Up @@ -401,7 +403,7 @@ function parseAssertions(sourceFile: ts.SourceFile): Assertions {
const duplicates: number[] = [];

const { text } = sourceFile;
const commentRegexp = /\/\/(.*)/g;
const commentRegexp = /^(.*?)\/\/(.*)$/gm;
const lineStarts = sourceFile.getLineStarts();
let curLine = 0;

Expand All @@ -410,13 +412,12 @@ function parseAssertions(sourceFile: ts.SourceFile): Assertions {
if (commentMatch === null) {
break;
}
// Match on the contents of that comment so we do nothing in a commented-out assertion,
// i.e. `// foo; // $ExpectType number`
if (!commentMatch[1].startsWith(" $ExpectType ")) {
const comment = commentMatch[2].trim();
if (!comment.startsWith(expectTypeToken)) {
continue;
}
const line = getLine(commentMatch.index);
const expectedType = commentMatch[1].slice(" $ExpectType ".length);
const line = getLine(commentMatch.index + commentMatch[1].length);
const expectedType = comment.slice(expectTypeToken.length).trim();
// Don't bother with the assertion if there are 2 assertions on 1 line. Just fail for the duplicate.
if (typeAssertions.delete(line)) {
duplicates.push(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ types/expect/expect-tests.ts
number
got:
1234 @definitelytyped/expect
14:1 error TypeScript expected type to be:
24:1 error TypeScript expected type to be:
NotRightAtAll
got:
1234 @definitelytyped/expect
45:1 error Cannot match a node to this assertion. If this is a multiline function call, ensure the assertion is on the line above @definitelytyped/expect
49:1 error Cannot match a node to this assertion. If this is a multiline function call, ensure the assertion is on the line above @definitelytyped/expect
26:1 error TypeScript expected type to be:
NotRightAtAll
got:
1234 @definitelytyped/expect
28:1 error TypeScript expected type to be:
NotRightAtAll
got:
1234 @definitelytyped/expect
67:1 error Cannot match a node to this assertion. If this is a multiline function call, ensure the assertion is on the line above @definitelytyped/expect
71:1 error Cannot match a node to this assertion. If this is a multiline function call, ensure the assertion is on the line above @definitelytyped/expect

4 problems (4 errors, 0 warnings)
6 problems (6 errors, 0 warnings)

==== types/expect/expect-tests.ts ====

Expand All @@ -30,6 +38,16 @@ got:
// $ExpectType 1234
expect.foo;

// $ExpectType 1234
expect.foo;

//$ExpectType 1234
expect.foo;

expect.foo; // $ExpectType 1234

const foo = expect.foo;
foo; // $ExpectType 1234

// $ExpectType NotRightAtAll
expect.foo;
Expand All @@ -39,6 +57,28 @@ got:
!!! : got:
!!! : 1234

expect.foo; // $ExpectType NotRightAtAll
~~~~~~~~~~
!!! @definitelytyped/expect: TypeScript expected type to be:
!!! : NotRightAtAll
!!! : got:
!!! : 1234

foo; // $ExpectType NotRightAtAll
~~~
!!! @definitelytyped/expect: TypeScript expected type to be:
!!! : NotRightAtAll
!!! : got:
!!! : 1234

// These should not be matched.
// // $ExpectType NotRightAtAll
expect.foo;

expect.foo; // // $ExpectType NotRightAtAll

expect.foo; /// $ExpectType NotRightAtAll


// $ExpectType string | number | undefined
expect.aUnion;
Expand Down
22 changes: 22 additions & 0 deletions packages/eslint-plugin/test/fixtures/types/expect/expect-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,32 @@ expect.foo;
// $ExpectType 1234
expect.foo;

// $ExpectType 1234
expect.foo;

//$ExpectType 1234
expect.foo;

expect.foo; // $ExpectType 1234

const foo = expect.foo;
foo; // $ExpectType 1234

// $ExpectType NotRightAtAll
expect.foo;

expect.foo; // $ExpectType NotRightAtAll

foo; // $ExpectType NotRightAtAll

// These should not be matched.
// // $ExpectType NotRightAtAll
expect.foo;

expect.foo; // // $ExpectType NotRightAtAll

expect.foo; /// $ExpectType NotRightAtAll


// $ExpectType string | number | undefined
expect.aUnion;
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6dd1778

Please sign in to comment.