Skip to content

Commit

Permalink
explicit-length-check: Ignore .length || number (#1977)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 18, 2023
1 parent 2907805 commit b9b8794
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
19 changes: 17 additions & 2 deletions rules/explicit-length-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {checkVueTemplate} = require('./utils/rule.js');
const isLogicalExpression = require('./utils/is-logical-expression.js');
const {isBooleanNode, getBooleanAncestor} = require('./utils/boolean.js');
const {fixSpaceAroundKeyword} = require('./fix/index.js');
const {isLiteral, isMemberExpression} = require('./ast/index.js');
const {isLiteral, isMemberExpression, isNumberLiteral} = require('./ast/index.js');

const TYPE_NON_ZERO = 'non-zero';
const TYPE_ZERO = 'zero';
Expand Down Expand Up @@ -90,6 +90,15 @@ function getLengthCheckNode(node) {
return {};
}

function isNodeValueNumber(node, context) {
if (isNumberLiteral(node)) {
return true;
}

const staticValue = getStaticValue(node, context.sourceCode.getScope(node));
return staticValue && typeof staticValue.value === 'number';
}

function create(context) {
const options = {
'non-zero': 'greater-than',
Expand Down Expand Up @@ -171,7 +180,13 @@ function create(context) {
if (isBooleanNode(ancestor)) {
isZeroLengthCheck = isNegative;
node = ancestor;
} else if (isLogicalExpression(lengthNode.parent)) {
} else if (
isLogicalExpression(lengthNode.parent)
&& !(
lengthNode.parent.operator === '||'
&& isNodeValueNumber(lengthNode.parent.right, context)
)
) {
isZeroLengthCheck = isNegative;
node = lengthNode;
autoFix = false;
Expand Down
13 changes: 13 additions & 0 deletions test/explicit-length-check.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,26 @@ test({
'const foo = { length: 1.5 }; if (foo.length) {}', // Array lengths must be integers
'const foo = { length: NaN }; if (foo.length) {}', // Array lengths cannot be NaN
'const foo = { length: Infinity }; if (foo.length) {}', // Array lengths cannot be Infinity
// Logical OR
'const x = foo.length || 2',
'const A_NUMBER = 2; const x = foo.length || A_NUMBER',
],
invalid: [
suggestionCase({
code: 'const x = foo.length || bar()',
output: 'const x = foo.length > 0 || bar()',
desc: 'Replace `.length` with `.length > 0`.',
}),
suggestionCase({
code: 'const x = foo.length || unknown',
output: 'const x = foo.length > 0 || unknown',
desc: 'Replace `.length` with `.length > 0`.',
}),
suggestionCase({
code: 'const NON_NUMBER = "2"; const x = foo.length || NON_NUMBER',
output: 'const NON_NUMBER = "2"; const x = foo.length > 0 || NON_NUMBER',
desc: 'Replace `.length` with `.length > 0`.',
}),
suggestionCase({
code: 'const x = foo.length || bar()',
output: 'const x = foo.length !== 0 || bar()',
Expand Down

0 comments on commit b9b8794

Please sign in to comment.