Skip to content
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
9 changes: 9 additions & 0 deletions .changeset/gentle-mangos-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
swc_compiler_base: patch
swc_config: patch
swc_ecma_lexer: patch
swc_ecma_parser: patch
swc_core: patch
---

feat(es/parser): Support parsing CommonJS
4 changes: 2 additions & 2 deletions bindings/binding_core_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export function minify(src: string, opts?: JsMinifyOptions): Promise<Output>;
export function minifySync(code: string, opts?: JsMinifyOptions): Output;

export function parse(src: string, options: ParseOptions & {
isModule: false;
isModule: false | "commonjs";
}): Promise<Script>;
export function parse(src: string, options?: ParseOptions): Promise<Module>;
export function parseSync(src: string, options: ParseOptions & {
isModule: false;
isModule: false | "commonjs";
}): Script;
export function parseSync(src: string, options?: ParseOptions): Module;

Expand Down
2 changes: 1 addition & 1 deletion bindings/binding_core_wasm/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ export interface Options extends Config {

plugin?: Plugin;

isModule?: boolean | 'unknown';
isModule?: boolean | 'unknown' | 'commonjs';

/**
* Destination path. Note that this value is used only to fix source path
Expand Down
9 changes: 8 additions & 1 deletion crates/swc_compiler_base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use swc_config::{file_pattern::FilePattern, is_module::IsModule, types::BoolOr};
use swc_ecma_ast::{EsVersion, Ident, IdentName, Program};
use swc_ecma_codegen::{text_writer::WriteJs, Emitter, Node};
use swc_ecma_minifier::js::JsMinifyCommentOption;
use swc_ecma_parser::{parse_file_as_module, parse_file_as_program, parse_file_as_script, Syntax};
use swc_ecma_parser::{
parse_file_as_commonjs, parse_file_as_module, parse_file_as_program, parse_file_as_script,
Syntax,
};
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
use swc_timer::timer;

Expand Down Expand Up @@ -79,6 +82,10 @@ pub fn parse_js(
parse_file_as_script(&fm, syntax, target, comments, &mut errors)
.map(Program::Script)
}
IsModule::CommonJS => {
parse_file_as_commonjs(&fm, syntax, target, comments, &mut errors)
.map(Program::Script)
}
IsModule::Unknown => parse_file_as_program(&fm, syntax, target, comments, &mut errors),
};

Expand Down
3 changes: 3 additions & 0 deletions crates/swc_config/src/is_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::merge::Merge;
pub enum IsModule {
Bool(bool),
Unknown,
CommonJS,
}

impl Default for IsModule {
Expand All @@ -35,6 +36,7 @@ impl Serialize for IsModule {
match *self {
IsModule::Bool(ref b) => b.serialize(serializer),
IsModule::Unknown => "unknown".serialize(serializer),
IsModule::CommonJS => "commonjs".serialize(serializer),
}
}
}
Expand All @@ -61,6 +63,7 @@ impl Visitor<'_> for IsModuleVisitor {
{
match s {
"unknown" => Ok(IsModule::Unknown),
"commonjs" => Ok(IsModule::CommonJS),
_ => Err(serde::de::Error::invalid_value(Unexpected::Str(s), &self)),
}
}
Expand Down
19 changes: 19 additions & 0 deletions crates/swc_ecma_lexer/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ impl<I: Tokens<TokenAndSpan>> Parser<I> {
})
}

pub fn parse_commonjs(&mut self) -> PResult<Script> {
trace_cur!(self, parse_commonjs);

// CommonJS module is acctually in a function scope
let ctx = (self.ctx() & !Context::Module)
| Context::InFunction
| Context::InsideNonArrowFunctionScope;
self.set_ctx(ctx);

let start = cur_pos!(self);
let shebang = parse_shebang(self)?;

parse_stmt_block_body(self, true, None).map(|body| Script {
span: span!(self, start),
body,
shebang,
})
}

#[cfg(test)]
pub fn parse_typescript_module(&mut self) -> PResult<Module> {
trace_cur!(self, parse_typescript_module);
Expand Down
93 changes: 93 additions & 0 deletions crates/swc_ecma_parser/examples/parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::{ffi::OsStr, path::Path};

use swc_common::{
errors::{ColorConfig, Handler},
sync::Lrc,
SourceMap,
};
use swc_ecma_ast::Program;
use swc_ecma_lexer::{EsSyntax, TsSyntax};
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax};

fn main() {
let cm: Lrc<SourceMap> = Default::default();
let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(cm.clone()));

// read file path from command line argument or use a default
let file_path = std::env::args()
.nth(1)
.unwrap_or_else(|| "test.js".to_string());

let file_path = Path::new(&file_path);

let ext = file_path.extension().map(OsStr::to_ascii_lowercase);
let is_ts = ext.as_ref().is_some_and(|e| {
e == "ts" || e == "tsx" || e == "mts" || e == "cts" || e == "mtsx" || e == "ctsx"
});
let is_d_ts = is_ts
&& file_path
.file_name()
.and_then(OsStr::to_str)
.is_some_and(|s| {
let mut iter = s.rsplit('.');

if iter.next() != Some("ts") {
return false;
}

iter.next() == Some("d") || iter.next() == Some("d")
});

let is_jsx = ext
.as_ref()
.is_some_and(|e| e == "jsx" || e == "tsx" || e == "mjsx" || e == "cjsx");
let is_esm = ext
.as_ref()
.is_some_and(|e| e == "mjs" || e == "mts" || e == "mjsx" || e == "mtsx");
let is_commonjs = ext
.as_ref()
.is_some_and(|e| e == "cjs" || e == "cts" || e == "cjsx" || e == "ctsx");

let fm = cm.load_file(file_path).expect("failed to load test.ts");

let syntax = if is_ts {
Syntax::Typescript(TsSyntax {
tsx: is_jsx,
dts: is_d_ts,
..Default::default()
})
} else {
Syntax::Es(EsSyntax {
jsx: is_jsx,
..Default::default()
})
};

let lexer = Lexer::new(syntax, Default::default(), StringInput::from(&*fm), None);

let mut parser = Parser::new_from(lexer);

let program = if is_esm {
parser
.parse_module()
.map(Program::Module)
.map_err(|e| e.into_diagnostic(&handler).emit())
} else if is_commonjs {
parser
.parse_commonjs()
.map(Program::Script)
.map_err(|e| e.into_diagnostic(&handler).emit())
} else {
parser
.parse_program()
.map_err(|e| e.into_diagnostic(&handler).emit())
};

for e in parser.take_errors() {
e.into_diagnostic(&handler).emit();
}

let program = program.expect("Failed to parse program.");

eprintln!("Parsed program:\n {program:#?}");
}
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,5 @@ expose!(parse_file_as_expr, Box<Expr>, |p| {
});
expose!(parse_file_as_module, Module, |p| { p.parse_module() });
expose!(parse_file_as_script, Script, |p| { p.parse_script() });
expose!(parse_file_as_commonjs, Script, |p| { p.parse_commonjs() });
expose!(parse_file_as_program, Program, |p| { p.parse_program() });
19 changes: 19 additions & 0 deletions crates/swc_ecma_parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,25 @@ impl<I: Tokens> Parser<I> {
})
}

pub fn parse_commonjs(&mut self) -> PResult<Script> {
trace_cur!(self, parse_commonjs);

// CommonJS module is acctually in a function scope
let ctx = (self.ctx() & !Context::Module)
| Context::InFunction
| Context::InsideNonArrowFunctionScope;
self.set_ctx(ctx);

let start = cur_pos!(self);
let shebang = parse_shebang(self)?;

parse_stmt_block_body(self, true, None).map(|body| Script {
span: span!(self, start),
body,
shebang,
})
}

pub fn parse_typescript_module(&mut self) -> PResult<Module> {
trace_cur!(self, parse_typescript_module);

Expand Down
11 changes: 10 additions & 1 deletion crates/swc_ecma_parser/tests/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::common::Normalizer;
mod common;

#[testing::fixture("tests/js/**/*.js")]
#[testing::fixture("tests/js/**/*.cjs")]
fn spec(file: PathBuf) {
let output = file.parent().unwrap().join(format!(
"{}.json",
Expand All @@ -28,6 +29,8 @@ fn spec(file: PathBuf) {
}

fn run_spec(file: &Path, output_json: &Path, config_path: &Path) {
let is_commonjs = file.extension().map(|ext| ext == "cjs").unwrap_or_default();

let file_name = file
.display()
.to_string()
Expand All @@ -49,7 +52,13 @@ fn run_spec(file: &Path, output_json: &Path, config_path: &Path) {
}

with_parser(false, file, false, config_path, |p, _| {
let program = p.parse_program()?.fold_with(&mut Normalizer {
let program = if is_commonjs {
p.parse_commonjs().map(Program::Script)?
} else {
p.parse_program()?
};

let program = program.fold_with(&mut Normalizer {
drop_span: false,
is_test262: false,
});
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_parser/tests/js/issue-10597/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"syntax": "ecmascript",
"explicitResourceManagement": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new.target;
25 changes: 25 additions & 0 deletions crates/swc_ecma_parser/tests/js/issue-10597/new_target.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"type": "Script",
"span": {
"start": 1,
"end": 12
},
"body": [
{
"type": "ExpressionStatement",
"span": {
"start": 1,
"end": 12
},
"expression": {
"type": "MetaProperty",
"span": {
"start": 1,
"end": 11
},
"kind": "new.target"
}
}
],
"interpreter": null
}
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/tests/js/issue-10597/return.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return 0;
26 changes: 26 additions & 0 deletions crates/swc_ecma_parser/tests/js/issue-10597/return.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"type": "Script",
"span": {
"start": 1,
"end": 10
},
"body": [
{
"type": "ReturnStatement",
"span": {
"start": 1,
"end": 10
},
"argument": {
"type": "NumericLiteral",
"span": {
"start": 8,
"end": 9
},
"value": 0.0,
"raw": "0"
}
}
],
"interpreter": null
}
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/tests/js/issue-10597/using.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
using foo = null;
46 changes: 46 additions & 0 deletions crates/swc_ecma_parser/tests/js/issue-10597/using.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"type": "Script",
"span": {
"start": 1,
"end": 18
},
"body": [
{
"type": "UsingDeclaration",
"span": {
"start": 1,
"end": 18
},
"isAwait": false,
"decls": [
{
"type": "VariableDeclarator",
"span": {
"start": 7,
"end": 17
},
"id": {
"type": "Identifier",
"span": {
"start": 7,
"end": 10
},
"ctxt": 0,
"value": "foo",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "NullLiteral",
"span": {
"start": 13,
"end": 17
}
},
"definite": false
}
]
}
],
"interpreter": null
}
2 changes: 1 addition & 1 deletion packages/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ export interface Options extends Config {

plugin?: Plugin;

isModule?: boolean | "unknown";
isModule?: boolean | "unknown" | "commonjs";

/**
* Destination path. Note that this value is used only to fix source path
Expand Down
Loading