Skip to content

Commit

Permalink
fix(es/minifier): Fix if_return bug related to await and yield (#…
Browse files Browse the repository at this point in the history
…8328)

**Related issue:**

 - Closes #8324
  • Loading branch information
kdy1 authored Nov 23, 2023
1 parent 0004ce3 commit 01e2c7f
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 1 deletion.
65 changes: 65 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8324/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2022",
"loose": false,
"minify": {
"compress": {
"arguments": false,
"arrows": false,
"booleans": false,
"booleans_as_integers": false,
"collapse_vars": false,
"comparisons": false,
"computed_props": false,
"conditionals": false,
"dead_code": false,
"directives": false,
"drop_console": false,
"drop_debugger": false,
"evaluate": false,
"expression": false,
"hoist_funs": false,
"hoist_props": false,
"hoist_vars": false,
"if_return": true,
"join_vars": false,
"keep_classnames": false,
"keep_fargs": false,
"keep_fnames": false,
"keep_infinity": false,
"loops": false,
"negate_iife": false,
"properties": false,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": false,
"switches": false,
"typeofs": false,
"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": false,
"const_to_let": false,
"pristine_globals": false,
"passes": 3
},
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true
}
24 changes: 24 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8324/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function Deferred() {
const deferred = this;
deferred.promise = new Promise(function (resolve, reject) {
deferred.resolve = resolve;
deferred.reject = reject;
});
}

export async function bug() {
const s = `next`;
if (!window[s]) {
for (window[s] = new Deferred(); ;)
if (window.current) await window.current.promise;
else {
window.current = window[s];
try {
return await window[s].promise // This line compressed to 'break'. I guess compressor intended jump to 23 line which is looks like same code.
} finally {
delete window.current // Above 'break' makes unintended delete
}
}
}
return await window[s].promise
}
19 changes: 19 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8324/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function Deferred() {
const deferred = this;
deferred.promise = new Promise(function(resolve, reject) {
deferred.resolve = resolve, deferred.reject = reject;
});
}
export async function bug() {
const s = "next";
if (!window[s]) for(window[s] = new Deferred();;)if (window.current) await window.current.promise;
else {
window.current = window[s];
try {
return await window[s].promise;
} finally{
delete window.current;
}
}
return await window[s].promise;
}
10 changes: 9 additions & 1 deletion crates/swc_ecma_minifier/src/compress/pure/dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use swc_ecma_utils::{extract_var_ids, ExprExt, StmtExt, StmtLike, Value};
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};

use super::Pure;
use crate::{compress::util::is_fine_for_if_cons, maybe_par, util::ModuleItemExt};
use crate::{
compress::util::is_fine_for_if_cons,
maybe_par,
util::{contains_await_or_yield, ModuleItemExt},
};

/// Methods related to option `dead_code`.
impl Pure<'_> {
Expand Down Expand Up @@ -285,6 +289,10 @@ impl Pure<'_> {
_ => return,
};

if contains_await_or_yield(last) {
return;
}

fn drop<T: StmtLike>(stmt: &mut T, last: &Stmt, need_break: bool) -> bool {
match stmt.as_stmt_mut() {
Some(s) if s.eq_ignore_span(last) => {
Expand Down
17 changes: 17 additions & 0 deletions crates/swc_ecma_minifier/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ where

#[derive(Default)]
pub(crate) struct LeapFinder {
found_await: bool,
found_yield: bool,
found_continue_with_label: bool,
target_label: Option<Id>,
Expand All @@ -219,6 +220,12 @@ pub(crate) struct LeapFinder {
impl Visit for LeapFinder {
noop_visit_type!();

fn visit_await_expr(&mut self, n: &AwaitExpr) {
n.visit_children_with(self);

self.found_await = true;
}

fn visit_arrow_expr(&mut self, _: &ArrowExpr) {}

fn visit_class_method(&mut self, _: &ClassMethod) {}
Expand Down Expand Up @@ -249,6 +256,16 @@ impl Visit for LeapFinder {
}
}

#[allow(unused)]
pub(crate) fn contains_await_or_yield<N>(n: &N) -> bool
where
N: VisitWith<LeapFinder>,
{
let mut v = LeapFinder::default();
n.visit_with(&mut v);
v.found_yield || v.found_await
}

/// This method returns true only if `T` is `var`. (Not `const` or `let`)
pub(crate) fn is_hoisted_var_decl_without_init<T>(t: &T) -> bool
where
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"defaults": false,
"if_return": true,
"passes": 3
}
24 changes: 24 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8324/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function Deferred() {
const deferred = this;
deferred.promise = new Promise(function (resolve, reject) {
deferred.resolve = resolve;
deferred.reject = reject;
});
}

export async function bug() {
const s = `next`;
if (!window[s]) {
for (window[s] = new Deferred(); ;)
if (window.current) await window.current.promise;
else {
window.current = window[s];
try {
return await window[s].promise // This line compressed to 'break'. I guess compressor intended jump to 23 line which is looks like same code.
} finally {
delete window.current // Above 'break' makes unintended delete
}
}
}
return await window[s].promise
}
22 changes: 22 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8324/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function Deferred() {
const deferred = this;
deferred.promise = new Promise(function(resolve, reject) {
deferred.resolve = resolve;
deferred.reject = reject;
});
}
export async function bug() {
const s = "next";
if (!window[s]) {
for(window[s] = new Deferred();;)if (window.current) await window.current.promise;
else {
window.current = window[s];
try {
return await window[s].promise;
} finally{
delete window.current;
}
}
}
return await window[s].promise;
}

0 comments on commit 01e2c7f

Please sign in to comment.