diff --git a/crates/swc/tests/fixture/issues-8xxx/8505/input/.swcrc b/crates/swc/tests/fixture/issues-8xxx/8505/input/.swcrc new file mode 100644 index 000000000000..70af276eca9c --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8505/input/.swcrc @@ -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 +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8505/input/index.ts b/crates/swc/tests/fixture/issues-8xxx/8505/input/index.ts new file mode 100644 index 000000000000..f4b4ca6c6658 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8505/input/index.ts @@ -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; + } + } +}; diff --git a/crates/swc/tests/fixture/issues-8xxx/8505/output/index.ts b/crates/swc/tests/fixture/issues-8xxx/8505/output/index.ts new file mode 100644 index 000000000000..4d4c8a86852f --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8505/output/index.ts @@ -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; + } + } + }); + } + }; +}); diff --git a/crates/swc_ecma_transforms_module/src/system_js.rs b/crates/swc_ecma_transforms_module/src/system_js.rs index bcd68c70ba57..2cbbeb8194c1 100644 --- a/crates/swc_ecma_transforms_module/src/system_js.rs +++ b/crates/swc_ecma_transforms_module/src/system_js.rs @@ -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>, tla: bool, enter_async_fn: u32, - is_global_this: bool, root_fn_decl_idents: Vec, module_item_meta_list: Vec, import_idents: Vec, @@ -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(&mut self, n: T) -> T - where - T: FoldWith, - { - 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 = vec![]; let mut execute_stmts = vec![];