Skip to content

Commit

Permalink
fix(es/systemjs): Handle top level this (#8506)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8505
  • Loading branch information
magic-akari authored Jan 16, 2024
1 parent 64036d3 commit 0f94c8c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 30 deletions.
24 changes: 24 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8505/input/.swcrc
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8505/input/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
};
20 changes: 20 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8505/output/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
});
}
};
});
38 changes: 8 additions & 30 deletions crates/swc_ecma_transforms_module/src/system_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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>,
Expand All @@ -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![],
Expand All @@ -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![],
Expand All @@ -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,
Expand Down Expand Up @@ -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,
}
}
Expand All @@ -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);

Expand All @@ -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![];

Expand Down

0 comments on commit 0f94c8c

Please sign in to comment.