object-shorthand
要求或禁止对象字面的方法和属性速记语法
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
const foo = {
x: x,
y: y,
z: z,
};
// methods
const bar = {
a: function() {},
b: function() {}
};
现在这里是 ES6 等价物:
🌐 Now here are ES6 equivalents:
// properties
const foo = {x, y, z};
// methods
const bar = {
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"*/
const foo = {
w: function() {},
x: function *() {},
[y]: function() {},
z: z
};
在这种情况下,预期的语法应该是:
🌐 In that case the expected syntax would have been:
/*eslint object-shorthand: "error"*/
const 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"*/
const 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"(默认)期望在可能时使用简写。"methods"确保使用方法简写(也适用于生成器)。"properties"确保使用属性简写(当键名和变量名相同时)。"never"确保在任何对象字面量中不使用属性或方法简写。"consistent"确保在对象字面量中要么全部使用简写,要么全部使用长格式。"consistent-as-needed"确保在对象字面量中要么全部使用简写,要么全部使用长形式,但尽可能确保全部使用简写。
你可以像这样在配置中设置选项:
🌐 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"时才能启用。"ignoreConstructors": true可以用来防止该规则对构造函数报告错误。(默认情况下,该规则将构造函数与其他函数一样处理。)请注意,只有当字符串选项设置为"always"或"methods"时,才能启用此选项。"methodsIgnorePattern"(string)对于其名称匹配此正则表达式模式的方法,将不会强制使用方法简写。请注意,此选项仅在字符串选项设置为"always"或"methods"时可用。"avoidExplicitReturnArrows": true表示对于函数属性,方法优先于显式返回的箭头函数。(默认情况下,该规则允许使用任意一种。)请注意,只有当字符串选项设置为"always"或"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 }]*/
const 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 }]*/
const 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 }]*/
const 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$" }]*/
const 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 }]*/
const 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 }]*/
const 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"]*/
const foo = {
a,
b: "foo",
};
使用 "consistent" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "consistent" option:
/*eslint object-shorthand: [2, "consistent"]*/
const foo = {
a: a,
b: "foo"
};
const 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"]*/
const 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 中引入。