Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/module): Skip Script transformations for CJS and AMD #7661

Merged
merged 3 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7650/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"jsc": {
"target": "es2020",
"parser": {
"syntax": "typescript"
}
},
"isModule": "unknown",
"module": {
"type": "commonjs"
}
}
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7650/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const a = require('foo')
console.log(a)
import('other')
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7650/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const a = require('foo');
console.log(a);
import('other');
18 changes: 11 additions & 7 deletions crates/swc_ecma_transforms_module/src/amd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ where
{
noop_visit_mut_type!();

fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
if let Some(first) = n.first() {
fn visit_mut_module(&mut self, n: &mut Module) {
if let Some(first) = n.body.first() {
if self.module_id.is_none() {
self.module_id = self.get_amd_module_id_from_comments(first.span());
}
Expand All @@ -139,13 +139,13 @@ where
let import_interop = self.config.import_interop();

let mut strip = ModuleDeclStrip::new(self.const_var_kind);
n.visit_mut_with(&mut strip);
n.body.visit_mut_with(&mut strip);

let mut stmts: Vec<Stmt> = Vec::with_capacity(n.len() + 4);
let mut stmts: Vec<Stmt> = Vec::with_capacity(n.body.len() + 4);

// "use strict";
if self.config.strict_mode {
stmts.push(clone_first_use_directive(n, true).unwrap_or_else(use_strict));
stmts.push(clone_first_use_directive(&n.body, true).unwrap_or_else(use_strict));
}

let ModuleDeclStrip {
Expand All @@ -169,7 +169,7 @@ where
.map(From::from),
);

stmts.extend(n.take().into_iter().filter_map(|item| match item {
stmts.extend(n.body.take().into_iter().filter_map(|item| match item {
ModuleItem::Stmt(stmt) if !stmt.is_directive() => Some(stmt),
_ => None,
}));
Expand Down Expand Up @@ -256,14 +256,18 @@ where
.as_arg(),
);

*n = vec![
n.body = vec![
quote_ident!(DUMMY_SP.apply_mark(self.unresolved_mark), "define")
.as_call(DUMMY_SP, amd_call_args)
.into_stmt()
.into(),
];
}

fn visit_mut_script(&mut self, _: &mut Script) {
// skip script
}

fn visit_mut_expr(&mut self, n: &mut Expr) {
match n {
Expr::Call(CallExpr {
Expand Down
20 changes: 12 additions & 8 deletions crates/swc_ecma_transforms_module/src/common_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ where
{
noop_visit_mut_type!();

fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
fn visit_mut_module(&mut self, n: &mut Module) {
let import_interop = self.config.import_interop();

let mut module_map = Default::default();

let mut has_ts_import_equals = false;

// handle `import foo = require("mod")`
n.iter_mut().for_each(|item| {
n.body.iter_mut().for_each(|item| {
if let ModuleItem::ModuleDecl(module_decl) = item {
*item = self.handle_ts_import_equals(
module_decl.take(),
Expand All @@ -109,16 +109,16 @@ where
});

let mut strip = ModuleDeclStrip::new(self.const_var_kind);
n.visit_mut_with(&mut strip);
n.body.visit_mut_with(&mut strip);

let mut stmts: Vec<ModuleItem> = Vec::with_capacity(n.len() + 6);
let mut stmts: Vec<ModuleItem> = Vec::with_capacity(n.body.len() + 6);

stmts.extend(clone_first_use_directive(n, false).map(From::from));
stmts.extend(clone_first_use_directive(&n.body, false).map(From::from));

// "use strict";
if self.config.strict_mode {
stmts.push(
clone_first_use_directive(n, true)
clone_first_use_directive(&n.body, true)
.unwrap_or_else(use_strict)
.into(),
);
Expand Down Expand Up @@ -155,7 +155,7 @@ where
.map(From::from),
);

stmts.extend(n.take().into_iter().filter(|item| match item {
stmts.extend(n.body.take().into_iter().filter(|item| match item {
ModuleItem::Stmt(stmt) => !stmt.is_directive(),
_ => false,
}));
Expand Down Expand Up @@ -184,7 +184,11 @@ where
self.config.allow_top_level_this,
));

*n = stmts;
n.body = stmts;
}

fn visit_mut_script(&mut self, _: &mut Script) {
// skip script
}

fn visit_mut_expr(&mut self, n: &mut Expr) {
Expand Down
Loading