ESLint 无多空格允许在对象声明中

ESLint no-multi-spaces allow within object declaration

本文关键字:对象 声明 空格 ESLint      更新时间:2023-09-26

如何配置无多空格规则以允许以下内容:

var arr = [
    {id: 'abc',    content: 'foo'},
    {id: 'cdefgh', content: 'bar'}
];

默认情况下,它会抱怨content之前的空间。我想我需要在exceptions中添加一个 AST 节点,但我不知道是哪个。

在文档中指出:

确定异常节点类型的最简单方法是使用联机演示。

所以我继续把你的代码放在那里,得到了AST。以下部分似乎是相关的部分:

{
  "type": "ObjectExpression",
  "start": 16,
  "end": 46,
  "properties": [
    {
      "type": "Property",
      "start": 17,
      "end": 26,
      "key": {
        "type": "Identifier",
        "start": 17,
        "end": 19,
        "name": "id"
      },
      "value": {
        "type": "Literal",
        "start": 21,
        "end": 26,
        "value": "abc",
        "raw": "'abc'"
      },
      "kind": "init"
    },
    {
      "type": "Property",
      "start": 31,
      "end": 45,
      "key": {
        "type": "Identifier",
        "start": 31,
        "end": 38,
        "name": "content"
      },
      "value": {
        "type": "Literal",
        "start": 40,
        "end": 45,
        "value": "foo",
        "raw": "'foo'"
      },
      "kind": "init"
    }
  ]
},

由于您的代码似乎与整个对象有关,因此我猜您寻找的 AST 节点是ObjectExpression .

/* eslint no-multi-spaces: [2, { exceptions: { "ObjectExpression": true } }] */

请让我知道这是否有效。