【问题标题】:ESLint always throwing key-spacing errorESLint 总是抛出键间距错误
【发布时间】:2017-02-04 11:45:47
【问题描述】:

我的 .eslintrc 中有以下内容:

'key-spacing': [ 'error', {
    'singleLine': {
        'beforeColon'   : false,
        'afterColon'    : true
    },
    'multiLine': {
        'beforeColon'   : false,
        'afterColon'    : true,
        'align'         : 'colon'
    }
}]

目标是确保在对象分配中满足以下条件:

  1. 在单行赋值中,每个冒号前没有空格, 但后面还有一个。
  2. 在多行赋值中,冒号水平排列,并且 每个冒号前后都有一个空格。

奇怪的是下面三个代码sn-ps:

1 [来自我的 app.vue 文件]。

export default {
    name        : 'app',
    components  : {
        todos
    }
}

2 [来自我的 main.js 文件]。

new Vue({
    el      : '#app',
    render  : h => h( App )
})

3 [来自我的 Hello.spec.js 文件]。

const vm = new Vue({
    el      : document.createElement( 'div' ),
    render  : h => h( Hello )
})

在 key-spacing eslint 规则中每个都抛出错误:

/Users/autoboxer/development/learning-vue/src/app.vue
    12:3  error  Missing space after key 'name'  key-spacing

/Users/autoboxer/development/learning-vue/src/main.js
    6:2  error  Missing space after key 'el'  key-spacing

/Users/autoboxer/development/learning-vue/test/unit/specs/Hello.spec.js
    7:4  error  Missing space after key 'el'  key-spacing

我无法从我的设置中弄清楚为什么它们会导致列出的错误,因为每个指定的单词后面都有必要的空格,但我们将不胜感激。

【问题讨论】:

  • 使用标签,也许?
  • 我正在使用标签。奇怪的是,它只抱怨每种情况下的顶线。所有三个文件都应该抛出两个错误,但第一个对象成员是它抱怨的唯一一个。
  • 这就是问题所在,我需要使用空格而不是制表符来对齐它们。

标签: vue.js eslint


【解决方案1】:

此规则的配置非常复杂。我认为您正在寻找的是这样的:

'key-spacing': [ 'error', {
    'singleLine': {
        'beforeColon' : false,
        'afterColon'  : true
    },
    "align": {
        "beforeColon" : true,
        "afterColon"  : true,
        "on"          : "colon"
    }
}]

但是,即使使用这种配置,ESLint 也不允许冒号的任意位置。它们必须尽可能接近对象中最长的键名。因此,通过上述配置,您的代码必须更改为:

export default {
    name       : 'app',
    components : {
        todos
    }
}

这将使用我提供的配置正确 lint。

【讨论】:

  • 感谢 Ilya,这绝对是我面临的问题的一半。另一半是我使用制表符而不是空格。由于难以确定与制表符的对齐方式,因此无法使用它们。本期对此进行了概述:github.com/eslint/eslint/issues/1727
猜你喜欢
  • 2018-08-06
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多