英文:
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 '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>
`;
}
The when
function creates a conflict between our Prettier and ESLint configuration.
Here is our prettier
configuration:
{
"printWidth": 100,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
}
and our eslint/indent
rule:
"indent": [
"error",
2,
{
"SwitchCase": 1,
"CallExpression": { "arguments": 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-len、no-mixed-spaces-and-tabs、keyword-spacing、comma-style…
>
> Prettier 可以减少对这整个类别规则的需求!Prettier 会以一种一致的方式重新打印整个程序,因此程序员不再可能在这方面犯错误
>
> 代码质量规则:例如 no-unused-vars、no-extra-bind、no-implicit-globals、prefer-promise-reject-errors…
>
> Prettier 对这类规则没有任何帮助。它们也是代码检查工具提供的最重要的规则,因为它们有可能捕捉到代码中的真实错误!
>
> 换句话说,使用 Prettier 进行格式化,使用 代码检查工具捕捉错误!
这个建议也适用于 eslint/indent 规则。关于这个冲突(以及解决方法),在其他地方有比较详细的文档:
- Indentation rule conflict with prettier #372
- Prettier and eslint indents not working together
- Airbnb, ESLint, Prettier conflict over Switch and Case indentation
- eslint indent and @typescript-eslint/indent conflict
- Set up ESlint, Prettier & EditorConfig without conflicts
如果还没有的话,您很可能会受益于按照 与代码检查工具集成 中描述的更纯粹的 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
>
> 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:
- Indentation rule conflict with prettier #372
- Prettier and eslint indents not working together
- Airbnb, ESLint, Prettier conflict over Switch and Case indentation
- eslint indent and @typescript-eslint/indent conflict
- Set up ESlint, Prettier & EditorConfig without conflicts
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论