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

refactor(es/minifier): Don't create invalid nodes #6191

Merged
merged 69 commits into from Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from 68 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
bc327eb
Add assertions
kdy1 Oct 18, 2022
ba40d58
More assertions
kdy1 Oct 18, 2022
b20c98f
normalize
kdy1 Oct 18, 2022
d971155
More assertions
kdy1 Oct 18, 2022
dbe4499
normalize more
kdy1 Oct 18, 2022
d353009
Some fix
kdy1 Oct 18, 2022
c0e2f8a
More assertions
kdy1 Oct 18, 2022
8ebc740
More assertions
kdy1 Oct 18, 2022
b4cb8dd
More assertions
kdy1 Oct 18, 2022
9c87028
More assertions
kdy1 Oct 18, 2022
aad6e2c
More assertions
kdy1 Oct 18, 2022
df2b84f
Remove wrong asssertion
kdy1 Oct 18, 2022
b43dd47
Remove wrong asssertion
kdy1 Oct 18, 2022
b3dac37
More assertions
kdy1 Oct 18, 2022
2c1c70d
Remove one normalization
kdy1 Oct 18, 2022
14e3701
Some fix
kdy1 Oct 18, 2022
09a415a
More fix
kdy1 Oct 18, 2022
e833d9c
More assertions
kdy1 Oct 18, 2022
1ab82fb
Fix
kdy1 Oct 18, 2022
259855f
Fix
kdy1 Oct 18, 2022
86b964c
Fix
kdy1 Oct 18, 2022
76db4c0
lints
kdy1 Oct 18, 2022
b714285
Fix
kdy1 Oct 18, 2022
e73abff
More assertions
kdy1 Oct 18, 2022
da95580
Normalize more
kdy1 Oct 18, 2022
4a6b807
More assertions
kdy1 Oct 18, 2022
97753d0
More assertions
kdy1 Oct 18, 2022
d98d69c
Fix `ignore_return_value`
kdy1 Oct 18, 2022
536a7d4
More assertions
kdy1 Oct 18, 2022
3529a53
Fix `ignore_return_value`
kdy1 Oct 18, 2022
2afd1a5
Fix `ignore_return_value`
kdy1 Oct 18, 2022
a055031
Fix dead branch remover pass
kdy1 Oct 18, 2022
8e1bacc
Better message
kdy1 Oct 18, 2022
49c5da6
Fix dead branch remover pass
kdy1 Oct 18, 2022
01131cb
Fix pure
kdy1 Oct 18, 2022
21c7037
Fix wrong assertion
kdy1 Oct 18, 2022
679fb9c
Merge assertvalid
kdy1 Oct 18, 2022
80ed754
lints
kdy1 Oct 18, 2022
cab6b92
Fix pure
kdy1 Oct 18, 2022
5dfee1d
Fix pure
kdy1 Oct 18, 2022
c4ce3ad
Fix compressor
kdy1 Oct 18, 2022
8d97676
More assertions
kdy1 Oct 18, 2022
1b47b32
More assertions
kdy1 Oct 18, 2022
baecb60
More assertions
kdy1 Oct 18, 2022
4a0f9df
More fix
kdy1 Oct 18, 2022
76f7de5
More fix
kdy1 Oct 18, 2022
9b5f720
clippy
kdy1 Oct 18, 2022
128cb59
lints
kdy1 Oct 18, 2022
5bce62f
More asseryions
kdy1 Oct 18, 2022
5282ef2
lints
kdy1 Oct 18, 2022
b99d682
Normalize
kdy1 Oct 18, 2022
405c168
Fix
kdy1 Oct 18, 2022
b74ca3c
Improve expression normalization
kdy1 Oct 18, 2022
d53c98b
Normalize more
kdy1 Oct 18, 2022
e2d5d4c
Change order
kdy1 Oct 18, 2022
f42ee81
Normalize more
kdy1 Oct 18, 2022
2e2e09d
More assertions
kdy1 Oct 18, 2022
5596457
Merge code
kdy1 Oct 18, 2022
177708b
More assertion
kdy1 Oct 18, 2022
dedc237
More assertion
kdy1 Oct 18, 2022
71776d2
More assertion
kdy1 Oct 18, 2022
bed4083
More assertion
kdy1 Oct 18, 2022
8037b3a
TODO
kdy1 Oct 18, 2022
2f0e900
Log
kdy1 Oct 20, 2022
f25d4e2
Add more assertions
kdy1 Oct 20, 2022
7cdbbe3
Log
kdy1 Oct 20, 2022
adf8317
Fix
kdy1 Oct 20, 2022
b45f828
Update test refs
kdy1 Oct 20, 2022
f13fc4e
Merge branch 'main' into more-rules
swc-bot Oct 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/swc/tests/tsc.rs
Expand Up @@ -413,6 +413,8 @@ fn matrix(input: &Path) -> Vec<TestUnitData> {
}

fn compile(output: &Path, test_unit_data: TestUnitData) {
let _testing = testing::init();

let cm = test_unit_data.cm;

let c = Compiler::new(cm.clone());
Expand Down
17 changes: 9 additions & 8 deletions crates/swc_ecma_minifier/src/compress/optimize/conditionals.rs
Expand Up @@ -3,6 +3,7 @@ use std::mem::swap;
use swc_common::{util::take::Take, EqIgnoreSpan, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::ext::ExprRefExt;
use swc_ecma_transforms_optimization::debug_assert_valid;
use swc_ecma_utils::{ExprExt, ExprFactory, StmtExt, StmtLike};

use super::Optimizer;
Expand Down Expand Up @@ -282,7 +283,10 @@ where
};

let new_expr = self.compress_similar_cons_alt(&mut stmt.test, cons, alt, true);

if let Some(v) = new_expr {
debug_assert_valid(&v);

self.changed = true;
*s = Stmt::Expr(ExprStmt {
span: stmt.span,
Expand Down Expand Up @@ -346,6 +350,9 @@ where
alt: &mut Expr,
is_for_if_stmt: bool,
) -> Option<Expr> {
debug_assert_valid(cons);
debug_assert_valid(alt);

if cons.eq_ignore_span(alt) && !matches!(&*cons, Expr::Yield(..) | Expr::Fn(..)) {
report_change!("conditionals: cons is same as alt");
return Some(Expr::Seq(SeqExpr {
Expand Down Expand Up @@ -578,10 +585,7 @@ where
span: DUMMY_SP,
left: test.take(),
op: op!("||"),
right: Box::new(Expr::Seq(SeqExpr {
span: alt.span,
exprs: alt.exprs.take(),
})),
right: Expr::from_exprs(alt.exprs.take()),
}));
Some(Expr::Seq(SeqExpr {
span: DUMMY_SP,
Expand All @@ -601,10 +605,7 @@ where
span: DUMMY_SP,
left: test.take(),
op: op!("&&"),
right: Box::new(Expr::Seq(SeqExpr {
span: cons.span,
exprs: cons.exprs.take(),
})),
right: Expr::from_exprs(cons.exprs.take()),
}));
Some(Expr::Seq(SeqExpr {
span: DUMMY_SP,
Expand Down
11 changes: 11 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/if_return.rs
@@ -1,5 +1,6 @@
use swc_common::{util::take::Take, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_optimization::debug_assert_valid;
use swc_ecma_utils::{undefined, StmtExt, StmtLike};
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};

Expand Down Expand Up @@ -55,6 +56,8 @@ where

for stmt in stmts.iter_mut() {
self.merge_nested_if_returns(stmt, terminates);

debug_assert_valid(&*stmt);
}

if terminates || is_fn_body {
Expand All @@ -69,12 +72,18 @@ where
match s {
Stmt::Block(s) => {
self.merge_if_returns(&mut s.stmts, terminate, false);

debug_assert_valid(&*s);
}
Stmt::If(s) => {
self.merge_nested_if_returns(&mut s.cons, can_work);

debug_assert_valid(&s.cons);

if let Some(alt) = s.alt.as_deref_mut() {
self.merge_nested_if_returns(alt, can_work);

debug_assert_valid(&*alt);
}
}
_ => {}
Expand Down Expand Up @@ -366,6 +375,8 @@ where
}

if let Some(mut cur) = cur {
self.normalize_expr(&mut cur);

match &*cur {
Expr::Seq(seq)
if !should_preserve_last_return
Expand Down
14 changes: 7 additions & 7 deletions crates/swc_ecma_minifier/src/compress/optimize/iife.rs
Expand Up @@ -450,7 +450,6 @@ where
Callee::Super(_) | Callee::Import(_) => return,
Callee::Expr(e) => &mut **e,
};
self.normalize_expr(callee);

if self.ctx.dont_invoke_iife {
log_abort!("iife: Inline is prevented");
Expand Down Expand Up @@ -582,10 +581,7 @@ where
exprs.push(body.take());

report_change!("inline: Inlining a call to an arrow function");
*e = Expr::Seq(SeqExpr {
span: DUMMY_SP,
exprs,
});
*e = *Expr::from_exprs(exprs);
e.visit_mut_with(self);
}
}
Expand Down Expand Up @@ -986,7 +982,9 @@ where
};
self.merge_sequences_in_seq_expr(&mut e);

return Some(Expr::Seq(e));
let mut e = Expr::Seq(e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know exprs has more than 1 element?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We normalize instead of verifying, at the line below.
I think we can improve this in the future

self.normalize_expr(&mut e);
return Some(e);
}
_ => {}
}
Expand All @@ -1008,7 +1006,9 @@ where
};
self.merge_sequences_in_seq_expr(&mut e);

Some(Expr::Seq(e))
let mut e = Expr::Seq(e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

self.normalize_expr(&mut e);
Some(e)
}

fn can_be_inlined_for_iife(&self, arg: &Expr) -> bool {
Expand Down