忽略模板文字中的缩进,使用ESLint`indent`规则 [英] Ignore the indentation in a template literal, with the ESLint `indent` rule
问题描述
针对 indent 的ESLint规则,您可以在确定是否要忽略哪些节点时指定该规则应使用ignoredNodes
选项应用于该节点.
我有以下要用此规则忽略的代码:
const a = b
? `c${
d
}`
: e
具体来说,报告带有d
的行和随后的行比应有的空格多两个.我想忽略规则中的那些行,但是我无法弄清楚应该应用的节点.
在此存储库中指定了节点类型.我知道像在此代码中使用的那样,三元表达式是一个ConditionalExpression
节点,它看起来像一个解决方案
通过简单地查看 ast树 ${foo}
,我确认它确实是TemplateLiteral
节点.但是我碰巧也从那里知道解决方案:您还需要选择它的子项.这可以通过TemplateLiteral > *
完成.您的解决方案无法正常工作的原因是,ESLint
将免除缩进规则中的TemplateLiteral
,但只删除了${
和}
的一小部分,而没有内容./p>
这可以这样进入您的ESLintrc文件:
"indent": ["error", "tab", { "ignoredNodes": ["TemplateLiteral > *"] }]
对不起,没有其他人能够尽快回答您的问题.
The ESLint rule for indent allows for you to specify which nodes are ignored, when determining whether the rule should apply to that node, using the ignoredNodes
option.
I've got the following code that I want to ignore with this rule:
const a = b
? `c${
d
}`
: e
Specifically, the line with d
and the subsequent line are reported as having two more spaces than they should. I want to ignore those lines from the rule, but I can't figure out the node that should apply.
Node types are specified in this repo. I know that ternary expressions, like used in this code, is a ConditionalExpression
node, and it seems like a template literal node exists, but I can't get it to work.
I know I can use eslint-disable-next-line
, eslint-disable
, etc., but I don't want to use those, because then I'd have to use them every time this comes up.
From looking at an ast tree with simply ${foo}
, I confirmed that it is truly the TemplateLiteral
node. However I happened to also know the solution from there: you need to select it's children as well. This can be done with TemplateLiteral > *
. The reason why your solution wasn't working is because ESLint
will exempt the TemplateLiteral
from the indent rules, but only the little ${
and }
parts, not the contents.
This can go into your ESLintrc file as such:
"indent": ["error", "tab", { "ignoredNodes": ["TemplateLiteral > *"] }]
I'm sorry no one else was able to answer your question sooner.
这篇关于忽略模板文字中的缩进,使用ESLint`indent`规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!