简体   繁体   English

使用 ESLint `indent` 规则忽略模板文字中的缩进

[英]Ignore the indentation in a template literal, with the ESLint `indent` rule

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.用于缩进的 ESLint 规则允许您在确定规则是否应应用于该节点时指定忽略哪些节点,使用ignoredNodes选项。

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.具体来说,带有d的行和随后的行被报告为多出两个空格。 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 .节点类型在此 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.我知道这个代码中使用的三元表达式是一个ConditionalExpression节点,它似乎存在一个模板文字节点,但我无法让它工作。

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.我知道我可以使用eslint-disable-next-lineeslint-disable等,但我不想使用这些,因为每次出现这种情况时我都必须使用它们。

From looking at an ast tree with simply ${foo} , I confirmed that it is truly the TemplateLiteral node.通过简单地查看带有${foo}ast 树,我确认它确实是TemplateLiteral节点。 However I happened to also know the solution from there: you need to select its children as well.但是我碰巧也知道那里的解决方案:您还需要选择它的孩子。 This can be done with TemplateLiteral > * .这可以通过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.您的解决方案不起作用的原因是因为ESLintTemplateLiteral排除在缩进规则之外,但只有小的${}部分,而不是内容。

This can go into your ESLintrc file as such:这可以像这样进入您的 ESLintrc 文件:

"indent": ["error", "tab", { "ignoredNodes": ["TemplateLiteral > *"] }]

I'm sorry no one else was able to answer your question sooner.很抱歉没有其他人能够更早地回答您的问题。

To ignore all indentation and newline errors in ESLint inside a template literal, including function calls or deeply nested JS syntax, you can use "ignoredNodes": ["TemplateLiteral *"] (without the > child selector, to target all descendants.)要忽略模板文字中 ESLint 中的所有缩进和换行错误,包括函数调用或深度嵌套的 JS 语法,您可以使用"ignoredNodes": ["TemplateLiteral *"] (没有>子选择器,以定位所有后代。)

  "rules": {
    "indent": ["error", 2, { "ignoredNodes": ["TemplateLiteral *"] }],
    "function-paren-newline": "off"
  }

You also have to specify your normal indentation rule, eg "error", 2 for enforcing 2 spaces or "error", "tab" for tabs.您还必须指定您的正常缩进规则,例如"error", 2用于强制 2 个空格或"error", "tab"用于制表符。

"function-paren-newline" was also giving errors inside my template literals, I also wanted to disable that (because I'm extending an existing ESLint config that displayed an error for that). "function-paren-newline"也在我的模板文字中给出了错误,我也想禁用它(因为我正在扩展一个显示错误的现有 ESLint 配置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM