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

Limit TS rules to TS files

Open fregante opened this issue 4 years ago • 7 comments

{
	"name": "my-awesome-project",
	"eslintConfig": {
		"extends": [
			"xo",
			"xo-typescript"
		]
	}
}

I haven't tested fully, but it seems that this is not enough to enable the configuration on TS/TSX files. I had to call eslint src --ext ts to have the errors show up. This was not required when I was extending plugin:@typescript-eslint/* directly

fregante avatar Jul 12 '21 12:07 fregante

This enables XO’s config on TS files as well:

  "extends": [
    "plugin:@typescript-eslint/eslint-recommended",
    "xo",
    "xo-typescript"
  ],

I don't see anything special about it here: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/configs/eslint-recommended.ts

fregante avatar Jul 12 '21 12:07 fregante

That's weird. I don't see anything regarding extensions in the typescript-eslint config.

sindresorhus avatar Jul 12 '21 12:07 sindresorhus

Weird indeed. Also confirmed with this repro:

npm init -y
npm install eslint eslint-config-xo eslint-config-xo-typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin
echo '// @ts-ignore' > index.ts
echo '{}' > tsconfig.json
echo '{"extends":["xo","xo-typescript"]}' > .eslintrc
npx eslint .

Then this config produces:

Oops! Something went wrong! :(

ESLint: 7.30.0

No files matching the pattern "." were found.
Please check for typing mistakes in the pattern.

Add that the plugin and run again:

echo '{"extends":["plugin:@typescript-eslint/recommended","xo","xo-typescript"]}' > .eslintrc
npx eslint .
./index.ts
  1:1  error  Use "@ts-expect-error" to ensure an error is actually being suppressed  @typescript-eslint/prefer-ts-expect-error
  1:1  error  Do not use "@ts-ignore" because it alters compilation errors            @typescript-eslint/ban-ts-comment

✖ 2 problems (2 errors, 0 warnings)
  1 error and 0 warnings potentially fixable with the `--fix` option.

Then remove XO’s config:

echo '{"extends":["plugin:@typescript-eslint/recommended"]}' > .eslintrc
npx eslint .
./index.ts
  1:1  error  Do not use "@ts-ignore" because it alters compilation errors  @typescript-eslint/ban-ts-comment

✖ 1 problem (1 error, 0 warnings)

fregante avatar Jul 13 '21 10:07 fregante

Worth an update of docs and config everywhere? https://github.com/typescript-eslint/typescript-eslint/issues/3824#issuecomment-908763764

fregante avatar Aug 30 '21 23:08 fregante

Sure

sindresorhus avatar Sep 05 '21 10:09 sindresorhus

I think I know why, and I think this config should follow suit:

https://github.com/typescript-eslint/typescript-eslint/blob/a8830c681f53af4e23a2cda231b3300159b18e7d/packages/eslint-plugin/src/configs/eslint-recommended.ts#L9

The rules are defined in an override for .ts files, specifying those extensions, whereas eslint-config-xo-typescript just loads all rules in the top level.

If the change isn't made here, I think the user should set up the config as:

{
	"name": "my-awesome-project",
	"eslintConfig": {
		"extends": [
			"xo",
		],
		overrides: [{
			files: ['*.ts', '*.tsx', '*.mts', '*.cts'],
			"extends": [
				"xo-typescript"
			]
		}]
	}
}

Note that this means users will also need to specify TypeScript-related rule changes in an override, if they have any. For example this won't work:

{
	"name": "my-awesome-project",
	"eslintConfig": {
		"extends": [
			"xo",
		],
+		"rules": {
+			"@typescript-eslint/no-unsafe-assignment": "warn" // Must be in overrides too
+		},
		overrides: [{
			files: ['*.ts', '*.tsx', '*.mts', '*.cts'],
			"extends": [
				"xo-typescript"
			],
		}]
	}
}

fregante avatar Nov 11 '23 16:11 fregante