gpt4 book ai didi

javascript - 有没有办法获得 ESLint 报告的独特错误

转载 作者:行者123 更新时间:2023-12-04 16:40:08 25 4
gpt4 key购买 nike

我正在使用 TypeScript 和 React 重构现有项目。我已经介绍了 ESLint,代码中报告了 300 多个错误。

这是一项相当大的任务,因为 ESLint 本身无法自动修复这些问题。

有没有办法从 ESLint CLI 输出中获取明显的错误,然后使用该规则来获取引发规则错误的文件,以便我可以逐个规则而不是逐个文件地修复它?

最佳答案

Eslint 支持 custom formatters .所以你可以做一些事情:

添加文件lint-formatter.js

module.exports = results => {
const byRuleId = results.reduce(
(map, current) => {
current.messages.forEach(({ ruleId, line, column }) => {
if (!map[ruleId]) {
map[ruleId] = [];
}

const occurrence = `${current.filePath}:${line}:${column}`;
map[ruleId].push(occurrence);
});
return map;
}, {}
);

return Object.entries(byRuleId)
.map(([ruleId, occurrences]) => `${ruleId} (total: ${occurrences.length})\n${occurrences.join('\n')}`)
.join('\n########################\n');
};

上面的示例按规则 ID 对错误/警告进行分组,但当然一切都在您手中。

然后使用您的自定义格式化程序运行 linter:
eslint -f ./lint-formatter.js

样本输出:
object-curly-spacing (total: 2)
foo/bar.js:2:8
foo/bar.js:3:13
########################
no-trailing-spaces (total: 1)
foo/bar.js:7:11
########################
object-curly-newline (total: 2)
foo/bar.js:14:8
foo/bar.js:15:31
########################
space-infix-ops (total: 1)
foo/bar.js:18:29

关于javascript - 有没有办法获得 ESLint 报告的独特错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62450965/

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