Skip to content

Commit 2fee8ad

Browse files
naomiajacobsnzakas
authored andcommitted
Fix: object-shorthand's consistent-as-needed option (issue #7214) (#7215)
* Fix: object-shorthand's consistent-as-needed option (issue #7214) * Revise isRedundant and add more specs * Trigger CLA agreement check again (empty commit) * Fix typo
1 parent c05a19c commit 2fee8ad

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

lib/rules/object-shorthand.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ const OPTIONS = {
1414
consistentAsNeeded: "consistent-as-needed"
1515
};
1616

17+
//------------------------------------------------------------------------------
18+
// Requirements
19+
//------------------------------------------------------------------------------
20+
const astUtils = require("../ast-utils");
21+
1722
//------------------------------------------------------------------------------
1823
// Rule Definition
1924
//------------------------------------------------------------------------------
@@ -149,15 +154,17 @@ module.exports = {
149154
* @returns {boolean} True if the key and value are named equally, false if not.
150155
* @private
151156
**/
152-
function isRedudant(property) {
153-
return (property.key && (
157+
function isRedundant(property) {
158+
const value = property.value;
154159

155-
// A function expression
156-
property.value && property.value.id && property.value.id.name === property.key.name ||
160+
if (value.type === "FunctionExpression") {
161+
return !value.id; // Only anonymous should be shorthand method.
162+
}
163+
if (value.type === "Identifier") {
164+
return astUtils.getStaticPropertyName(property) === value.name;
165+
}
157166

158-
// A property
159-
property.value && property.value.name === property.key.name
160-
));
167+
return false;
161168
}
162169

163170
/**
@@ -185,8 +192,8 @@ module.exports = {
185192
} else if (checkRedundancy) {
186193

187194
// If all properties of the object contain a method or value with a name matching it's key,
188-
// all the keys are redudant.
189-
const canAlwaysUseShorthand = properties.every(isRedudant);
195+
// all the keys are redundant.
196+
const canAlwaysUseShorthand = properties.every(isRedundant);
190197

191198
if (canAlwaysUseShorthand) {
192199
context.report(node, "Expected shorthand for all properties.");

tests/lib/rules/object-shorthand.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ ruleTester.run("object-shorthand", rule, {
103103

104104
// consistent-as-needed
105105
{ code: "var x = {a, b}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
106-
{ code: "var x = {a, b, get test(){return 1;}}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] }
106+
{ code: "var x = {a, b, get test(){return 1;}}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
107+
{ code: "var x = {0: 'foo'}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
108+
{ code: "var x = {'key': 'baz'}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
109+
{ code: "var x = {foo: 'foo'}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
110+
{ code: "var x = {[foo]: foo}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
111+
{ code: "var x = {foo: function foo() {}}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
112+
{ code: "var x = {[foo]: 'foo'}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] }
107113
],
108114
invalid: [
109115
{ code: "var x = {x: x}", output: "var x = {x}", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected property shorthand.", type: "Property" }] },
@@ -158,6 +164,7 @@ ruleTester.run("object-shorthand", rule, {
158164

159165
// consistent-as-needed
160166
{ code: "var x = {a: a, b: b}", parserOptions: { ecmaVersion: 6}, errors: [{ message: "Expected shorthand for all properties.", type: "" }], options: ["consistent-as-needed"] },
161-
{ code: "var x = {a, z: function z(){}}", parserOptions: { ecmaVersion: 6}, errors: [{ message: "Unexpected mix of shorthand and non-shorthand properties.", type: "" }], options: ["consistent-as-needed"] }
167+
{ code: "var x = {a, z: function z(){}}", parserOptions: { ecmaVersion: 6}, errors: [{ message: "Unexpected mix of shorthand and non-shorthand properties.", type: "" }], options: ["consistent-as-needed"] },
168+
{ code: "var x = {foo: function() {}}", parserOptions: { ecmaVersion: 6}, errors: [{ message: "Expected shorthand for all properties.", type: "" }], options: ["consistent-as-needed"] },
162169
]
163170
});

0 commit comments

Comments
 (0)