fix(es/minifier): Fix usage counter to fix infinite loop (#6744)

**Description:**

We skip non-computed property names while checking if we can inline an expression.

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/6729.
This commit is contained in:
Donny/강동윤 2023-01-04 13:31:12 +09:00 committed by GitHub
parent 62c0d7291a
commit 88d40e8ab8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 9 deletions

View File

@ -0,0 +1,19 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true,
"tsx": false
},
"target": "es2020",
"loose": false,
"minify": {
"compress": true,
"mangle": false
}
},
"module": {
"type": "es6"
},
"isModule": true
}

View File

@ -0,0 +1,13 @@
export async function foo() {
if (undefined_var_1) {
let replace;
if (undefined_var_2) {
replace = 1;
} else {
replace = 2;
}
await a({ replace })
}
}

View File

@ -0,0 +1,5 @@
export async function foo() {
undefined_var_1 && await a({
replace: undefined_var_2 ? 1 : 2
});
}

View File

@ -2444,15 +2444,6 @@ impl Visit for UsageCounter<'_> {
}
}
fn visit_super_prop_expr(&mut self, e: &SuperPropExpr) {
if let SuperProp::Computed(c) = &e.prop {
let old = self.in_lhs;
self.in_lhs = false;
c.expr.visit_with(self);
self.in_lhs = old;
}
}
fn visit_pat(&mut self, p: &Pat) {
let old = self.in_lhs;
self.in_lhs = true;
@ -2466,6 +2457,21 @@ impl Visit for UsageCounter<'_> {
p.visit_children_with(self);
self.in_lhs = old;
}
fn visit_prop_name(&mut self, p: &PropName) {
if let PropName::Computed(p) = p {
p.visit_with(self)
}
}
fn visit_super_prop_expr(&mut self, e: &SuperPropExpr) {
if let SuperProp::Computed(c) = &e.prop {
let old = self.in_lhs;
self.in_lhs = false;
c.expr.visit_with(self);
self.in_lhs = old;
}
}
}
#[derive(Debug)]

View File

@ -10407,3 +10407,25 @@ fn issue_6641() {
"###,
)
}
#[test]
fn issue_6728() {
run_default_exec_test(
r###"
async function foo() {
if (undefined_var_1) {
let replace;
if (undefined_var_2) {
replace = 1;
} else {
replace = 2;
}
await a({ replace })
}
}
console.log('PASS')
"###,
)
}