diff --git a/lib/rules/object-shorthand.js b/lib/rules/object-shorthand.js index 912c606b118..493ee895903 100644 --- a/lib/rules/object-shorthand.js +++ b/lib/rules/object-shorthand.js @@ -14,6 +14,11 @@ const OPTIONS = { consistentAsNeeded: "consistent-as-needed" }; +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ +const astUtils = require("../ast-utils"); + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -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; } /** @@ -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."); diff --git a/tests/lib/rules/object-shorthand.js b/tests/lib/rules/object-shorthand.js index 0f9c7070178..26b8c8c97c6 100644 --- a/tests/lib/rules/object-shorthand.js +++ b/tests/lib/rules/object-shorthand.js @@ -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" }] }, @@ -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"] }, ] });