
object-shorthand
要求或禁止对象字面量的方法和属性简写语法
ECMAScript 6 提供了一种简洁的形式来定义对象字面量方法和属性。这种语法可以使定义复杂的对象字面量更加清晰。
以下是一些使用 ES5 语法的常见示例
// properties
const foo = {
x: x,
y: y,
z: z,
};
// methods
const bar = {
a: function() {},
b: function() {}
};
现在这里是 ES6 等效项
// properties
const foo = {x, y, z};
// methods
const bar = {
a() {},
b() {}
};
规则详情
此规则强制使用简写语法。这适用于对象字面量中定义的所有方法(包括生成器),以及键名与赋值变量名称匹配的任何属性。
以下每个属性都会发出警告
/*eslint object-shorthand: "error"*/
const foo = {
w: function() {},
x: function *() {},
[y]: function() {},
z: z
};
在这种情况下,预期的语法应该是
/*eslint object-shorthand: "error"*/
const foo = {
w() {},
*x() {},
[y]() {},
z
};
此规则不标记对象字面量内部的箭头函数。以下代码不会发出警告
/*eslint object-shorthand: "error"*/
const foo = {
x: (y) => y
};
选项
该规则接受一个选项,用于指定何时应应用该规则。它可以设置为以下值之一
"always"
(默认)期望在任何可能的情况下都使用简写。"methods"
确保使用方法简写(也适用于生成器)。"properties"
确保使用属性简写(其中键和变量名匹配)。"never"
确保在任何对象字面量中都不使用属性或方法简写。"consistent"
确保在对象字面量中使用所有简写或所有长格式。"consistent-as-needed"
确保在对象字面量中使用所有简写或所有长格式,但在可能的情况下确保所有简写。
您可以在配置中像这样设置选项
{
"object-shorthand": ["error", "always"]
}
此外,该规则接受一个可选的对象配置
"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 }
选项时,不正确代码示例
/*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
const foo = {
};
使用 "always", { "avoidQuotes": true }
选项时,正确代码示例
/*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
const foo = {
"bar-baz": function() {},
"qux": qux
};
ignoreConstructors
{
"object-shorthand": ["error", "always", { "ignoreConstructors": true }]
}
使用 "always", { "ignoreConstructors": true }
选项时,正确代码示例
/*eslint object-shorthand: ["error", "always", { "ignoreConstructors": true }]*/
const foo = {
ConstructorFunction: function() {}
};
methodsIgnorePattern
使用 "always", { "methodsIgnorePattern": "^bar$" }
选项时,正确代码示例
/*eslint object-shorthand: ["error", "always", { "methodsIgnorePattern": "^bar$" }]*/
const foo = {
bar: function() {}
};
avoidExplicitReturnArrows
{
"object-shorthand": ["error", "always", { "avoidExplicitReturnArrows": true }]
}
使用 "always", { "avoidExplicitReturnArrows": true }
选项时,不正确代码示例
/*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
const foo = {
,
};
使用 "always", { "avoidExplicitReturnArrows": true }
选项时,正确代码示例
/*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
const foo = {
foo(bar, baz) {
return bar + baz;
},
qux: foobar => foobar * 2
};
使用 "consistent"
选项时,不正确代码示例
/*eslint object-shorthand: [2, "consistent"]*/
const foo = ;
使用 "consistent"
选项时,正确代码示例
/*eslint object-shorthand: [2, "consistent"]*/
const foo = {
a: a,
b: "foo"
};
const bar = {
a,
b,
};
使用 "consistent-as-needed"
选项时,不正确代码示例,它与 "consistent"
非常相似
/*eslint object-shorthand: [2, "consistent-as-needed"]*/
const foo = ;
何时不使用
任何尚未处于 ES6 环境中的人都不希望应用此规则。其他人可能会发现简写语法的简洁性难以阅读,并且可能不希望通过此规则来鼓励它。
相关规则
版本
此规则在 ESLint v0.20.0 中引入。
延伸阅读
