fix(es/minifier): Fix inlining into shorthand properties (#2348)

swc_ecma_minifer:
 - Fix inlining of bindings into shorthand properties.
This commit is contained in:
Donny/강동윤 2021-10-05 18:53:00 +09:00 committed by GitHub
parent ef4c80be7d
commit 87b20a8896
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 127 additions and 45 deletions

2
Cargo.lock generated
View File

@ -2650,7 +2650,7 @@ dependencies = [
[[package]]
name = "swc_ecma_minifier"
version = "0.33.0"
version = "0.33.1"
dependencies = [
"ansi_term 0.12.1",
"anyhow",

View File

@ -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.33.0"
version = "0.33.1"
[features]
debug = ["backtrace"]

View File

@ -6,7 +6,7 @@ use crate::{
util::idents_used_by,
};
use swc_atoms::js_word;
use swc_common::{util::take::Take, Spanned, DUMMY_SP};
use swc_common::{util::take::Take, Spanned};
use swc_ecma_ast::*;
use swc_ecma_utils::{ident::IdentLike, ExprExt, UsageFinder};
@ -208,13 +208,6 @@ where
return;
}
if self.ctx.inline_as_assignment {
if cfg!(feature = "debug") {
tracing::trace!("inline: [x] inline_as_assignment is true");
}
return;
}
// Single use => inlined
if is_inline_enabled
&& !should_preserve
@ -397,13 +390,6 @@ where
return;
}
if self.ctx.inline_as_assignment {
if cfg!(feature = "debug") {
tracing::trace!("inline: [x] inline_as_assignment=true");
}
return;
}
if self
.data
.as_ref()
@ -606,29 +592,6 @@ where
}
}
if self.ctx.inline_as_assignment {
if let Some(value) = self.state.vars_for_inlining.remove(&i.to_id()) {
self.changed = true;
tracing::debug!(
"inline: Inlining '{}{:?}' using assignment",
i.sym,
i.span.ctxt
);
*e = Expr::Assign(AssignExpr {
span: DUMMY_SP,
op: op!("="),
left: PatOrExpr::Pat(Box::new(Pat::Ident(i.clone().into()))),
right: value,
});
if cfg!(feature = "debug") {
tracing::trace!("inline: [Change] {}", dump(&*e))
}
return;
}
}
if let Some(value) = self.state.vars_for_inlining.get(&i.to_id()) {
self.changed = true;
tracing::debug!(

View File

@ -111,9 +111,6 @@ struct Ctx {
is_callee: bool,
in_call_arg: bool,
/// If this is `true`, the first usage will be inlined as an assignment.
inline_as_assignment: bool,
var_kind: Option<VarDeclKind>,
/// `true` if we should not inline values.
@ -2091,6 +2088,27 @@ where
n.retain(|p| !p.pat.is_invalid());
}
fn visit_mut_prop(&mut self, n: &mut Prop) {
n.visit_mut_children_with(self);
match n {
Prop::Shorthand(i) => {
if self.lits.contains_key(&i.to_id())
|| self.state.vars_for_inlining.contains_key(&i.to_id())
{
let mut e = Box::new(Expr::Ident(i.clone()));
e.visit_mut_with(self);
*n = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(i.clone()),
value: e,
});
}
}
_ => {}
}
}
fn visit_mut_return_stmt(&mut self, n: &mut ReturnStmt) {
n.visit_mut_children_with(self);

View File

@ -0,0 +1,65 @@
import { a, b } from './utils';
if (typeof window !== 'undefined') {
require('intersection-observer');
}
const manager = (function makeManager() {
const c = new Map();
function d(e) {
return (
f(e) ||
new IntersectionObserver(g, e)
);
}
function f(g = {}) {
const h = b(g);
for (const i of c.keys()) {
if (a(i, h)) {
return i;
}
}
return null;
}
function j(k) {
return !c.has(k)
? c.set(k, new Map()).get(k)
: c.get(k);
}
function l(m, n, o) {
const p = j(m);
p.set(n, o);
m.observe(n);
}
function q(r, s) {
const t = j(r);
t.delete(s);
r.unobserve(s);
}
function g(u, v) {
for (let w of u) {
const x = j(v);
const y = x.get(w.target);
if (y) {
y(w);
}
}
}
return {
d,
l,
q,
};
})();
export default manager;
export const { d } = manager;
export const { l } = manager;
export const { q } = manager;

View File

@ -0,0 +1,36 @@
import { a, b } from "./utils";
"undefined" != typeof window && require("intersection-observer");
const manager = function() {
const c = new Map();
function j(k) {
return c.has(k) ? c.get(k) : c.set(k, new Map()).get(k);
}
function g(u, v) {
for (let w of u){
const x = j(v), y = x.get(w.target);
y && y(w);
}
}
return {
d: function(e) {
return (function(g = {
}) {
const h = b(g);
for (const i of c.keys())if (a(i, h)) return i;
return null;
})(e) || new IntersectionObserver(g, e);
},
l: function(m, n, o) {
const p = j(m);
p.set(n, o), m.observe(n);
},
q: function(r, s) {
const t = j(r);
t.delete(s), r.unobserve(s);
}
};
}();
export default manager;
export const { d } = manager;
export const { l } = manager;
export const { q } = manager;

View File

@ -897,6 +897,8 @@ pure_getters/issue_2313_7/input.js
pure_getters/issue_2678/input.js
pure_getters/issue_2938_1/input.js
pure_getters/issue_2938_2/input.js
pure_getters/set_immutable_2/input.js
pure_getters/set_immutable_4/input.js
reduce_vars/accessor_1/input.js
reduce_vars/array_forin_2/input.js
reduce_vars/array_forof_1/input.js

View File

@ -355,9 +355,7 @@ pure_getters/collapse_vars_2_true/input.js
pure_getters/issue_2265_1/input.js
pure_getters/issue_2265_3/input.js
pure_getters/issue_2313_6/input.js
pure_getters/set_immutable_2/input.js
pure_getters/set_immutable_3/input.js
pure_getters/set_immutable_4/input.js
pure_getters/set_immutable_5/input.js
pure_getters/strict/input.js
pure_getters/strict_reduce_vars/input.js