object-shorthand

要求或禁止对象字面的方法和属性速记语法

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行选项自动修复

ECMAScript 6 为定义对象字面量方法和属性提供了一种简洁的形式。这种语法可以使定义复杂的对象字面量更加清晰。

¥ECMAScript 6 provides a concise form for defining object literal methods and properties. This syntax can make defining complex object literals much cleaner.

以下是一些使用 ES5 语法的常见示例:

¥Here are a few common examples using the ES5 syntax:

// properties
var foo = {
    x: x,
    y: y,
    z: z,
};

// methods
var foo = {
    a: function() {},
    b: function() {}
};

现在这里是 ES6 等价物:

¥Now here are ES6 equivalents:

// properties
var foo = {x, y, z};

// methods
var foo = {
    a() {},
    b() {}
};

规则详情

¥Rule Details

此规则强制使用速记语法。这适用于在对象字面量中定义的所有方法(包括生成器)以及在键名称与分配变量名称匹配的情况下定义的任何属性。

¥This rule enforces the use of the shorthand syntax. This applies to all methods (including generators) defined in object literals and any properties defined where the key name matches name of the assigned variable.

以下每个属性都会触发警告:

¥Each of the following properties would warn:

/*eslint object-shorthand: "error"*/

var foo = {
    w: function() {},
    x: function *() {},
    [y]: function() {},
    z: z
};

在这种情况下,预期的语法应该是:

¥In that case the expected syntax would have been:

/*eslint object-shorthand: "error"*/

var foo = {
    w() {},
    *x() {},
    [y]() {},
    z
};

此规则不标记对象字面量内的箭头函数。以下不会警告:

¥This rule does not flag arrow functions inside of object literals. The following will not warn:

/*eslint object-shorthand: "error"*/

var foo = {
    x: (y) => y
};

选项

¥Options

该规则采用一个选项,该选项指定何时应用它。它可以设置为以下值之一:

¥The rule takes an option which specifies when it should be applied. It can be set to one of the following values:

  • "always"(默认)希望尽可能使用简写。

    ¥"always" (default) expects that the shorthand will be used whenever possible.

  • "methods" 确保使用方法简写(也适用于生成器)。

    ¥"methods" ensures the method shorthand is used (also applies to generators).

  • "properties" 确保使用属性速记(键和变量名称匹配)。

    ¥"properties" ensures the property shorthand is used (where the key and variable name match).

  • "never" 确保在任何对象字面量中不使用任何属性或方法简写。

    ¥"never" ensures that no property or method shorthand is used in any object literal.

  • "consistent" 确保在对象字面量中使用所有速记形式或所有长形式。

    ¥"consistent" ensures that either all shorthand or all long-form will be used in an object literal.

  • "consistent-as-needed" 确保在对象字面量中使用所有简写形式或所有长形式,但尽可能确保所有简写形式。

    ¥"consistent-as-needed" ensures that either all shorthand or all long-form will be used in an object literal, but ensures all shorthand whenever possible.

你可以像这样在配置中设置选项:

¥You can set the option in configuration like this:

{
    "object-shorthand": ["error", "always"]
}

此外,该规则采用可选的对象配置:

¥Additionally, the rule takes an optional object configuration:

  • "avoidQuotes": true 表示只要对象键是字符串字面量,就首选长格式语法(默认值:false)。请注意,仅当字符串选项设置为 "always""methods""properties" 时,才能启用此选项。

    ¥"avoidQuotes": true indicates that long-form syntax is preferred whenever the object key is a string literal (default: false). Note that this option can only be enabled when the string option is set to "always", "methods", or "properties".

  • "ignoreConstructors": true 可用于防止规则报告构造函数的错误。(默认情况下,该规则以与其他函数相同的方式对待构造函数。)请注意,只有当字符串选项设置为 "always""methods" 时才能启用此选项。

    ¥"ignoreConstructors": true can be used to prevent the rule from reporting errors for constructor functions. (By default, the rule treats constructors the same way as other functions.) Note that this option can only be enabled when the string option is set to "always" or "methods".

  • "methodsIgnorePattern" (string) 对于名称与此正则表达式模式匹配的方法,将不会强制执行方法简写。请注意,此选项只能在字符串选项设置为 "always""methods" 时使用。

    ¥"methodsIgnorePattern" (string) for methods whose names match this regex pattern, the method shorthand will not be enforced. Note that this option can only be used when the string option is set to "always" or "methods".

  • "avoidExplicitReturnArrows": true 表示对于函数属性,方法优先于显式返回箭头函数。(默认情况下,规则允许其中任何一个。)请注意,只有当字符串选项设置为 "always""methods" 时才能启用此选项。

    ¥"avoidExplicitReturnArrows": true indicates that methods are preferred over explicit-return arrow functions for function properties. (By default, the rule allows either of these.) Note that this option can only be enabled when the string option is set to "always" or "methods".

avoidQuotes

{
    "object-shorthand": ["error", "always", { "avoidQuotes": true }]
}

使用 "always", { "avoidQuotes": true } 选项的此规则的错误代码示例:

¥Example of incorrect code for this rule with the "always", { "avoidQuotes": true } option:

在线运行
/*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/

var foo = {
    "bar-baz"() {}
};

使用 "always", { "avoidQuotes": true } 选项的此规则的正确代码示例:

¥Example of correct code for this rule with the "always", { "avoidQuotes": true } option:

在线运行
/*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/

var foo = {
    "bar-baz": function() {},
    "qux": qux
};

ignoreConstructors

{
    "object-shorthand": ["error", "always", { "ignoreConstructors": true }]
}

使用 "always", { "ignoreConstructors": true } 选项的此规则的正确代码示例:

¥Example of correct code for this rule with the "always", { "ignoreConstructors": true } option:

在线运行
/*eslint object-shorthand: ["error", "always", { "ignoreConstructors": true }]*/

var foo = {
    ConstructorFunction: function() {}
};

methodsIgnorePattern

使用 "always", { "methodsIgnorePattern": "^bar$" } 选项的此规则的正确代码示例:

¥Example of correct code for this rule with the "always", { "methodsIgnorePattern": "^bar$" } option:

在线运行
/*eslint object-shorthand: ["error", "always", { "methodsIgnorePattern": "^bar$" }]*/

var foo = {
    bar: function() {}
};

avoidExplicitReturnArrows

{
    "object-shorthand": ["error", "always", { "avoidExplicitReturnArrows": true }]
}

使用 "always", { "avoidExplicitReturnArrows": true } 选项的此规则的错误代码示例:

¥Example of incorrect code for this rule with the "always", { "avoidExplicitReturnArrows": true } option:

在线运行
/*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/

var foo = {
  foo: (bar, baz) => {
    return bar + baz;
  },

  qux: (foobar) => {
    return foobar * 2;
  }
};

使用 "always", { "avoidExplicitReturnArrows": true } 选项的此规则的正确代码示例:

¥Example of correct code for this rule with the "always", { "avoidExplicitReturnArrows": true } option:

在线运行
/*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/

var foo = {
  foo(bar, baz) {
    return bar + baz;
  },

  qux: foobar => foobar * 2
};

使用 "consistent" 选项的此规则的错误代码示例:

¥Example of incorrect code for this rule with the "consistent" option:

在线运行
/*eslint object-shorthand: [2, "consistent"]*/

var foo = {
    a,
    b: "foo",
};

使用 "consistent" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "consistent" option:

在线运行
/*eslint object-shorthand: [2, "consistent"]*/

var foo = {
    a: a,
    b: "foo"
};

var bar = {
    a,
    b,
};

使用 "consistent-as-needed" 选项的错误代码示例,与 "consistent" 非常相似:

¥Example of incorrect code with the "consistent-as-needed" option, which is very similar to "consistent":

在线运行
/*eslint object-shorthand: [2, "consistent-as-needed"]*/

var foo = {
    a: a,
    b: b,
};

何时不使用

¥When Not To Use It

任何尚未处于 ES6 环境中的人都不想应用此规则。其他人可能会发现简写语法的简洁性更难阅读,并且可能不想通过此规则来鼓励它。

¥Anyone not yet in an ES6 environment would not want to apply this rule. Others may find the terseness of the shorthand syntax harder to read and may not want to encourage it with this rule.

版本

此规则是在 ESLint v0.20.0 中引入。

进阶读物

资源