diff --git a/.changeset/eighty-dolls-train.md b/.changeset/eighty-dolls-train.md new file mode 100644 index 0000000000..383f6e21e0 --- /dev/null +++ b/.changeset/eighty-dolls-train.md @@ -0,0 +1,5 @@ +--- +"@definitelytyped/eslint-plugin": patch +--- + +Re-fix ExpectType diff --git a/packages/eslint-plugin/src/rules/expect.ts b/packages/eslint-plugin/src/rules/expect.ts index 730dde8079..e948ab8387 100644 --- a/packages/eslint-plugin/src/rules/expect.ts +++ b/packages/eslint-plugin/src/rules/expect.ts @@ -239,6 +239,8 @@ const zeroSourceLocation: Readonly = { end: { line: 0, column: 0 }, }; +const expectTypeToken = "$ExpectType" + function walk( getLocFromIndex: (index: number) => Readonly, report: Reporter, @@ -285,7 +287,7 @@ function walk( } } - if (sourceFile.isDeclarationFile || !sourceFile.text.includes("$ExpectType")) { + if (sourceFile.isDeclarationFile || !sourceFile.text.includes(expectTypeToken)) { // Normal file. return; } @@ -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; @@ -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); diff --git a/packages/eslint-plugin/test/__file_snapshots__/types/expect/expect-tests.ts.lint b/packages/eslint-plugin/test/__file_snapshots__/types/expect/expect-tests.ts.lint index b2520ea08f..b792b32116 100644 --- a/packages/eslint-plugin/test/__file_snapshots__/types/expect/expect-tests.ts.lint +++ b/packages/eslint-plugin/test/__file_snapshots__/types/expect/expect-tests.ts.lint @@ -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 ==== @@ -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; @@ -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; diff --git a/packages/eslint-plugin/test/fixtures/types/expect/expect-tests.ts b/packages/eslint-plugin/test/fixtures/types/expect/expect-tests.ts index b2689c1a9b..a7ed38e730 100644 --- a/packages/eslint-plugin/test/fixtures/types/expect/expect-tests.ts +++ b/packages/eslint-plugin/test/fixtures/types/expect/expect-tests.ts @@ -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; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1c0b7b17a3..7f4755774e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -382,7 +382,7 @@ importers: version: /typescript@5.4.2 typescript-5.5: specifier: npm:typescript@~5.5.0-0 - version: /typescript@5.5.0-dev.20240306 + version: /typescript@5.5.2 typescript-5.6: specifier: npm:typescript@~5.6.0-0 version: /typescript@5.6.0-dev.20240618 @@ -7116,8 +7116,8 @@ packages: engines: {node: '>=14.17'} hasBin: true - /typescript@5.5.0-dev.20240306: - resolution: {integrity: sha512-8fi3+9qaHf/GQh7oo4X0HDxiWBwzRdGfgyXQlA/qUQ5NBBSbt3LCJFs2nlRBEd9pFBN5RD9Gfzyr0c3b+Ev9+A==} + /typescript@5.5.2: + resolution: {integrity: sha512-NcRtPEOsPFFWjobJEtfihkLCZCXZt/os3zf8nTxjVH3RvTSxjrCamJpbExGvYOF+tFHc3pA65qpdwPbzjohhew==} engines: {node: '>=14.17'} hasBin: true dev: false