From fd09a954436d382e8e28e2f4bf51013e39ab74a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 15 Oct 2022 21:24:49 +0900 Subject: [PATCH 1/2] Fix NaN --- .../src/simplify/expr/mod.rs | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs b/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs index d77fe755fba6..bdddc47e335c 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs @@ -663,13 +663,22 @@ impl SimplifyExpr { { if *left_op == *op { if let Known(value) = self.perform_arithmetic_op(*op, left_rhs, right) { + let value_expr = if !value.is_nan() { + Expr::Lit(Lit::Num(Number { + value, + span: *span, + raw: None, + })) + } else { + Expr::Ident(Ident::new( + js_word!("NaN"), + span.with_ctxt(self.expr_ctx.unresolved_ctxt), + )) + }; + self.changed = true; *left = left_lhs.take(); - *right = Box::new(Expr::Lit(Lit::Num(Number { - value, - span: *span, - raw: None, - }))) + *right = Box::new(value_expr); } } } @@ -1504,9 +1513,7 @@ impl VisitMut for SimplifyExpr { } } - Expr::Lit(..) | Expr::Ident(..) - if self.in_callee && !expr.may_have_side_effects(&self.expr_ctx) => - { + Expr::Lit(..) | Expr::Ident(..) if self.in_callee => { if exprs.is_empty() { self.changed = true; @@ -1586,18 +1593,6 @@ impl VisitMut for SimplifyExpr { self.is_modifying = old; } - fn visit_mut_tagged_tpl(&mut self, n: &mut TaggedTpl) { - let old = self.in_callee; - self.in_callee = true; - - n.tag.visit_mut_with(self); - - self.in_callee = false; - n.tpl.visit_mut_with(self); - - self.in_callee = old; - } - fn visit_mut_with_stmt(&mut self, n: &mut WithStmt) { n.obj.visit_mut_with(self); } From 766916900f0032633be541b3f1588601858d715f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 15 Oct 2022 21:26:16 +0900 Subject: [PATCH 2/2] fixup --- .../src/simplify/expr/mod.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs b/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs index bdddc47e335c..eae73ffe8a3b 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs @@ -1513,7 +1513,9 @@ impl VisitMut for SimplifyExpr { } } - Expr::Lit(..) | Expr::Ident(..) if self.in_callee => { + Expr::Lit(..) | Expr::Ident(..) + if self.in_callee && !expr.may_have_side_effects(&self.expr_ctx) => + { if exprs.is_empty() { self.changed = true; @@ -1593,6 +1595,18 @@ impl VisitMut for SimplifyExpr { self.is_modifying = old; } + fn visit_mut_tagged_tpl(&mut self, n: &mut TaggedTpl) { + let old = self.in_callee; + self.in_callee = true; + + n.tag.visit_mut_with(self); + + self.in_callee = false; + n.tpl.visit_mut_with(self); + + self.in_callee = old; + } + fn visit_mut_with_stmt(&mut self, n: &mut WithStmt) { n.obj.visit_mut_with(self); }