Skip to content

Commit

Permalink
Don't consider ?? operator LogicExpression as a boolean value node (
Browse files Browse the repository at this point in the history
#986)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
fisker and sindresorhus committed Jan 2, 2021
1 parent ff1cd2e commit dc7f79b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion rules/explicit-length-check.js
Expand Up @@ -2,6 +2,7 @@
const {isParenthesized} = require('eslint-utils');
const getDocumentationUrl = require('./utils/get-documentation-url');
const isLiteralValue = require('./utils/is-literal-value');
const isLogicalExpression = require('./utils/is-logical-expression');
const {isBooleanNode, getBooleanAncestor} = require('./utils/boolean');

const TYPE_NON_ZERO = 'non-zero';
Expand Down Expand Up @@ -165,7 +166,7 @@ function create(context) {
if (isBooleanNode(ancestor)) {
isZeroLengthCheck = isNegative;
node = ancestor;
} else if (lengthNode.parent.type === 'LogicalExpression') {
} else if (isLogicalExpression(lengthNode.parent)) {
isZeroLengthCheck = isNegative;
node = lengthNode;
autoFix = false;
Expand Down
4 changes: 3 additions & 1 deletion rules/utils/boolean.js
@@ -1,5 +1,7 @@
'use strict';

const isLogicalExpression = require('./is-logical-expression');

const isLogicNot = node =>
node &&
node.type === 'UnaryExpression' &&
Expand Down Expand Up @@ -48,7 +50,7 @@ function isBooleanNode(node) {
return true;
}

if (parent.type === 'LogicalExpression') {
if (isLogicalExpression(parent)) {
return isBooleanNode(parent);
}

Expand Down
17 changes: 17 additions & 0 deletions rules/utils/is-logical-expression.js
@@ -0,0 +1,17 @@
'use strict';

/**
Check if the given node is a true logical expression or not.
The three binary expressions logical-or (`||`), logical-and (`&&`), and coalesce (`??`) are known as `ShortCircuitExpression`, but ESTree represents these by the `LogicalExpression` node type. This function rejects coalesce expressions of `LogicalExpression` node type.
@param {Node} node - The node to check.
@returns {boolean} `true` if the node is `&&` or `||`.
@see https://tc39.es/ecma262/#prod-ShortCircuitExpression
*/
const isLogicalExpression = node =>
node &&
node.type === 'LogicalExpression' &&
(node.operator === '&&' || node.operator === '||');

module.exports = isLogicalExpression;
2 changes: 2 additions & 0 deletions test/explicit-length-check.js
Expand Up @@ -58,6 +58,8 @@ test({
'const x = Boolean(foo.length, foo.length)',
'const x = new Boolean(foo.length)',
'const x = NotBoolean(foo.length)',
'const length = foo.length ?? 0',
'if (foo.length ?? bar) {}',

// Checking 'non-zero'
'if (foo.length > 0) {}',
Expand Down
1 change: 1 addition & 0 deletions test/prefer-array-some.js
Expand Up @@ -25,6 +25,7 @@ test({
// Not `boolean`
'const bar = foo.find(fn)',
'const bar = foo.find(fn) || baz',
'if (foo.find(fn) ?? bar) {}',

// Not matched `CallExpression`
...[
Expand Down

0 comments on commit dc7f79b

Please sign in to comment.