From 4f66ddb247a45a1222a7685a072bed1a3741bca3 Mon Sep 17 00:00:00 2001 From: Rel1cx Date: Wed, 16 Apr 2025 04:40:59 +0800 Subject: [PATCH] fix: use preferred loc in error maker --- .vscode/settings.json | 3 +- apps/website/package.json | 2 +- examples/next-app/.vscode/settings.json | 3 +- .../vite-react-dom-app/.vscode/settings.json | 3 +- examples/vite-react-dom-app/package.json | 2 +- .../.vscode/settings.json | 3 +- examples/vite-react-dom-js-app/package.json | 2 +- .../.vscode/settings.json | 3 +- .../package.json | 2 +- .../.vscode/settings.json | 3 +- .../package.json | 2 +- package.json | 2 +- .../src/rules/no-unnecessary-use-prefix.ts | 27 ++++++- packages/shared/package.json | 2 +- packages/utilities/kit/package.json | 2 +- pnpm-lock.yaml | 72 +++++++++---------- 16 files changed, 76 insertions(+), 57 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 4534ba63ca..f4047335eb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -31,8 +31,7 @@ } ], "eslint.rules.customizations": [ - { "rule": "@eslint-react/debug/*", "severity": "info" }, - { "rule": "@eslint-react/hooks-extra/no-unnecessary-use-prefix", "severity": "info" } + { "rule": "@eslint-react/debug/*", "severity": "info" } ], "files.exclude": { "**/.zed": true, diff --git a/apps/website/package.json b/apps/website/package.json index d70a159326..833a3b733c 100644 --- a/apps/website/package.json +++ b/apps/website/package.json @@ -50,7 +50,7 @@ "dedent": "^1.5.3", "eslint": "^9.24.0", "eslint-plugin-de-morgan": "^1.2.1", - "eslint-plugin-import-x": "^4.10.3", + "eslint-plugin-import-x": "^4.10.5", "eslint-plugin-perfectionist": "^4.11.0", "eslint-plugin-react-hooks": "^5.2.0", "eslint-plugin-react-refresh": "^0.4.19", diff --git a/examples/next-app/.vscode/settings.json b/examples/next-app/.vscode/settings.json index 14e8b6116f..802c5083af 100644 --- a/examples/next-app/.vscode/settings.json +++ b/examples/next-app/.vscode/settings.json @@ -1,6 +1,5 @@ { "eslint.rules.customizations": [ - { "rule": "@eslint-react/debug/*", "severity": "info" }, - { "rule": "@eslint-react/hooks-extra/no-unnecessary-use-prefix", "severity": "info" } + { "rule": "@eslint-react/debug/*", "severity": "info" } ] } diff --git a/examples/vite-react-dom-app/.vscode/settings.json b/examples/vite-react-dom-app/.vscode/settings.json index 14e8b6116f..802c5083af 100644 --- a/examples/vite-react-dom-app/.vscode/settings.json +++ b/examples/vite-react-dom-app/.vscode/settings.json @@ -1,6 +1,5 @@ { "eslint.rules.customizations": [ - { "rule": "@eslint-react/debug/*", "severity": "info" }, - { "rule": "@eslint-react/hooks-extra/no-unnecessary-use-prefix", "severity": "info" } + { "rule": "@eslint-react/debug/*", "severity": "info" } ] } diff --git a/examples/vite-react-dom-app/package.json b/examples/vite-react-dom-app/package.json index 7b103e3ca3..67f8776ff5 100644 --- a/examples/vite-react-dom-app/package.json +++ b/examples/vite-react-dom-app/package.json @@ -23,7 +23,7 @@ "@tsconfig/vite-react": "^3.4.0", "@types/react": "^19.1.2", "@types/react-dom": "^19.1.2", - "@vitejs/plugin-react": "^4.3.4", + "@vitejs/plugin-react": "^4.4.0", "eslint": "^9.24.0", "eslint-plugin-react-hooks": "^5.2.0", "eslint-plugin-react-refresh": "^0.4.19", diff --git a/examples/vite-react-dom-js-app/.vscode/settings.json b/examples/vite-react-dom-js-app/.vscode/settings.json index 14e8b6116f..802c5083af 100644 --- a/examples/vite-react-dom-js-app/.vscode/settings.json +++ b/examples/vite-react-dom-js-app/.vscode/settings.json @@ -1,6 +1,5 @@ { "eslint.rules.customizations": [ - { "rule": "@eslint-react/debug/*", "severity": "info" }, - { "rule": "@eslint-react/hooks-extra/no-unnecessary-use-prefix", "severity": "info" } + { "rule": "@eslint-react/debug/*", "severity": "info" } ] } diff --git a/examples/vite-react-dom-js-app/package.json b/examples/vite-react-dom-js-app/package.json index b3ecf5820b..cae558e0b2 100644 --- a/examples/vite-react-dom-js-app/package.json +++ b/examples/vite-react-dom-js-app/package.json @@ -20,7 +20,7 @@ "@eslint/js": "^9.24.0", "@types/react": "^19.1.2", "@types/react-dom": "^19.1.2", - "@vitejs/plugin-react": "^4.3.4", + "@vitejs/plugin-react": "^4.4.0", "eslint": "^9.24.0", "eslint-plugin-react-hooks": "^5.2.0", "eslint-plugin-react-refresh": "^0.4.19", diff --git a/examples/vite-react-dom-js-with-babel-eslint-parser-app/.vscode/settings.json b/examples/vite-react-dom-js-with-babel-eslint-parser-app/.vscode/settings.json index 14e8b6116f..802c5083af 100644 --- a/examples/vite-react-dom-js-with-babel-eslint-parser-app/.vscode/settings.json +++ b/examples/vite-react-dom-js-with-babel-eslint-parser-app/.vscode/settings.json @@ -1,6 +1,5 @@ { "eslint.rules.customizations": [ - { "rule": "@eslint-react/debug/*", "severity": "info" }, - { "rule": "@eslint-react/hooks-extra/no-unnecessary-use-prefix", "severity": "info" } + { "rule": "@eslint-react/debug/*", "severity": "info" } ] } diff --git a/examples/vite-react-dom-js-with-babel-eslint-parser-app/package.json b/examples/vite-react-dom-js-with-babel-eslint-parser-app/package.json index ed8d20ec2b..7c06f81333 100644 --- a/examples/vite-react-dom-js-with-babel-eslint-parser-app/package.json +++ b/examples/vite-react-dom-js-with-babel-eslint-parser-app/package.json @@ -26,7 +26,7 @@ "@types/babel__preset-env": "~7.10.0", "@types/react": "^19.1.2", "@types/react-dom": "^19.1.2", - "@vitejs/plugin-react": "^4.3.4", + "@vitejs/plugin-react": "^4.4.0", "eslint": "^9.24.0", "eslint-plugin-react-hooks": "^5.2.0", "eslint-plugin-react-refresh": "^0.4.19", diff --git a/examples/vite-react-dom-with-ts-blank-eslint-parser-app/.vscode/settings.json b/examples/vite-react-dom-with-ts-blank-eslint-parser-app/.vscode/settings.json index 14e8b6116f..802c5083af 100644 --- a/examples/vite-react-dom-with-ts-blank-eslint-parser-app/.vscode/settings.json +++ b/examples/vite-react-dom-with-ts-blank-eslint-parser-app/.vscode/settings.json @@ -1,6 +1,5 @@ { "eslint.rules.customizations": [ - { "rule": "@eslint-react/debug/*", "severity": "info" }, - { "rule": "@eslint-react/hooks-extra/no-unnecessary-use-prefix", "severity": "info" } + { "rule": "@eslint-react/debug/*", "severity": "info" } ] } diff --git a/examples/vite-react-dom-with-ts-blank-eslint-parser-app/package.json b/examples/vite-react-dom-with-ts-blank-eslint-parser-app/package.json index e6b11c6e71..2e8d6b4e6a 100644 --- a/examples/vite-react-dom-with-ts-blank-eslint-parser-app/package.json +++ b/examples/vite-react-dom-with-ts-blank-eslint-parser-app/package.json @@ -25,7 +25,7 @@ "@tsconfig/vite-react": "^3.4.0", "@types/react": "^19.1.2", "@types/react-dom": "^19.1.2", - "@vitejs/plugin-react": "^4.3.4", + "@vitejs/plugin-react": "^4.4.0", "eslint": "^9.24.0", "eslint-plugin-react-hooks": "^5.2.0", "eslint-plugin-react-refresh": "^0.4.19", diff --git a/package.json b/package.json index 08c0bb7e9d..bc262d088b 100644 --- a/package.json +++ b/package.json @@ -87,7 +87,7 @@ "tslib": "^2.8.1", "tsup": "^8.4.0", "tsx": "^4.19.3", - "type-fest": "^4.39.1", + "type-fest": "^4.40.0", "typedoc": "^0.28.2", "typedoc-plugin-markdown": "^4.6.2", "typedoc-plugin-mdn-links": "^5.0.1", diff --git a/packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-unnecessary-use-prefix.ts b/packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-unnecessary-use-prefix.ts index ee01e6dfa5..f649248db1 100644 --- a/packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-unnecessary-use-prefix.ts +++ b/packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-unnecessary-use-prefix.ts @@ -64,9 +64,19 @@ export function create(context: RuleContext): RuleListener { if (containsUseComments(context, node)) { continue; } + if (id != null) { + context.report({ + messageId: "noUnnecessaryUsePrefix", + data: { + name, + }, + loc: getPreferredLoc(context, id), + }); + continue; + } context.report({ messageId: "noUnnecessaryUsePrefix", - node: id ?? node, + node, data: { name, }, @@ -75,3 +85,18 @@ export function create(context: RuleContext): RuleListener { }, }; } + +function getPreferredLoc(context: RuleContext, id: TSESTree.Identifier) { + if (AST.isMultiLine(id)) return id.loc; + if (!context.sourceCode.getText(id).startsWith("use")) return id.loc; + return { + end: { + column: id.loc.start.column + 3, + line: id.loc.start.line, + }, + start: { + column: id.loc.start.column, + line: id.loc.start.line, + }, + }; +} diff --git a/packages/shared/package.json b/packages/shared/package.json index 3a726140b5..511c330004 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -55,7 +55,7 @@ "@tsconfig/node22": "^22.0.1", "@types/picomatch": "^4.0.0", "tsup": "^8.4.0", - "type-fest": "^4.39.1" + "type-fest": "^4.40.0" }, "engines": { "bun": ">=1.0.15", diff --git a/packages/utilities/kit/package.json b/packages/utilities/kit/package.json index be2181b460..d1b9517900 100644 --- a/packages/utilities/kit/package.json +++ b/packages/utilities/kit/package.json @@ -51,7 +51,7 @@ "@local/configs": "workspace:*", "@tsconfig/node22": "^22.0.1", "tsup": "^8.4.0", - "type-fest": "^4.39.1" + "type-fest": "^4.40.0" }, "engines": { "bun": ">=1.0.15", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a3b7660742..cc1231f97f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -127,8 +127,8 @@ importers: specifier: ^4.19.3 version: 4.19.3 type-fest: - specifier: ^4.39.1 - version: 4.39.1 + specifier: ^4.40.0 + version: 4.40.0 typedoc: specifier: ^0.28.2 version: 0.28.2(typescript@5.8.3) @@ -372,8 +372,8 @@ importers: specifier: ^1.2.1 version: 1.2.1(eslint@9.24.0(jiti@2.4.2)) eslint-plugin-import-x: - specifier: ^4.10.3 - version: 4.10.3(eslint@9.24.0(jiti@2.4.2))(typescript@5.8.3) + specifier: ^4.10.5 + version: 4.10.5(eslint@9.24.0(jiti@2.4.2))(typescript@5.8.3) eslint-plugin-perfectionist: specifier: ^4.11.0 version: 4.11.0(eslint@9.24.0(jiti@2.4.2))(typescript@5.8.3) @@ -500,8 +500,8 @@ importers: specifier: ^19.1.2 version: 19.1.2(@types/react@19.1.2) '@vitejs/plugin-react': - specifier: ^4.3.4 - version: 4.3.4(vite@6.2.6(@types/node@22.14.1)(jiti@2.4.2)(lightningcss@1.29.2)(tsx@4.19.3)(yaml@2.7.1)) + specifier: ^4.4.0 + version: 4.4.0(vite@6.2.6(@types/node@22.14.1)(jiti@2.4.2)(lightningcss@1.29.2)(tsx@4.19.3)(yaml@2.7.1)) eslint: specifier: ^9.24.0 version: 9.24.0(jiti@2.4.2) @@ -546,8 +546,8 @@ importers: specifier: ^19.1.2 version: 19.1.2(@types/react@19.1.2) '@vitejs/plugin-react': - specifier: ^4.3.4 - version: 4.3.4(vite@6.2.6(@types/node@22.14.1)(jiti@2.4.2)(lightningcss@1.29.2)(tsx@4.19.3)(yaml@2.7.1)) + specifier: ^4.4.0 + version: 4.4.0(vite@6.2.6(@types/node@22.14.1)(jiti@2.4.2)(lightningcss@1.29.2)(tsx@4.19.3)(yaml@2.7.1)) eslint: specifier: ^9.24.0 version: 9.24.0(jiti@2.4.2) @@ -607,8 +607,8 @@ importers: specifier: ^19.1.2 version: 19.1.2(@types/react@19.1.2) '@vitejs/plugin-react': - specifier: ^4.3.4 - version: 4.3.4(vite@6.2.6(@types/node@22.14.1)(jiti@2.4.2)(lightningcss@1.29.2)(tsx@4.19.3)(yaml@2.7.1)) + specifier: ^4.4.0 + version: 4.4.0(vite@6.2.6(@types/node@22.14.1)(jiti@2.4.2)(lightningcss@1.29.2)(tsx@4.19.3)(yaml@2.7.1)) eslint: specifier: ^9.24.0 version: 9.24.0(jiti@2.4.2) @@ -665,8 +665,8 @@ importers: specifier: ^19.1.2 version: 19.1.2(@types/react@19.1.2) '@vitejs/plugin-react': - specifier: ^4.3.4 - version: 4.3.4(vite@6.2.6(@types/node@22.14.1)(jiti@2.4.2)(lightningcss@1.29.2)(tsx@4.19.3)(yaml@2.7.1)) + specifier: ^4.4.0 + version: 4.4.0(vite@6.2.6(@types/node@22.14.1)(jiti@2.4.2)(lightningcss@1.29.2)(tsx@4.19.3)(yaml@2.7.1)) eslint: specifier: ^9.24.0 version: 9.24.0(jiti@2.4.2) @@ -1181,8 +1181,8 @@ importers: specifier: ^8.4.0 version: 8.4.0(@swc/core@1.11.21)(jiti@2.4.2)(postcss@8.5.3)(tsx@4.19.3)(typescript@5.8.3)(yaml@2.7.1) type-fest: - specifier: ^4.39.1 - version: 4.39.1 + specifier: ^4.40.0 + version: 4.40.0 packages/utilities/ast: dependencies: @@ -1246,8 +1246,8 @@ importers: specifier: ^8.4.0 version: 8.4.0(@swc/core@1.11.21)(jiti@2.4.2)(postcss@8.5.3)(tsx@4.19.3)(typescript@5.8.3)(yaml@2.7.1) type-fest: - specifier: ^4.39.1 - version: 4.39.1 + specifier: ^4.40.0 + version: 4.40.0 packages/utilities/var: dependencies: @@ -2832,8 +2832,8 @@ packages: resolution: {integrity: sha512-fdDH1LSGfZdTH2sxdpVMw31BanV28K/Gry0cVFxaNP77neJSkd82mM8ErPNYs9e+0O7SdHBLTDzDgwUuy18RnQ==} engines: {node: ^12.20.0 || ^14.18.0 || >=16.0.0} - '@pkgr/core@0.2.3': - resolution: {integrity: sha512-yMV8bb9prWI21N6FsrnPCbhoYb8UUvYCDGoSvPHBloVC095Ef2ker43hzXkJ6TpJPw53S8FeFYkARa7GGIGwxg==} + '@pkgr/core@0.2.4': + resolution: {integrity: sha512-ROFF39F6ZrnzSUEmQQZUar0Jt4xVoP9WnDRdWwF4NNcXs3xBTLgBUDoOwW141y1jP+S8nahIbdxbFC7IShw9Iw==} engines: {node: ^12.20.0 || ^14.18.0 || >=16.0.0} '@polka/url@0.5.0': @@ -3884,8 +3884,8 @@ packages: cpu: [x64] os: [win32] - '@vitejs/plugin-react@4.3.4': - resolution: {integrity: sha512-SCCPBJtYLdE8PX/7ZQAs1QAZ8Jqwih+0VBLum1EGqmCCQal+MIUqLCzj3ZUy8ufbC0cAM4LRlSTm7IQJwWT4ug==} + '@vitejs/plugin-react@4.4.0': + resolution: {integrity: sha512-x/EztcTKVj+TDeANY1WjNeYsvZjZdfWRMP/KXi5Yn8BoTzpa13ZltaQqKfvWYbX8CE10GOHHdC5v86jY9x8i/g==} engines: {node: ^14.18.0 || >=16.0.0} peerDependencies: vite: ^4.2.0 || ^5.0.0 || ^6.0.0 @@ -4811,8 +4811,8 @@ packages: ts-api-utils: ^2.1.0 typescript: ^5.8.3 - eslint-plugin-import-x@4.10.3: - resolution: {integrity: sha512-Q7yx76tlOfzLGtXDt/sppdYrGNcbr9CrRXqJXTNIpM/7hfX9lRp1noE+PkYgN+xvW47TEeGZ0pgy29hlX9Y7UQ==} + eslint-plugin-import-x@4.10.5: + resolution: {integrity: sha512-cmteCl8P5q1lkuL/4qqQw1uvnQHytpv2fjHFZ2UIqSfkM0RwWm/KLgasXKIqDRjgMnmUJTeyP8+9hDpJJuiZgg==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: eslint: ^8.57.0 || ^9.0.0 @@ -6412,8 +6412,8 @@ packages: react: ^19.1.0 react-dom: ^19.1.0 - react-refresh@0.14.2: - resolution: {integrity: sha512-jCvmsr+1IUSMUyzOkRcvnVbX3ZYC6g9TDrDbFuFmRDq7PD4yaGbLKNQL6k2jnArV8hjYxh7hVhAZB6s9HDGpZA==} + react-refresh@0.17.0: + resolution: {integrity: sha512-z6F7K9bV85EfseRCp2bzrpyQ0Gkw1uLoCel9XBVWPg/TjRj94SkJzUTGfOa4bs7iJvBWtQG0Wq7wnI0syw3EBQ==} engines: {node: '>=0.10.0'} react-remove-scroll-bar@2.3.8: @@ -6988,8 +6988,8 @@ packages: resolution: {integrity: sha512-XleUoc9uwGXqjWwXaUTZAmzMcFZ5858QA2vvx1Ur5xIcixXIP+8LnFDgRplU30us6teqdlskFfu+ae4K79Ooew==} engines: {node: '>= 0.8.0'} - type-fest@4.39.1: - resolution: {integrity: sha512-uW9qzd66uyHYxwyVBYiwS4Oi0qZyUqwjU+Oevr6ZogYiXt99EOYtwvzMSLw1c3lYo2HzJsep/NB23iEVEgjG/w==} + type-fest@4.40.0: + resolution: {integrity: sha512-ABHZ2/tS2JkvH1PEjxFDTUWC8dB5OsIGZP4IFLhR293GqT5Y5qB1WwL2kMPYhQW9DVgVD8Hd7I8gjwPIf5GFkw==} engines: {node: '>=16'} typedoc-plugin-markdown@4.6.2: @@ -8906,7 +8906,7 @@ snapshots: '@pkgr/core@0.1.2': {} - '@pkgr/core@0.2.3': {} + '@pkgr/core@0.2.4': {} '@polka/url@0.5.0': {} @@ -9942,13 +9942,13 @@ snapshots: '@unrs/resolver-binding-win32-x64-msvc@1.5.0': optional: true - '@vitejs/plugin-react@4.3.4(vite@6.2.6(@types/node@22.14.1)(jiti@2.4.2)(lightningcss@1.29.2)(tsx@4.19.3)(yaml@2.7.1))': + '@vitejs/plugin-react@4.4.0(vite@6.2.6(@types/node@22.14.1)(jiti@2.4.2)(lightningcss@1.29.2)(tsx@4.19.3)(yaml@2.7.1))': dependencies: '@babel/core': 7.26.10 '@babel/plugin-transform-react-jsx-self': 7.25.9(@babel/core@7.26.10) '@babel/plugin-transform-react-jsx-source': 7.25.9(@babel/core@7.26.10) '@types/babel__core': 7.20.5 - react-refresh: 0.14.2 + react-refresh: 0.17.0 vite: 6.2.6(@types/node@22.14.1)(jiti@2.4.2)(lightningcss@1.29.2)(tsx@4.19.3)(yaml@2.7.1) transitivePeerDependencies: - supports-color @@ -10980,9 +10980,9 @@ snapshots: transitivePeerDependencies: - supports-color - eslint-plugin-import-x@4.10.3(eslint@9.24.0(jiti@2.4.2))(typescript@5.8.3): + eslint-plugin-import-x@4.10.5(eslint@9.24.0(jiti@2.4.2))(typescript@5.8.3): dependencies: - '@pkgr/core': 0.2.3 + '@pkgr/core': 0.2.4 '@types/doctrine': 0.0.9 '@typescript-eslint/utils': 8.30.1(eslint@9.24.0(jiti@2.4.2))(typescript@5.8.3) debug: 4.4.0 @@ -12912,7 +12912,7 @@ snapshots: dependencies: '@babel/code-frame': 7.26.2 index-to-position: 1.1.0 - type-fest: 4.39.1 + type-fest: 4.40.0 parse-passwd@1.0.0: {} @@ -13046,7 +13046,7 @@ snapshots: react: 19.1.0 react-dom: 19.1.0(react@19.1.0) - react-refresh@0.14.2: {} + react-refresh@0.17.0: {} react-remove-scroll-bar@2.3.8(@types/react@19.1.2)(react@19.1.0): dependencies: @@ -13081,14 +13081,14 @@ snapshots: dependencies: find-up-simple: 1.0.1 read-pkg: 9.0.1 - type-fest: 4.39.1 + type-fest: 4.40.0 read-pkg@9.0.1: dependencies: '@types/normalize-package-data': 2.4.4 normalize-package-data: 6.0.2 parse-json: 8.3.0 - type-fest: 4.39.1 + type-fest: 4.40.0 unicorn-magic: 0.1.0 readable-stream@3.6.2: @@ -13727,7 +13727,7 @@ snapshots: dependencies: prelude-ls: 1.2.1 - type-fest@4.39.1: {} + type-fest@4.40.0: {} typedoc-plugin-markdown@4.6.2(typedoc@0.28.2(typescript@5.8.3)): dependencies: