From 30fde0e779eea7b14f60c6cd9ca8b211acd7dd7f Mon Sep 17 00:00:00 2001 From: Omer Ganim Date: Tue, 26 Jan 2016 10:48:25 +0200 Subject: [PATCH] Make matches-shorthand only work on multiple properties (so it doesn't collide with matches-prop-shorthand) --- lib/rules/matches-shorthand.js | 8 ++++++- tests/lib/rules/matches-shorthand.js | 36 ++++++++++------------------ 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/lib/rules/matches-shorthand.js b/lib/rules/matches-shorthand.js index c496718..baf75ac 100644 --- a/lib/rules/matches-shorthand.js +++ b/lib/rules/matches-shorthand.js @@ -18,9 +18,15 @@ module.exports = function (context) { return exp && exp.type === 'LogicalExpression' && exp.operator === '&&'; } + function canBeObjectLiteralWithShorthandProperty(node, paramName) { + return context.ecmaFeatures.objectLiteralShorthandProperties && astUtil.isEqEqEq(node) && + (astUtil.isMemberExpOf(node.left, paramName, 1) && node.left.property.type === 'Identifier' && node.right.type === 'Identifier' && node.left.property.name === node.right.name || + astUtil.isMemberExpOf(node.right, paramName, 1) && node.right.property.type === 'Identifier' && node.left.type === 'Identifier' && node.right.property.name === node.left.name); + } + function isConjunctionOfEqEqEqToMemberOf(exp, paramName, maxPropertyPathLength) { var allowComputed = context.options[2] && context.ecmaFeatures.objectLiteralComputedProperties; - if (exp) { + if (isConjunction(exp) || canBeObjectLiteralWithShorthandProperty(exp, paramName)) { var checkStack = [exp]; var curr; var allParamMemberEq = true; diff --git a/tests/lib/rules/matches-shorthand.js b/tests/lib/rules/matches-shorthand.js index eafbe5f..2921a93 100644 --- a/tests/lib/rules/matches-shorthand.js +++ b/tests/lib/rules/matches-shorthand.js @@ -23,46 +23,36 @@ ruleTester.run('matches-shorthand', rule, { 'var r = _.findIndex(this.packages, {name: name});', 'var isPublic = _.map([], function (i) { return i.id + "?"; });', 'lang.fonts = _.filter(lang.fonts, function (font) { return font.permissions !== "legacy"});', - 'var isPublic = _.findLastIndex([], function (i) { return i.id == 3; });', + 'var isPublic = _.findLastIndex([], function (i) { return i.id == 3 && f(i); });', {code: 'var isPublic = _.find([], function(i) { return i.id === 3});', options: ['never']} ], invalid: [{ - code: 'var isPublic = _.find([], function (i) { return i.id === 3; });', - errors: [{message: messages.always, column: 27}] + code: 'var isPublic = _.find([], function (i) { return i.id === id; });', + errors: [{message: messages.always, column: 27}], + ecmaFeatures: {objectLiteralShorthandProperties: true} }, { - code: 'var isPublic = _.detect([], function (i) { return i.id === 3 && i.a === "string" && {a: 10} === i.b;});', - errors: [{message: messages.always, column: 29}] + code: 'var isPublic = _.find([], function (i) { return id === i.id; });', + errors: [{message: messages.always, column: 27}], + ecmaFeatures: {objectLiteralShorthandProperties: true} }, { - code: 'var isPublic = _.filter(arr, i => i.id === 3)', - ecmaFeatures: {arrowFunctions: true}, - errors: [{message: messages.always, column: 30}] + code: 'var isPublic = _.find([], function (i) { return i.id === 3 && i.a === "string" && {a: 10} === i.b;});', + errors: [{message: messages.always, column: 27}] }, { - code: 'var isPublic = _.select(arr, i => i.id === 3 && i.a === "string" && {a: 10} === i.b)', + code: 'var isPublic = _.filter(arr, i => i.id === 3 && i.name === name)', ecmaFeatures: {arrowFunctions: true}, errors: [{message: messages.always, column: 30}] }, { - code: 'var isPublic = _.findIndex(arr, (i) => {return i.id === 3})', - ecmaFeatures: {arrowFunctions: true}, + code: 'var isPublic = _.findIndex(arr, (i) => {return i.id === id})', + ecmaFeatures: {arrowFunctions: true, objectLiteralShorthandProperties: true}, errors: [{message: messages.always, column: 33}] }, { code: 'var isPublic = _.some(arr, (i) => {return i.id === 3 && i.a === "string" && {a: 10} === i.b})', ecmaFeatures: {arrowFunctions: true}, errors: [{message: messages.always, column: 28}] }, { - code: 'var isPublic = _.every(arr, (i) => {return i.id === b.id})', - ecmaFeatures: {arrowFunctions: true}, - errors: [{message: messages.always, column: 29}] - }, { - code: 'var isPublic = _.find([], i => i[0] === 3);', - ecmaFeatures: {arrowFunctions: true}, - errors: [{message: messages.always, column: 27}] - }, { - code: '_.findLastIndex(arr, function(i) { return i[b].c === compId; });', + code: '_.findLastIndex(arr, function(i) { return i[b].c === compId && i[b].d === x});', options: ['always', 3, true], errors: [{message: messages.always, column: 22}] - }, { - code: '_.findLastIndex(arr, function(i) { return i.b.c === compId; });', - errors: [{message: messages.always, column: 22}] }, { code: '_.findLastIndex(arr, {b: {c: compId}});', options: ['never'],