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

ESLint、indent和ignoreNodes-使AST选择器正常工作时遇到问题

  •  0
  • kshetline  · 技术社区  · 5 年前

    随着TSLint被弃用,我正在将项目转换为使用ESLint。

    我想设置我的缩进规则来允许这样做:

    type HtmlColor = 'attrib' | 'background' | 'bg_whitespace' | 'comment' | 'entity' | 'error' | 'foreground' |
                     'invalid' | 'markup' | 'tag' | 'value' | 'warning' | 'whitespace';
    

    我目前收到一个关于第二行缩进的错误,它应该是0个空格而不是17个。我可能以为这个错误会抱怨它不是2个空格,但肯定不是0。

    无论如何,如果我不能得到我想要识别的特定缩进,在这种情况下,我至少希望忽略缩进。

    我试着用 ignoredNodes ESLint的选项 indent 规则如下:

        "@typescript-eslint/indent": [
          "error",
          2,
          {
            "ArrayExpression": "first",
            "FunctionDeclaration": { parameters: "first" },
            "ignoredNodes": [
              "ArrowFunction > Block",
              "NoSubstitutionTemplateLiteral",
              "TemplateLiteral",
              "TypeAliasDeclaration *"
            ],
            "ObjectExpression": "first",
            "VariableDeclarator": "first",
            "SwitchCase": 1
          }
        ],
    

    我看得出来 零件 忽略节点 列表正在按我的预期工作,例如 "TemplateLiteral" 部分,但我无法找出正确的AST语法来处理 type 声明和其他类似箭头函数的东西。

    我用过 AST Explorer 以帮助弄清楚AST选择器应该是什么,但到目前为止还没有成功。

    0 回复  |  直到 5 年前
        1
  •  3
  •   kshetline    5 年前

    我找到了我要找的语法:

            "ignoredNodes": [
              "ArrowFunctionExpression > BlockStatement",
              "NoSubstitutionTemplateLiteral",
              "TemplateLiteral",
              "TSTypeAliasDeclaration *"
            ],
    

    诀窍在于意识到不同的设置 AST Explorer 生产 不同的节点名称 对于相同的TypeScript语言构造!直到我专门设置AST Explorer使用 @typescript-eslint/parser ,而不仅仅是 typescript ,以及“ESLint v4”的转换,我在从我的TypeScript代码生成的解析树中得到了错误的选择器名称。