Skip to content

Commit

Permalink
Add conditional expressions in explicit-length-check rule (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehurpeau authored and sindresorhus committed Jul 28, 2018
1 parent b546647 commit ad1fd85
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions rules/explicit-length-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ const create = context => {
return {
IfStatement: node => {
checkExpression(context, node.test);
},
ConditionalExpression: node => {
checkExpression(context, node.test);
}
};
};
Expand Down
8 changes: 7 additions & 1 deletion test/explicit-length-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ ruleTester.run('explicit-length-check', rule, {
testCase('if (array.length <= 1) {}'),
testCase('if (array.length > 1) {}'),
testCase('if (array.length < 2) {}'),
testCase('const foo = [].length === 0 ? null : undefined'),
testCase('array.length', 'not-equal'),
testCase('array.length > 0', 'not-equal'),
testCase('array.length >= 1', 'not-equal'),
Expand Down Expand Up @@ -182,6 +183,11 @@ ruleTester.run('explicit-length-check', rule, {
'not-equal',
[errorMessages.zeroEqual, errorMessages.nonZeroEqual],
'if (array.length === 0 || array.length !== 0) {}'
)
),
testCase(
'const foo = [].length ? null : undefined',
undefined,
[errorMessages.compareToValue]
),
]
});

0 comments on commit ad1fd85

Please sign in to comment.