Skip to content

Commit 06feec9

Browse files
committed
fix regression of (condition || true) case
1 parent c674007 commit 06feec9

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

packages/commonjs/src/ast-utils.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
export { default as isReference } from 'is-reference';
22

3+
function triStateAnd(a, b) {
4+
if (a === false) return false;
5+
if (b === false) return false;
6+
if (a === true && b === true) return true;
7+
return null;
8+
}
9+
10+
function triStateOr(a, b) {
11+
if (a === true) return true;
12+
if (b === true) return true;
13+
if (a === false && b === false) return false;
14+
return null;
15+
}
16+
317
const operators = {
418
'==': (x) => equals(x.left, x.right, false),
519

@@ -11,20 +25,9 @@ const operators = {
1125

1226
'!': (x) => isFalsy(x.argument),
1327

14-
'&&': (x) => isTruthy(x.left) && isTruthy(x.right),
15-
16-
'||': (x) => {
17-
const leftTruthy = isTruthy(x.left);
18-
const rightTruthy = isTruthy(x.right);
19-
// If left is definitely truthy, the whole expression is truthy
20-
if (leftTruthy === true) return true;
21-
// If both are definitely falsy, the whole expression is falsy
22-
if (leftTruthy === false && rightTruthy === false) return false;
23-
// If left is falsy but right is truthy, the whole expression is truthy
24-
if (leftTruthy === false && rightTruthy === true) return true;
25-
// Otherwise it's conditional
26-
return null;
27-
}
28+
'&&': (x) => triStateAnd(isTruthy(x.left), isTruthy(x.right)),
29+
30+
'||': (x) => triStateOr(isTruthy(x.left), isTruthy(x.right))
2831
};
2932

3033
function not(value) {
@@ -137,3 +140,4 @@ export function hasDefineEsmProperty(node) {
137140
return false;
138141
});
139142
}
143+

packages/commonjs/test/fixtures/form/conditional-exports-or-true/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function requireInput () {
1414
if (condition || true) {
1515
input.handler = prodHandler;
1616
} else {
17-
input.handler = defaultHandler;
17+
exports.handler = defaultHandler;
1818
}
1919
return input;
2020
}

0 commit comments

Comments
 (0)