diff --git a/crates/swc_ecma_minifier/src/compress/pure/evaluate.rs b/crates/swc_ecma_minifier/src/compress/pure/evaluate.rs index c568e991cb6..a1590d5ed43 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/evaluate.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/evaluate.rs @@ -282,7 +282,12 @@ impl Pure<'_> { } if &*method.sym == "toFixed" { - if let Some(precision) = eval_as_number(&self.expr_ctx, &args[0].expr) { + if let Some(precision) = args + .first() + // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.prototype.tofixed + // 3. Assert: If fractionDigits is undefined, then f is 0. + .map_or(Some(0f64), |arg| eval_as_number(&self.expr_ctx, &arg.expr)) + { let precision = precision.floor() as usize; let value = num_to_fixed(num.value, precision + 1); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/5864/analysis-snapshot.rust-debug b/crates/swc_ecma_minifier/tests/fixture/issues/5864/analysis-snapshot.rust-debug new file mode 100644 index 00000000000..165d75bda9f --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/5864/analysis-snapshot.rust-debug @@ -0,0 +1,42 @@ +TestSnapshot { + vars: [ + ( + ( + Atom('foo' type=inline), + #1, + ), + VarUsageInfo { + inline_prevented: false, + ref_count: 1, + cond_init: false, + declared: false, + declared_count: 0, + declared_as_fn_param: false, + declared_as_fn_expr: false, + assign_count: 0, + mutation_by_call_count: 0, + usage_count: 0, + reassigned_with_assignment: false, + reassigned_with_var_decl: false, + mutated: true, + has_property_access: false, + has_property_mutation: false, + accessed_props: {}, + exported: false, + used_above_decl: true, + is_fn_local: true, + used_by_nested_fn: false, + executed_multiple_time: false, + used_in_cond: false, + var_kind: None, + var_initialized: true, + declared_as_catch_param: false, + no_side_effect_for_member_access: false, + used_as_callee: false, + used_as_arg: false, + pure_fn: false, + infects: [], + }, + ), + ], +} diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/5864/config.json b/crates/swc_ecma_minifier/tests/fixture/issues/5864/config.json new file mode 100644 index 00000000000..b38da13846f --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/5864/config.json @@ -0,0 +1,46 @@ +{ + "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 +} \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/5864/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/5864/input.js new file mode 100644 index 00000000000..493b69b5741 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/5864/input.js @@ -0,0 +1,3 @@ +foo = { + v: (0).toFixed(), +}; diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/5864/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/5864/output.js new file mode 100644 index 00000000000..0475b256851 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/5864/output.js @@ -0,0 +1,3 @@ +foo = { + v: "0" +}; diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/5864/output.mangleOnly.js b/crates/swc_ecma_minifier/tests/fixture/issues/5864/output.mangleOnly.js new file mode 100644 index 00000000000..19740caacc5 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/5864/output.mangleOnly.js @@ -0,0 +1,3 @@ +foo = { + v: (0).toFixed() +};