fix(es/minifier): Abort IIFE invoker in function parameters (#8828)

**Related issue:**

 - Closes #8826
This commit is contained in:
Donny/강동윤 2024-04-09 15:22:38 +09:00 committed by GitHub
parent 7fcfc74d15
commit ebb68db24d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 92 additions and 4 deletions

View File

@ -9,12 +9,15 @@ then
fi
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
TARGET_DIR="$(cargo metadata --format-version 1 --no-deps | jq -r '.target_directory')"
echo "Reducing $1"
ls -al $1
# Build swc minifier
export MINIFY=$(cargo profile bin-path --release --example compress)
cargo build --release --example compress
export MINIFY="$TARGET_DIR/release/examples/compress"
wd="$(mktemp -d)"
echo "Using $MINIFY; dir = $wd"

View File

@ -8,6 +8,6 @@ export SWC_RUN=0
touch tests/compress.rs
cargo test -q -p swc_ecma_minifier -p swc --no-fail-fast --test projects --test tsc --test compress --test mangle --features concurrent $@
cargo test -p swc_ecma_minifier -p swc --no-fail-fast --test projects --test tsc --test compress --test mangle --features concurrent $@
# find ../swc/tests/ -type f -empty -delete

View File

@ -480,6 +480,11 @@ impl Optimizer<'_> {
return;
}
if self.ctx.in_param && !f.params.is_empty() {
log_abort!("iife: We don't invoke IIFE with params in function params");
return;
}
if !self.may_add_ident() {
match &*f.body {
BlockStmtOrExpr::BlockStmt(body) => {
@ -595,6 +600,11 @@ impl Optimizer<'_> {
return;
}
if self.ctx.in_param && !f.function.params.is_empty() {
log_abort!("iife: We don't invoke IIFE with params in function params");
return;
}
// Abort if a parameter is complex
if f.function.params.iter().any(|param| {
matches!(

View File

@ -172,6 +172,8 @@ struct Ctx {
in_with_stmt: bool,
in_param: bool,
/// Current scope.
scope: SyntaxContext,
}
@ -1459,9 +1461,16 @@ impl VisitMut for Optimizer<'_> {
let prepend = self.prepend_stmts.take();
let ctx = self.ctx;
{
let ctx = Ctx {
in_param: true,
..self.ctx
};
n.visit_mut_children_with(&mut *self.with_ctx(ctx));
n.params.visit_mut_with(&mut *self.with_ctx(ctx));
}
n.body.visit_mut_with(self);
if !self.prepend_stmts.is_empty() {
let mut stmts = self.prepend_stmts.take().take_stmts();
@ -1561,6 +1570,7 @@ impl VisitMut for Optimizer<'_> {
top_level: false,
in_block: true,
scope: n.span.ctxt,
in_param: false,
..self.ctx
};
n.visit_mut_children_with(&mut *self.with_ctx(ctx));
@ -2360,6 +2370,7 @@ impl VisitMut for Optimizer<'_> {
fn visit_mut_param(&mut self, n: &mut Param) {
let ctx = Ctx {
var_kind: None,
in_param: true,
..self.ctx
};
let mut o = self.with_ctx(ctx);

View File

@ -0,0 +1,47 @@
{
"defaults": true,
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"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": true,
"const_to_let": true,
"pristine_globals": true
}

View File

@ -0,0 +1,12 @@
export function createTypeChecker(host) {
return {
getFlowTypeOfReference
}
function getFlowTypeOfReference(reference, declaredType, initialType = declaredType, flowContainer, flowNode = (_a2 => (_a2 = tryCast(reference, canHaveFlowNode)) == null ? void 0 : _a2.flowNode)()) {
}
}

View File

@ -0,0 +1,5 @@
export function createTypeChecker(host) {
return {
getFlowTypeOfReference: function(reference, declaredType, initialType = declaredType, flowContainer, flowNode = ((_a2)=>null == (_a2 = tryCast(reference, canHaveFlowNode)) ? void 0 : _a2.flowNode)()) {}
};
}