Skip to content

Commit

Permalink
fix(es/minifier): Fix usage counter to fix infinite loop (#6744)
Browse files Browse the repository at this point in the history
**Description:**

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

**Related issue:**

 - Closes #6729.
  • Loading branch information
kdy1 committed Jan 4, 2023
1 parent 62c0d72 commit 88d40e8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
19 changes: 19 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6729/input/.swcrc
@@ -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
}
13 changes: 13 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6729/input/index.js
@@ -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 })
}
}
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6729/output/index.js
@@ -0,0 +1,5 @@
export async function foo() {
undefined_var_1 && await a({
replace: undefined_var_2 ? 1 : 2
});
}
24 changes: 15 additions & 9 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Expand Up @@ -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;
Expand All @@ -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)]
Expand Down
22 changes: 22 additions & 0 deletions crates/swc_ecma_minifier/tests/exec.rs
Expand Up @@ -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')
"###,
)
}

0 comments on commit 88d40e8

Please sign in to comment.