Skip to content

Commit

Permalink
Make matches-shorthand only work on multiple properties (so it doesn…
Browse files Browse the repository at this point in the history
…'t collide with matches-prop-shorthand)
  • Loading branch information
ganimomer committed Jan 26, 2016
1 parent c28ba30 commit 30fde0e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
8 changes: 7 additions & 1 deletion lib/rules/matches-shorthand.js
Expand Up @@ -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;
Expand Down
36 changes: 13 additions & 23 deletions tests/lib/rules/matches-shorthand.js
Expand Up @@ -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'],
Expand Down

0 comments on commit 30fde0e

Please sign in to comment.