Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Force rename synthesized identifiers #9473

Merged
merged 7 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/many-lobsters-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_ecma_minifier: patch
swc_core: patch
---

fix(es/minifier): Force rename synthesized identifiers
18 changes: 6 additions & 12 deletions crates/swc_ecma_minifier/src/compress/pure/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use radix_fmt::Radix;
use swc_common::{util::take::Take, Spanned, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_utils::{number::ToJsString, ExprExt, IsEmpty, Value};
#[cfg(feature = "debug")]
use {crate::debug::dump, tracing::debug};

use super::Pure;
use crate::compress::util::{eval_as_number, is_pure_undefined_or_null};
#[cfg(feature = "debug")]
use crate::debug::dump;

impl Pure<'_> {
pub(super) fn eval_array_method_call(&mut self, e: &mut Expr) {
Expand Down Expand Up @@ -659,21 +659,15 @@ impl Pure<'_> {
_ => return,
};

#[cfg(feature = "debug")]
debug!(
"before: optimize_member_expr: {}",
dump(&*member_expr, false)
);

if let Some(replacement) =
self.optimize_member_expr(&mut member_expr.obj, &member_expr.prop)
{
*e = replacement;
self.changed = true;
report_change!("member_expr: Optimized member expression");

#[cfg(feature = "debug")]
debug!("after: optimize_member_expr: {}", dump(&*e, false));
report_change!(
"member_expr: Optimized member expression as {}",
dump(&*e, false)
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub fn optimize(
let _timer = timer!("mangle names");
// TODO: base54.reset();

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

let chars = CharFreq::compute(
&n,
Expand Down
24 changes: 17 additions & 7 deletions crates/swc_ecma_minifier/src/pass/mangle_names/preserver.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
use rustc_hash::FxHashSet;
use swc_ecma_ast::*;
use swc_ecma_usage_analyzer::marks::Marks;
use swc_ecma_utils::find_pat_ids;
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, n: &N) -> FxHashSet<Id>
pub(crate) fn idents_to_preserve<N>(options: MangleOptions, marks: Marks, n: &N) -> FxHashSet<Id>
where
N: VisitWith<Preserver>,
N: for<'a> VisitWith<Preserver<'a>>,
{
let mut v = Preserver {
options,
options: &options,
preserved: Default::default(),
should_preserve: false,
in_top_level: false,
};
n.visit_with(&mut v);

let top_level_mark = marks.top_level_ctxt.outer();

// Force rename synthesized names
// See https://github.com/swc-project/swc/issues/9468
v.preserved.retain(|id| {
options.reserved.contains(&id.0) || id.1.outer().is_descendant_of(top_level_mark)
});

v.preserved
}
pub(crate) struct Preserver {
options: MangleOptions,
pub(crate) struct Preserver<'a> {
options: &'a MangleOptions,

preserved: FxHashSet<Id>,

should_preserve: bool,
in_top_level: bool,
}

impl Preserver {
impl<'a> Preserver<'a> {
fn is_reserved(&self, ident: &Ident) -> bool {
self.options.reserved.contains(&ident.sym)
}
}

impl Visit for Preserver {
impl<'a> Visit for Preserver<'a> {
noop_visit_type!();

fn visit_block_stmt(&mut self, n: &BlockStmt) {
Expand Down
10 changes: 10 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9468/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function func1(arg1, arg2) {
return getX(arg1) + arg2
}
function getX(x) {
const v = document.getElementById('eid').getAttribute(x)
return v
}
console.log(func1(7, getX('data-x')))
console.log(func1(7, getX('data-y')))

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var t, e;
function getX(t) {
return document.getElementById('eid').getAttribute(t);
}
console.log((t = getX('data-x'), getX(7) + t)), console.log((e = getX('data-y'), getX(7) + e));
Loading