首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否有一种以编程方式逐行抑制所有ESLint错误的方法?

是否有一种以编程方式逐行抑制所有ESLint错误的方法?
EN

Stack Overflow用户
提问于 2020-03-11 03:50:48
回答 7查看 4.4K关注 0票数 21

如果我添加任何给定的ESLint规则,例如,在现有的代码库中添加no-param-reassign,可能会有很多违规行为。

是否有一种好的方法以编程方式在逐行基础上添加对所有现有违规行为的抑制?

在这个例子中:

代码语言:javascript
复制
// eslint-diable-next-line no-param-reassign
param = foo;

澄清

do想要我的项目中的规则,保护我们编写的所有新代码。我不希望修复所有手工释放违规的旧代码(如果可能,我希望脚本可以帮我修复这些代码)。这就是为什么我要制止所有现有的侵权行为,但尊重所有新的侵权行为。我的主要目标是尽快遵守新规则,以便在所有新代码上获得它的值。我不介意以前挥之不去的压制违规行为。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2020-09-22 21:08:54

我遇到了同样的问题,一堆react-hooks/exhaustive-deps lint警告最终使用了json格式化程序和一个脚本来插入eslint禁用注释。我运行yarn lint . -f json -o warnings.json来获取lints的json列表,然后如下

代码语言:javascript
复制
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);
  });
});

对我来说效果很好,虽然我希望我插入的这些评论是自动格式化的。

票数 11
EN

Stack Overflow用户

发布于 2022-08-02 13:20:45

我尝试了@ work 15185791回答的抑制-避免错误npm,但是它不适用于ESLint8。但是,我从npm的问题上找到了https://github.com/mizdra/eslint-interactive。到目前为止,这一奇妙的国家预防机制正在完美地运作。

执行如下所示。

代码语言:javascript
复制
yarn add -D eslint-interactive
yarn eslint-interactive src
yarn remove eslint-interactive
票数 4
EN

Stack Overflow用户

发布于 2021-04-07 17:28:11

我通常使用这个:https://github.com/amanda-mitchell/suppress-eslint-errors

让jscodemod为您的回购配置是有点困难,但值得的是,一旦你让它发挥作用。外面有很多强大的共鬼。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60629026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档