diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index c70276c53058..0c3c7016564c 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -147,6 +147,7 @@ use swc_ecma_transforms::{ pass::noop, resolver, }; +use swc_ecma_transforms_base::fixer::paren_remover; use swc_ecma_visit::{FoldWith, VisitMutWith, VisitWith}; pub use swc_error_reporters::handler::{try_with_handler, HandlerOpts}; pub use swc_node_comments::SwcComments; @@ -865,6 +866,8 @@ impl Compiler { let is_mangler_enabled = min_opts.mangle.is_some(); let module = self.run_transform(handler, false, || { + let module = module.fold_with(&mut paren_remover(Some(&comments))); + let module = module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false)); diff --git a/node-swc/__tests__/minify/issue_8437_test.mjs b/node-swc/__tests__/minify/issue_8437_test.mjs new file mode 100644 index 000000000000..98a7ad12df96 --- /dev/null +++ b/node-swc/__tests__/minify/issue_8437_test.mjs @@ -0,0 +1,61 @@ +import swc from "../../.."; + +it("should output same result", async () => { + const origin = ` +function toFixed(value, maxDecimals, roundingFunction, optionals) { + var splitValue = value.toString().split('.'), + minDecimals = maxDecimals - (optionals || 0), + + optionalsRegExp, + power, + output; + var boundedPrecisions; + // var unused = 'xxxx'; + + // Use the smallest precision value possible to avoid errors from floating point representation + if (splitValue.length === 2) { + boundedPrecisions = Math.min(Math.max(splitValue[1].length, minDecimals), maxDecimals); + } else { + boundedPrecisions = minDecimals; + } + + power = Math.pow(10, boundedPrecisions); + + // Multiply up by precision, round accurately, then divide and use native toFixed(): + output = (roundingFunction(value + 'e+' + boundedPrecisions) / power).toFixed(boundedPrecisions); + + if (optionals > maxDecimals - boundedPrecisions) { + optionalsRegExp = new RegExp('\\.?0{1,' + (optionals - (maxDecimals - boundedPrecisions)) + '}$'); + output = output.replace(optionalsRegExp, ''); + } + + return output; +} +toFixed(1.2345, 2, Math.round, 1); + ` + + async function minify() { + const { code } = await swc.minify(origin, { + compress: true, + mangle: false + }); + return code; + } + + async function transform() { + const { code } = await swc.transform(origin, { + jsc: { + minify: { + compress: true, + mangle: false + }, + }, + isModule: false, + minify: true + }); + return code; + } + + const [minifyResult, transformResult] = await Promise.all([minify(), transform()]); + expect(minifyResult).toEqual(transformResult); +}); \ No newline at end of file