Skip to content

Commit

Permalink
fix merge equal
Browse files Browse the repository at this point in the history
  • Loading branch information
Austaras committed Apr 20, 2022
1 parent 192f498 commit a78c535
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 48 deletions.
52 changes: 25 additions & 27 deletions crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Expand Up @@ -2474,6 +2474,30 @@ where
return;
}

let ctx = Ctx { ..self.ctx };

self.with_ctx(ctx).inject_else(stmts);

self.with_ctx(ctx).handle_stmt_likes(stmts);

self.with_ctx(ctx).merge_var_decls(stmts);

drop_invalid_stmts(stmts);

if stmts.len() == 1 {
if let Stmt::Expr(ExprStmt { expr, .. }) = &stmts[0] {
if let Expr::Lit(Lit::Str(s)) = &**expr {
if s.value == *"use strict" {
stmts.clear();
}
}
}
}

if cfg!(debug_assertions) {
stmts.visit_with(&mut AssertValid);
}

if self.options.dead_code {
// copy from [Remover]
// TODO: make it better
Expand All @@ -2482,9 +2506,7 @@ where
let mut new_stmts = Vec::with_capacity(stmts.len());

let mut iter = stmts.take().into_iter();
while let Some(mut stmt) = iter.next() {
stmt.visit_mut_with(self);

while let Some(stmt) = iter.next() {
let stmt = match stmt {
// Remove empty statements.
Stmt::Empty(..) => continue,
Expand Down Expand Up @@ -2548,30 +2570,6 @@ where

*stmts = new_stmts;
}

let ctx = Ctx { ..self.ctx };

self.with_ctx(ctx).inject_else(stmts);

self.with_ctx(ctx).handle_stmt_likes(stmts);

self.with_ctx(ctx).merge_var_decls(stmts);

drop_invalid_stmts(stmts);

if stmts.len() == 1 {
if let Stmt::Expr(ExprStmt { expr, .. }) = &stmts[0] {
if let Expr::Lit(Lit::Str(s)) = &**expr {
if s.value == *"use strict" {
stmts.clear();
}
}
}
}

if cfg!(debug_assertions) {
stmts.visit_with(&mut AssertValid);
}
}

fn visit_mut_str(&mut self, s: &mut Str) {
Expand Down
12 changes: 8 additions & 4 deletions crates/swc_ecma_minifier/src/compress/optimize/switches.rs
Expand Up @@ -228,15 +228,16 @@ where
continue;
}

let mut cannot_cross = false;

for j in (i + 1)..len {
// TODO: default
cannot_cross |= boundary[j];
if cases[j].cons.is_empty() {
continue;
}
if boundary[j] {
// TODO: default
break;
}

// first case with a body and don't cross non-primitive branch
let found = if j != len - 1 {
cases[i].cons.eq_ignore_span(&cases[j].cons)
} else {
Expand All @@ -258,6 +259,9 @@ where
cases[(i + 1)..=j].rotate_right(len);
i += 1;
}
if cannot_cross {
break;
}
}

i += 1;
Expand Down
Expand Up @@ -3477,13 +3477,7 @@
if (!BUGGY_SAFARI_ITERATORS && KIND in IterablePrototype) return IterablePrototype[KIND];
switch(KIND){
case KEYS:
return function() {
return new IteratorConstructor(this, KIND);
};
case VALUES:
return function() {
return new IteratorConstructor(this, KIND);
};
case ENTRIES:
return function() {
return new IteratorConstructor(this, KIND);
Expand Down
Expand Up @@ -2445,6 +2445,11 @@
switch(cType){
case 0:
case 1:
case 13:
case 14:
case 16:
case 17:
case 15:
lastArabic = !1;
case 4:
case 3:
Expand All @@ -2454,6 +2459,7 @@
case 7:
return lastArabic = !0, hasUBAT_AL = !0, 1;
case 8:
case 18:
return 4;
case 9:
if (ix < 1 || ix + 1 >= types.length || 2 != (wType = classes[ix - 1]) && 3 != wType || 2 != (nType = types[ix + 1]) && 3 != nType) return 4;
Expand All @@ -2479,14 +2485,6 @@
return lastArabic = !1, hasUBAT_B = !0, dir;
case 6:
return hasUBAT_S = !0, 4;
case 13:
case 14:
case 16:
case 17:
case 15:
lastArabic = !1;
case 18:
return 4;
}
}
function _getCharacterType(ch) {
Expand Down

Large diffs are not rendered by default.

@@ -1,2 +1 @@
if ("bar" === foo);
else other();
"bar" === foo || other();

0 comments on commit a78c535

Please sign in to comment.