代码之家  ›  专栏  ›  技术社区  ›  Gary

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

  •  5
  • Gary  · 技术社区  · 6 年前

    indent 允许您在确定规则是否应应用于该节点时,使用 ignoredNodes 选项。

    我想用这个规则忽略以下代码:

    const a = b
      ? `c${
          d
        }`
      : e
    

    特别是 d 随后的行被报告为比应该的多了两个空格。我想忽略规则中的那些行,但我无法找出应该应用的节点。

    in this repo . 我知道三元表达式,就像在这段代码中使用的,是一个 ConditionalExpression 节点,看起来像一个 template literal node 存在,但我不能让它工作。

    eslint-disable-next-line , eslint-disable ,等等,但我不想用它们,因为每次出现这个问题我都要用它们。

    2 回复  |  直到 6 年前
        1
  •  10
  •   fregante    4 年前

    从看一个 ast tree 简单地 ${foo} ,我确认这是真正的 TemplateLiteral TemplateLiteral > * . 你的解决方案不起作用的原因是 ESLint 免除 从缩进规则,但只有一点点 ${ } 部分,而不是内容。

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

    很抱歉没人能早点回答你的问题。

        2
  •  7
  •   Maarten ter Horst    5 年前

    "ignoredNodes": ["TemplateLiteral *"] (没有 > 子选择器,以所有子体为目标。)

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

    您还必须指定正常缩进规则,例如。 "error", 2 用于强制2个空格或 "error", "tab"

    "function-paren-newline" 我还想禁用它(因为我正在扩展一个显示错误的现有ESLint配置)。