Skip to content

Commit

Permalink
chore: upgrade typescript-eslint to v8
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Nov 22, 2024
1 parent 1848169 commit 55cf874
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 133 deletions.
6 changes: 6 additions & 0 deletions .changeset/tricky-islands-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fenge/eslint-config": minor
"smells": patch
---

chore: upgrade typescript-eslint to v8
4 changes: 2 additions & 2 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"dependencies": {
"@fenge/eslint-plugin": "workspace:*",
"@fenge/eslint-plugin-ts": "workspace:*",
"@typescript-eslint/eslint-plugin": "7.16.1",
"@typescript-eslint/parser": "7.16.1",
"@typescript-eslint/eslint-plugin": "8.15.0",
"@typescript-eslint/parser": "8.15.0",
"confusing-browser-globals": "1.0.11",
"eslint-plugin-deprecation": "3.0.0",
"eslint-plugin-es-x": "8.4.1",
Expand Down
80 changes: 31 additions & 49 deletions packages/eslint-config/src/config/typescript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,38 @@ import { typescript } from "./typescript.js";
await describe("ts config", async () => {
await it("ts main config rules values should be error", () => {
Object.entries(typescript()[0].rules).forEach(([key, value]) => {
// Key is js rule, value is ts rule
// https://typescript-eslint.io/rules/?=extension
const extensionRuleKeys = [
"block-spacing",
"brace-style",
"class-methods-use-this",
"comma-dangle",
"comma-spacing",
"consistent-return",
"default-param-last",
"dot-notation",
"func-call-spacing",
"indent",
"init-declarations",
"key-spacing",
"keyword-spacing",
"lines-around-comment",
"lines-between-class-members",
"max-params",
"no-array-constructor",
"no-dupe-class-members",
"no-empty-function",
"no-extra-parens",
"no-extra-semi",
"no-implied-eval",
"no-invalid-this",
"no-loop-func",
// "no-loss-of-precision",
"no-magic-numbers",
"no-redeclare",
"no-restricted-imports",
"no-shadow",
"no-throw-literal",
"no-unused-expressions",
"no-unused-vars",
"no-use-before-define",
"no-useless-constructor",
"object-curly-spacing",
"only-throw-error", // this rule based on 'eslint/no-throw-literal'
"padding-line-between-statements",
"prefer-destructuring",
"prefer-promise-reject-errors",
"quotes",
"require-await",
"return-await", // this rule based on 'eslint/no-return-await' instead of 'eslint/return-await'
"semi",
"space-before-blocks",
"space-before-function-paren",
"space-infix-ops",
];
if (extensionRuleKeys.includes(key)) {
const extensionRuleMap = {
"class-methods-use-this": "@typescript-eslint/class-methods-use-this",
"consistent-return": "@typescript-eslint/consistent-return",
"default-param-last": "@typescript-eslint/default-param-last",
"dot-notation": "@typescript-eslint/dot-notation",
"init-declarations": "@typescript-eslint/init-declarations",
"max-params": "@typescript-eslint/max-params",
"no-array-constructor": "@typescript-eslint/no-array-constructor",
"no-dupe-class-members": "@typescript-eslint/no-dupe-class-members",
"no-empty-function": "@typescript-eslint/no-empty-function",
"no-implied-eval": "@typescript-eslint/no-implied-eval",
"no-invalid-this": "@typescript-eslint/no-invalid-this",
"no-loop-func": "@typescript-eslint/no-loop-func",
// "no-loss-of-precision": "@typescript-eslint/no-loss-of-precision", // This rule has been deprecated
"no-magic-numbers": "@typescript-eslint/no-magic-numbers",
"no-redeclare": "@typescript-eslint/no-redeclare",
"no-restricted-imports": "@typescript-eslint/no-restricted-imports",
"no-shadow": "@typescript-eslint/no-shadow",
"no-unused-expressions": "@typescript-eslint/no-unused-expressions",
"no-unused-vars": "@typescript-eslint/no-unused-vars",
"no-use-before-define": "@typescript-eslint/no-use-before-define",
"no-useless-constructor": "@typescript-eslint/no-useless-constructor",
"no-throw-literal": "@typescript-eslint/only-throw-error",
"prefer-destructuring": "@typescript-eslint/prefer-destructuring",
"prefer-promise-reject-errors":
"@typescript-eslint/prefer-promise-reject-errors",
"require-await": "@typescript-eslint/require-await",
"no-return-await": "@typescript-eslint/return-await", // no-return-await has been deprecated
} as const;
if (key in extensionRuleMap) {
assert.strictEqual(getValueString(value), "off");
} else {
assert.strictEqual(getValueString(value), "error");
Expand Down
127 changes: 56 additions & 71 deletions packages/eslint-config/src/config/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,69 @@ export function typescript(project?: string) {
const jsConfig = javascript()[0];

const getTsExtensionRules = () => {
// Key is js rule, value is ts rule
// https://typescript-eslint.io/rules/?=extension
const extensionRuleKeys = [
"block-spacing",
"brace-style",
"class-methods-use-this",
"comma-dangle",
"comma-spacing",
"consistent-return",
"default-param-last",
"dot-notation",
"func-call-spacing",
"indent",
"init-declarations",
"key-spacing",
"keyword-spacing",
"lines-around-comment",
"lines-between-class-members",
"max-params",
"no-array-constructor",
"no-dupe-class-members",
"no-empty-function",
"no-extra-parens",
"no-extra-semi",
"no-implied-eval",
"no-invalid-this",
"no-loop-func",
// "no-loss-of-precision",
"no-magic-numbers",
"no-redeclare",
"no-restricted-imports",
"no-shadow",
"no-throw-literal",
"no-unused-expressions",
"no-unused-vars",
"no-use-before-define",
"no-useless-constructor",
"object-curly-spacing",
"only-throw-error", // this rule based on 'eslint/no-throw-literal'
"padding-line-between-statements",
"prefer-destructuring",
"prefer-promise-reject-errors",
"quotes",
"require-await",
"return-await", // this rule based on 'eslint/no-return-await' instead of 'eslint/return-await'
"semi",
"space-before-blocks",
"space-before-function-paren",
"space-infix-ops",
] as const;
type ExtensionRuleKey = (typeof extensionRuleKeys)[number];
type JsConfigRuleKey = keyof typeof jsConfig.rules;
const extensionRuleMap = {
"class-methods-use-this": "@typescript-eslint/class-methods-use-this",
"consistent-return": "@typescript-eslint/consistent-return",
"default-param-last": "@typescript-eslint/default-param-last",
"dot-notation": "@typescript-eslint/dot-notation",
"init-declarations": "@typescript-eslint/init-declarations",
"max-params": "@typescript-eslint/max-params",
"no-array-constructor": "@typescript-eslint/no-array-constructor",
"no-dupe-class-members": "@typescript-eslint/no-dupe-class-members",
"no-empty-function": "@typescript-eslint/no-empty-function",
"no-implied-eval": "@typescript-eslint/no-implied-eval",
"no-invalid-this": "@typescript-eslint/no-invalid-this",
"no-loop-func": "@typescript-eslint/no-loop-func",
// "no-loss-of-precision": "@typescript-eslint/no-loss-of-precision", // This rule has been deprecated
"no-magic-numbers": "@typescript-eslint/no-magic-numbers",
"no-redeclare": "@typescript-eslint/no-redeclare",
"no-restricted-imports": "@typescript-eslint/no-restricted-imports",
"no-shadow": "@typescript-eslint/no-shadow",
"no-unused-expressions": "@typescript-eslint/no-unused-expressions",
"no-unused-vars": "@typescript-eslint/no-unused-vars",
"no-use-before-define": "@typescript-eslint/no-use-before-define",
"no-useless-constructor": "@typescript-eslint/no-useless-constructor",
"no-throw-literal": "@typescript-eslint/only-throw-error",
"prefer-destructuring": "@typescript-eslint/prefer-destructuring",
"prefer-promise-reject-errors":
"@typescript-eslint/prefer-promise-reject-errors",
"require-await": "@typescript-eslint/require-await",
"no-return-await": "@typescript-eslint/return-await", // no-return-await has been deprecated
} as const;

type JsExtensionKey = Extract<ExtensionRuleKey, JsConfigRuleKey>; // Extract
type TsExtensionKey = `@typescript-eslint/${JsExtensionKey}`;

type GetJsKey<T extends TsExtensionKey> =
T extends `@typescript-eslint/${infer K}` ? K : never;
type JsResult = {
[Key in JsExtensionKey]: "off";
type Js2TsRuleMap = typeof extensionRuleMap;
type Ts2JsRuleMap = {
[K in keyof Js2TsRuleMap as Js2TsRuleMap[K]]: K; // reverse
};

type JsExtensionKey = Extract<
keyof Js2TsRuleMap,
keyof typeof jsConfig.rules
>; // Extract
type TsExtensionKey = Js2TsRuleMap[JsExtensionKey];

type JsResult = Record<JsExtensionKey, "off">;
type TsResult = {
[Key in TsExtensionKey]: (typeof jsConfig.rules)[GetJsKey<Key>];
[Key in TsExtensionKey]: (typeof jsConfig.rules)[Ts2JsRuleMap[Key]];
};
type Result = JsResult & TsResult;

const extensionRuleKeySet = new Set<string>(extensionRuleKeys);
return Object.entries(jsConfig.rules)
.filter(([key]) => extensionRuleKeySet.has(key))
.reduce(
(prev, [jsRuleKey, jsRuleValue]) => ({
...prev,
[jsRuleKey]: "off",
[`@typescript-eslint/${jsRuleKey}`]: jsRuleValue,
}),
{} as Result,
);
const isInExtensionRuleMap = (
key: string,
): key is keyof typeof extensionRuleMap => key in extensionRuleMap;
return Object.entries(jsConfig.rules).reduce(
(result, [jsRuleKey, jsRuleValue]) =>
isInExtensionRuleMap(jsRuleKey)
? {
...result,
[jsRuleKey]: "off",
[extensionRuleMap[jsRuleKey]]: jsRuleValue,
}
: result,
{} as Result,
);
};

return [
Expand Down Expand Up @@ -122,7 +109,6 @@ export function typescript(project?: string) {
"@typescript-eslint/adjacent-overload-signatures": "error",
// "@typescript-eslint/array-type": ["error", 'array-simple'], // The default option is 'array'. Not very sure if we need to change the option. So disabled it.
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/ban-types": "error",
"@typescript-eslint/consistent-generic-constructors": "error",
"@typescript-eslint/consistent-indexed-object-style": "error",
"@typescript-eslint/consistent-type-assertions": [
Expand Down Expand Up @@ -204,7 +190,6 @@ export function typescript(project?: string) {
"@typescript-eslint/no-unsafe-declaration-merging": "error",
// '@typescript-eslint/no-unsafe-function-type': "error",
// "@typescript-eslint/no-wrapper-object-types": "error",
"@typescript-eslint/only-throw-error": "error",
"@typescript-eslint/prefer-as-const": "error",
"@typescript-eslint/prefer-function-type": "error",
"@typescript-eslint/prefer-optional-chain": "error",
Expand Down
17 changes: 13 additions & 4 deletions packages/eslint-config/test/no-duplicated.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,25 @@ await describe("no duplicated", async () => {
.forEach((rule) => {
if (
[
// "@typescript-eslint/consistent-type-assertions",
"@typescript-eslint/no-floating-promises",
// "@typescript-eslint/no-non-null-assertion",
"@typescript-eslint/unbound-method",
"import/no-default-export",
// extension rule
"@typescript-eslint/dot-notation",
"@typescript-eslint/return-await",
].includes(rule)
) {
assert.strictEqual(count(configContent, rule), 2);
assert.strictEqual(
count(configContent, rule),
2,
`The rule name is ${rule}`,
);
} else {
assert.strictEqual(count(configContent, rule) <= 1, true);
assert.strictEqual(
count(configContent, rule) <= 1,
true,
`Duplicated rule name is ${rule}`,
);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@types/estree": "1.0.6",
"@types/json-schema": "7.0.15",
"@types/node": "22.9.1",
"@typescript-eslint/parser": "7.16.1",
"@typescript-eslint/parser": "8.15.0",
"eslint": "8.57.1",
"outdent": "0.8.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@types/estree": "1.0.6",
"@types/json-schema": "7.0.15",
"@types/node": "22.9.1",
"@typescript-eslint/parser": "7.16.1",
"@typescript-eslint/parser": "8.15.0",
"eslint": "8.57.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { noDeclaresInTsFile } from "./no-declares-in-ts-file.js";
const codes = [
"declare class A {}",

"declare var A: number = 123",
"declare let A: number = 123",
"declare const A: number = 123",
"declare var A: number",
"declare let A: number",
"declare const A: number",
"declare function A(a: string): number",
"declare enum A{A1,A2}",
"declare namespace A{}",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@types/estree": "1.0.6",
"@types/json-schema": "7.0.15",
"@types/node": "22.9.1",
"@typescript-eslint/parser": "7.16.1",
"@typescript-eslint/parser": "8.15.0",
"eslint": "8.57.1"
}
}
2 changes: 1 addition & 1 deletion packages/smells/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"build": "tsc -p tsconfig.build.json"
},
"dependencies": {
"@typescript-eslint/typescript-estree": "7.16.1",
"@typescript-eslint/typescript-estree": "8.15.0",
"commander": "12.1.0",
"globby": "14.0.2"
},
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/promise-catch.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/method-signature-style */
/* eslint-disable @typescript-eslint/no-duplicate-type-constituents */
/**
* Represents the completion of an asynchronous operation
*/
Expand Down

0 comments on commit 55cf874

Please sign in to comment.