eslint:如何仅对接触过的文件进行 lint

lzz*_*uca 5 javascript eslint webpack

我最近eslint在一个以前从未用 linter 解析过的代码库中添加了,作为 webpack 加载器。

显然,触发的错误数量是无穷无尽的:有没有机会配置 eslint 来仅解析触及的文件?我希望 linter 解析开发人员在其中进行更改的每个文件,并且仅解析这些文件。

这是我目前使用的加载器(以防万一),非常标准的配置:

{test: /\.(jsx|js)$/, loader: "eslint-loader?{cache: true}", exclude: /node_modules/}
Run Code Online (Sandbox Code Playgroud)

谢谢

lzz*_*uca 1

我通过使用观察者来完成它;这是详细的解决方案:

\n\n

Webpack 配置的依赖项:

\n\n
var logger = require(\'reliable-logger\');\nvar watch = require(\'watch\');\nvar CLIEngine =  require(\'eslint\').CLIEngine\n
Run Code Online (Sandbox Code Playgroud)\n\n

watcher 和 linter 配置并启动;我将其与所有待办事项一起粘贴,如下所示:

\n\n
var configureLinterAndWatchFiles = function() {\nvar changedFiles = [];\nvar formatter;\nvar report;\nvar SEPARATOR = "////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////";\n\n// TODO I got the feeling that one of those settings is breaking the\n//   linter (probably the path resolving?)\nvar linter =  new CLIEngine({\n    // TODO do I need this? Looks like I don\'t...\n    // envs: ["node"],\n    // TODO what is the default?\n    useEslintrc: true,\n    // TODO I find weird that I get no error with this: configFile: "../.eslintrc1111"\n    //  make sure that the configuration file is correctly picked up\n    configFile: ".eslintrc",\n    // TODO useless if your root is src\n    // ignorePath: "node_modules"\n    // TODO probably both useless... the first I still don\'t get it,\n    //   the second you are enforcing the filtering yourself by checks\n    // cache: false,\n    // extensions: [".js", ".jsx"]\n});\n\nvar fileUpdatedFn = function(f) {\n    // TODO I would prefer much more to get the list of changed files from\n    //   git status (how to?). Here I am building my own\n    // resetting the array only for debug purpose\n    // changedFiles = [];\n    if(/.js$/.test(f) || /.jsx$/.test(f)) {\n        changedFiles.push(f);\n        logger.info(SEPARATOR);\n        report = linter.executeOnFiles(changedFiles);\n        logger.info(formatter(report.results));\n    }\n};\n\n// get the default formatter\nformatter = linter.getFormatter();\n\nwatch.watchTree(\'src\', function(f, curr, prev) {\n    if (typeof f == "object" && prev === null && curr === null) {\n    // Finished walking the tree\n    } else if (prev === null) {\n    // f is a new file\n    } else if (curr.nlink === 0) {\n    // f was removed\n    } else {\n        // f was changed\n        fileUpdatedFn(f);\n    }\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

};

\n\n

在 module.exports 中,最后一行:

\n\n
module.exports = function(callback, options){\n  // ... more code ...\n  configureLinterAndWatchFiles();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

应该是这样。正如我在评论中指出的:

\n\n
\n

不过,我想知道缓存标志(eslint.org/docs/developer-guide/nodejs-api#cliengine)是否是解决该问题的最佳选择。从这里(github.com/adametry/gulp-eslint/issues/\xe2\x80\xa6):“--cache 标志将跳过上次运行中没有问题的任何文件,除非它们已被修改”:不确定如果这是我的情况但很有趣。

\n
\n