diff --git a/crates/swc/tests/tsc-references/doWhileBreakStatements.2.minified.js b/crates/swc/tests/tsc-references/doWhileBreakStatements.2.minified.js index 62636b8a8cc..9c18cfcd8ca 100644 --- a/crates/swc/tests/tsc-references/doWhileBreakStatements.2.minified.js +++ b/crates/swc/tests/tsc-references/doWhileBreakStatements.2.minified.js @@ -1,4 +1,2 @@ //// [doWhileBreakStatements.ts] -FOUR: for(;;)FIVE: for(;;)break FOUR; for(;;); -for(;;)for(;;); diff --git a/crates/swc/tests/tsc-references/doWhileContinueStatements.2.minified.js b/crates/swc/tests/tsc-references/doWhileContinueStatements.2.minified.js index 2f66d81f6de..20c6d3597db 100644 --- a/crates/swc/tests/tsc-references/doWhileContinueStatements.2.minified.js +++ b/crates/swc/tests/tsc-references/doWhileContinueStatements.2.minified.js @@ -1,8 +1,8 @@ //// [doWhileContinueStatements.ts] for(;;); for(;;); -TWO: for(;;); -FOUR: for(;;)FIVE: for(;;)continue FOUR; -for(;;)SIX: for(;;)continue SIX; +for(;;); +FOUR: for(;;)for(;;)continue FOUR; +for(;;)for(;;)continue; SEVEN: for(;;)for(;;)for(;;)continue SEVEN; for(;;); diff --git a/crates/swc/tests/tsc-references/forBreakStatements.2.minified.js b/crates/swc/tests/tsc-references/forBreakStatements.2.minified.js index 1b0d68a3ecb..ea66347ba68 100644 --- a/crates/swc/tests/tsc-references/forBreakStatements.2.minified.js +++ b/crates/swc/tests/tsc-references/forBreakStatements.2.minified.js @@ -1,4 +1,2 @@ //// [forBreakStatements.ts] -FOUR: for(;;)FIVE: for(;;)break FOUR; for(;;); -for(;;)for(;;); diff --git a/crates/swc/tests/tsc-references/forContinueStatements.2.minified.js b/crates/swc/tests/tsc-references/forContinueStatements.2.minified.js index 9cb92c894b0..47ff83c7a1f 100644 --- a/crates/swc/tests/tsc-references/forContinueStatements.2.minified.js +++ b/crates/swc/tests/tsc-references/forContinueStatements.2.minified.js @@ -1,8 +1,8 @@ //// [forContinueStatements.ts] for(;;); for(;;); -TWO: for(;;); -FOUR: for(;;)FIVE: for(;;)continue FOUR; -for(;;)SIX: for(;;)continue SIX; +for(;;); +FOUR: for(;;)for(;;)continue FOUR; +for(;;)for(;;)continue; SEVEN: for(;;)for(;;)for(;;)continue SEVEN; for(;;); diff --git a/crates/swc/tests/tsc-references/forInBreakStatements.2.minified.js b/crates/swc/tests/tsc-references/forInBreakStatements.2.minified.js index a82de03c6c8..2b8158f196c 100644 --- a/crates/swc/tests/tsc-references/forInBreakStatements.2.minified.js +++ b/crates/swc/tests/tsc-references/forInBreakStatements.2.minified.js @@ -1,8 +1,8 @@ //// [forInBreakStatements.ts] for(var x in {})break; -ONE: for(var x in {})break ONE; -TWO: THREE: for(var x in {})break THREE; -FOUR: for(var x in {})FIVE: for(var x in {})break FOUR; -for(var x in {})SIX: for(var x in {})break SIX; +for(var x in {})break; +for(var x in {})break; +FOUR: for(var x in {})for(var x in {})break FOUR; +for(var x in {})for(var x in {})break; SEVEN: for(var x in {})for(var x in {})for(var x in {})break SEVEN; -EIGHT: for(var x in {})break EIGHT; +for(var x in {})break; diff --git a/crates/swc/tests/tsc-references/forInContinueStatements.2.minified.js b/crates/swc/tests/tsc-references/forInContinueStatements.2.minified.js index ceb60f3e773..eff52fd163a 100644 --- a/crates/swc/tests/tsc-references/forInContinueStatements.2.minified.js +++ b/crates/swc/tests/tsc-references/forInContinueStatements.2.minified.js @@ -1,8 +1,8 @@ //// [forInContinueStatements.ts] for(var x in {}); for(var x in {}); -TWO: for(var x in {}); -FOUR: for(var x in {})FIVE: for(var x in {})continue FOUR; -for(var x in {})SIX: for(var x in {})continue SIX; +for(var x in {}); +FOUR: for(var x in {})for(var x in {})continue FOUR; +for(var x in {})for(var x in {})continue; SEVEN: for(var x in {})for(var x in {})for(var x in {})continue SEVEN; for(var x in {}); diff --git a/crates/swc/tests/tsc-references/parser_breakTarget3.2.minified.js b/crates/swc/tests/tsc-references/parser_breakTarget3.2.minified.js index 63090f82f7c..5f4273d490a 100644 --- a/crates/swc/tests/tsc-references/parser_breakTarget3.2.minified.js +++ b/crates/swc/tests/tsc-references/parser_breakTarget3.2.minified.js @@ -1,2 +1 @@ //// [parser_breakTarget3.ts] -target1: target2: for(;;)break target1; diff --git a/crates/swc/tests/tsc-references/parser_continueTarget3.2.minified.js b/crates/swc/tests/tsc-references/parser_continueTarget3.2.minified.js index e50c91f489a..d7720e8d668 100644 --- a/crates/swc/tests/tsc-references/parser_continueTarget3.2.minified.js +++ b/crates/swc/tests/tsc-references/parser_continueTarget3.2.minified.js @@ -1,2 +1,2 @@ //// [parser_continueTarget3.ts] -target1: target2: for(;;)continue target1; +for(;;)continue; diff --git a/crates/swc/tests/tsc-references/parser_continueTarget4.2.minified.js b/crates/swc/tests/tsc-references/parser_continueTarget4.2.minified.js index 3cb989094b6..cdaaeb7554f 100644 --- a/crates/swc/tests/tsc-references/parser_continueTarget4.2.minified.js +++ b/crates/swc/tests/tsc-references/parser_continueTarget4.2.minified.js @@ -1,2 +1,2 @@ //// [parser_continueTarget4.ts] -target1: for(;;); +for(;;); diff --git a/crates/swc/tests/tsc-references/parser_duplicateLabel3.2.minified.js b/crates/swc/tests/tsc-references/parser_duplicateLabel3.2.minified.js index e38a822a9e6..2b499952c84 100644 --- a/crates/swc/tests/tsc-references/parser_duplicateLabel3.2.minified.js +++ b/crates/swc/tests/tsc-references/parser_duplicateLabel3.2.minified.js @@ -1,2 +1,2 @@ //// [parser_duplicateLabel3.ts] -target: for(;;); +for(;;); diff --git a/crates/swc/tests/tsc-references/parser_duplicateLabel4.2.minified.js b/crates/swc/tests/tsc-references/parser_duplicateLabel4.2.minified.js index 02cf5879983..7862a8c70f7 100644 --- a/crates/swc/tests/tsc-references/parser_duplicateLabel4.2.minified.js +++ b/crates/swc/tests/tsc-references/parser_duplicateLabel4.2.minified.js @@ -1,3 +1,3 @@ //// [parser_duplicateLabel4.ts] -target: for(;;); -target: for(;;); +for(;;); +for(;;); diff --git a/crates/swc/tests/tsc-references/typeofOperatorWithAnyOtherType.2.minified.js b/crates/swc/tests/tsc-references/typeofOperatorWithAnyOtherType.2.minified.js index f7613eaefb4..1794c6afa74 100644 --- a/crates/swc/tests/tsc-references/typeofOperatorWithAnyOtherType.2.minified.js +++ b/crates/swc/tests/tsc-references/typeofOperatorWithAnyOtherType.2.minified.js @@ -21,11 +21,4 @@ var A = function() { M.n = n; }(M || (M = {})); var objA = new A(); -void 0 === ANY1 || _type_of(ANY1), _type_of(ANY2), void 0 === A || _type_of(A), void 0 === M || _type_of(M), void 0 === obj || _type_of(obj), _type_of(obj1), _type_of(null), _type_of({}), _type_of(ANY2[0]), _type_of(objA.a), _type_of(obj1.x), _type_of(M.n), _type_of(foo()), _type_of(A.foo()), _type_of(ANY + ANY1), _type_of(NaN), _type_of(0), _type_of(NaN), _type_of(void 0 === ANY ? "undefined" : _type_of(ANY)), _type_of(_type_of(_type_of(ANY + ANY1))), void 0 === ANY || _type_of(ANY), void 0 === ANY1 || _type_of(ANY1), _type_of(ANY2[0]), void 0 === ANY || _type_of(ANY), _type_of(obj1), _type_of(obj1.x), _type_of(objA.a), _type_of(M.n); -z: void 0 === ANY || _type_of(ANY); -x: _type_of(ANY2); -r: _type_of(foo); -z: _type_of(objA.a); -z: _type_of(A.foo); -z: _type_of(M.n); -z: _type_of(obj1.x); +void 0 === ANY1 || _type_of(ANY1), _type_of(ANY2), void 0 === A || _type_of(A), void 0 === M || _type_of(M), void 0 === obj || _type_of(obj), _type_of(obj1), _type_of(null), _type_of({}), _type_of(ANY2[0]), _type_of(objA.a), _type_of(obj1.x), _type_of(M.n), _type_of(foo()), _type_of(A.foo()), _type_of(ANY + ANY1), _type_of(NaN), _type_of(0), _type_of(NaN), _type_of(void 0 === ANY ? "undefined" : _type_of(ANY)), _type_of(_type_of(_type_of(ANY + ANY1))), void 0 === ANY || _type_of(ANY), void 0 === ANY1 || _type_of(ANY1), _type_of(ANY2[0]), void 0 === ANY || _type_of(ANY), _type_of(obj1), _type_of(obj1.x), _type_of(objA.a), _type_of(M.n), void 0 === ANY || _type_of(ANY), _type_of(ANY2), _type_of(foo), _type_of(objA.a), _type_of(A.foo), _type_of(M.n), _type_of(obj1.x); diff --git a/crates/swc/tests/tsc-references/typeofOperatorWithBooleanType.2.minified.js b/crates/swc/tests/tsc-references/typeofOperatorWithBooleanType.2.minified.js index 8a18ec10adc..af5d22c4809 100644 --- a/crates/swc/tests/tsc-references/typeofOperatorWithBooleanType.2.minified.js +++ b/crates/swc/tests/tsc-references/typeofOperatorWithBooleanType.2.minified.js @@ -21,10 +21,4 @@ var objA = new A(); void 0 === BOOLEAN || _type_of(BOOLEAN), _type_of(!0), _type_of({ x: !0, y: !1 -}), _type_of(objA.a), _type_of(M.n), _type_of(foo()), _type_of(A.foo()), _type_of(void 0 === BOOLEAN ? "undefined" : _type_of(BOOLEAN)), _type_of(!0), void 0 === BOOLEAN || _type_of(BOOLEAN), _type_of(foo()), _type_of(!0), _type_of(objA.a), _type_of(M.n); -z: void 0 === BOOLEAN || _type_of(BOOLEAN); -r: _type_of(foo); -z: _type_of(!0); -z: _type_of(objA.a); -z: _type_of(A.foo); -z: _type_of(M.n); +}), _type_of(objA.a), _type_of(M.n), _type_of(foo()), _type_of(A.foo()), _type_of(void 0 === BOOLEAN ? "undefined" : _type_of(BOOLEAN)), _type_of(!0), void 0 === BOOLEAN || _type_of(BOOLEAN), _type_of(foo()), _type_of(!0), _type_of(objA.a), _type_of(M.n), void 0 === BOOLEAN || _type_of(BOOLEAN), _type_of(foo), _type_of(!0), _type_of(objA.a), _type_of(A.foo), _type_of(M.n); diff --git a/crates/swc/tests/tsc-references/typeofOperatorWithEnumType.2.minified.js b/crates/swc/tests/tsc-references/typeofOperatorWithEnumType.2.minified.js index 69c206810c1..c6e7cccfa15 100644 --- a/crates/swc/tests/tsc-references/typeofOperatorWithEnumType.2.minified.js +++ b/crates/swc/tests/tsc-references/typeofOperatorWithEnumType.2.minified.js @@ -3,6 +3,4 @@ var ENUM, ENUM1, z; import { _ as _type_of } from "@swc/helpers/_/_type_of"; ENUM || (ENUM = {}), function(ENUM1) { ENUM1[ENUM1.A = 0] = "A", ENUM1[ENUM1.B = 1] = "B", ENUM1[ENUM1[""] = 2] = ""; -}(ENUM1 || (ENUM1 = {})), void 0 === ENUM || _type_of(ENUM), void 0 === ENUM1 || _type_of(ENUM1), _type_of(ENUM1.A), _type_of(ENUM[0] + ENUM1.B), _type_of(void 0 === ENUM ? "undefined" : _type_of(ENUM)), _type_of(_type_of(_type_of(ENUM[0] + ENUM1.B))), void 0 === ENUM || _type_of(ENUM), void 0 === ENUM1 || _type_of(ENUM1), _type_of(ENUM1.B), void 0 === ENUM || _type_of(ENUM), z || (z = {}); -z: void 0 === ENUM || _type_of(ENUM); -z: void 0 === ENUM1 || _type_of(ENUM1); +}(ENUM1 || (ENUM1 = {})), void 0 === ENUM || _type_of(ENUM), void 0 === ENUM1 || _type_of(ENUM1), _type_of(ENUM1.A), _type_of(ENUM[0] + ENUM1.B), _type_of(void 0 === ENUM ? "undefined" : _type_of(ENUM)), _type_of(_type_of(_type_of(ENUM[0] + ENUM1.B))), void 0 === ENUM || _type_of(ENUM), void 0 === ENUM1 || _type_of(ENUM1), _type_of(ENUM1.B), void 0 === ENUM || _type_of(ENUM), z || (z = {}), void 0 === ENUM || _type_of(ENUM), void 0 === ENUM1 || _type_of(ENUM1); diff --git a/crates/swc/tests/tsc-references/typeofOperatorWithNumberType.2.minified.js b/crates/swc/tests/tsc-references/typeofOperatorWithNumberType.2.minified.js index 1d229c0825d..26774070160 100644 --- a/crates/swc/tests/tsc-references/typeofOperatorWithNumberType.2.minified.js +++ b/crates/swc/tests/tsc-references/typeofOperatorWithNumberType.2.minified.js @@ -30,11 +30,4 @@ void 0 === NUMBER || _type_of(NUMBER), _type_of(NUMBER1), _type_of(1), _type_of( y: function(n) { return n; } -}), _type_of(objA.a), _type_of(M.n), _type_of(NUMBER1[0]), _type_of(foo()), _type_of(A.foo()), _type_of(NUMBER + NUMBER), _type_of(void 0 === NUMBER ? "undefined" : _type_of(NUMBER)), _type_of(_type_of(_type_of(NUMBER + NUMBER))), _type_of(1), void 0 === NUMBER || _type_of(NUMBER), _type_of(NUMBER1), _type_of(foo()), _type_of(objA.a), _type_of(M.n), _type_of(objA.a), M.n; -z: void 0 === NUMBER || _type_of(NUMBER); -x: _type_of(NUMBER1); -r: _type_of(foo); -z: _type_of(1); -z: _type_of(objA.a); -z: _type_of(A.foo); -z: _type_of(M.n); +}), _type_of(objA.a), _type_of(M.n), _type_of(NUMBER1[0]), _type_of(foo()), _type_of(A.foo()), _type_of(NUMBER + NUMBER), _type_of(void 0 === NUMBER ? "undefined" : _type_of(NUMBER)), _type_of(_type_of(_type_of(NUMBER + NUMBER))), _type_of(1), void 0 === NUMBER || _type_of(NUMBER), _type_of(NUMBER1), _type_of(foo()), _type_of(objA.a), _type_of(M.n), _type_of(objA.a), M.n, void 0 === NUMBER || _type_of(NUMBER), _type_of(NUMBER1), _type_of(foo), _type_of(1), _type_of(objA.a), _type_of(A.foo), _type_of(M.n); diff --git a/crates/swc/tests/tsc-references/typeofOperatorWithStringType.2.minified.js b/crates/swc/tests/tsc-references/typeofOperatorWithStringType.2.minified.js index c8a45e47d72..b7f4718167b 100644 --- a/crates/swc/tests/tsc-references/typeofOperatorWithStringType.2.minified.js +++ b/crates/swc/tests/tsc-references/typeofOperatorWithStringType.2.minified.js @@ -30,11 +30,4 @@ void 0 === STRING || _type_of(STRING), _type_of(STRING1), _type_of(""), _type_of y: function(s) { return s; } -}), _type_of(objA.a), _type_of(M.n), _type_of(STRING1[0]), _type_of(foo()), _type_of(A.foo()), _type_of(STRING + STRING), _type_of(STRING.charAt(0)), _type_of(void 0 === STRING ? "undefined" : _type_of(STRING)), _type_of(_type_of(_type_of(STRING + STRING))), _type_of(""), void 0 === STRING || _type_of(STRING), _type_of(STRING1), _type_of(foo()), _type_of(objA.a), M.n; -z: void 0 === STRING || _type_of(STRING); -x: _type_of(STRING1); -r: _type_of(foo); -z: _type_of(""); -z: _type_of(objA.a); -z: _type_of(A.foo); -z: _type_of(M.n); +}), _type_of(objA.a), _type_of(M.n), _type_of(STRING1[0]), _type_of(foo()), _type_of(A.foo()), _type_of(STRING + STRING), _type_of(STRING.charAt(0)), _type_of(void 0 === STRING ? "undefined" : _type_of(STRING)), _type_of(_type_of(_type_of(STRING + STRING))), _type_of(""), void 0 === STRING || _type_of(STRING), _type_of(STRING1), _type_of(foo()), _type_of(objA.a), M.n, void 0 === STRING || _type_of(STRING), _type_of(STRING1), _type_of(foo), _type_of(""), _type_of(objA.a), _type_of(A.foo), _type_of(M.n); diff --git a/crates/swc/tests/tsc-references/whileBreakStatements.2.minified.js b/crates/swc/tests/tsc-references/whileBreakStatements.2.minified.js index e9f5ed585e3..6de76b6622e 100644 --- a/crates/swc/tests/tsc-references/whileBreakStatements.2.minified.js +++ b/crates/swc/tests/tsc-references/whileBreakStatements.2.minified.js @@ -1,4 +1,2 @@ //// [whileBreakStatements.ts] -FOUR: for(;;)FIVE: for(;;)break FOUR; for(;;); -for(;;)for(;;); diff --git a/crates/swc/tests/tsc-references/whileContinueStatements.2.minified.js b/crates/swc/tests/tsc-references/whileContinueStatements.2.minified.js index 8b66454e66a..213834aa72f 100644 --- a/crates/swc/tests/tsc-references/whileContinueStatements.2.minified.js +++ b/crates/swc/tests/tsc-references/whileContinueStatements.2.minified.js @@ -2,9 +2,9 @@ for(;;); for(;;); for(;;); -TWO: for(;;); -FOUR: for(;;)FIVE: for(;;)continue FOUR; -for(;;)SIX: for(;;)continue SIX; +for(;;); +FOUR: for(;;)for(;;)continue FOUR; +for(;;)for(;;)continue; SEVEN: for(;;)for(;;)for(;;)continue SEVEN; for(;;); for(;;); diff --git a/crates/swc_ecma_minifier/src/compress/optimize/loops.rs b/crates/swc_ecma_minifier/src/compress/optimize/loops.rs index a882a755e16..ae1feb8f50d 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/loops.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/loops.rs @@ -19,27 +19,12 @@ impl Optimizer<'_> { }; // We only care about instant breaks. - match &*f.body { - Stmt::Break(BreakStmt { label: None, .. }) => {} - Stmt::Break(BreakStmt { - label: Some(label), .. - }) => { - if let Some(closest_label) = self.label.clone() { - if closest_label.0 != label.sym { - return; - } - } else { - return; - } - } - _ => { - return; - } - } + let label = match &mut *f.body { + Stmt::Break(b) => b.label.take(), + _ => return, + }; self.changed = true; - // Remove the labeled statement. - self.label = None; report_change!("loops: Removing a for loop with instant break"); self.prepend_stmts .extend(f.init.take().map(|init| match init { @@ -55,6 +40,13 @@ impl Optimizer<'_> { expr, }) })); + if label.is_some() { + self.prepend_stmts.push(Stmt::Break(BreakStmt { + span: DUMMY_SP, + label, + })); + } + *s = Stmt::Empty(EmptyStmt { span: DUMMY_SP }) } diff --git a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs index 927fbc4ec7b..4aa1fb375c4 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs @@ -86,7 +86,6 @@ pub(super) fn optimizer<'a>( typeofs: Default::default(), data, ctx: Default::default(), - label: Default::default(), mode, debug_infinite_loop, functions: Default::default(), @@ -216,11 +215,6 @@ struct Optimizer<'a> { data: &'a mut ProgramData, ctx: Ctx, - /// Closest label. - /// - /// Setting this to `None` means the label should be removed. - label: Option, - mode: &'a dyn Mode, #[allow(unused)] @@ -2190,9 +2184,6 @@ impl VisitMut for Optimizer<'_> { #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] fn visit_mut_labeled_stmt(&mut self, n: &mut LabeledStmt) { - let old_label = self.label.take(); - self.label = Some(n.label.to_id()); - let ctx = Ctx { dont_use_prepend_nor_append: contains_leaping_continue_with_label( &n.body, @@ -2203,12 +2194,7 @@ impl VisitMut for Optimizer<'_> { n.visit_mut_children_with(&mut *self.with_ctx(ctx)); - if self.label.is_none() { - report_change!("Removing label `{}`", n.label); - n.label.take(); - } - - self.label = old_label; + self.try_remove_label(n); } #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] diff --git a/crates/swc_ecma_minifier/src/compress/optimize/util.rs b/crates/swc_ecma_minifier/src/compress/optimize/util.rs index d2f6170580a..3cb685d14ef 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/util.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/util.rs @@ -4,7 +4,7 @@ use std::{ }; use rustc_hash::{FxHashMap, FxHashSet}; -use swc_atoms::js_word; +use swc_atoms::{js_word, JsWord}; use swc_common::{util::take::Take, Span, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_transforms_base::perf::{Parallel, ParallelExt}; @@ -102,6 +102,39 @@ impl<'b> Optimizer<'b> { orig_ctx, } } + + pub(super) fn try_remove_label(&mut self, s: &mut LabeledStmt) { + if !self.options.dead_code { + return; + } + + let mut analyer = LabelAnalyzer { + label: s.label.sym.clone(), + ..Default::default() + }; + + match &mut *s.body { + Stmt::For(ForStmt { body, .. }) + | Stmt::ForIn(ForInStmt { body, .. }) + | Stmt::ForOf(ForOfStmt { body, .. }) + | Stmt::While(WhileStmt { body, .. }) + | Stmt::DoWhile(DoWhileStmt { body, .. }) => { + analyer.top_breakable = true; + body.visit_mut_with(&mut analyer) + } + Stmt::Switch(SwitchStmt { cases, .. }) => { + analyer.top_breakable = true; + cases.visit_mut_with(&mut analyer) + } + _ => s.body.visit_mut_with(&mut analyer), + }; + + if analyer.count == 0 { + let _label = s.label.take(); + self.changed = true; + report_change!("Removing label `{}`", _label); + } + } } pub(super) struct WithCtx<'a, 'b> { @@ -567,3 +600,87 @@ impl Drop for SynthesizedStmts { } } } + +#[derive(Default)] +struct LabelAnalyzer { + label: JsWord, + /// If top level is a normal block, labelled break must be preserved + top_breakable: bool, + count: usize, + break_layer: usize, + continue_layer: usize, +} + +impl LabelAnalyzer { + fn visit_mut_loop(&mut self, n: &mut impl VisitMutWith) { + self.break_layer += 1; + self.continue_layer += 1; + + n.visit_mut_children_with(self); + + self.break_layer -= 1; + self.continue_layer -= 1; + } +} + +impl VisitMut for LabelAnalyzer { + fn visit_mut_function(&mut self, _: &mut Function) {} + + fn visit_mut_class(&mut self, _: &mut Class) {} + + fn visit_mut_arrow_expr(&mut self, _: &mut ArrowExpr) {} + + fn visit_mut_object_lit(&mut self, _: &mut ObjectLit) {} + + fn visit_mut_for_stmt(&mut self, n: &mut ForStmt) { + self.visit_mut_loop(n) + } + + fn visit_mut_for_in_stmt(&mut self, n: &mut ForInStmt) { + self.visit_mut_loop(n) + } + + fn visit_mut_for_of_stmt(&mut self, n: &mut ForOfStmt) { + self.visit_mut_loop(n) + } + + fn visit_mut_while_stmt(&mut self, n: &mut WhileStmt) { + self.visit_mut_loop(n) + } + + fn visit_mut_do_while_stmt(&mut self, n: &mut DoWhileStmt) { + self.visit_mut_loop(n) + } + + fn visit_mut_switch_stmt(&mut self, n: &mut SwitchStmt) { + self.break_layer += 1; + + n.visit_mut_children_with(self); + + self.break_layer -= 1; + } + + fn visit_mut_break_stmt(&mut self, n: &mut BreakStmt) { + if let Some(lb) = &n.label { + if lb.sym == self.label { + if self.break_layer > 0 || !self.top_breakable { + self.count += 1; + } else { + n.label = None + } + } + } + } + + fn visit_mut_continue_stmt(&mut self, n: &mut ContinueStmt) { + if let Some(lb) = &n.label { + if lb.sym == self.label { + if self.continue_layer > 0 { + self.count += 1; + } else { + n.label = None + } + } + } + } +} diff --git a/crates/swc_ecma_minifier/tests/TODO.txt b/crates/swc_ecma_minifier/tests/TODO.txt index ee6a2301b95..882c73468a6 100644 --- a/crates/swc_ecma_minifier/tests/TODO.txt +++ b/crates/swc_ecma_minifier/tests/TODO.txt @@ -275,9 +275,6 @@ keep_names/keep_some_classnames/input.js keep_names/keep_some_fnames/input.js keep_names/keep_some_fnames_reduce/input.js keep_quoted_strict/keep_quoted_strict/input.js -labels/labels_1/input.js -labels/labels_2/input.js -labels/labels_4/input.js logical_assignment/assign_in_conditional_part/input.js logical_assignment/assignment_in_left_part_2/input.js loops/drop_if_else_break_1/input.js diff --git a/crates/swc_ecma_minifier/tests/benches-full/terser.js b/crates/swc_ecma_minifier/tests/benches-full/terser.js index 95c296c5e9f..ceacf04ce71 100644 --- a/crates/swc_ecma_minifier/tests/benches-full/terser.js +++ b/crates/swc_ecma_minifier/tests/benches-full/terser.js @@ -5092,8 +5092,7 @@ var ext = scope.enclosed, nth_identifier = options.nth_identifier; out: for(;;){ var m = nth_identifier.get(++scope.cname); - if (!(ALL_RESERVED_WORDS.has(m) || options.reserved.has(m))) { - if (unmangleable_names && unmangleable_names.has(m)) continue out; + if (!(ALL_RESERVED_WORDS.has(m) || options.reserved.has(m)) && !(unmangleable_names && unmangleable_names.has(m))) { for(let i = ext.length; --i >= 0;){ const def = ext[i], name = def.mangled_name || def.unmangleable(options) && def.name; if (m == name) continue out; diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/2257/full/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/2257/full/output.js index 3e87aa79301..af270ac0695 100644 --- a/crates/swc_ecma_minifier/tests/fixture/issues/2257/full/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/issues/2257/full/output.js @@ -11238,8 +11238,7 @@ if (null === c) return null; var d = Db(c); if (null === d) return null; - c = d[b]; - a: switch(b){ + switch(c = d[b], b){ case "onClick": case "onClickCapture": case "onDoubleClick": @@ -11252,7 +11251,7 @@ case "onMouseUpCapture": case "onMouseEnter": (d = !d.disabled) || (d = !("button" === (a = a.type) || "input" === a || "select" === a || "textarea" === a)), a = !d; - break a; + break; default: a = !1; } @@ -12298,7 +12297,7 @@ } } if (0 == (7 & b)) { - a: if (h = "mouseover" === a || "pointerover" === a, k = "mouseout" === a || "pointerout" === a, !(h && 0 == (16 & b) && (x = c.relatedTarget || c.fromElement) && (wc(x) || x[ff])) && (k || h) && (h = e.window === e ? e : (h = e.ownerDocument) ? h.defaultView || h.parentWindow : window, k ? (x = c.relatedTarget || c.toElement, k = d, null !== (x = x ? wc(x) : null) && (z = Zb(x), x !== z || 5 !== x.tag && 6 !== x.tag) && (x = null)) : (k = null, x = d), k !== x)) { + if (h = "mouseover" === a || "pointerover" === a, k = "mouseout" === a || "pointerout" === a, !(h && 0 == (16 & b) && (x = c.relatedTarget || c.fromElement) && (wc(x) || x[ff])) && (k || h) && (h = e.window === e ? e : (h = e.ownerDocument) ? h.defaultView || h.parentWindow : window, k ? (x = c.relatedTarget || c.toElement, k = d, null !== (x = x ? wc(x) : null) && (z = Zb(x), x !== z || 5 !== x.tag && 6 !== x.tag) && (x = null)) : (k = null, x = d), k !== x)) { if (w = Bd, v = "onMouseLeave", u = "onMouseEnter", t = "mouse", ("pointerout" === a || "pointerover" === a) && (w = Td, v = "onPointerLeave", u = "onPointerEnter", t = "pointer"), z = null == k ? h : ue(k), q = null == x ? h : ue(x), (h = new w(v, t + "leave", k, c, e)).target = z, h.relatedTarget = q, v = null, wc(e) === d && ((w = new w(u, t + "enter", x, c, e)).target = q, w.relatedTarget = z, v = w), z = v, k && x) b: { for(w = k, u = x, t = 0, q = w; q; q = gf(q))t++; for(q = 0, v = u; v; v = gf(v))q++; @@ -13781,7 +13780,7 @@ a.child.return = a, a = a.child; continue; } - if (a === b) break a; + if (a === b) break; for(; null === a.sibling;){ if (null === a.return || a.return === b) break a; a = a.return; @@ -14132,7 +14131,7 @@ if (5 === c.tag || 6 === c.tag) { a: for(var g = a, h = c, k = h;;)if (bj(g, k), null !== k.child && 4 !== k.tag) k.child.return = k, k = k.child; else { - if (k === h) break a; + if (k === h) break; for(; null === k.sibling;){ if (null === k.return || k.return === h) break a; k = k.return; @@ -17001,11 +17000,10 @@ } function H(a, b) { var c = a.length; - a.push(b); - a: for(;;){ + for(a.push(b);;){ var d = c - 1 >>> 1, e = a[d]; if (void 0 !== e && 0 < I(e, b)) a[d] = b, a[c] = e, c = d; - else break a; + else break; } } function J(a) { @@ -17017,11 +17015,11 @@ var c = a.pop(); if (c !== b) { a[0] = c; - a: for(var d = 0, e = a.length; d < e;){ + for(var d = 0, e = a.length; d < e;){ var m = 2 * (d + 1) - 1, n = a[m], v = m + 1, r = a[v]; if (void 0 !== n && 0 > I(n, c)) void 0 !== r && 0 > I(r, n) ? (a[d] = r, a[v] = c, d = v) : (a[d] = n, a[m] = c, d = m); else if (void 0 !== r && 0 > I(r, c)) a[d] = r, a[v] = c, d = v; - else break a; + else break; } } return b; diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/firebase-core/1/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/firebase-core/1/output.js index fdb52f35eb5..e8aff3dff95 100644 --- a/crates/swc_ecma_minifier/tests/fixture/issues/firebase-core/1/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/issues/firebase-core/1/output.js @@ -1204,7 +1204,7 @@ a.h = !1; try { const n = a.ba(); - a: switch(n){ + switch(n){ case 200: case 201: case 202: @@ -1213,7 +1213,7 @@ case 304: case 1223: var c, d, b = !0; - break a; + break; default: b = !1; } diff --git a/crates/swc_ecma_minifier/tests/fixture/next/react-pdf-renderer/output.js b/crates/swc_ecma_minifier/tests/fixture/next/react-pdf-renderer/output.js index e5e42461f92..39e359a2164 100644 --- a/crates/swc_ecma_minifier/tests/fixture/next/react-pdf-renderer/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/next/react-pdf-renderer/output.js @@ -4445,11 +4445,10 @@ } function S(e, t) { var r = e.length; - e.push(t); - e: for(;;){ + for(e.push(t);;){ var n = Math.floor((r - 1) / 2), i = e[n]; if (void 0 !== i && 0 < T(i, t)) e[n] = t, e[r] = i, r = n; - else break e; + else break; } } function A(e) { @@ -4461,11 +4460,11 @@ var r = e.pop(); if (r !== t) { e[0] = r; - e: for(var n = 0, i = e.length; n < i;){ + for(var n = 0, i = e.length; n < i;){ var o = 2 * (n + 1) - 1, a = e[o], u = o + 1, l = e[u]; if (void 0 !== a && 0 > T(a, r)) void 0 !== l && 0 > T(l, a) ? (e[n] = l, e[u] = r, n = u) : (e[n] = a, e[o] = r, n = o); else if (void 0 !== l && 0 > T(l, r)) e[n] = l, e[u] = r, n = u; - else break e; + else break; } } return t; @@ -9358,7 +9357,7 @@ function() { var t = ""; s = void 0; - t: for(; f <= e.length;){ + e: for(; f <= e.length;){ var r = e.charAt(f++), n = !!r && !/\s|[\!-\@\[-\`\{-\~\u2013-\u203C]/.test(r), u = "<" === r, d = ">" === r, p = r === i; do { if (1 === c) { @@ -9386,10 +9385,10 @@ break; case 3: t += r; - break t; + break e; case 4: f--; - break t; + break e; } } return t || void 0; @@ -18820,78 +18819,74 @@ e.exports = function(e, t) { var r, n, i, o, a, u, l, s, c, f, d, p, h, y, g, v, b, m, D, w, E, _, x, S, A; r = e.state, n = e.next_in, S = e.input, i = n + (e.avail_in - 5), o = e.next_out, A = e.output, a = o - (t - e.avail_out), u = o + (e.avail_out - 257), l = r.dmax, s = r.wsize, c = r.whave, f = r.wnext, d = r.window, p = r.hold, h = r.bits, y = r.lencode, g = r.distcode, v = (1 << r.lenbits) - 1, b = (1 << r.distbits) - 1; - r: do { - h < 15 && (p += S[n++] << h, h += 8, p += S[n++] << h, h += 8), m = y[p & v]; - n: for(;;){ - if (p >>>= D = m >>> 24, h -= D, 0 == (D = m >>> 16 & 255)) A[o++] = 65535 & m; - else if (16 & D) { - w = 65535 & m, (D &= 15) && (h < D && (p += S[n++] << h, h += 8), w += p & (1 << D) - 1, p >>>= D, h -= D), h < 15 && (p += S[n++] << h, h += 8, p += S[n++] << h, h += 8), m = g[p & b]; - i: for(;;){ - if (p >>>= D = m >>> 24, h -= D, 16 & (D = m >>> 16 & 255)) { - if (E = 65535 & m, h < (D &= 15) && (p += S[n++] << h, (h += 8) < D && (p += S[n++] << h, h += 8)), (E += p & (1 << D) - 1) > l) { - e.msg = "invalid distance too far back", r.mode = 30; - break r; + t: do for(h < 15 && (p += S[n++] << h, h += 8, p += S[n++] << h, h += 8), m = y[p & v];;){ + if (p >>>= D = m >>> 24, h -= D, 0 == (D = m >>> 16 & 255)) A[o++] = 65535 & m; + else if (16 & D) for(w = 65535 & m, (D &= 15) && (h < D && (p += S[n++] << h, h += 8), w += p & (1 << D) - 1, p >>>= D, h -= D), h < 15 && (p += S[n++] << h, h += 8, p += S[n++] << h, h += 8), m = g[p & b];;){ + if (p >>>= D = m >>> 24, h -= D, 16 & (D = m >>> 16 & 255)) { + if (E = 65535 & m, h < (D &= 15) && (p += S[n++] << h, (h += 8) < D && (p += S[n++] << h, h += 8)), (E += p & (1 << D) - 1) > l) { + e.msg = "invalid distance too far back", r.mode = 30; + break t; + } + if (p >>>= D, h -= D, E > (D = o - a)) { + if ((D = E - D) > c && r.sane) { + e.msg = "invalid distance too far back", r.mode = 30; + break t; + } + if (_ = 0, x = d, 0 === f) { + if (_ += s - D, D < w) { + w -= D; + do A[o++] = d[_++]; + while (--D) + _ = o - E, x = A; } - if (p >>>= D, h -= D, E > (D = o - a)) { - if ((D = E - D) > c && r.sane) { - e.msg = "invalid distance too far back", r.mode = 30; - break r; - } - if (_ = 0, x = d, 0 === f) { - if (_ += s - D, D < w) { - w -= D; - do A[o++] = d[_++]; - while (--D) - _ = o - E, x = A; - } - } else if (f < D) { - if (_ += s + f - D, (D -= f) < w) { - w -= D; - do A[o++] = d[_++]; - while (--D) - if (_ = 0, f < w) { - w -= D = f; - do A[o++] = d[_++]; - while (--D) - _ = o - E, x = A; - } - } - } else if (_ += f - D, D < w) { - w -= D; + } else if (f < D) { + if (_ += s + f - D, (D -= f) < w) { + w -= D; + do A[o++] = d[_++]; + while (--D) + if (_ = 0, f < w) { + w -= D = f; do A[o++] = d[_++]; while (--D) _ = o - E, x = A; } - for(; w > 2;)A[o++] = x[_++], A[o++] = x[_++], A[o++] = x[_++], w -= 3; - w && (A[o++] = x[_++], w > 1 && (A[o++] = x[_++])); - } else { - _ = o - E; - do A[o++] = A[_++], A[o++] = A[_++], A[o++] = A[_++], w -= 3; - while (w > 2) - w && (A[o++] = A[_++], w > 1 && (A[o++] = A[_++])); } - } else if ((64 & D) == 0) { - m = g[(65535 & m) + (p & (1 << D) - 1)]; - continue i; - } else { - e.msg = "invalid distance code", r.mode = 30; - break r; + } else if (_ += f - D, D < w) { + w -= D; + do A[o++] = d[_++]; + while (--D) + _ = o - E, x = A; } - break; + for(; w > 2;)A[o++] = x[_++], A[o++] = x[_++], A[o++] = x[_++], w -= 3; + w && (A[o++] = x[_++], w > 1 && (A[o++] = x[_++])); + } else { + _ = o - E; + do A[o++] = A[_++], A[o++] = A[_++], A[o++] = A[_++], w -= 3; + while (w > 2) + w && (A[o++] = A[_++], w > 1 && (A[o++] = A[_++])); } } else if ((64 & D) == 0) { - m = y[(65535 & m) + (p & (1 << D) - 1)]; - continue n; - } else if (32 & D) { - r.mode = 12; - break r; + m = g[(65535 & m) + (p & (1 << D) - 1)]; + continue; } else { - e.msg = "invalid literal/length code", r.mode = 30; - break r; + e.msg = "invalid distance code", r.mode = 30; + break t; } break; } - }while (n < i && o < u) + else if ((64 & D) == 0) { + m = y[(65535 & m) + (p & (1 << D) - 1)]; + continue; + } else if (32 & D) { + r.mode = 12; + break t; + } else { + e.msg = "invalid literal/length code", r.mode = 30; + break t; + } + break; + } + while (n < i && o < u) n -= w = h >> 3, h -= w << 3, p &= (1 << h) - 1, e.next_in = n, e.next_out = o, e.avail_in = n < i ? 5 + (i - n) : 5 - (n - i), e.avail_out = o < u ? 257 + (u - o) : 257 - (o - u), r.hold = p, r.bits = h; }; }, @@ -18950,14 +18945,14 @@ ]; if (!e || !e.state || !e.output || !e.input && 0 !== e.avail_in) return -2; 12 === (r = e.state).mode && (r.mode = 13), h = e.next_out, d = e.output, b = e.avail_out, p = e.next_in, f = e.input, y = e.avail_in, m = r.hold, D = r.bits, w = y, E = b, R = 0; - r: for(;;)switch(r.mode){ + t: for(;;)switch(r.mode){ case 1: if (0 === r.wrap) { r.mode = 13; break; } for(; D < 16;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } if (2 & r.wrap && 35615 === m) { @@ -18981,7 +18976,7 @@ break; case 2: for(; D < 16;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } if (r.flags = m, (255 & r.flags) != 8) { @@ -18995,50 +18990,50 @@ r.head && (r.head.text = m >> 8 & 1), 512 & r.flags && (M[0] = 255 & m, M[1] = m >>> 8 & 255, r.check = u(r.check, M, 2, 0)), m = 0, D = 0, r.mode = 3; case 3: for(; D < 32;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } r.head && (r.head.time = m), 512 & r.flags && (M[0] = 255 & m, M[1] = m >>> 8 & 255, M[2] = m >>> 16 & 255, M[3] = m >>> 24 & 255, r.check = u(r.check, M, 4, 0)), m = 0, D = 0, r.mode = 4; case 4: for(; D < 16;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } r.head && (r.head.xflags = 255 & m, r.head.os = m >> 8), 512 & r.flags && (M[0] = 255 & m, M[1] = m >>> 8 & 255, r.check = u(r.check, M, 2, 0)), m = 0, D = 0, r.mode = 5; case 5: if (1024 & r.flags) { for(; D < 16;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } r.length = m, r.head && (r.head.extra_len = m), 512 & r.flags && (M[0] = 255 & m, M[1] = m >>> 8 & 255, r.check = u(r.check, M, 2, 0)), m = 0, D = 0; } else r.head && (r.head.extra = null); r.mode = 6; case 6: - if (1024 & r.flags && ((_ = r.length) > y && (_ = y), _ && (r.head && (F = r.head.extra_len - r.length, r.head.extra || (r.head.extra = Array(r.head.extra_len)), o.arraySet(r.head.extra, f, p, _, F)), 512 & r.flags && (r.check = u(r.check, f, _, p)), y -= _, p += _, r.length -= _), r.length)) break r; + if (1024 & r.flags && ((_ = r.length) > y && (_ = y), _ && (r.head && (F = r.head.extra_len - r.length, r.head.extra || (r.head.extra = Array(r.head.extra_len)), o.arraySet(r.head.extra, f, p, _, F)), 512 & r.flags && (r.check = u(r.check, f, _, p)), y -= _, p += _, r.length -= _), r.length)) break t; r.length = 0, r.mode = 7; case 7: if (2048 & r.flags) { - if (0 === y) break r; + if (0 === y) break t; _ = 0; do F = f[p + _++], r.head && F && r.length < 65536 && (r.head.name += String.fromCharCode(F)); while (F && _ < y) - if (512 & r.flags && (r.check = u(r.check, f, _, p)), y -= _, p += _, F) break r; + if (512 & r.flags && (r.check = u(r.check, f, _, p)), y -= _, p += _, F) break t; } else r.head && (r.head.name = null); r.length = 0, r.mode = 8; case 8: if (4096 & r.flags) { - if (0 === y) break r; + if (0 === y) break t; _ = 0; do F = f[p + _++], r.head && F && r.length < 65536 && (r.head.comment += String.fromCharCode(F)); while (F && _ < y) - if (512 & r.flags && (r.check = u(r.check, f, _, p)), y -= _, p += _, F) break r; + if (512 & r.flags && (r.check = u(r.check, f, _, p)), y -= _, p += _, F) break t; } else r.head && (r.head.comment = null); r.mode = 9; case 9: if (512 & r.flags) { for(; D < 16;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } if (m !== (65535 & r.check)) { @@ -19051,7 +19046,7 @@ break; case 10: for(; D < 32;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } e.adler = r.check = c(m), m = 0, D = 0, r.mode = 11; @@ -19059,14 +19054,14 @@ if (0 === r.havedict) return e.next_out = h, e.avail_out = b, e.next_in = p, e.avail_in = y, r.hold = m, r.bits = D, 2; e.adler = r.check = 1, r.mode = 12; case 12: - if (5 === t || 6 === t) break r; + if (5 === t || 6 === t) break t; case 13: if (r.last) { m >>>= 7 & D, D -= 7 & D, r.mode = 27; break; } for(; D < 3;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } switch(r.last = 1 & m, D -= 1, 3 & (m >>>= 1)){ @@ -19091,7 +19086,7 @@ e.lencode = n, e.lenbits = 9, e.distcode = i, e.distbits = 5; }(r), r.mode = 20, 6 === t) { m >>>= 2, D -= 2; - break r; + break t; } break; case 2: @@ -19104,19 +19099,19 @@ break; case 14: for(m >>>= 7 & D, D -= 7 & D; D < 32;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } if ((65535 & m) != (m >>> 16 ^ 65535)) { e.msg = "invalid stored block lengths", r.mode = 30; break; } - if (r.length = 65535 & m, m = 0, D = 0, r.mode = 15, 6 === t) break r; + if (r.length = 65535 & m, m = 0, D = 0, r.mode = 15, 6 === t) break t; case 15: r.mode = 16; case 16: if (_ = r.length) { - if (_ > y && (_ = y), _ > b && (_ = b), 0 === _) break r; + if (_ > y && (_ = y), _ > b && (_ = b), 0 === _) break t; o.arraySet(d, f, p, _, h), y -= _, p += _, b -= _, h += _, r.length -= _; break; } @@ -19124,7 +19119,7 @@ break; case 17: for(; D < 14;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } if (r.nlen = (31 & m) + 257, m >>>= 5, D -= 5, r.ndist = (31 & m) + 1, m >>>= 5, D -= 5, r.ncode = (15 & m) + 4, m >>>= 4, D -= 4, r.nlen > 286 || r.ndist > 30) { @@ -19135,7 +19130,7 @@ case 18: for(; r.have < r.ncode;){ for(; D < 3;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } r.lens[N[r.have++]] = 7 & m, m >>>= 3, D -= 3; @@ -19151,14 +19146,14 @@ case 19: for(; r.have < r.nlen + r.ndist;){ for(; A = (B = r.lencode[m & (1 << r.lenbits) - 1]) >>> 24, k = B >>> 16 & 255, T = 65535 & B, !(A <= D);){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } if (T < 16) m >>>= A, D -= A, r.lens[r.have++] = T; else { if (16 === T) { for(j = A + 2; D < j;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } if (m >>>= A, D -= A, 0 === r.have) { @@ -19168,13 +19163,13 @@ F = r.lens[r.have - 1], _ = 3 + (3 & m), m >>>= 2, D -= 2; } else if (17 === T) { for(j = A + 3; D < j;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } m >>>= A, D -= A, F = 0, _ = 3 + (7 & m), m >>>= 3, D -= 3; } else { for(j = A + 7; D < j;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } m >>>= A, D -= A, F = 0, _ = 11 + (127 & m), m >>>= 7, D -= 7; @@ -19203,7 +19198,7 @@ e.msg = "invalid distances set", r.mode = 30; break; } - if (r.mode = 20, 6 === t) break r; + if (r.mode = 20, 6 === t) break t; case 20: r.mode = 21; case 21: @@ -19212,12 +19207,12 @@ break; } for(r.back = 0; A = (B = r.lencode[m & (1 << r.lenbits) - 1]) >>> 24, k = B >>> 16 & 255, T = 65535 & B, !(A <= D);){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } if (k && (240 & k) == 0) { for(O = A, C = k, P = T; A = (B = r.lencode[P + ((m & (1 << O + C) - 1) >> O)]) >>> 24, k = B >>> 16 & 255, T = 65535 & B, !(O + A <= D);){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } m >>>= O, D -= O, r.back += O; @@ -19238,7 +19233,7 @@ case 22: if (r.extra) { for(j = r.extra; D < j;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } r.length += m & (1 << r.extra) - 1, m >>>= r.extra, D -= r.extra, r.back += r.extra; @@ -19246,12 +19241,12 @@ r.was = r.length, r.mode = 23; case 23: for(; A = (B = r.distcode[m & (1 << r.distbits) - 1]) >>> 24, k = B >>> 16 & 255, T = 65535 & B, !(A <= D);){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } if ((240 & k) == 0) { for(O = A, C = k, P = T; A = (B = r.distcode[P + ((m & (1 << O + C) - 1) >> O)]) >>> 24, k = B >>> 16 & 255, T = 65535 & B, !(O + A <= D);){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } m >>>= O, D -= O, r.back += O; @@ -19264,7 +19259,7 @@ case 24: if (r.extra) { for(j = r.extra; D < j;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } r.offset += m & (1 << r.extra) - 1, m >>>= r.extra, D -= r.extra, r.back += r.extra; @@ -19275,7 +19270,7 @@ } r.mode = 25; case 25: - if (0 === b) break r; + if (0 === b) break t; if (_ = E - b, r.offset > _) { if ((_ = r.offset - _) > r.whave && r.sane) { e.msg = "invalid distance too far back", r.mode = 30; @@ -19289,13 +19284,13 @@ 0 === r.length && (r.mode = 21); break; case 26: - if (0 === b) break r; + if (0 === b) break t; d[h++] = r.length, b--, r.mode = 21; break; case 27: if (r.wrap) { for(; D < 32;){ - if (0 === y) break r; + if (0 === y) break t; y--, m |= f[p++] << D, D += 8; } if (E -= b, e.total_out += E, r.total += E, E && (e.adler = r.check = r.flags ? u(r.check, d, E, h - E) : a(r.check, d, E, h - E)), E = b, (r.flags ? m : c(m)) !== r.check) { @@ -19308,7 +19303,7 @@ case 28: if (r.wrap && r.flags) { for(; D < 32;){ - if (0 === y) break r; + if (0 === y) break t; y--, m += f[p++] << D, D += 8; } if (m !== (4294967295 & r.total)) { @@ -19320,10 +19315,10 @@ r.mode = 29; case 29: R = 1; - break r; + break t; case 30: R = -3; - break r; + break t; case 31: return -4; default: @@ -24381,7 +24376,7 @@ function eR(e) { var t = ""; do { - e: switch(e.tag){ + switch(e.tag){ case 3: case 4: case 6: @@ -24389,7 +24384,7 @@ case 10: case 9: var r = ""; - break e; + break; default: var n = e._debugOwner, i = e._debugSource, o = C(e.type); r = null, n && (r = C(n.type)), n = o, o = "", i ? o = " (at " + i.fileName.replace(eF, "") + ":" + i.lineNumber + ")" : r && (o = " (created by " + r + ")"), r = "\n in " + (n || "Unknown") + o; @@ -24824,12 +24819,12 @@ var m = "object" == typeof s && null !== s; if (m) switch(s.$$typeof){ case y: - e: { + r: { for(m = s.key, b = l; null !== b;){ if (b.key === m) { if (7 === b.tag ? s.type === v : b.elementType === s.type) { r(u, b.sibling), (l = i(b, s.type === v ? s.props.children : s.props, c)).ref = tX(u, b, s), l.return = u, u = l; - break e; + break r; } r(u, b); break; @@ -24840,12 +24835,12 @@ } return a(u); case g: - e: { + r: { for(b = s.key; null !== l;){ if (l.key === b) { if (4 === l.tag && l.stateNode.containerInfo === s.containerInfo && l.stateNode.implementation === s.implementation) { r(u, l.sibling), (l = i(l, s.children || [], c)).return = u, u = l; - break e; + break r; } r(u, l); break; @@ -25473,16 +25468,16 @@ var n = t.pendingProps, i = n.revealOrder, o = n.tail; if (rY(e, t, n.children, r), 0 != (2 & (n = t7.current))) n = 1 & n | 2, t.effectTag |= 64; else { - if (null !== e && 0 != (64 & e.effectTag)) e: for(e = t.child; null !== e;){ + if (null !== e && 0 != (64 & e.effectTag)) r: for(e = t.child; null !== e;){ if (13 === e.tag) null !== e.memoizedState && r6(e, r); else if (19 === e.tag) r6(e, r); else if (null !== e.child) { e.child.return = e, e = e.child; continue; } - if (e === t) break e; + if (e === t) break; for(; null === e.sibling;){ - if (null === e.return || e.return === t) break e; + if (null === e.return || e.return === t) break r; e = e.return; } e.sibling.return = e.return, e = e.sibling; @@ -25731,11 +25726,11 @@ } function nd(e) { if (K) { - e: { + r: { for(var t = e.return; null !== t;){ if (nf(t)) { var r = t; - break e; + break r; } t = t.return; } @@ -25753,21 +25748,21 @@ throw Error(d(161)); } 16 & r.effectTag && (es(t), r.effectTag &= -17); - e: o: for(r = e;;){ + r: n: for(r = e;;){ for(; null === r.sibling;){ if (null === r.return || nf(r.return)) { r = null; - break e; + break r; } r = r.return; } for(r.sibling.return = r.return, r = r.sibling; 5 !== r.tag && 6 !== r.tag && 18 !== r.tag;){ - if (2 & r.effectTag || null === r.child || 4 === r.tag) continue o; + if (2 & r.effectTag || null === r.child || 4 === r.tag) continue n; r.child.return = r, r = r.child; } if (!(2 & r.effectTag)) { r = r.stateNode; - break e; + break r; } } for(var i = e;;){ @@ -25790,16 +25785,16 @@ for(var n, i, o = t, a = !1;;){ if (!a) { a = o.return; - e: for(;;){ + r: for(;;){ if (null === a) throw Error(d(160)); switch(n = a.stateNode, a.tag){ case 5: i = !1; - break e; + break r; case 3: case 4: n = n.containerInfo, i = !0; - break e; + break r; } a = a.return; } @@ -25881,16 +25876,16 @@ case 3: $ && (r = t.stateNode).hydrate && (r.hydrate = !1, eC(r.containerInfo)); } - e: if (Q) switch(t.tag){ + r: if (Q) switch(t.tag){ case 1: case 5: case 6: case 20: - break e; + break r; case 3: case 4: eb((t = t.stateNode).containerInfo, t.pendingChildren); - break e; + break r; default: throw Error(d(163)); } @@ -25901,7 +25896,7 @@ if (null === e.memoizedState) var r = !1; else r = !0, t = e.child, nI = ti(); if (K && null !== t) { - e: if (e = t, K) for(t = e;;){ + r: if (e = t, K) for(t = e;;){ if (5 === t.tag) { var n = t.stateNode; r ? ec(n) : ed(t.stateNode, t.memoizedProps); @@ -25913,9 +25908,9 @@ t.child.return = t, t = t.child; continue; } - if (t === e) break e; + if (t === e) break r; for(; null === t.sibling;){ - if (null === t.return || t.return === e) break e; + if (null === t.return || t.return === e) break r; t = t.return; } t.sibling.return = t.return, t = t.sibling; @@ -26185,7 +26180,7 @@ for(;;){ try { if (tw(), rm(), null === nS || null === nS.return) return nk = 1, nT = t, null; - e: { + r: { var r = nS.return, n = nS, i = t; if (t = nA, n.effectTag |= 2048, n.firstEffect = n.lastEffect = null, null !== i && "object" == typeof i && "function" == typeof i.then) { var o, a = i, u = 0 != (1 & t7.current), l = r; @@ -26213,7 +26208,7 @@ } } n.expirationTime = 1073741823; - break e; + break r; } i = void 0, n = t; var h = e.pingCache; @@ -26223,7 +26218,7 @@ a.then(y, y); } l.effectTag |= 4096, l.expirationTime = t; - break e; + break r; } l = l.return; }while (null !== l) @@ -26236,7 +26231,7 @@ a = i, l.effectTag |= 4096, l.expirationTime = t; var g = nb(l, a, t); tR(l, g); - break e; + break r; case 1: a = i; var v = l.type, b = l.stateNode; @@ -26244,7 +26239,7 @@ l.effectTag |= 4096, l.expirationTime = t; var m = nm(l, a, t); tR(l, m); - break e; + break r; } } l = l.return; @@ -26277,7 +26272,7 @@ do { var t = nS.alternate; if (e = nS.return, 0 == (2048 & nS.effectTag)) { - e: { + r: { var r = t, n = nA, l = (t = nS).pendingProps; switch(t.tag){ case 2: @@ -26328,7 +26323,7 @@ case 13: if (eB(t7, t), l = t.memoizedState, 0 != (64 & t.effectTag)) { t.expirationTime = n; - break e; + break r; } l = null !== l, s = !1, null === r ? void 0 !== t.memoizedProps.fallback && rH(t) : (s = null !== (n = r.memoizedState), l || null === n || null !== (n = r.child.sibling) && (null !== (c = t.firstEffect) ? (t.firstEffect = n, n.nextEffect = c) : (t.firstEffect = t.lastEffect = n, n.nextEffect = null), n.effectTag = 8)), l && !s && 0 != (2 & t.mode) && (null === r && !0 !== t.memoizedProps.unstable_avoidThisFallback || 0 != (1 & t7.current) ? 0 === nk && (nk = 3) : ((0 === nk || 3 === nk) && (nk = 4), 0 !== nF && null !== nx && (iw(nx, nA), iE(nx, nF)))), Q && l && (t.effectTag |= 4), K && (l || s) && (t.effectTag |= 4); break; @@ -26350,7 +26345,7 @@ responders: n.responders }), l = l.sibling; eM(t7, 1 & t7.current | 2, t), t = t.child; - break e; + break r; } r = r.sibling; } @@ -26367,7 +26362,7 @@ } if (null !== l.tail) { 0 === l.tailExpiration && (l.tailExpiration = ti() + 500), r = l.tail, l.rendering = r, l.tail = r.sibling, l.lastEffect = t.lastEffect, r.sibling = null, l = t7.current, eM(t7, l = s ? 1 & l | 2 : 1 & l, t), t = r; - break e; + break r; } break; default: @@ -26768,13 +26763,13 @@ case 12: return rY(e, t, t.pendingProps.children, r), t.child; case 10: - e: { + r: { if (n = t.type._context, i = t.pendingProps, a = t.memoizedProps, o = i.value, tE(t, o), null !== a) { var u = a.value; if (0 == (o = tp(u, o) ? 0 : ("function" == typeof n._calculateChangedBits ? n._calculateChangedBits(u, o) : 1073741823) | 0)) { if (a.children === i.children && !eU.current) { t = ne(e, t, r); - break e; + break r; } } else for(null !== (u = t.child) && (u.return = t); null !== u;){ var l = u.dependencies; @@ -26841,7 +26836,7 @@ var a = 2; if (n = e, "function" == typeof e) ip(e) && (a = 1); else if ("string" == typeof e) a = 5; - else e: switch(e){ + else r: switch(e){ case v: return ig(r.children, i, o, t); case E: @@ -26860,19 +26855,19 @@ if ("object" == typeof e && null !== e) switch(e.$$typeof){ case D: a = 10; - break e; + break r; case w: a = 9; - break e; + break r; case _: a = 11; - break e; + break r; case A: a = 14; - break e; + break r; case k: a = 16, n = null; - break e; + break r; } throw Error(d(130, null == e ? e : typeof e, "")); } @@ -26932,20 +26927,20 @@ updateContainer: function(e, t, r, n) { var i = t.current, o = nH(), a = tL.suspense; o = nZ(o, i, a); - e: if (r) { + r: if (r) { r = r._reactInternalFiber; - o: { + n: { if (P(r) !== r || 1 !== r.tag) throw Error(d(170)); var u = r; do { switch(u.tag){ case 3: u = u.stateNode.context; - break o; + break n; case 1: if (eG(u.type)) { u = u.stateNode.__reactInternalMemoizedMergedChildContext; - break o; + break n; } } u = u.return; @@ -26956,7 +26951,7 @@ var l = r.type; if (eG(l)) { r = eZ(r, l, u); - break e; + break r; } } r = u; @@ -27202,11 +27197,10 @@ } function S(e, t) { var r = e.length; - e.push(t); - e: for(;;){ + for(e.push(t);;){ var n = Math.floor((r - 1) / 2), i = e[n]; if (void 0 !== i && 0 < T(i, t)) e[n] = t, e[r] = i, r = n; - else break e; + else break; } } function A(e) { @@ -27218,11 +27212,11 @@ var r = e.pop(); if (r !== t) { e[0] = r; - e: for(var n = 0, i = e.length; n < i;){ + for(var n = 0, i = e.length; n < i;){ var o = 2 * (n + 1) - 1, a = e[o], u = o + 1, l = e[u]; if (void 0 !== a && 0 > T(a, r)) void 0 !== l && 0 > T(l, a) ? (e[n] = l, e[u] = r, n = u) : (e[n] = a, e[o] = r, n = o); else if (void 0 !== l && 0 > T(l, r)) e[n] = l, e[u] = r, n = u; - else break e; + else break; } } return t; diff --git a/crates/swc_ecma_minifier/tests/fixture/next/regression-1/framework-798bab57daac3897/output.js b/crates/swc_ecma_minifier/tests/fixture/next/regression-1/framework-798bab57daac3897/output.js index 04e6017ebc4..959692dcee1 100644 --- a/crates/swc_ecma_minifier/tests/fixture/next/regression-1/framework-798bab57daac3897/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/next/regression-1/framework-798bab57daac3897/output.js @@ -554,8 +554,7 @@ if (null === c) return null; var d = Cb(c); if (null === d) return null; - c = d[b]; - a: switch(b){ + switch(c = d[b], b){ case "onClick": case "onClickCapture": case "onDoubleClick": @@ -568,7 +567,7 @@ case "onMouseUpCapture": case "onMouseEnter": (d = !d.disabled) || (d = !("button" === (a = a.type) || "input" === a || "select" === a || "textarea" === a)), a = !d; - break a; + break; default: a = !1; } @@ -1637,7 +1636,7 @@ } } if (0 == (7 & b)) { - a: if (h = "mouseover" === a || "pointerover" === a, k = "mouseout" === a || "pointerout" === a, !(h && c !== vb && (m = c.relatedTarget || c.fromElement) && (Vc(m) || m[tf])) && (k || h) && (h = e.window === e ? e : (h = e.ownerDocument) ? h.defaultView || h.parentWindow : window, k ? (m = c.relatedTarget || c.toElement, k = d, null !== (m = m ? Vc(m) : null) && (J = Ub(m), m !== J || 5 !== m.tag && 6 !== m.tag) && (m = null)) : (k = null, m = d), k !== m)) { + if (h = "mouseover" === a || "pointerover" === a, k = "mouseout" === a || "pointerout" === a, !(h && c !== vb && (m = c.relatedTarget || c.fromElement) && (Vc(m) || m[tf])) && (k || h) && (h = e.window === e ? e : (h = e.ownerDocument) ? h.defaultView || h.parentWindow : window, k ? (m = c.relatedTarget || c.toElement, k = d, null !== (m = m ? Vc(m) : null) && (J = Ub(m), m !== J || 5 !== m.tag && 6 !== m.tag) && (m = null)) : (k = null, m = d), k !== m)) { if (w = Ad, F = "onMouseLeave", v = "onMouseEnter", x = "mouse", ("pointerout" === a || "pointerover" === a) && (w = Sd, F = "onPointerLeave", v = "onPointerEnter", x = "pointer"), J = null == k ? h : te(k), r = null == m ? h : te(m), (h = new w(F, x + "leave", k, c, e)).target = J, h.relatedTarget = r, F = null, Vc(e) === d && ((w = new w(v, x + "enter", m, c, e)).target = r, w.relatedTarget = J, F = w), J = F, k && m) b: { for(w = k, v = m, x = 0, r = w; r; r = uf(r))x++; for(r = 0, F = v; F; F = uf(F))r++; @@ -3491,7 +3490,7 @@ a.child.return = a, a = a.child; continue; } - if (a === b) break a; + if (a === b) break; for(; null === a.sibling;){ if (null === a.return || a.return === b) break a; a = a.return; @@ -3798,7 +3797,7 @@ q.child.return = q, q = q.child; continue; } - if (q === a) break a; + if (q === a) break; for(; null === q.sibling;){ if (null === q.return || q.return === a) break a; u === q && (u = null), q = q.return; @@ -4946,7 +4945,7 @@ } } if (0 != (2064 & f.subtreeFlags) && null !== g) g.return = f, T = g; - else b: for(; null !== T;){ + else for(; null !== T;){ if (f = T, 0 != (2048 & f.flags)) switch(f.tag){ case 0: case 11: @@ -4956,7 +4955,7 @@ var v = f.sibling; if (null !== v) { v.return = f.return, T = v; - break b; + break; } T = f.return; } @@ -4965,7 +4964,7 @@ for(T = x; null !== T;){ var r = (g = T).child; if (0 != (2064 & g.subtreeFlags) && null !== r) r.return = g, T = r; - else b: for(g = x; null !== T;){ + else for(g = x; null !== T;){ if (h = T, 0 != (2048 & h.flags)) try { switch(h.tag){ case 0: @@ -4978,12 +4977,12 @@ } if (h === g) { T = null; - break b; + break; } var F = h.sibling; if (null !== F) { F.return = h.return, T = F; - break b; + break; } T = h.return; } @@ -5628,11 +5627,10 @@ 9670: function(__unused_webpack_module, exports) { function f(a, b) { var c = a.length; - a.push(b); - a: for(; 0 < c;){ + for(a.push(b); 0 < c;){ var d = c - 1 >>> 1, e = a[d]; if (0 < g(e, b)) a[d] = b, a[c] = e, c = d; - else break a; + else break; } } function h(a) { @@ -5643,11 +5641,11 @@ var b = a[0], c = a.pop(); if (c !== b) { a[0] = c; - a: for(var d = 0, e = a.length, w = e >>> 1; d < w;){ + for(var d = 0, e = a.length, w = e >>> 1; d < w;){ var m = 2 * (d + 1) - 1, C = a[m], n = m + 1, x = a[n]; if (0 > g(C, c)) n < e && 0 > g(x, C) ? (a[d] = x, a[n] = c, d = n) : (a[d] = C, a[m] = c, d = m); else if (n < e && 0 > g(x, c)) a[d] = x, a[n] = c, d = n; - else break a; + else break; } } return b; diff --git a/crates/swc_ecma_minifier/tests/passing.txt b/crates/swc_ecma_minifier/tests/passing.txt index bd5e69ccf69..ce2f8603386 100644 --- a/crates/swc_ecma_minifier/tests/passing.txt +++ b/crates/swc_ecma_minifier/tests/passing.txt @@ -998,13 +998,16 @@ keep_names/keep_classnames/input.js keep_names/keep_fnames/input.js keep_names/keep_fnames_and_avoid_collisions/input.js keep_names/keep_var_fnames/input.js -labels/labels_10/input.js +labels/labels_1/input.js +labels/labels_2/input.js labels/labels_3/input.js +labels/labels_4/input.js labels/labels_5/input.js labels/labels_6/input.js labels/labels_7/input.js labels/labels_8/input.js labels/labels_9/input.js +labels/labels_10/input.js logical_assignment/assign_in_conditional_part_reused/input.js logical_assignment/logical_assignment_not_always_happens/input.js logical_assignment/prematurely_evaluate_assignment/input.js diff --git a/crates/swc_ecma_minifier/tests/projects/output/mootools-1.4.5.js b/crates/swc_ecma_minifier/tests/projects/output/mootools-1.4.5.js index b19164ae98a..7b388bbe0ef 100644 --- a/crates/swc_ecma_minifier/tests/projects/output/mootools-1.4.5.js +++ b/crates/swc_ecma_minifier/tests/projects/output/mootools-1.4.5.js @@ -1496,7 +1496,7 @@ Event.Keys = {}, Event.Keys = new Hash(Event.Keys), function() { return; } } - getByTag: if ((children1 = node1.getElementsByTagName(tag1)) && children1.length) for(this.brokenStarGEBTN || (tag1 = null), i1 = 0; item1 = children1[i1++];)this.push(item1, tag1, id1, classes1, attributes1, pseudos1); + if ((children1 = node1.getElementsByTagName(tag1)) && children1.length) for(this.brokenStarGEBTN || (tag1 = null), i1 = 0; item1 = children1[i1++];)this.push(item1, tag1, id1, classes1, attributes1, pseudos1); }, ">": function(node1, tag1, id1, classes1, attributes1, pseudos1) { if (node1 = node1.firstChild) do 1 == node1.nodeType && this.push(node1, tag1, id1, classes1, attributes1, pseudos1); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/labels/labels_10/output.js b/crates/swc_ecma_minifier/tests/terser/compress/labels/labels_10/output.js index c308567ea49..e6f25cab0fc 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/labels/labels_10/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/labels/labels_10/output.js @@ -1,5 +1,5 @@ -out: while (foo) { +while(foo){ x(); y(); - break out; + break; } diff --git a/crates/swc_ecma_minifier/tests/terser/compress/labels/labels_5/output.js b/crates/swc_ecma_minifier/tests/terser/compress/labels/labels_5/output.js index 61e92a74949..46d8db80911 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/labels/labels_5/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/labels/labels_5/output.js @@ -1,8 +1,8 @@ -while (foo) { +while(foo){ if (bar) break; console.log("foo"); } -out: while (foo) { - if (bar) break out; +while(foo){ + if (bar) break; console.log("foo"); } diff --git a/crates/swc_html_minifier/tests/fixture/attribute/disabled-normalize_attributes/output.min.html b/crates/swc_html_minifier/tests/fixture/attribute/disabled-normalize_attributes/output.min.html index 6d7d554503b..ee6eb0b7683 100644 --- a/crates/swc_html_minifier/tests/fixture/attribute/disabled-normalize_attributes/output.min.html +++ b/crates/swc_html_minifier/tests/fixture/attribute/disabled-normalize_attributes/output.min.html @@ -1,2 +1,2 @@ Document -
\ No newline at end of file +
\ No newline at end of file