Skip to content

Commit ad1fd85

Browse files
christophehurpeausindresorhus
authored andcommitted
Add conditional expressions in explicit-length-check rule (#179)
1 parent b546647 commit ad1fd85

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

rules/explicit-length-check.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ const create = context => {
136136
return {
137137
IfStatement: node => {
138138
checkExpression(context, node.test);
139+
},
140+
ConditionalExpression: node => {
141+
checkExpression(context, node.test);
139142
}
140143
};
141144
};

test/explicit-length-check.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ ruleTester.run('explicit-length-check', rule, {
5353
testCase('if (array.length <= 1) {}'),
5454
testCase('if (array.length > 1) {}'),
5555
testCase('if (array.length < 2) {}'),
56+
testCase('const foo = [].length === 0 ? null : undefined'),
5657
testCase('array.length', 'not-equal'),
5758
testCase('array.length > 0', 'not-equal'),
5859
testCase('array.length >= 1', 'not-equal'),
@@ -182,6 +183,11 @@ ruleTester.run('explicit-length-check', rule, {
182183
'not-equal',
183184
[errorMessages.zeroEqual, errorMessages.nonZeroEqual],
184185
'if (array.length === 0 || array.length !== 0) {}'
185-
)
186+
),
187+
testCase(
188+
'const foo = [].length ? null : undefined',
189+
undefined,
190+
[errorMessages.compareToValue]
191+
),
186192
]
187193
});

0 commit comments

Comments
 (0)