fix(minifier): do not fold 0 && (module.exports = {}) for cjs-module-lexer (#4878)

This commit is contained in:
Boshen 2024-08-13 12:54:58 +00:00
parent 48a1c32ed3
commit 81fd6379e8
2 changed files with 14 additions and 0 deletions

View file

@ -592,7 +592,19 @@ impl<'a> FoldConstants<'a> {
if !matches!(op, LogicalOperator::And | LogicalOperator::Or) {
return None;
}
if let Some(boolean_value) = ctx.get_boolean_value(&logical_expr.left) {
// Bail `0 && (module.exports = {})` for `cjs-module-lexer`.
if !boolean_value {
if let Expression::AssignmentExpression(assign_expr) = &logical_expr.right {
if let Some(member_expr) = assign_expr.left.as_member_expression() {
if member_expr.is_specific_member_access("module", "exports") {
return None;
}
}
}
}
// (TRUE || x) => TRUE (also, (3 || x) => 3)
// (FALSE && x) => FALSE
if (boolean_value && op == LogicalOperator::Or)

View file

@ -29,6 +29,8 @@ fn cjs() {
}
});",
);
// Bail `cjs-module-lexer`.
test_same("0 && (module.exports = { version });");
}
#[test] // https://github.com/oxc-project/oxc/issues/4341