如果我添加任何给定的ESLint规则,例如,在现有的代码库中添加no-param-reassign,可能会有很多违规行为。
是否有一种好的方法以编程方式在逐行基础上添加对所有现有违规行为的抑制?
在这个例子中:
// eslint-diable-next-line no-param-reassign
param = foo;澄清
我do想要我的项目中的规则,保护我们编写的所有新代码。我不希望修复所有手工释放违规的旧代码(如果可能,我希望脚本可以帮我修复这些代码)。这就是为什么我要制止所有现有的侵权行为,但尊重所有新的侵权行为。我的主要目标是尽快遵守新规则,以便在所有新代码上获得它的值。我不介意以前挥之不去的压制违规行为。
发布于 2020-09-22 21:08:54
我遇到了同样的问题,一堆react-hooks/exhaustive-deps lint警告最终使用了json格式化程序和一个脚本来插入eslint禁用注释。我运行yarn lint . -f json -o warnings.json来获取lints的json列表,然后如下
const json = require('./warnings.json');
const fs = require('fs');
json.forEach(({ filePath, messages, source }) => {
// if there is no source we have nothing that needs to be eslint-ignore'd
if (!source) {
return;
}
const data = source.split('\n');
// if the source has multiple lines which need to be eslint-ignored our offset changes per addition
// offset is 1 because line numbers start at 1 but index numbers in an array start at 0
let offset = 1;
// group errors/warnings by line because we want to have one eslint disable comment with all the rules to disable
const groupedMessages = messages.reduce((acc, next) => {
const prevMessages = acc[next.line] ? acc[next.line] : [];
// some lines may have the same rule twice
const duplicateRuleForLine = prevMessages.find(message => message.ruleId === next.ruleId);
// ignore jsx and graphql lint rules
const applicableRule = next.ruleId && !next.ruleId.includes('jsx') && !next.ruleId.includes('graphql');
// ignore the eslint-ignore addition for duplicates and non applicable rules
if (duplicateRuleForLine || !applicableRule) {
return acc;
}
return {
...acc,
[next.line]: [...prevMessages, next],
};
}, {});
Object.entries(groupedMessages).forEach(([line, messages]) => {
// grouped ignores
const ignore = `// eslint-disable-next-line ${messages.map(({ ruleId }) => ruleId).join(' ')}`;
data.splice(line - offset, 0, ignore);
offset--;
});
const updated = data.join('\n');
fs.writeFile(filePath, updated, function(err) {
if (err) return console.log(err);
});
});对我来说效果很好,虽然我希望我插入的这些评论是自动格式化的。
发布于 2022-08-02 13:20:45
我尝试了@ work 15185791回答的抑制-避免错误npm,但是它不适用于ESLint8。但是,我从npm的问题上找到了https://github.com/mizdra/eslint-interactive。到目前为止,这一奇妙的国家预防机制正在完美地运作。
执行如下所示。
yarn add -D eslint-interactive
yarn eslint-interactive src
yarn remove eslint-interactive发布于 2021-04-07 17:28:11
我通常使用这个:https://github.com/amanda-mitchell/suppress-eslint-errors
让jscodemod为您的回购配置是有点困难,但值得的是,一旦你让它发挥作用。外面有很多强大的共鬼。
https://stackoverflow.com/questions/60629026
复制相似问题