typescript-eslint icon indicating copy to clipboard operation
typescript-eslint copied to clipboard

Bug: `--fix` unexpectedly removes eslint-disable comment

Open andersk opened this issue 1 year ago • 2 comments

Before You File a Bug Report Please Confirm You Have Done The Following...

  • [X] I have tried restarting my IDE and the issue persists.
  • [X] I have updated to the latest version of the packages.
  • [X] I have searched for related issues and found none that matched my issue.
  • [X] I have read the FAQ and my problem is not listed.

Issue Description

typescript-eslint correctly flags a @typescript-eslint/no-unsafe-member-access error when accessing an untyped module.

However, if I ignore this error with an eslint-disable-next-line comment, and reportUnusedDisableDirectives is enabled, the comment is unexpectedly removed by eslint --fix.

With the comment removed, the error is not reported when running with --fix, but it is reported when running without --fix.

Reproduction Repository Link

https://gist.github.com/andersk/5bacfa857d080e6ed6627063f0700694

Repro Steps

$ git clone https://gist.github.com/andersk/5bacfa857d080e6ed6627063f0700694 test
$ cd test
$ npm i
$ npx eslint test.ts
$ npx eslint --fix test.ts
$ git diff
diff --git a/test.ts b/test.ts
index 533595b..049fb4d 100644
--- a/test.ts
+++ b/test.ts
@@ -1,5 +1,5 @@
 // @ts-expect-error This module is not typed
 import * as untyped from "./untyped";
 
-// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
+ 
 console.log(untyped.hello);
$ npx eslint test.ts
/tmp/test/test.ts
  5:21  error  Unsafe member access .hello on an `error` typed value  @typescript-eslint/no-unsafe-member-access

✖ 1 problem (1 error, 0 warnings)

Versions

package version
@typescript-eslint/eslint-plugin 8.6.0
@typescript-eslint/parser 8.6.0
@typescript-eslint/scope-manager 8.6.0
@typescript-eslint/typescript-estree 8.6.0
@typescript-eslint/type-utils 8.6.0
@typescript-eslint/utils 8.6.0
TypeScript 5.5.4
ESLint 9.10.0
node 20.17.0

andersk avatar Sep 18 '24 21:09 andersk

Confirmed locally.... weird!

wondering if it's to do with single run inference related things? Note that the report also shows up as unused in my editor.

kirkwaiblinger avatar Sep 18 '24 22:09 kirkwaiblinger

Running eslint test.ts without --fix implies: single run === true -> TS programs are created from tsconfig.json specified in eslint.config.mjs https://github.com/typescript-eslint/typescript-eslint/blob/2055cfbbdef5d9b7ee4ed7180f0af93eed245235/packages/typescript-estree/src/parser.ts#L168-L196 Running eslint --fix test.ts implies: single run === false -> create watch program -> create watch compiler host -> watch compiler host created with allowJs: true since it is set as the default compiler option https://github.com/typescript-eslint/typescript-eslint/blob/2055cfbbdef5d9b7ee4ed7180f0af93eed245235/packages/typescript-estree/src/create-program/shared.ts#L34-L52

In the second case (with --fix), the TS program allows importing .js files. Therefore, untyped.hello is not reported as an "error typed value".


optionsToExtend (containing allowJs: true) passed to watch compiler host will take precedence over compiler options from the project: 'tsconfig.json' specified. Even if user specified "allowJs": false, importing JS will be allowed during eslint file.ts run. I think we should exclude allowJs from watch compiler host options. WDYT?


A very similar issue was reported in some repositories after single run inference was enabled by default: https://github.com/typescript-eslint/typescript-eslint/issues/9749

auvred avatar Oct 03 '24 13:10 auvred