我已将 eslint v9 新安装到我的 TypeScript 项目中。 我的文件根目录中有这个
eslint.config.mjs
。
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
export default tseslint.config({
files: ['**/*.ts'],
extends: [
eslint.configs.recommended,
...tseslint.configs.strict,
]
});
它基本上按预期工作,但我在生成的 javascript 文件中也遇到错误;由 npm 包中的 eslint 指令引起;例如我看到 ChartJs 生成的文件:
/ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js
3346:5 error Definition for rule '@typescript-eslint/no-empty-function' was not found @typescript-eslint/no-empty-function
12515:9 error Definition for rule '@typescript-eslint/no-use-before-define' was not found @typescript-eslint/no-use-before-define
12545:5 error Definition for rule '@typescript-eslint/no-use-before-define' was not found @typescript-eslint/no-use-before-define
13490:5 warning Unused eslint-disable directive (no problems were reported from 'complexity')
13559:5 error Definition for rule '@typescript-eslint/no-use-before-define' was not found @typescript-eslint/no-use-before-define
13562:1 warning Unused eslint-disable directive (no problems were reported from 'complexity')
14621:1 warning Unused eslint-disable directive (no problems were reported from 'complexity')
如果我运行
npx eslint --debug
我会在上面的示例中看到这一点:
eslint:eslint Lint /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js +0ms
eslint:linter Linting code for /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js (pass 1) +0ms
eslint:linter Verify +0ms
eslint:linter With flat config: /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js +0ms
eslint:languages:js Parsing: /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js +2ms
eslint:languages:js Parsing successful: /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js +231ms
eslint:languages:js Scope analysis: /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js +0ms
eslint:languages:js Scope analysis successful: /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js +285ms
eslint:config-comment-parser Parsing list config +0ms
eslint:config-comment-parser Parsing list config +1ms
eslint:config-comment-parser Parsing list config +0ms
eslint:config-comment-parser Parsing list config +0ms
eslint:rules Loading rule 'complexity' (remaining=228) +4s
eslint:config-comment-parser Parsing list config +1ms
eslint:config-comment-parser Parsing list config +0ms
eslint:config-comment-parser Parsing list config +0ms
eslint:linter Generating fixed text for /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js (pass 1) +680ms
eslint:source-code-fixer Applying fixes +680ms
eslint:source-code-fixer shouldFix parameter was false, not attempting fixes +0ms
所以它似乎使用 javascript 文件作为配置。 对于每个失败的 JavaScript 文件都是如此;它使用该文件作为配置。
调试的顶部如下所示:
eslint:cli CLI args: [ '--debug' ] +0ms
eslint:cli Using flat config? true +2ms
eslint:cli Running on files +3ms
eslint:eslint Using file patterns: . +0ms
eslint:eslint Searching for eslint.config.js +0ms
eslint:eslint Loading config from /ProjectRoot/eslint.config.mjs +2ms
eslint:eslint Config file URL is file:///ProjectRoot/eslint.config.mjs +0ms
eslint:rules Loading rule 'consistent-return' (remaining=289) +0ms
## loads more rules...
eslint:eslint Deleting cache file at /ProjectRoot/.eslintcache +998ms
## loads even more rules...
eslint:eslint 191 files found in: 244ms +244ms
eslint:eslint Lint /ProjectRoot/eslint.config.mjs +19ms
eslint:linter Linting code for /ProjectRoot/eslint.config.mjs (pass 1) +0ms
eslint:linter Verify +0ms
eslint:linter With flat config: /ProjectRoot/eslint.config.mjs +0ms
eslint:languages:js Parsing: /ProjectRoot/eslint.config.mjs +0ms
eslint:languages:js Parsing successful: /ProjectRoot/eslint.config.mjs +7ms
eslint:languages:js Scope analysis: /ProjectRoot/eslint.config.mjs +0ms
eslint:languages:js Scope analysis successful: /ProjectRoot/eslint.config.mjs +2ms
eslint:linter Generating fixed text for /ProjectRoot/eslint.config.mjs (pass 1) +15ms
eslint:source-code-fixer Applying fixes +0ms
eslint:source-code-fixer shouldFix parameter was false, not attempting fixes +0ms
eslint:eslint Lint /ProjectRoot/.template-scripts/publish/index.ts +16ms
eslint:linter Linting code for /ProjectRoot/webpack.config.js (pass 1) +0ms
eslint:linter Verify +0ms
eslint:linter With flat config: /ProjectRoot/webpack.config.js +0ms
### etc...
所以它是从顶部开始执行的,包括配置文件,然后是我的 webpack.config.js 文件,其自身作为配置
这是一个 eslint bug 吗?或者我错过了什么?
调试日志有点误导。这意味着 ESLint 尝试使用平面配置对给定名称的文件进行 lint 检测,请参阅源代码。它确实不意味着 ESLint 尝试将这些文件用作平面配置。
默认情况下,ESLint 包含存储库中的所有 JavaScript 文件,除非明确忽略它们。您正在尝试对构建和缩小的代码进行 lint 处理,预计这些代码会出现 lint 问题。要排除文件或文件夹,请使用
ignores
选项:
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
export default tseslint.config({
files: ['**/*.ts'],
ignores: [
'dist/', // exclude specific folder
'**/*.js', // exclude all JavaScript files
],
extends: [
eslint.configs.recommended,
...tseslint.configs.strict,
]
});