Skip to content

Commit

Permalink
Fix: object-shorthand's consistent-as-needed option (issue #7214) (#7215
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
naomiajacobs authored and nzakas committed Sep 30, 2016
1 parent c05a19c commit 2fee8ad
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
25 changes: 16 additions & 9 deletions lib/rules/object-shorthand.js
Expand Up @@ -14,6 +14,11 @@ const OPTIONS = {
consistentAsNeeded: "consistent-as-needed"
};

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -149,15 +154,17 @@ module.exports = {
* @returns {boolean} True if the key and value are named equally, false if not.
* @private
**/
function isRedudant(property) {
return (property.key && (
function isRedundant(property) {
const value = property.value;

// A function expression
property.value && property.value.id && property.value.id.name === property.key.name ||
if (value.type === "FunctionExpression") {
return !value.id; // Only anonymous should be shorthand method.
}
if (value.type === "Identifier") {
return astUtils.getStaticPropertyName(property) === value.name;
}

// A property
property.value && property.value.name === property.key.name
));
return false;
}

/**
Expand Down Expand Up @@ -185,8 +192,8 @@ module.exports = {
} else if (checkRedundancy) {

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

if (canAlwaysUseShorthand) {
context.report(node, "Expected shorthand for all properties.");
Expand Down
11 changes: 9 additions & 2 deletions tests/lib/rules/object-shorthand.js
Expand Up @@ -103,7 +103,13 @@ ruleTester.run("object-shorthand", rule, {

// consistent-as-needed
{ code: "var x = {a, b}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
{ code: "var x = {a, b, get test(){return 1;}}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] }
{ code: "var x = {a, b, get test(){return 1;}}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
{ code: "var x = {0: 'foo'}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
{ code: "var x = {'key': 'baz'}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
{ code: "var x = {foo: 'foo'}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
{ code: "var x = {[foo]: foo}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
{ code: "var x = {foo: function foo() {}}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] },
{ code: "var x = {[foo]: 'foo'}", parserOptions: { ecmaVersion: 6}, options: ["consistent-as-needed"] }
],
invalid: [
{ code: "var x = {x: x}", output: "var x = {x}", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected property shorthand.", type: "Property" }] },
Expand Down Expand Up @@ -158,6 +164,7 @@ ruleTester.run("object-shorthand", rule, {

// consistent-as-needed
{ code: "var x = {a: a, b: b}", parserOptions: { ecmaVersion: 6}, errors: [{ message: "Expected shorthand for all properties.", type: "" }], options: ["consistent-as-needed"] },
{ 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"] }
{ 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"] },
{ code: "var x = {foo: function() {}}", parserOptions: { ecmaVersion: 6}, errors: [{ message: "Expected shorthand for all properties.", type: "" }], options: ["consistent-as-needed"] },
]
});

0 comments on commit 2fee8ad

Please sign in to comment.