mirror of
https://github.com/swc-project/swc.git
synced 2024-12-24 22:22:34 +03:00
fix(es/minifier): Fix if_return
bug related to await
and yield
(#8328)
**Related issue:** - Closes #8324
This commit is contained in:
parent
0004ce3f47
commit
01e2c7fc5a
65
crates/swc/tests/fixture/issues-8xxx/8324/input/.swcrc
Normal file
65
crates/swc/tests/fixture/issues-8xxx/8324/input/.swcrc
Normal file
@ -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
crates/swc/tests/fixture/issues-8xxx/8324/input/1.js
Normal file
24
crates/swc/tests/fixture/issues-8xxx/8324/input/1.js
Normal file
@ -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
crates/swc/tests/fixture/issues-8xxx/8324/output/1.js
Normal file
19
crates/swc/tests/fixture/issues-8xxx/8324/output/1.js
Normal file
@ -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;
|
||||
}
|
@ -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<'_> {
|
||||
@ -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) => {
|
||||
|
@ -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>,
|
||||
@ -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) {}
|
||||
@ -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
|
||||
|
@ -0,0 +1,5 @@
|
||||
{
|
||||
"defaults": false,
|
||||
"if_return": true,
|
||||
"passes": 3
|
||||
}
|
24
crates/swc_ecma_minifier/tests/fixture/issues/8324/input.js
Normal file
24
crates/swc_ecma_minifier/tests/fixture/issues/8324/input.js
Normal file
@ -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
crates/swc_ecma_minifier/tests/fixture/issues/8324/output.js
Normal file
22
crates/swc_ecma_minifier/tests/fixture/issues/8324/output.js
Normal file
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user