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): Fix minifier #2477

Merged
merged 9 commits into from
Oct 19, 2021
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecmascript/minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "src/lists/*.json"]
license = "Apache-2.0/MIT"
name = "swc_ecma_minifier"
repository = "https://github.com/swc-project/swc.git"
version = "0.44.0"
version = "0.44.1"

[features]
debug = ["backtrace"]
Expand Down
27 changes: 1 addition & 26 deletions ecmascript/minifier/src/compress/optimize/collapse_vars.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use super::Optimizer;
use crate::mode::Mode;
use swc_common::collections::AHashMap;
use swc_ecma_ast::*;
use swc_ecma_utils::{ident::IdentLike, Id};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use swc_ecma_utils::ident::IdentLike;

/// Methods related to the option `collapse_vars`.
impl<M> Optimizer<'_, M>
Expand Down Expand Up @@ -78,26 +76,3 @@ where
}
}
}

struct Inliner<'a> {
values: &'a mut AHashMap<Id, Option<Box<Expr>>>,
}

impl VisitMut for Inliner<'_> {
noop_visit_mut_type!();

fn visit_mut_expr(&mut self, e: &mut Expr) {
e.visit_mut_children_with(self);

match e {
Expr::Ident(i) => {
if let Some(value) = self.values.remove(&i.to_id()) {
tracing::debug!("collapse_vars: Inlining {}{:?}", i.sym, i.span.ctxt);

*e = *value.expect("should be used only once");
}
}
_ => {}
}
}
}
44 changes: 44 additions & 0 deletions ecmascript/minifier/src/compress/optimize/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,27 @@ impl VisitMut for MultiReplacer {
}
}
}

fn visit_mut_prop(&mut self, p: &mut Prop) {
p.visit_mut_children_with(self);

match p {
Prop::Shorthand(i) => {
if let Some(value) = self.vars.remove(&i.to_id()) {
debug!("multi-replacer: Replaced `{}` as shorthand", i);
self.changed = true;

*p = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(i.clone()),
value,
});

return;
}
}
_ => {}
}
}
}

pub(crate) fn replace_id_with_expr<N>(node: &mut N, from: Id, to: Box<Expr>) -> Option<Box<Expr>>
Expand Down Expand Up @@ -266,4 +287,27 @@ impl VisitMut for ExprReplacer {
e.prop.visit_mut_with(self);
}
}

fn visit_mut_prop(&mut self, p: &mut Prop) {
p.visit_mut_children_with(self);

match p {
Prop::Shorthand(i) => {
if self.from.0 == i.sym && self.from.1 == i.span.ctxt {
let value = if let Some(new) = self.to.take() {
new
} else {
unreachable!("`{}` is already taken", i)
};
*p = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(i.clone()),
value,
});

return;
}
}
_ => {}
}
}
}
8 changes: 8 additions & 0 deletions ecmascript/minifier/src/compress/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,14 @@ where

(self.op)(e);
}

fn visit_mut_member_expr(&mut self, e: &mut MemberExpr) {
e.obj.visit_mut_with(self);

if e.computed {
e.prop.visit_mut_with(self);
}
}
}

pub fn replace_expr<N, F>(node: &mut N, op: F)
Expand Down
Loading