javascript - eslint - vue/script-indent 忽略对象字段

标签 javascript vue.js eslint eslintrc

我有以下 ESLint 规则设置:

"vue/script-indent": [
    "error",
    4,
    {
        "baseIndent": 1,
        "switchCase": 1,
        "ignores":
        [
            "[init.type=\"ObjectExpression\"]",
            "[init.type=\"ArrayExpression\"]"
        ]
    }
]

但是,对于以下情况(对象键的值是另一个对象),我希望忽略缩进。

这是 linter 的输出:

let example =
    {
        example:
            {
                test: 
                    "test"
            }
    }

但我希望嵌套对象不受影响,所以它看起来像这样:

let example =
    {
        example:
        {
            test: 
                "test"
        }
    }

所以它应该是一个对象,它位于一个应该被忽略的对象中。我也希望对象内的数组也被忽略(因此我忽略了对象和数组)

最佳答案

以下规则将 vue/script-indent 配置为忽略 .vue 中的嵌套对象/数组:

"vue/script-indent": [
    "error",
    4,
    {
        "baseIndent": 1,
        "switchCase": 1,
        "ignores": [
            // nested objects, excluding top level of exported object (data, methods, computed, etc.)
            "[value.type='ObjectExpression']:not(:matches(ExportDefaultDeclaration, [left.property.name='exports']) > * > [value.type='ObjectExpression'])",

            // nested arrays
            "[value.type='ArrayExpression']"
        ]
    }
],

注意事项

  • 在 TypeScript 中,类属性的装饰器(例如,@Prop private msg!: string)会导致 linter 错误,之后的每一行都会被 linter 忽略。 ( vuejs/eslint-plugin-vue#834 )解决方法:插入一个空方法(即 _unused() {})作为类的第一个元素。

  • 对象字段的顺序可能会导致 linter 忽略整个对象 (vuejs/eslint-plugin-vue#833)。 解决方法:确保对象将文字作为其第一个字段。

关于javascript - eslint - vue/script-indent 忽略对象字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54755357/

相关文章:

javascript - Bootstrap-tour 与 Bootstrap 4 alpha 6 n.popover 不是一个函数

javascript - Vuejs IF 语句

eslint - 值(value)...不应该有额外的属性

javascript - 期望 'this' 被类方法使用

javascript - 如何在react中获取boolean类型的props值

javascript - 如何使用 Protractor 处理具有相同中继器的两个表

javascript - 为什么 [].concat(...arr) 有助于展平嵌套数组

javascript - 如何让我的 VueJA/Buefy 自动完成显示筛选选项?

javascript - Vue-Router 抽象父路由

javascript - 如何自动检查意外遗留在 Mocha 规范中的 '.only' 调用?