fix(es/compat): Insert the variable declaration nearest to the available statements (#7067)

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/7064.
This commit is contained in:
magic-akari 2023-03-13 09:57:14 +08:00 committed by GitHub
parent 136c498c80
commit ae348e32e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 4 deletions

View File

@ -0,0 +1,12 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true
},
"target": "es2020"
},
"module": {
"type": "commonjs"
}
}

View File

@ -0,0 +1,7 @@
function method() {
const obj = {};
obj.prop ??= "x".repeat(10000000);
}
method();
// the large string should be eventually GCed along with the large string, but it will never happen in SWC+es2020

View File

@ -0,0 +1,7 @@
"use strict";
function method() {
var _obj;
const obj = {};
(_obj = obj).prop ?? (_obj.prop = "x".repeat(10000000));
}
method(); // the large string should be eventually GCed along with the large string, but it will never happen in SWC+es2020

View File

@ -174,12 +174,12 @@ impl VisitMut for Operators {
}
}
fn visit_mut_module(&mut self, n: &mut Module) {
fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
n.visit_mut_children_with(self);
if !self.vars.is_empty() {
prepend_stmt(
&mut n.body,
n,
VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
@ -191,12 +191,12 @@ impl VisitMut for Operators {
}
}
fn visit_mut_script(&mut self, n: &mut Script) {
fn visit_mut_stmts(&mut self, n: &mut Vec<Stmt>) {
n.visit_mut_children_with(self);
if !self.vars.is_empty() {
prepend_stmt(
&mut n.body,
n,
VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,