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): Do not drop used properties #7702

Merged
merged 52 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
332160b
Add a test
kdy1 Jul 25, 2023
ba81a3a
`for_each_id_ref_in_expr`
kdy1 Jul 25, 2023
aa240dd
Use it
kdy1 Jul 25, 2023
7c8dde5
Fix it
kdy1 Jul 25, 2023
21b7624
Update test refs
kdy1 Jul 25, 2023
2354518
More fix
kdy1 Jul 26, 2023
306b87a
Remove unreduced
kdy1 Jul 26, 2023
af9b230
Update test refs
kdy1 Jul 26, 2023
7860190
Add a test
kdy1 Jul 28, 2023
c73d4c7
#7710
kdy1 Jul 28, 2023
9ad41c3
export vars
kdy1 Jul 28, 2023
168cac5
Add tests
kdy1 Jul 28, 2023
08d52b2
Update test refs
kdy1 Jul 28, 2023
c22c809
swcrc
kdy1 Jul 28, 2023
1517cbd
Update test refs
kdy1 Jul 28, 2023
fe3bb69
Cleanup
kdy1 Jul 28, 2023
5b80edf
Add tests
kdy1 Jul 28, 2023
84b3f54
Add tests
kdy1 Jul 28, 2023
5b3c677
lint
kdy1 Jul 28, 2023
de25494
Update test refs
kdy1 Jul 28, 2023
13166f5
WIP
kdy1 Jul 28, 2023
ec651eb
Update test refs
kdy1 Jul 28, 2023
58dfac7
Add a test
kdy1 Jul 28, 2023
faee022
Add tests
kdy1 Jul 28, 2023
ceac98e
WIP
kdy1 Jul 28, 2023
c327223
Add a test
kdy1 Jul 28, 2023
28ae552
id refs
kdy1 Jul 28, 2023
3dd56c8
Update test refs
kdy1 Jul 28, 2023
61a5d9d
Some ctx fix
kdy1 Jul 28, 2023
afbcd82
sort
kdy1 Jul 28, 2023
d9d3e5b
`visit_computed_prop_name`
kdy1 Jul 28, 2023
9eccfb6
Use single visitor
kdy1 Jul 28, 2023
c6ed6aa
skip_all
kdy1 Jul 28, 2023
5b47a87
More ctx work
kdy1 Jul 28, 2023
2a8fa3d
Update test refs
kdy1 Jul 28, 2023
77a70cb
Update test refs
kdy1 Jul 28, 2023
7db8b47
Update test refs
kdy1 Jul 28, 2023
0dbe59c
fix
kdy1 Jul 28, 2023
c0cbf7f
ctx
kdy1 Jul 28, 2023
c16980d
type
kdy1 Jul 28, 2023
0c90608
Add a test
kdy1 Jul 28, 2023
f404807
Add a test
kdy1 Jul 28, 2023
86e0f4f
swcrc
kdy1 Jul 28, 2023
6fc8bae
Update test refs
kdy1 Jul 28, 2023
bb76dc3
`for_each_id_ref_in_prop_name`
kdy1 Jul 28, 2023
ae93775
more
kdy1 Jul 28, 2023
d2a7597
more work
kdy1 Jul 28, 2023
fadcf45
more
kdy1 Jul 28, 2023
df97f63
more
kdy1 Jul 28, 2023
0517536
Update test refs
kdy1 Jul 28, 2023
5c21bf6
Update test refs
kdy1 Jul 28, 2023
49426dc
Merge branch 'main' into issue-7700
swc-bot Jul 28, 2023
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
66 changes: 66 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7700/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true
},
"transform": {
"react": {}
},
"target": "es5",
"loose": true,
"minify": {
"compress": {
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": true,
"pristine_globals": true
}
},
},
"isModule": true,
"module": {
"type": "commonjs"
},
"minify": false
}
27 changes: 27 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7700/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
interface IProps {
isRtl: boolean | string;
position: keyof typeof positions;
}

const positions = {
top: 1,
left: 2,
right: 3,
bottom: 4,
}

const rtlPositions = {
top: 1,
right: 2,
left: 3,
bottom: 4,
}

export function PositionRender({ isRtl, position }: IProps) {

const display = (isRtl === 'fe-fe-fe' ? rtlPositions : positions)[position];

return <h1>
PositionRender: {display}
</h1>
}
40 changes: 28 additions & 12 deletions crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ where
self.with_ctx(ctx).visit_in_cond(&e.right);
} else {
if e.op == op!("in") {
if let Expr::Ident(obj) = &*e.right {
for_each_id_ref_in_expr(&e.right, &mut |obj| {
let var = self.data.var_or_default(obj.to_id());

match &*e.left {
Expand All @@ -318,7 +318,7 @@ where
var.mark_indexed_with_dynamic_key();
}
}
}
})
}

let ctx = Ctx {
Expand Down Expand Up @@ -354,11 +354,11 @@ where
}

if let Callee::Expr(callee) = &n.callee {
if let Expr::Ident(callee) = &**callee {
for_each_id_ref_in_expr(callee, &mut |callee| {
self.data
.var_or_default(callee.to_id())
.mark_used_as_callee();
}
});

match &**callee {
Expr::Fn(callee) => {
Expand Down Expand Up @@ -419,9 +419,9 @@ where
}

for arg in &n.args {
if let Expr::Ident(arg) = &*arg.expr {
for_each_id_ref_in_expr(&arg.expr, &mut |arg| {
self.data.var_or_default(arg.to_id()).mark_used_as_arg();
}
})
}

if let Callee::Expr(callee) = &n.callee {
Expand Down Expand Up @@ -644,11 +644,11 @@ where
e.visit_children_with(self);

if e.spread.is_some() {
if let Expr::Ident(i) = &*e.expr {
for_each_id_ref_in_expr(&e.expr, &mut |i| {
self.data
.var_or_default(i.to_id())
.mark_indexed_with_dynamic_key();
}
});
}
}

Expand Down Expand Up @@ -873,7 +873,7 @@ where
};
c.visit_with(&mut *self.with_ctx(ctx));
}
if let Expr::Ident(obj) = &*e.obj {
for_each_id_ref_in_expr(&e.obj, &mut |obj| {
let v = self.data.var_or_default(obj.to_id());
v.mark_has_property_access();

Expand All @@ -892,7 +892,7 @@ where
if self.ctx.in_assign_lhs || self.ctx.is_delete_arg {
self.data.mark_property_mutattion(obj.to_id(), self.ctx)
}
}
})
}

#[cfg_attr(feature = "debug", tracing::instrument(skip(self, n)))]
Expand Down Expand Up @@ -1065,11 +1065,11 @@ where
fn visit_spread_element(&mut self, e: &SpreadElement) {
e.visit_children_with(self);

if let Expr::Ident(i) = &*e.expr {
for_each_id_ref_in_expr(&e.expr, &mut |i| {
self.data
.var_or_default(i.to_id())
.mark_indexed_with_dynamic_key();
}
});
}

#[cfg_attr(feature = "debug", tracing::instrument(skip(self, n)))]
Expand Down Expand Up @@ -1307,6 +1307,22 @@ where
}
}

/// - `a` => `a`
/// - `a ? b : c` => `b`, `c`
fn for_each_id_ref_in_expr(e: &Expr, op: &mut impl FnMut(&Ident)) {
match e {
Expr::Ident(i) => op(i),
Expr::Cond(c) => {
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
for_each_id_ref_in_expr(&c.cons, op);
for_each_id_ref_in_expr(&c.alt, op);
}
Expr::Seq(s) => {
for_each_id_ref_in_expr(s.exprs.last().unwrap(), op);
}
_ => {}
}
}

fn leftmost(p: &Expr) -> Option<Id> {
match p {
Expr::Ident(i) => Some(i.to_id()),
Expand Down
Loading