Skip to content

Commit f84c78d

Browse files
authored
fix: use typescript-eslint for flat config (#1179)
1 parent 324372b commit f84c78d

File tree

4 files changed

+92
-81
lines changed

4 files changed

+92
-81
lines changed

package-lock.json

Lines changed: 26 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,14 @@
4848
"@eslint-community/eslint-plugin-eslint-comments": "^4.1.0",
4949
"@eslint/js": "^8.56.0",
5050
"@types/eslint": "^8.56.2",
51-
"@typescript-eslint/eslint-plugin": "^7.0.1",
52-
"@typescript-eslint/parser": "^7.0.1",
5351
"deepmerge": "^4.3.1",
5452
"eslint-import-resolver-typescript": "^3.6.1",
5553
"eslint-plugin-import": "^2.29.1",
5654
"eslint-plugin-jsdoc": "^48.0.6",
5755
"eslint-plugin-n": "^16.6.2",
5856
"eslint-plugin-unicorn": "^51.0.1",
59-
"globals": "^14.0.0"
57+
"globals": "^14.0.0",
58+
"typescript-eslint": "^7.0.1"
6059
},
6160
"devDependencies": {
6261
"@types/node": "^18.19.15",

src/configs/typescript.ts

Lines changed: 57 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,66 @@
1-
import tsEslintPlugin from "@typescript-eslint/eslint-plugin";
2-
import tsEslintParser from "@typescript-eslint/parser";
1+
import type { Linter } from "eslint";
2+
import tsEslint from "typescript-eslint";
33
import { merge } from "../merge.js";
4-
import { nonNull } from "../utils.js";
54
import { moduleBase } from "./module-base.js";
65

7-
const tsEsEslintRecomRules = nonNull(
8-
nonNull(tsEslintPlugin.configs["eslint-recommended"].overrides)[0].rules,
9-
);
10-
11-
export const typescript = merge(moduleBase, {
12-
files: ["**/*.{ts,tsx,mts,cts}"],
13-
languageOptions: {
14-
ecmaVersion: "latest",
15-
sourceType: "module",
16-
// @ts-expect-error TSESLint doesn't support flat config typings yet
17-
parser: tsEslintParser,
18-
},
19-
plugins: {
20-
// @ts-expect-error TSESLint doesn't support flat config typings yet
21-
"@typescript-eslint": tsEslintPlugin,
22-
},
23-
rules: {
24-
...tsEsEslintRecomRules,
25-
...tsEslintPlugin.configs.recommended.rules,
6+
export const typescript = merge(
7+
moduleBase,
8+
...(tsEslint.configs.recommended as Linter.FlatConfig[]),
9+
{
10+
files: ["**/*.{ts,tsx,mts,cts}"],
11+
languageOptions: {
12+
ecmaVersion: "latest",
13+
},
14+
rules: {
15+
// Allow special triple slashes comment: "/// <reference />"
16+
"spaced-comment": [
17+
2,
18+
"always",
19+
{ line: { markers: ["/"] }, block: { balanced: true } },
20+
],
2621

27-
// Allow special triple slashes comment: "/// <reference />"
28-
"spaced-comment": [
29-
2,
30-
"always",
31-
{ line: { markers: ["/"] }, block: { balanced: true } },
32-
],
22+
// Check with TSC instead
23+
"n/no-missing-import": 0,
3324

34-
// Check with TSC instead
35-
"n/no-missing-import": 0,
25+
// Extend ESLint rules (enabled in base config)
26+
// NOTE: skip extending stylistic rules that are overrided by prettier
27+
"no-invalid-this": 0,
28+
"@typescript-eslint/no-invalid-this": 2,
29+
"no-loop-func": 0,
30+
"@typescript-eslint/no-loop-func": 2,
31+
"no-unused-expressions": 0,
32+
"@typescript-eslint/no-unused-expressions": [
33+
2,
34+
{
35+
allowShortCircuit: true,
36+
allowTernary: true,
37+
allowTaggedTemplates: true,
38+
},
39+
],
3640

37-
// Extend ESLint rules (enabled in base config)
38-
// NOTE: skip extending stylistic rules that are overrided by prettier
39-
"no-invalid-this": 0,
40-
"@typescript-eslint/no-invalid-this": 2,
41-
"no-loop-func": 0,
42-
"@typescript-eslint/no-loop-func": 2,
43-
"no-unused-expressions": 0,
44-
"@typescript-eslint/no-unused-expressions": [
45-
2,
46-
{
47-
allowShortCircuit: true,
48-
allowTernary: true,
49-
allowTaggedTemplates: true,
50-
},
51-
],
41+
// Override recommended rules
42+
"@typescript-eslint/no-explicit-any": 0,
43+
"@typescript-eslint/no-namespace": [2, { allowDeclarations: true }],
44+
"@typescript-eslint/no-unused-vars": [2, { args: "none" }],
5245

53-
// Override recommended rules
54-
"@typescript-eslint/no-explicit-any": 0,
55-
"@typescript-eslint/no-namespace": [2, { allowDeclarations: true }],
56-
"@typescript-eslint/no-unused-vars": [2, { args: "none" }],
46+
// Stylistic rules
47+
"@typescript-eslint/adjacent-overload-signatures": 2,
48+
"@typescript-eslint/consistent-type-assertions": 2,
49+
"@typescript-eslint/no-empty-interface": 2,
50+
"@typescript-eslint/no-non-null-assertion": 2,
51+
"@typescript-eslint/no-inferrable-types": 2,
5752

58-
// Stylistic rules
59-
"@typescript-eslint/adjacent-overload-signatures": 2,
60-
"@typescript-eslint/consistent-type-assertions": 2,
61-
"@typescript-eslint/no-empty-interface": 2,
62-
"@typescript-eslint/no-non-null-assertion": 2,
63-
"@typescript-eslint/no-inferrable-types": 2,
64-
65-
// Additional rules
66-
"@typescript-eslint/consistent-type-imports": 2,
67-
"@typescript-eslint/no-import-type-side-effects": 2,
68-
// allow require for power-assert and verbatimModuleSyntax
69-
// '@typescript-eslint/no-require-imports': 2,
70-
"@typescript-eslint/prefer-literal-enum-member": 2,
71-
"@typescript-eslint/prefer-ts-expect-error": 2,
72-
},
73-
settings: {
74-
// Don't enable rules that requires TypeScript parser for perf reasons.
75-
"import/resolver": "typescript",
53+
// Additional rules
54+
"@typescript-eslint/consistent-type-imports": 2,
55+
"@typescript-eslint/no-import-type-side-effects": 2,
56+
// allow require for power-assert and verbatimModuleSyntax
57+
// '@typescript-eslint/no-require-imports': 2,
58+
"@typescript-eslint/prefer-literal-enum-member": 2,
59+
"@typescript-eslint/prefer-ts-expect-error": 2,
60+
},
61+
settings: {
62+
// Don't enable rules that requires TypeScript parser for perf reasons.
63+
"import/resolver": "typescript",
64+
},
7665
},
77-
});
66+
);

src/merge.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import type { Linter } from "eslint";
44

55
type FlatConfig = Linter.FlatConfig;
66

7-
export function merge(
8-
first: FlatConfig,
9-
second: FlatConfig,
10-
...rest: FlatConfig[]
11-
): FlatConfig {
7+
export function merge(first: FlatConfig, ...rest: FlatConfig[]): FlatConfig {
8+
if (rest.length === 0) {
9+
return { ...first };
10+
}
11+
const second = rest[0];
1212
const merged = {
1313
...first,
1414
...second,
@@ -42,8 +42,8 @@ export function merge(
4242
merged.languageOptions.ecmaVersion;
4343
}
4444

45-
if (rest.length > 0) {
46-
return merge(merged, rest[0], ...rest.slice(1));
45+
if (rest.length > 1) {
46+
return merge(merged, rest[1], ...rest.slice(2));
4747
} else {
4848
return merged;
4949
}

0 commit comments

Comments
 (0)