From 1312b63f7407fe74a01f07024650cd82ae006933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Tue, 19 Dec 2023 17:47:55 +0800 Subject: [PATCH] style: add `@typescript-eslint/eslint-plugin` plugin to eslint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 丶远方 --- .eslintrc.cjs | 28 ++++++++-- .prettierrc | 3 +- package.json | 1 + pnpm-lock.yaml | 118 ++++++++++++++++++++++++++++++++++++++++++- tsconfig.eslint.json | 4 ++ 5 files changed, 148 insertions(+), 6 deletions(-) create mode 100644 tsconfig.eslint.json diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 10640525eda..e4ea0886151 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -16,9 +16,10 @@ const banConstEnum = { module.exports = { parser: '@typescript-eslint/parser', parserOptions: { - sourceType: 'module' + sourceType: 'module', + project: './tsconfig.eslint.json' }, - plugins: ['jest', 'import'], + plugins: ['jest', 'import', '@typescript-eslint'], rules: { 'no-debugger': 'error', // most of the codebase are expected to be env agnostic @@ -38,7 +39,23 @@ module.exports = { 'import/no-nodejs-modules': [ 'error', { allow: builtinModules.map(mod => `node:${mod}`) } - ] + ], + // This rule enforces the preference for using '@ts-expect-error' comments in TypeScript + // code to indicate intentional type errors, improving code clarity and maintainability. + '@typescript-eslint/prefer-ts-expect-error': 'error', + // Avoid unnecessary type assertions + '@typescript-eslint/no-unnecessary-type-assertion': 'error', + // Enforce the use of 'import type' for importing types + '@typescript-eslint/consistent-type-imports': [ + 'error', + { + fixStyle: 'inline-type-imports' + } + ], + // Enforce the use of top-level import type qualifier when an import only has specifiers with inline type qualifiers + '@typescript-eslint/no-import-type-side-effects': 'error', + // Ensure consistent usage of indexed object access style in TypeScript code. + '@typescript-eslint/consistent-indexed-object-style': 'error' }, overrides: [ // tests, no restrictions (runs in Node / jest with jsdom) @@ -103,6 +120,11 @@ module.exports = { rules: { 'import/no-nodejs-modules': ['error', { allow: builtinModules }] } + }, + { + // disable type checking for JS files + files: ['*.js'], + extends: ['plugin:@typescript-eslint/disable-type-checked'] } ] } diff --git a/.prettierrc b/.prettierrc index 759232e7cf6..9f8ecd9f03a 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,6 @@ { "semi": false, "singleQuote": true, - "arrowParens": "avoid" + "arrowParens": "avoid", + "trailingComma": "none" } diff --git a/package.json b/package.json index 749b5047bfa..89671da2ab3 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "@types/minimist": "^1.2.5", "@types/node": "^20.10.4", "@types/semver": "^7.5.5", + "@typescript-eslint/eslint-plugin": "^6.15.0", "@typescript-eslint/parser": "^6.13.2", "@vitest/coverage-istanbul": "^1.0.4", "@vue/consolidate": "0.17.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 61f623ebaa5..994ed162035 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -47,6 +47,9 @@ importers: '@types/semver': specifier: ^7.5.5 version: 7.5.5 + '@typescript-eslint/eslint-plugin': + specifier: ^6.15.0 + version: 6.15.0(@typescript-eslint/parser@6.13.2)(eslint@8.55.0)(typescript@5.2.2) '@typescript-eslint/parser': specifier: ^6.13.2 version: 6.13.2(eslint@8.55.0)(typescript@5.2.2) @@ -79,7 +82,7 @@ importers: version: /eslint-plugin-i@2.29.1(@typescript-eslint/parser@6.13.2)(eslint@8.55.0) eslint-plugin-jest: specifier: ^27.6.0 - version: 27.6.0(eslint@8.55.0)(typescript@5.2.2) + version: 27.6.0(@typescript-eslint/eslint-plugin@6.15.0)(eslint@8.55.0)(typescript@5.2.2) estree-walker: specifier: ^2.0.2 version: 2.0.2 @@ -1580,6 +1583,35 @@ packages: dev: true optional: true + /@typescript-eslint/eslint-plugin@6.15.0(@typescript-eslint/parser@6.13.2)(eslint@8.55.0)(typescript@5.2.2): + resolution: {integrity: sha512-j5qoikQqPccq9QoBAupOP+CBu8BaJ8BLjaXSioDISeTZkVO3ig7oSIKh3H+rEpee7xCXtWwSB4KIL5l6hWZzpg==} + engines: {node: ^16.0.0 || >=18.0.0} + peerDependencies: + '@typescript-eslint/parser': ^6.0.0 || ^6.0.0-alpha + eslint: ^7.0.0 || ^8.0.0 + typescript: '*' + peerDependenciesMeta: + typescript: + optional: true + dependencies: + '@eslint-community/regexpp': 4.9.1 + '@typescript-eslint/parser': 6.13.2(eslint@8.55.0)(typescript@5.2.2) + '@typescript-eslint/scope-manager': 6.15.0 + '@typescript-eslint/type-utils': 6.15.0(eslint@8.55.0)(typescript@5.2.2) + '@typescript-eslint/utils': 6.15.0(eslint@8.55.0)(typescript@5.2.2) + '@typescript-eslint/visitor-keys': 6.15.0 + debug: 4.3.4 + eslint: 8.55.0 + graphemer: 1.4.0 + ignore: 5.2.4 + natural-compare: 1.4.0 + semver: 7.5.4 + ts-api-utils: 1.0.3(typescript@5.2.2) + typescript: 5.2.2 + transitivePeerDependencies: + - supports-color + dev: true + /@typescript-eslint/parser@6.13.2(eslint@8.55.0)(typescript@5.2.2): resolution: {integrity: sha512-MUkcC+7Wt/QOGeVlM8aGGJZy1XV5YKjTpq9jK6r6/iLsGXhBVaGP5N0UYvFsu9BFlSpwY9kMretzdBH01rkRXg==} engines: {node: ^16.0.0 || >=18.0.0} @@ -1617,6 +1649,34 @@ packages: '@typescript-eslint/visitor-keys': 6.13.2 dev: true + /@typescript-eslint/scope-manager@6.15.0: + resolution: {integrity: sha512-+BdvxYBltqrmgCNu4Li+fGDIkW9n//NrruzG9X1vBzaNK+ExVXPoGB71kneaVw/Jp+4rH/vaMAGC6JfMbHstVg==} + engines: {node: ^16.0.0 || >=18.0.0} + dependencies: + '@typescript-eslint/types': 6.15.0 + '@typescript-eslint/visitor-keys': 6.15.0 + dev: true + + /@typescript-eslint/type-utils@6.15.0(eslint@8.55.0)(typescript@5.2.2): + resolution: {integrity: sha512-CnmHKTfX6450Bo49hPg2OkIm/D/TVYV7jO1MCfPYGwf6x3GO0VU8YMO5AYMn+u3X05lRRxA4fWCz87GFQV6yVQ==} + engines: {node: ^16.0.0 || >=18.0.0} + peerDependencies: + eslint: ^7.0.0 || ^8.0.0 + typescript: '*' + peerDependenciesMeta: + typescript: + optional: true + dependencies: + '@typescript-eslint/typescript-estree': 6.15.0(typescript@5.2.2) + '@typescript-eslint/utils': 6.15.0(eslint@8.55.0)(typescript@5.2.2) + debug: 4.3.4 + eslint: 8.55.0 + ts-api-utils: 1.0.3(typescript@5.2.2) + typescript: 5.2.2 + transitivePeerDependencies: + - supports-color + dev: true + /@typescript-eslint/types@5.62.0: resolution: {integrity: sha512-87NVngcbVXUahrRTqIK27gD2t5Cu1yuCXxbLcFtCzZGlfyVWWh8mLHkoxzjsB6DDNnvdL+fW8MiwPEJyGJQDgQ==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} @@ -1627,6 +1687,11 @@ packages: engines: {node: ^16.0.0 || >=18.0.0} dev: true + /@typescript-eslint/types@6.15.0: + resolution: {integrity: sha512-yXjbt//E4T/ee8Ia1b5mGlbNj9fB9lJP4jqLbZualwpP2BCQ5is6BcWwxpIsY4XKAhmdv3hrW92GdtJbatC6dQ==} + engines: {node: ^16.0.0 || >=18.0.0} + dev: true + /@typescript-eslint/typescript-estree@5.62.0(typescript@5.2.2): resolution: {integrity: sha512-CmcQ6uY7b9y694lKdRB8FEel7JbU/40iSAPomu++SjLMntB+2Leay2LO6i8VnJk58MtE9/nQSFIH6jpyRWyYzA==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} @@ -1669,6 +1734,27 @@ packages: - supports-color dev: true + /@typescript-eslint/typescript-estree@6.15.0(typescript@5.2.2): + resolution: {integrity: sha512-7mVZJN7Hd15OmGuWrp2T9UvqR2Ecg+1j/Bp1jXUEY2GZKV6FXlOIoqVDmLpBiEiq3katvj/2n2mR0SDwtloCew==} + engines: {node: ^16.0.0 || >=18.0.0} + peerDependencies: + typescript: '*' + peerDependenciesMeta: + typescript: + optional: true + dependencies: + '@typescript-eslint/types': 6.15.0 + '@typescript-eslint/visitor-keys': 6.15.0 + debug: 4.3.4 + globby: 11.1.0 + is-glob: 4.0.3 + semver: 7.5.4 + ts-api-utils: 1.0.3(typescript@5.2.2) + typescript: 5.2.2 + transitivePeerDependencies: + - supports-color + dev: true + /@typescript-eslint/utils@5.62.0(eslint@8.55.0)(typescript@5.2.2): resolution: {integrity: sha512-n8oxjeb5aIbPFEtmQxQYOLI0i9n5ySBEY/ZEHHZqKQSFnxio1rv6dthascc9dLuwrL0RC5mPCxB7vnAVGAYWAQ==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} @@ -1689,6 +1775,25 @@ packages: - typescript dev: true + /@typescript-eslint/utils@6.15.0(eslint@8.55.0)(typescript@5.2.2): + resolution: {integrity: sha512-eF82p0Wrrlt8fQSRL0bGXzK5nWPRV2dYQZdajcfzOD9+cQz9O7ugifrJxclB+xVOvWvagXfqS4Es7vpLP4augw==} + engines: {node: ^16.0.0 || >=18.0.0} + peerDependencies: + eslint: ^7.0.0 || ^8.0.0 + dependencies: + '@eslint-community/eslint-utils': 4.4.0(eslint@8.55.0) + '@types/json-schema': 7.0.14 + '@types/semver': 7.5.5 + '@typescript-eslint/scope-manager': 6.15.0 + '@typescript-eslint/types': 6.15.0 + '@typescript-eslint/typescript-estree': 6.15.0(typescript@5.2.2) + eslint: 8.55.0 + semver: 7.5.4 + transitivePeerDependencies: + - supports-color + - typescript + dev: true + /@typescript-eslint/visitor-keys@5.62.0: resolution: {integrity: sha512-07ny+LHRzQXepkGg6w0mFY41fVUNBrL2Roj/++7V1txKugfjm/Ci/qSND03r2RhlJhJYMcTn9AhhSSqQp0Ysyw==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} @@ -1705,6 +1810,14 @@ packages: eslint-visitor-keys: 3.4.3 dev: true + /@typescript-eslint/visitor-keys@6.15.0: + resolution: {integrity: sha512-1zvtdC1a9h5Tb5jU9x3ADNXO9yjP8rXlaoChu0DQX40vf5ACVpYIVIZhIMZ6d5sDXH7vq4dsZBT1fEGj8D2n2w==} + engines: {node: ^16.0.0 || >=18.0.0} + dependencies: + '@typescript-eslint/types': 6.15.0 + eslint-visitor-keys: 3.4.3 + dev: true + /@ungap/structured-clone@1.2.0: resolution: {integrity: sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==} dev: true @@ -2944,7 +3057,7 @@ packages: - supports-color dev: true - /eslint-plugin-jest@27.6.0(eslint@8.55.0)(typescript@5.2.2): + /eslint-plugin-jest@27.6.0(@typescript-eslint/eslint-plugin@6.15.0)(eslint@8.55.0)(typescript@5.2.2): resolution: {integrity: sha512-MTlusnnDMChbElsszJvrwD1dN3x6nZl//s4JD23BxB6MgR66TZlL064su24xEIS3VACfAoHV1vgyMgPw8nkdng==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} peerDependencies: @@ -2957,6 +3070,7 @@ packages: jest: optional: true dependencies: + '@typescript-eslint/eslint-plugin': 6.15.0(@typescript-eslint/parser@6.13.2)(eslint@8.55.0)(typescript@5.2.2) '@typescript-eslint/utils': 5.62.0(eslint@8.55.0)(typescript@5.2.2) eslint: 8.55.0 transitivePeerDependencies: diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json new file mode 100644 index 00000000000..240cf8b5f6f --- /dev/null +++ b/tsconfig.eslint.json @@ -0,0 +1,4 @@ +{ + "extends": "./tsconfig.json", + "include": ["packages", "scripts", "./*.ts"] +}