Skip to content

Commit

Permalink
fix(es/minifier): Avoid generating reserved mangling names (#9710)
Browse files Browse the repository at this point in the history
**Description:**

Copy user defined `reserved` to `preserved_symbols`, so the generated name will be skipped by
https://github.com/swc-project/swc/blob/56e83e2392c8ae89ce3854b36c64fed5e90f14c4/crates/swc_ecma_transforms_base/src/rename/analyzer/scope.rs#L296


**Related issue:**

 - Closes #9650
  • Loading branch information
CPunisher authored Nov 6, 2024
1 parent 499c803 commit b49317a
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-otters-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
swc_ecma_transforms_base: major
---

fix(es/minifier): Avoid generating reserved mangling names
4 changes: 2 additions & 2 deletions crates/swc_ecma_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pub fn optimize(
let _timer = timer!("mangle names");
// TODO: base54.reset();

let preserved = idents_to_preserve(mangle.clone(), marks, &n);
let preserved = idents_to_preserve(mangle, marks, &n);

let chars = CharFreq::compute(
&n,
Expand All @@ -241,7 +241,7 @@ pub fn optimize(

mangle_names(
&mut n,
mangle.clone(),
mangle,
preserved,
chars,
extra.top_level_mark,
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_minifier/src/pass/mangle_names/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod private_name;

pub(crate) fn mangle_names(
program: &mut Program,
options: MangleOptions,
options: &MangleOptions,
preserved: FxHashSet<Id>,
chars: Base54Chars,
top_level_mark: Mark,
Expand Down Expand Up @@ -48,6 +48,7 @@ pub(crate) fn mangle_names(
keep_class_names: options.keep_class_names,
top_level_mark,
ignore_eval: options.eval,
preserved_symbols: options.reserved.iter().cloned().collect(),
..Default::default()
},
ManglingRenamer {
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_minifier/src/pass/mangle_names/preserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
use crate::option::MangleOptions;

/// Returns `(preserved, unresolved)`
pub(crate) fn idents_to_preserve<N>(options: MangleOptions, marks: Marks, n: &N) -> FxHashSet<Id>
pub(crate) fn idents_to_preserve<N>(options: &MangleOptions, marks: Marks, n: &N) -> FxHashSet<Id>
where
N: for<'a> VisitWith<Preserver<'a>>,
{
let mut v = Preserver {
options: &options,
options,
preserved: Default::default(),
should_preserve: false,
in_top_level: false,
Expand Down
95 changes: 95 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9650/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
export function logVariables(
var1,
var2,
var3,
var4,
var5,
var6,
var7,
var8,
var9,
var10,
var11,
var12,
var13,
var14,
var15,
var16,
var17,
var18,
var19,
var20,
var21,
var22,
var23,
var24,
var25,
var26,
var27,
var28,
var29,
var30,
var101,
var201,
var301,
var401,
var501,
var601,
var701,
var801,
var901,
var1001,
var1101,
var1201,
var1301,
var1401,
var1501,
var1601,
var1701,
var1801,
var1901,
var2001,
var2101,
var2201,
var2301,
var2401,
var2501,
var2601,
var2701,
var2801,
var2901,
var3001
) {
console.log(
var1,
var2,
var3,
var4,
var5,
var6,
var7,
var8,
var9,
var10,
var11,
var12,
var13,
var14,
var15,
var16,
var17,
var18,
var19,
var20,
var21,
var22,
var23,
var24,
var25,
var26,
var27,
var28,
var29,
var30
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"reserved": ["$"]
}
3 changes: 3 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9650/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function logVariables(o, l, e, n, a, c, g, i, r, s, t, b, f, p, u, x, V, d, h, j, k, m, q, v, w, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, W, X, Y, Z, _, oo, ol, oe, on, oa, oc, og) {
console.log(o, l, e, n, a, c, g, i, r, s, t, b, f, p, u, x, V, d, h, j, k, m, q, v, w, y, z, A, B, C);
}
5 changes: 5 additions & 0 deletions crates/swc_ecma_transforms_base/src/hygiene/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use rustc_hash::FxHashSet;
use swc_atoms::Atom;
use swc_common::Mark;
use swc_ecma_ast::*;
use swc_ecma_utils::stack_size::maybe_grow_default;
Expand All @@ -24,6 +26,9 @@ pub struct Config {

/// Mangle even if vars are visible to `eval` or `with`.
pub ignore_eval: bool,

/// Used for preventing mangler from renaming variables to reserved names.
pub preserved_symbols: FxHashSet<Atom>,
}

/// See [hygiene_with_config] for doc. Creates a `hygiene` pass with default
Expand Down
6 changes: 6 additions & 0 deletions crates/swc_ecma_transforms_base/src/rename/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ where
.extend(self.preserved.iter().map(|v| v.0.clone()));
}

if !self.config.preserved_symbols.is_empty() {
unresolved
.to_mut()
.extend(self.config.preserved_symbols.iter().cloned());
}

if R::MANGLE {
let cost = scope.rename_cost();
scope.rename_in_mangle_mode(
Expand Down

0 comments on commit b49317a

Please sign in to comment.