Skip to content

Commit b9b8794

Browse files
authored
explicit-length-check: Ignore .length || number (#1977)
1 parent 2907805 commit b9b8794

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

rules/explicit-length-check.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {checkVueTemplate} = require('./utils/rule.js');
44
const isLogicalExpression = require('./utils/is-logical-expression.js');
55
const {isBooleanNode, getBooleanAncestor} = require('./utils/boolean.js');
66
const {fixSpaceAroundKeyword} = require('./fix/index.js');
7-
const {isLiteral, isMemberExpression} = require('./ast/index.js');
7+
const {isLiteral, isMemberExpression, isNumberLiteral} = require('./ast/index.js');
88

99
const TYPE_NON_ZERO = 'non-zero';
1010
const TYPE_ZERO = 'zero';
@@ -90,6 +90,15 @@ function getLengthCheckNode(node) {
9090
return {};
9191
}
9292

93+
function isNodeValueNumber(node, context) {
94+
if (isNumberLiteral(node)) {
95+
return true;
96+
}
97+
98+
const staticValue = getStaticValue(node, context.sourceCode.getScope(node));
99+
return staticValue && typeof staticValue.value === 'number';
100+
}
101+
93102
function create(context) {
94103
const options = {
95104
'non-zero': 'greater-than',
@@ -171,7 +180,13 @@ function create(context) {
171180
if (isBooleanNode(ancestor)) {
172181
isZeroLengthCheck = isNegative;
173182
node = ancestor;
174-
} else if (isLogicalExpression(lengthNode.parent)) {
183+
} else if (
184+
isLogicalExpression(lengthNode.parent)
185+
&& !(
186+
lengthNode.parent.operator === '||'
187+
&& isNodeValueNumber(lengthNode.parent.right, context)
188+
)
189+
) {
175190
isZeroLengthCheck = isNegative;
176191
node = lengthNode;
177192
autoFix = false;

test/explicit-length-check.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,26 @@ test({
103103
'const foo = { length: 1.5 }; if (foo.length) {}', // Array lengths must be integers
104104
'const foo = { length: NaN }; if (foo.length) {}', // Array lengths cannot be NaN
105105
'const foo = { length: Infinity }; if (foo.length) {}', // Array lengths cannot be Infinity
106+
// Logical OR
107+
'const x = foo.length || 2',
108+
'const A_NUMBER = 2; const x = foo.length || A_NUMBER',
106109
],
107110
invalid: [
108111
suggestionCase({
109112
code: 'const x = foo.length || bar()',
110113
output: 'const x = foo.length > 0 || bar()',
111114
desc: 'Replace `.length` with `.length > 0`.',
112115
}),
116+
suggestionCase({
117+
code: 'const x = foo.length || unknown',
118+
output: 'const x = foo.length > 0 || unknown',
119+
desc: 'Replace `.length` with `.length > 0`.',
120+
}),
121+
suggestionCase({
122+
code: 'const NON_NUMBER = "2"; const x = foo.length || NON_NUMBER',
123+
output: 'const NON_NUMBER = "2"; const x = foo.length > 0 || NON_NUMBER',
124+
desc: 'Replace `.length` with `.length > 0`.',
125+
}),
113126
suggestionCase({
114127
code: 'const x = foo.length || bar()',
115128
output: 'const x = foo.length !== 0 || bar()',

0 commit comments

Comments
 (0)