gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:21:50 27 4
gpt4 key购买 nike

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

显然触发的错误是无穷无尽的:有没有机会配置 eslint 只解析被触动的文件?我希望 linter 解析开发人员进行更改的每个文件,并且只解析这些文件。

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

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

谢谢

最佳答案

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

Webpack 配置的依赖项:

var logger = require('reliable-logger');
var watch = require('watch');
var CLIEngine = require('eslint').CLIEngine

watcher 和 linter 配置和启动;我将它与所有待办事项一起粘贴,因为它是:
var configureLinterAndWatchFiles = function() {
var changedFiles = [];
var formatter;
var report;
var SEPARATOR = "////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////";

// TODO I got the feeling that one of those settings is breaking the
// linter (probably the path resolving?)
var linter = new CLIEngine({
// TODO do I need this? Looks like I don't...
// envs: ["node"],
// TODO what is the default?
useEslintrc: true,
// TODO I find weird that I get no error with this: configFile: "../.eslintrc1111"
// make sure that the configuration file is correctly picked up
configFile: ".eslintrc",
// TODO useless if your root is src
// ignorePath: "node_modules"
// TODO probably both useless... the first I still don't get it,
// the second you are enforcing the filtering yourself by checks
// cache: false,
// extensions: [".js", ".jsx"]
});

var fileUpdatedFn = function(f) {
// TODO I would prefer much more to get the list of changed files from
// git status (how to?). Here I am building my own
// resetting the array only for debug purpose
// changedFiles = [];
if(/.js$/.test(f) || /.jsx$/.test(f)) {
changedFiles.push(f);
logger.info(SEPARATOR);
report = linter.executeOnFiles(changedFiles);
logger.info(formatter(report.results));
}
};

// get the default formatter
formatter = linter.getFormatter();

watch.watchTree('src', function(f, curr, prev) {
if (typeof f == "object" && prev === null && curr === null) {
// Finished walking the tree
} else if (prev === null) {
// f is a new file
} else if (curr.nlink === 0) {
// f was removed
} else {
// f was changed
fileUpdatedFn(f);
}
});

};

在 module.exports 中,最后一行:
module.exports = function(callback, options){
// ... more code ...
configureLinterAndWatchFiles();
}

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

I wonder, though, if the cache flag (eslint.org/docs/developer-guide/nodejs-api#cliengine) was the best to be used for the problem. From here (github.com/adametry/gulp-eslint/issues/…): "--cache flag will skip over any files that had no problems in the previous run unless they have been modified": not sure if that is my case but is of interest.

关于javascript - eslint:如何对仅接触过的文件进行 lint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40388013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com