Skip to content

Commit 84246ec

Browse files
authored
explicit-length-check: Fix || fallback false positives (#2889)
1 parent 95a288b commit 84246ec

4 files changed

Lines changed: 6 additions & 74 deletions

File tree

rules/explicit-length-check.js

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
getBooleanAncestor,
88
} from './utils/index.js';
99
import {fixSpaceAroundKeyword} from './fix/index.js';
10-
import {isLiteral, isMemberExpression, isNumericLiteral} from './ast/index.js';
10+
import {isLiteral, isMemberExpression} from './ast/index.js';
1111

1212
const TYPE_NON_ZERO = 'non-zero';
1313
const TYPE_ZERO = 'zero';
@@ -93,28 +93,6 @@ function getLengthCheckNode(node) {
9393
return {};
9494
}
9595

96-
function isNodeValueNumber(node, context) {
97-
if (isNumericLiteral(node)) {
98-
return true;
99-
}
100-
101-
const staticValue = getStaticValue(node, context.sourceCode.getScope(node));
102-
return staticValue && typeof staticValue.value === 'number';
103-
}
104-
105-
function isNodeValueString(node, context) {
106-
if (node.type === 'Literal' && typeof node.value === 'string') {
107-
return true;
108-
}
109-
110-
if (node.type === 'TemplateLiteral') {
111-
return true;
112-
}
113-
114-
const staticValue = getStaticValue(node, context.sourceCode.getScope(node));
115-
return staticValue && typeof staticValue.value === 'string';
116-
}
117-
11896
function create(context) {
11997
const options = context.options[0];
12098
const nonZeroStyle = nonZeroStyles.get(options['non-zero']);
@@ -192,13 +170,7 @@ function create(context) {
192170
if (isBooleanNode(ancestor)) {
193171
isZeroLengthCheck = isNegative;
194172
node = ancestor;
195-
} else if (
196-
isLogicalExpression(lengthNode.parent)
197-
&& !(
198-
lengthNode.parent.operator === '||'
199-
&& (isNodeValueNumber(lengthNode.parent.right, context) || isNodeValueString(lengthNode.parent.right, context))
200-
)
201-
) {
173+
} else if (isLogicalExpression(lengthNode.parent) && lengthNode.parent.operator === '&&') {
202174
isZeroLengthCheck = isNegative;
203175
node = lengthNode;
204176
autoFix = false;

test/explicit-length-check.js

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -108,40 +108,11 @@ test({
108108
'const x = foo.length || `bar`',
109109
'const A_STRING = "bar"; const x = foo.length || A_STRING',
110110
'const size = props.size || "mini"',
111+
'const x = foo.length || unknown',
112+
'something(options.length || 500)',
113+
'const itemCount = result.totalCount || result.length',
111114
],
112115
invalid: [
113-
suggestionCase({
114-
code: 'const x = foo.length || bar()',
115-
messageId: TYPE_NON_ZERO,
116-
output: 'const x = foo.length > 0 || bar()',
117-
desc: 'Replace `.length` with `.length > 0`.',
118-
}),
119-
suggestionCase({
120-
code: 'const x = foo.length || unknown',
121-
messageId: TYPE_NON_ZERO,
122-
output: 'const x = foo.length > 0 || unknown',
123-
desc: 'Replace `.length` with `.length > 0`.',
124-
}),
125-
suggestionCase({
126-
code: 'const NON_NUMBER = true; const x = foo.length || NON_NUMBER',
127-
messageId: TYPE_NON_ZERO,
128-
output: 'const NON_NUMBER = true; const x = foo.length > 0 || NON_NUMBER',
129-
desc: 'Replace `.length` with `.length > 0`.',
130-
}),
131-
suggestionCase({
132-
code: 'const x = foo.length || bar()',
133-
messageId: TYPE_NON_ZERO,
134-
output: 'const x = foo.length !== 0 || bar()',
135-
desc: 'Replace `.length` with `.length !== 0`.',
136-
options: [{'non-zero': 'not-equal'}],
137-
}),
138-
suggestionCase({
139-
code: 'const x = foo.length || bar()',
140-
messageId: TYPE_NON_ZERO,
141-
output: 'const x = foo.length > 0 || bar()',
142-
desc: 'Replace `.length` with `.length > 0`.',
143-
options: [{'non-zero': 'greater-than'}],
144-
}),
145116
suggestionCase({
146117
code: '() => foo.length && bar()',
147118
messageId: TYPE_NON_ZERO,

test/snapshots/explicit-length-check.js.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ Generated by [AVA](https://avajs.dev).
13001300
1 | bar(!foo.length || foo.length)␊
13011301
`
13021302

1303-
> Error 1/2
1303+
> Error 1/1
13041304
13051305
`␊
13061306
Message:␊
@@ -1311,17 +1311,6 @@ Generated by [AVA](https://avajs.dev).
13111311
1 | bar(foo.length === 0 || foo.length)␊
13121312
`
13131313

1314-
> Error 2/2
1315-
1316-
`␊
1317-
Message:␊
1318-
> 1 | bar(!foo.length || foo.length)␊
1319-
| ^^^^^^^^^^ Use \`.length > 0\` when checking length is not zero.␊
1320-
1321-
Suggestion 1/1: Replace \`.length\` with \`.length > 0\`.:␊
1322-
1 | bar(!foo.length || foo.length > 0)␊
1323-
`
1324-
13251314
## invalid(14): const bar = void !foo.length;
13261315

13271316
> Input
-54 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)