Skip to content

Commit

Permalink
Fix ExpectType with extra whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Jun 18, 2024
1 parent ebe542b commit 94fe21c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-stingrays-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@definitelytyped/eslint-plugin": patch
---

Fix ExpectType with extra whitespace
9 changes: 2 additions & 7 deletions packages/eslint-plugin/src/rules/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ function parseAssertions(sourceFile: ts.SourceFile): Assertions {
const duplicates: number[] = [];

const { text } = sourceFile;
const commentRegexp = /\/\/(.*)/g;
const commentRegexp = /\/\/\s*\$ExpectType\s+(.*)/g;
const lineStarts = sourceFile.getLineStarts();
let curLine = 0;

Expand All @@ -410,13 +410,8 @@ 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 ")) {
continue;
}
const line = getLine(commentMatch.index);
const expectedType = commentMatch[1].slice(" $ExpectType ".length);
const expectedType = commentMatch[1].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,12 +3,12 @@ types/expect/expect-tests.ts
number
got:
1234 @definitelytyped/expect
14:1 error TypeScript expected type to be:
16: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
47: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
51: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)

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

// $ExpectType 1234
expect.foo;

// $ExpectType NotRightAtAll
expect.foo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ expect.foo;
// $ExpectType 1234
expect.foo;

// $ExpectType 1234
expect.foo;

// $ExpectType NotRightAtAll
expect.foo;
Expand Down

0 comments on commit 94fe21c

Please sign in to comment.