【问题标题】:How do you disable no-plusplus when using eslint?使用 eslint 时如何禁用 no-plusplus?
【发布时间】:2018-05-23 13:27:43
【问题描述】:

在 Visual Studio Code(AirBnB 风格的 Ubuntu Linux)中使用 eslint 时,默认启用 no-plusplus,因此在 for 循环中使用 ++ 会出错:[eslint] 使用了一元运算符 '++'。 (无加号)

如何禁用该设置?

【问题讨论】:

    标签: eslint eslint-config-airbnb


    【解决方案1】:

    您可以在 .eslintrc.js 文件中覆盖它,如下所示:

    'no-plusplus': 'off'
    

    或者如果您不想完全禁用它,但仅适用于for-loops

    'no-plusplus': [2, { allowForLoopAfterthoughts: true }]
    

    【讨论】:

    • 我需要用任何东西包围它吗?我收到此错误:(function (exports, require, module, __filename, __dirname) { 'no-plusplus': 'off' ^ SyntaxError: Unexpected token :
    • 对于较新版本的 ESLint,请使用 "allowForLoopAfterthoughts": true,如文档中所述:eslint.org/docs/rules/no-plusplus#options
    • 你可以简单地写成 += 1,正如 ESLint 所建议的那样。
    【解决方案2】:

    您也可以改写为variable += 1as suggested by ESLint

    【讨论】:

    • 我认为这是最好的答案
    • @БагдаулетСайын 除了它甚至没有回答问题...
    【解决方案3】:

    你可以在Linux上通过使用grep搜索关键字来定位你需要改变的文件位置,在这种情况下,在安装eslint的文件夹中搜索包含plusplus的文件使用

    grep -r plusplus

    正确的文件是 eslint-config 文件,在这种情况下应该是:node_modules/eslint-config-airbnb-base/rules/style.js

    要禁用设置注释掉 no-plusplus 行,如果需要,您可以轻松地重新启用:

    // 'no-plusplus': 'error',
    

    【讨论】:

    • 在 node_modules 中编辑文件不应被视为做某事的“正确方法”
    【解决方案4】:

    或者你可以这样:

    'no-plusplus': 0,
    

    【讨论】:

      【解决方案5】:

      您可以按照 ESLint 的建议简单地编写您声明的 variable += 1

      varible++ is similar as variable+=1.
      

      【讨论】:

      • 这个答案在您回答之前已经由 Fabian 提出?一些用户可能会对此投反对票,因为它没有添加任何新内容并从 Fabian 中删除信用,建议删除
      猜你喜欢
      • 2021-04-14
      • 2022-10-12
      • 2020-01-25
      • 2020-06-04
      • 2021-09-05
      • 1970-01-01
      • 2020-10-21
      • 2020-04-19
      • 2021-04-02
      相关资源
      最近更新 更多