Skip to content

Commit

Permalink
fix(es/minifier): Fix operand handling of ** (#8933)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8924
  • Loading branch information
kdy1 authored May 8, 2024
1 parent bd6873e commit c9d72cd
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 12 deletions.
20 changes: 14 additions & 6 deletions crates/swc_ecma_minifier/src/compress/optimize/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,20 @@ impl Optimizer<'_> {
self.changed = true;
report_change!("evaluate: Evaluated `{:?} ** {:?}`", l, r);

let value = l.powf(r);
*e = Expr::Lit(Lit::Num(Number {
span: bin.span,
value,
raw: None,
}));
if l.is_nan() || r.is_nan() {
*e = Expr::Ident(Ident::new(
"NaN".into(),
bin.span.with_ctxt(
SyntaxContext::empty().apply_mark(self.marks.unresolved_mark),
),
));
} else {
*e = Expr::Lit(Lit::Num(Number {
span: bin.span,
value: l.powf(r),
raw: None,
}));
};
}
}
}
Expand Down
26 changes: 20 additions & 6 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1552,21 +1552,35 @@ impl Optimizer<'_> {
}
}

if match &*b {
match &*b {
Expr::Arrow(..)
| Expr::Fn(..)
| Expr::Class(..)
| Expr::Lit(..)
| Expr::Await(..)
| Expr::Yield(..)
| Expr::Tpl(..)
| Expr::TaggedTpl(..) => true,
| Expr::TaggedTpl(..) => return Ok(false),

Expr::Assign(AssignExpr {
op: op!("**="),
right,
..
})
| Expr::Bin(BinExpr {
op: op!("**"),
right,
..
}) => {
if is_pure_undefined(&self.expr_ctx, right) {
return Ok(false);
}
}

Expr::Unary(UnaryExpr {
op: op!("delete"), ..
}) => true,
_ => false,
} {
return Ok(false);
}) => return Ok(false),
_ => {}
}

match a {
Expand Down
47 changes: 47 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8924/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"defaults": true,
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": true,
"pristine_globals": true
}
5 changes: 5 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8924/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
const k = (() => {
let x = 1; x **= undefined;
return x;
})();
2 changes: 2 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8924/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
const k = NaN;

0 comments on commit c9d72cd

Please sign in to comment.