Skip to content

Commit 0f94c8c

Browse files
authoredJan 16, 2024
fix(es/systemjs): Handle top level this (#8506)
**Related issue:** - Closes #8505
1 parent 64036d3 commit 0f94c8c

File tree

4 files changed

+64
-30
lines changed

4 files changed

+64
-30
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "typescript",
5+
"tsx": false
6+
},
7+
"target": "es2022",
8+
"transform": {
9+
"react": {
10+
"runtime": "automatic"
11+
}
12+
},
13+
"loose": false,
14+
"minify": {
15+
"compress": false,
16+
"mangle": false
17+
}
18+
},
19+
"module": {
20+
"type": "systemjs"
21+
},
22+
"minify": false,
23+
"isModule": true
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default () => {
2+
class Rectangle {
3+
height: number = 0;
4+
constructor(height, width) {
5+
this.height = height;
6+
this.width = width;
7+
}
8+
incrementHeight() {
9+
this.height = this.height + 1;
10+
}
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
System.register([], function(_export, _context) {
2+
"use strict";
3+
return {
4+
setters: [],
5+
execute: function() {
6+
_export("default", ()=>{
7+
class Rectangle {
8+
height = 0;
9+
constructor(height, width){
10+
this.height = height;
11+
this.width = width;
12+
}
13+
incrementHeight() {
14+
this.height = this.height + 1;
15+
}
16+
}
17+
});
18+
}
19+
};
20+
});

‎crates/swc_ecma_transforms_module/src/system_js.rs

+8-30
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use swc_ecma_visit::{noop_fold_type, Fold, FoldWith, VisitWith};
1010

1111
use crate::{
1212
path::{ImportResolver, Resolver},
13+
top_level_this::top_level_this,
1314
util::{local_name_for_src, use_strict},
1415
};
1516
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
@@ -33,7 +34,6 @@ struct SystemJs {
3334
export_values: Vec<Box<Expr>>,
3435
tla: bool,
3536
enter_async_fn: u32,
36-
is_global_this: bool,
3737
root_fn_decl_idents: Vec<Ident>,
3838
module_item_meta_list: Vec<ModuleItemMeta>,
3939
import_idents: Vec<Id>,
@@ -51,7 +51,6 @@ pub fn system_js(unresolved_mark: Mark, config: Config) -> impl Fold {
5151
export_map: Default::default(),
5252
export_names: vec![],
5353
export_values: vec![],
54-
is_global_this: true,
5554
tla: false,
5655
enter_async_fn: 0,
5756
root_fn_decl_idents: vec![],
@@ -72,7 +71,6 @@ pub fn system_js_with_resolver(
7271
unresolved_mark,
7372
resolver: Resolver::Real { base, resolver },
7473
config,
75-
is_global_this: true,
7674
declare_var_idents: vec![],
7775
export_map: Default::default(),
7876
export_names: vec![],
@@ -96,19 +94,6 @@ struct ModuleItemMeta {
9694
}
9795

9896
impl SystemJs {
99-
fn fold_children_with_non_global_this<T>(&mut self, n: T) -> T
100-
where
101-
T: FoldWith<Self>,
102-
{
103-
let is_global_this = self.is_global_this;
104-
105-
self.is_global_this = false;
106-
let node = n.fold_children_with(self);
107-
self.is_global_this = is_global_this;
108-
109-
node
110-
}
111-
11297
fn export_call(&self, name: JsWord, span: Span, expr: Expr) -> CallExpr {
11398
CallExpr {
11499
span,
@@ -609,12 +594,6 @@ impl Fold for SystemJs {
609594

610595
Expr::Await(await_expr)
611596
}
612-
Expr::This(this_expr) => {
613-
if !self.config.allow_top_level_this && self.is_global_this {
614-
return *undefined(DUMMY_SP);
615-
}
616-
Expr::This(this_expr)
617-
}
618597
_ => expr,
619598
}
620599
}
@@ -631,14 +610,6 @@ impl Fold for SystemJs {
631610
fold_fn_expr
632611
}
633612

634-
fn fold_class_expr(&mut self, n: ClassExpr) -> ClassExpr {
635-
self.fold_children_with_non_global_this(n)
636-
}
637-
638-
fn fold_function(&mut self, n: Function) -> Function {
639-
self.fold_children_with_non_global_this(n)
640-
}
641-
642613
fn fold_prop(&mut self, prop: Prop) -> Prop {
643614
let prop = prop.fold_children_with(self);
644615

@@ -659,6 +630,13 @@ impl Fold for SystemJs {
659630
}
660631

661632
fn fold_module(&mut self, module: Module) -> Module {
633+
let module = {
634+
let mut module = module;
635+
if !self.config.allow_top_level_this {
636+
top_level_this(&mut module, *undefined(DUMMY_SP));
637+
}
638+
module
639+
};
662640
let mut before_body_stmts: Vec<Stmt> = vec![];
663641
let mut execute_stmts = vec![];
664642

0 commit comments

Comments
 (0)
Please sign in to comment.