ESLint缩进规则配置错误,与链式调用表达式有关。

huangapple go评论83阅读模式
英文:

ESLint indent rule misconfiguration with chain Call Expressions

问题

在我们的JS文件中,我们有以下结构:

import { html } from 'lit-html';

function when(expression, trueValue, falseValue) {
  if (expression) {
    return trueValue();
  }

  if (falseValue) {
    return falseValue();
  }
  return undefined;
}

export class SomeClass extends LitElement {
  render = () => html`
    <someHtml>
      <p>Some text</p>
      ${when(
        someCondition,
        () => html`<div>on condition true</div>`,
        () => html`<div>on condition false</div>`
      )}
    </someHtml>
  `;
}

when 函数在我们的Prettier和ESLint配置之间创建了冲突。

这是我们的prettier配置:

{
  "printWidth": 100,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "es5"
}

以及我们的eslint/indent规则:

"indent": [
  "error",
  2,
  {
    "SwitchCase": 1,
    "CallExpression": { "arguments": 1 }
  }
]

你能告诉我为什么我在上述设置中会得到以下错误吗?

ESLint: 预期缩进为4个空格,但发现了8个。(indent)
ESLint: 预期缩进为6个空格,但发现了10个。(indent)
英文:

In our JS files, we have below structure:

import { html } from &#39;lit-html&#39;;

function when(expression, trueValue, falseValue) {
  if (expression) {
    return trueValue();
  }

  if (falseValue) {
    return falseValue();
  }
  return undefined;
}

export class SomeClass extends LitElement {
  render = () =&gt; html`
    &lt;someHtml&gt;
      &lt;p&gt;Some text&lt;/p&gt;
      ${when(
        someCondition,
        () =&gt; html`&lt;div&gt;on condition true&lt;/div&gt;`
        () =&gt; html`&lt;div&gt;on condition false&lt;/div&gt;`
      )}
    &lt;/someHtml&gt;
  `;
}

The when function creates a conflict between our Prettier and ESLint configuration.

Here is our prettier configuration:

{
  &quot;printWidth&quot;: 100,
  &quot;singleQuote&quot;: true,
  &quot;tabWidth&quot;: 2,
  &quot;trailingComma&quot;: &quot;es5&quot;,
}

and our eslint/indent rule:

 &quot;indent&quot;: [
  &quot;error&quot;,
  2,
  {
    &quot;SwitchCase&quot;: 1,
    &quot;CallExpression&quot;: { &quot;arguments&quot;: 1 }
  }
]

Can you tell me why I am getting below errors with the above setup?

ESLint: Expected indentation of 4 spaces but found 8.(indent)
ESLint: Expected indentation of 6 spaces but found 10.(indent)

答案1

得分: 2

不要使用 eslint 进行缩进。来自Prettier vs. Linters文档的说明:

> ## 与 ESLint/TSLint/stylelint 等的比较
>
> 代码检查工具有两类规则:
>
> 格式规则:例如:max-lenno-mixed-spaces-and-tabskeyword-spacingcomma-style
>
> Prettier 可以减少对这整个类别规则的需求!Prettier 会以一种一致的方式重新打印整个程序,因此程序员不再可能在这方面犯错误 ESLint缩进规则配置错误,与链式调用表达式有关。
>
> 代码质量规则:例如 no-unused-varsno-extra-bindno-implicit-globalsprefer-promise-reject-errors
>
> Prettier 对这类规则没有任何帮助。它们也是代码检查工具提供的最重要的规则,因为它们有可能捕捉到代码中的真实错误!
>
> 换句话说,使用 Prettier 进行格式化,使用 代码检查工具捕捉错误!

这个建议也适用于 eslint/indent 规则。关于这个冲突(以及解决方法),在其他地方有比较详细的文档:

如果还没有的话,您很可能会受益于按照 与代码检查工具集成 中描述的更纯粹的 eslint 和 prettier 集成方法,并确保不在之后覆盖 prettier/recommended 的配置。

英文:

Don't use eslint for indentation. From the Prettier vs. Linters documentation:

> ## How does it compare to ESLint/TSLint/stylelint, etc.?
>
> Linters have two categories of rules:
>
> Formatting rules: eg: max-len, no-mixed-spaces-and-tabs, keyword-spacing, comma-style
>
> Prettier alleviates the need for this whole category of rules! Prettier is going to reprint the entire program from scratch in a consistent way, so it’s not possible for the programmer to make a mistake there anymore ESLint缩进规则配置错误,与链式调用表达式有关。
>
> Code-quality rules: eg no-unused-vars, no-extra-bind, no-implicit-globals, prefer-promise-reject-errors
>
> Prettier does nothing to help with those kind of rules. They are also the most important ones provided by linters as they are likely to catch real bugs with your code!
>
> In other words, use Prettier for formatting and linters for catching bugs!

This guidance applies to the eslint/indent rule as well. This conflict (and resolution) is fairly well documented elsewhere:

You most likely would benefit from doing a more pure integration between eslint and prettier as described in Integrating with Linters if you haven't already, and then ensuring that you don't override prettier/recommended afterwards.

huangapple
  • 本文由 发表于 2023年5月11日 00:13:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定