enforce consistent spacing between keys and values in object literal properties (key-spacing)

强制在对象字面量的键和值之间使用一致的空格 (key-spacing)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

This rule enforces spacing around the colon in object literal properties. It can verify each property individually, or it can ensure horizontal alignment of adjacent properties in an object literal.

该规则强制对象属性的冒号左右的空格的一致性。它可以单独验证每一个属性,或它可以确保对象中的属性在垂直方向上对齐。

Rule Details

This rule enforces consistent spacing between keys and values in object literal properties. In the case of long lines, it is acceptable to add a new line wherever whitespace is allowed.

该规则强制在对象字面量的键和值之间使用一致的空格。如果某一行很长的话,在允许空白出现的情况下,可以增加一空行,这种情况是该规则可以接受的。

Options

This rule has an object option:

该规则有一个对象选项:

Please note that you can either use the top-level options or the grouped options (singleLine and multiLine) but not both.

请注意,你可以使用顶级选项或分组选项 (singleLinemultiLine),但不能同时使用两者。

beforeColon

Examples of incorrect code for this rule with the default { "beforeColon": false } option:

默认选项 { "beforeColon": false }错误 代码示例:

/*eslint key-spacing: ["error", { "beforeColon": false }]*/

var obj = { "foo" : 42 };

Examples of correct code for this rule with the default { "beforeColon": false } option:

默认选项 { "beforeColon": false }正确 代码示例:

/*eslint key-spacing: ["error", { "beforeColon": false }]*/

var obj = { "foo": 42 };

Examples of incorrect code for this rule with the { "beforeColon": true } option:

选项 { "beforeColon": true }错误 代码示例:

/*eslint key-spacing: ["error", { "beforeColon": true }]*/

var obj = { "foo": 42 };

Examples of correct code for this rule with the { "beforeColon": true } option:

选项 { "beforeColon": true }正确 代码示例:

/*eslint key-spacing: ["error", { "beforeColon": true }]*/

var obj = { "foo" : 42 };

afterColon

Examples of incorrect code for this rule with the default { "afterColon": true } option:

默认选项 { "afterColon": true }错误 代码示例:

/*eslint key-spacing: ["error", { "afterColon": true }]*/

var obj = { "foo":42 };

Examples of correct code for this rule with the default { "afterColon": true } option:

默认选项 { "afterColon": true }正确 代码示例:

/*eslint key-spacing: ["error", { "afterColon": true }]*/

var obj = { "foo": 42 };

Examples of incorrect code for this rule with the { "afterColon": false } option:

选项 { "afterColon": false }错误 代码示例:

/*eslint key-spacing: ["error", { "afterColon": false }]*/

var obj = { "foo": 42 };

Examples of correct code for this rule with the { "afterColon": false } option:

选项 { "afterColon": false }正确 代码示例:

/*eslint key-spacing: ["error", { "afterColon": false }]*/

var obj = { "foo":42 };

mode

Examples of incorrect code for this rule with the default { "mode": "strict" } option:

默认选项 { "mode": "strict" }错误 代码示例:

/*eslint key-spacing: ["error", { "mode": "strict" }]*/

call({
    foobar: 42,
    bat:    2 * 2
});

Examples of correct code for this rule with the default { "mode": "strict" } option:

默认选项 { "mode": "strict" }正确 代码示例:

/*eslint key-spacing: ["error", { "mode": "strict" }]*/

call({
    foobar: 42,
    bat: 2 * 2
});

Examples of correct code for this rule with the { "mode": "minimum" } option:

选项 { "mode": "minimum" }正确 代码示例:

/*eslint key-spacing: ["error", { "mode": "minimum" }]*/

call({
    foobar: 42,
    bat:    2 * 2
});

align

Examples of incorrect code for this rule with the { "align": "value" } option:

选项 { "align": "value" }错误 代码示例:

/*eslint key-spacing: ["error", { "align": "value" }]*/

var obj = {
    a: value,
    bcde:  42,
    fg :   foo()
};

Examples of correct code for this rule with the { "align": "value" } option:

选项 { "align": "value" }正确 代码示例:

/*eslint key-spacing: ["error", { "align": "value" }]*/

var obj = {
    a:    value,
    bcde: 42,

    fg: foo(),
    h:  function() {
        return this.a;
    },
    ijkl: 'Non-consecutive lines form a new group'
};

var obj = { a: "foo", longPropertyName: "bar" };

Examples of incorrect code for this rule with the { "align": "colon" } option:

选项 { "align": "colon" }错误 代码示例:

/*eslint key-spacing: ["error", { "align": "colon" }]*/

call({
    foobar: 42,
    bat:    2 * 2
});

Examples of correct code for this rule with the { "align": "colon" } option:

选项 { "align": "colon" }正确 代码示例:

/*eslint key-spacing: ["error", { "align": "colon" }]*/

call({
    foobar: 42,
    bat   : 2 * 2
});

align

The align option can take additional configuration through the beforeColon, afterColon, mode, and on options.

align 选项可以通过 beforeColonafterColonmodeon 进行额外的配置。

If align is defined as an object, but not all of the parameters are provided, undefined parameters will default to the following:

如果 align 被定义为一个对象,但是没有提供所有的参数,那么,未定义的参数将默认为:

// Defaults
align: {
    "beforeColon": false,
    "afterColon": true,
    "on": "colon",
    "mode": "strict"
}

Examples of correct code for this rule with sample { "align": { } } options:

选项 { "align": { } }正确 代码示例:

/*eslint key-spacing: ["error", {
    "align": {
        "beforeColon": true,
        "afterColon": true,
        "on": "colon"
    }
}]*/

var obj = {
    "one"   : 1,
    "seven" : 7
}
/*eslint key-spacing: ["error", {
    "align": {
        "beforeColon": false,
        "afterColon": false,
        "on": "value"
    }
}]*/

var obj = {
    "one":  1,
    "seven":7
}

align and multiLine

The multiLine and align options can differ, which allows for fine-tuned control over the key-spacing of your files. align will not inherit from multiLine if align is configured as an object.

multiLinealign 选项可以有所区别,这将允许对你的文件进行更细粒度的控制 key-spacing。如果 align 被配置为一个对象,align 将不会multiLine 继承。

multiLine is used any time an object literal spans multiple lines. The align configuration is used when there is a group of properties in the same object. For example:

multiLine 可以在任何时候被用在跨行的对象字面量上。而当一个对象有多个属性时,使用 align 配置。

var myObj = {
  key1: 1, // uses multiLine

  key2: 2, // uses align (when defined)
  key3: 3, // uses align (when defined)

  key4: 4 // uses multiLine
}

Examples of incorrect code for this rule with sample { "align": { }, "multiLine": { } } options:

选项 { "align": { }, "multiLine": { } }错误 代码示例:

/*eslint key-spacing: ["error", {
    "multiLine": {
        "beforeColon": false,
        "afterColon":true
    },
    "align": {
        "beforeColon": true,
        "afterColon": true,
        "on": "colon"
    }
}]*/

var obj = {
    "myObjectFunction": function() {
        // Do something
    },
    "one"             : 1,
    "seven"           : 7
}

Examples of correct code for this rule with sample { "align": { }, "multiLine": { } } options:

选项 { "align": { }, "multiLine": { } }正确 代码示例:

/*eslint key-spacing: ["error", {
    "multiLine": {
        "beforeColon": false,
        "afterColon": true

    },
    "align": {
        "beforeColon": true,
        "afterColon": true,
        "on": "colon"
    }
}]*/

var obj = {
    "myObjectFunction": function() {
        // Do something
        //
    }, // These are two separate groups, so no alignment between `myObjectFuction` and `one`
    "one"   : 1,
    "seven" : 7 // `one` and `seven` are in their own group, and therefore aligned
}

singleLine and multiLine

Examples of correct code for this rule with sample { "singleLine": { }, "multiLine": { } } options:

选项 { "singleLine": { }, "multiLine": { } }正确 代码示例:

/*eslint "key-spacing": [2, {
    "singleLine": {
        "beforeColon": false,
        "afterColon": true
    },
    "multiLine": {
        "beforeColon": true,
        "afterColon": true,
        "align": "colon"
    }
}]*/
var obj = { one: 1, "two": 2, three: 3 };
var obj2 = {
    "two" : 2,
    three : 3
};

When Not To Use It

If you have another convention for property spacing that might not be consistent with the available options, or if you want to permit multiple styles concurrently you can safely disable this rule.

对属性空格,如果你有其它约定,与上面可用的选项不一致;或者你允许同时使用多种风格,你可以关闭此规则。

Version

This rule was introduced in ESLint 0.9.0.

该规则在 ESLint 0.9.0 中被引入。

Resources


+