fix(es/systemjs): Handle top level this (#8506)

**Related issue:**

 - Closes #8505
This commit is contained in:
magic-akari 2024-01-16 12:00:08 +08:00 committed by GitHub
parent 64036d3302
commit 0f94c8cf05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 30 deletions

View File

@ -0,0 +1,24 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false
},
"target": "es2022",
"transform": {
"react": {
"runtime": "automatic"
}
},
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "systemjs"
},
"minify": false,
"isModule": true
}

View File

@ -0,0 +1,12 @@
export default () => {
class Rectangle {
height: number = 0;
constructor(height, width) {
this.height = height;
this.width = width;
}
incrementHeight() {
this.height = this.height + 1;
}
}
};

View File

@ -0,0 +1,20 @@
System.register([], function(_export, _context) {
"use strict";
return {
setters: [],
execute: function() {
_export("default", ()=>{
class Rectangle {
height = 0;
constructor(height, width){
this.height = height;
this.width = width;
}
incrementHeight() {
this.height = this.height + 1;
}
}
});
}
};
});

View File

@ -10,6 +10,7 @@ use swc_ecma_visit::{noop_fold_type, Fold, FoldWith, VisitWith};
use crate::{
path::{ImportResolver, Resolver},
top_level_this::top_level_this,
util::{local_name_for_src, use_strict},
};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
@ -33,7 +34,6 @@ struct SystemJs {
export_values: Vec<Box<Expr>>,
tla: bool,
enter_async_fn: u32,
is_global_this: bool,
root_fn_decl_idents: Vec<Ident>,
module_item_meta_list: Vec<ModuleItemMeta>,
import_idents: Vec<Id>,
@ -51,7 +51,6 @@ pub fn system_js(unresolved_mark: Mark, config: Config) -> impl Fold {
export_map: Default::default(),
export_names: vec![],
export_values: vec![],
is_global_this: true,
tla: false,
enter_async_fn: 0,
root_fn_decl_idents: vec![],
@ -72,7 +71,6 @@ pub fn system_js_with_resolver(
unresolved_mark,
resolver: Resolver::Real { base, resolver },
config,
is_global_this: true,
declare_var_idents: vec![],
export_map: Default::default(),
export_names: vec![],
@ -96,19 +94,6 @@ struct ModuleItemMeta {
}
impl SystemJs {
fn fold_children_with_non_global_this<T>(&mut self, n: T) -> T
where
T: FoldWith<Self>,
{
let is_global_this = self.is_global_this;
self.is_global_this = false;
let node = n.fold_children_with(self);
self.is_global_this = is_global_this;
node
}
fn export_call(&self, name: JsWord, span: Span, expr: Expr) -> CallExpr {
CallExpr {
span,
@ -609,12 +594,6 @@ impl Fold for SystemJs {
Expr::Await(await_expr)
}
Expr::This(this_expr) => {
if !self.config.allow_top_level_this && self.is_global_this {
return *undefined(DUMMY_SP);
}
Expr::This(this_expr)
}
_ => expr,
}
}
@ -631,14 +610,6 @@ impl Fold for SystemJs {
fold_fn_expr
}
fn fold_class_expr(&mut self, n: ClassExpr) -> ClassExpr {
self.fold_children_with_non_global_this(n)
}
fn fold_function(&mut self, n: Function) -> Function {
self.fold_children_with_non_global_this(n)
}
fn fold_prop(&mut self, prop: Prop) -> Prop {
let prop = prop.fold_children_with(self);
@ -659,6 +630,13 @@ impl Fold for SystemJs {
}
fn fold_module(&mut self, module: Module) -> Module {
let module = {
let mut module = module;
if !self.config.allow_top_level_this {
top_level_this(&mut module, *undefined(DUMMY_SP));
}
module
};
let mut before_body_stmts: Vec<Stmt> = vec![];
let mut execute_stmts = vec![];