Skip to content

Commit c3d86f3

Browse files
authored
Fix ExpectType with extra whitespace (microsoft#1011)
1 parent b2c15e6 commit c3d86f3

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@definitelytyped/eslint-plugin": patch
3+
---
4+
5+
Fix ExpectType with extra whitespace

packages/eslint-plugin/src/rules/expect.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ function parseAssertions(sourceFile: ts.SourceFile): Assertions {
401401
const duplicates: number[] = [];
402402

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

@@ -410,13 +410,8 @@ function parseAssertions(sourceFile: ts.SourceFile): Assertions {
410410
if (commentMatch === null) {
411411
break;
412412
}
413-
// Match on the contents of that comment so we do nothing in a commented-out assertion,
414-
// i.e. `// foo; // $ExpectType number`
415-
if (!commentMatch[1].startsWith(" $ExpectType ")) {
416-
continue;
417-
}
418413
const line = getLine(commentMatch.index);
419-
const expectedType = commentMatch[1].slice(" $ExpectType ".length);
414+
const expectedType = commentMatch[1].trim();
420415
// Don't bother with the assertion if there are 2 assertions on 1 line. Just fail for the duplicate.
421416
if (typeAssertions.delete(line)) {
422417
duplicates.push(line);

packages/eslint-plugin/test/__file_snapshots__/types/expect/expect-tests.ts.lint

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ types/expect/expect-tests.ts
33
number
44
got:
55
1234 @definitelytyped/expect
6-
14:1 error TypeScript expected type to be:
6+
16:1 error TypeScript expected type to be:
77
NotRightAtAll
88
got:
99
1234 @definitelytyped/expect
10-
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
11-
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
10+
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
11+
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
1212

1313
✖ 4 problems (4 errors, 0 warnings)
1414

@@ -30,6 +30,8 @@ got:
3030
// $ExpectType 1234
3131
expect.foo;
3232

33+
// $ExpectType 1234
34+
expect.foo;
3335

3436
// $ExpectType NotRightAtAll
3537
expect.foo;

packages/eslint-plugin/test/fixtures/types/expect/expect-tests.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ expect.foo;
99
// $ExpectType 1234
1010
expect.foo;
1111

12+
// $ExpectType 1234
13+
expect.foo;
1214

1315
// $ExpectType NotRightAtAll
1416
expect.foo;

0 commit comments

Comments
 (0)