From 0ae085b48a12359f5ecf64c524e850f06a4f1b7b Mon Sep 17 00:00:00 2001 From: AkaraChen Date: Mon, 21 Oct 2024 17:03:04 +0800 Subject: [PATCH] build: add eslint common rule --- web/eslint.config.mjs | 47 ++++++++++++++++++++++++++++++++++++++++--- web/package.json | 11 +++++----- web/pnpm-lock.yaml | 5 ++++- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/web/eslint.config.mjs b/web/eslint.config.mjs index f9d784920b..ce2abce910 100644 --- a/web/eslint.config.mjs +++ b/web/eslint.config.mjs @@ -1,8 +1,9 @@ -import { stylistic, typescript, combine, javascript } from '@antfu/eslint-config' +import { stylistic, typescript, combine, javascript, GLOB_TESTS, GLOB_JSX, GLOB_TSX } from '@antfu/eslint-config' import path from 'node:path' import { fileURLToPath } from 'node:url' import js from '@eslint/js' import { FlatCompat } from '@eslint/eslintrc' +import globals from 'globals' const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) @@ -65,7 +66,12 @@ export default combine( "ts/no-empty-object-type": "off", } }), - // javascript(), + javascript({ + overrides: { + 'no-unused-vars': 'off', + 'no-use-before-define': 'off' + } + }), // TODO: remove this when upgrade to nextjs 15 compat.extends('next'), { @@ -89,6 +95,9 @@ export default combine( "react-hooks/exhaustive-deps": "warn", "react/display-name": "off", "curly": "off", + "unused-imports/no-unused-vars": "warn", + "unused-imports/no-unused-imports": "warn", + "no-undef": "error" } }, storybook, @@ -96,7 +105,39 @@ export default combine( { rules: { // not exist in old version - "antfu/consistent-list-newline": "off" + "antfu/consistent-list-newline": "off", + + // useful, but big change + "no-useless-constructor": "off", + "no-undef": "warn" + } + }, + // suppress error for `no-undef` rule + { + files: GLOB_TESTS, + languageOptions: { + globals: { + ...globals.browser, + ...globals.es2021, + ...globals.node, + ...globals.jest + }, + }, + }, + { + files: [ + GLOB_JSX, + GLOB_TSX, + '**/hooks/*' + ], + languageOptions: { + globals: { + ...globals.browser, + ...globals.es2025, + ...globals.node, + 'React': 'readable', + 'JSX': 'readable', + } } } ) diff --git a/web/package.json b/web/package.json index 4c66eaf2d6..a56f218696 100644 --- a/web/package.json +++ b/web/package.json @@ -53,6 +53,7 @@ "echarts-for-react": "^3.0.2", "emoji-mart": "^5.5.2", "fast-deep-equal": "^3.1.3", + "globals": "^15.11.0", "i18next": "^22.4.13", "i18next-resources-to-backend": "^1.1.3", "immer": "^9.0.19", @@ -110,6 +111,7 @@ "devDependencies": { "@antfu/eslint-config": "^3.8.0", "@chromatic-com/storybook": "^1.9.0", + "@eslint-react/eslint-plugin": "^1.15.0", "@eslint/eslintrc": "^3.1.0", "@eslint/js": "^9.13.0", "@faker-js/faker": "^7.6.0", @@ -147,8 +149,10 @@ "bing-translate-api": "^4.0.2", "code-inspector-plugin": "^0.13.0", "cross-env": "^7.0.3", - "eslint": "^9.10.0", + "eslint": "^9.13.0", "eslint-config-next": "^15.0.0-canary.202", + "eslint-plugin-react-hooks": "^5.0.0", + "eslint-plugin-react-refresh": "^0.4.12", "eslint-plugin-storybook": "^0.9.0", "husky": "^8.0.3", "jest": "^29.7.0", @@ -161,10 +165,7 @@ "tailwindcss": "^3.4.4", "ts-node": "^10.9.2", "typescript": "4.9.5", - "uglify-js": "^3.17.4", - "@eslint-react/eslint-plugin": "^1.15.0", - "eslint-plugin-react-hooks": "^5.0.0", - "eslint-plugin-react-refresh": "^0.4.12" + "uglify-js": "^3.17.4" }, "resolutions": { "@types/react": "~18.2.0", diff --git a/web/pnpm-lock.yaml b/web/pnpm-lock.yaml index dc8b54c7c8..3aeb8f080c 100644 --- a/web/pnpm-lock.yaml +++ b/web/pnpm-lock.yaml @@ -100,6 +100,9 @@ importers: fast-deep-equal: specifier: ^3.1.3 version: 3.1.3 + globals: + specifier: ^15.11.0 + version: 15.11.0 i18next: specifier: ^22.4.13 version: 22.5.1 @@ -381,7 +384,7 @@ importers: specifier: ^7.0.3 version: 7.0.3 eslint: - specifier: ^9.10.0 + specifier: ^9.13.0 version: 9.13.0(jiti@1.21.6) eslint-config-next: specifier: ^15.0.0-canary.202