Skip to content

Commit

Permalink
perf: upgrade to swc_ecma_parser 0.148 (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jul 29, 2024
1 parent 0aa7f51 commit e43c6c0
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 186 deletions.
244 changes: 147 additions & 97 deletions Cargo.lock

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ base64 = { version = "0.21.6", optional = true }
deno_media_type = "0.1.4"
deno_terminal = "0.1.1"

dprint-swc-ext = "0.17.0"
dprint-swc-ext = "0.18.0"
once_cell = "1.19.0"
percent-encoding = "2.3.1"
serde = { version = "1.0.144", features = ["derive"] }
Expand All @@ -50,31 +50,31 @@ unicode-width = "0.1"
#
# NOTE: You can automatically update these dependencies by running ./scripts/update_swc_deps.ts
swc_atoms = "=0.6.7"
swc_common = "=0.34.4"
swc_common = "=0.36.2"
swc_config = { version = "=0.1.14", optional = true }
swc_config_macro = { version = "=0.1.4", optional = true }
swc_ecma_ast = { version = "=0.115.1", features = ["serde-impl"] }
swc_ecma_codegen = { version = "=0.151.1", optional = true }
swc_ecma_codegen_macros = { version = "=0.7.6", optional = true }
swc_ecma_loader = { version = "=0.46.1", optional = true }
swc_ecma_parser = "=0.146.12"
swc_ecma_transforms_base = { version = "=0.140.3", optional = true }
swc_ecma_transforms_classes = { version = "=0.129.0", optional = true }
swc_ecma_transforms_compat = { version = "=0.166.1", optional = true }
swc_ecma_ast = { version = "=0.117.4", features = ["serde-impl"] }
swc_ecma_codegen = { version = "=0.154.3", optional = true }
swc_ecma_codegen_macros = { version = "=0.7.7", optional = true }
swc_ecma_loader = { version = "=0.48.1", optional = true }
swc_ecma_parser = "=0.148.1"
swc_ecma_transforms_base = { version = "=0.143.3", optional = true }
swc_ecma_transforms_classes = { version = "=0.132.1", optional = true }
swc_ecma_transforms_compat = { version = "=0.169.2", optional = true }
swc_ecma_transforms_macros = { version = "=0.5.5", optional = true }
swc_ecma_transforms_optimization = { version = "=0.201.2", optional = true }
swc_ecma_transforms_proposal = { version = "=0.174.3", optional = true }
swc_ecma_transforms_react = { version = "=0.186.2", optional = true }
swc_ecma_transforms_typescript = { version = "=0.191.2", optional = true }
swc_ecma_utils = { version = "=0.130.3", optional = true }
swc_ecma_visit = { version = "=0.101.0", optional = true }
swc_eq_ignore_macros = "=0.1.3"
swc_bundler = { version = "=0.230.2", optional = true }
swc_graph_analyzer = { version = "=0.23.0", optional = true }
swc_macros_common = { version = "=0.3.11", optional = true }
swc_ecma_transforms_optimization = { version = "=0.204.2", optional = true }
swc_ecma_transforms_proposal = { version = "=0.177.1", optional = true }
swc_ecma_transforms_react = { version = "=0.189.2", optional = true }
swc_ecma_transforms_typescript = { version = "=0.194.2", optional = true }
swc_ecma_utils = { version = "=0.133.3", optional = true }
swc_ecma_visit = { version = "=0.103.3", optional = true }
swc_eq_ignore_macros = "=0.1.4"
swc_bundler = { version = "=0.233.3", optional = true }
swc_graph_analyzer = { version = "=0.25.1", optional = true }
swc_macros_common = { version = "=0.3.13", optional = true }
swc_trace_macro = { version = "=0.1.3", optional = true }
swc_visit = { version = "=0.5.14", optional = true }
swc_visit_macros = { version = "=0.5.12", optional = true }
swc_visit = { version = "=0.6.1", optional = true }
swc_visit_macros = { version = "=0.5.13", optional = true }
thiserror = "1.0.58"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.77.2"
channel = "1.80.0"
components = ["clippy", "rustfmt"]
profile = "minimal"
6 changes: 3 additions & 3 deletions src/dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,15 @@ impl<'a> Visit for DependencyCollector<'a> {
}

fn visit_module_items(&mut self, items: &[ast::ModuleItem]) {
swc_ecma_visit::visit_module_items(self, items);
items.visit_children_with(self);
}

fn visit_stmts(&mut self, items: &[ast::Stmt]) {
swc_ecma_visit::visit_stmts(self, items);
items.visit_children_with(self)
}

fn visit_call_expr(&mut self, node: &ast::CallExpr) {
swc_ecma_visit::visit_call_expr(self, node);
node.visit_children_with(self);

if !matches!(&node.callee, Callee::Import(_)) {
return;
Expand Down
31 changes: 4 additions & 27 deletions src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,21 @@

use std::rc::Rc;

use crate::swc::common::FileName;
use crate::swc::common::SourceFile;

use crate::ModuleSpecifier;

pub trait IntoSwcFileName {
fn into_file_name(self) -> FileName;
}

impl IntoSwcFileName for ModuleSpecifier {
fn into_file_name(self) -> FileName {
FileName::Url(self)
}
}

impl IntoSwcFileName for String {
fn into_file_name(self) -> FileName {
FileName::Custom(self)
}
}

impl IntoSwcFileName for &str {
fn into_file_name(self) -> FileName {
FileName::Custom(self.to_owned())
}
}

#[derive(Clone, Default)]
pub struct SourceMap {
inner: Rc<crate::swc::common::SourceMap>,
}

impl SourceMap {
pub fn single(file_name: impl IntoSwcFileName, source: String) -> Self {
pub fn single(specifier: ModuleSpecifier, source: String) -> Self {
let map = Self::default();
map
.inner
.new_source_file(file_name.into_file_name(), source);
.new_source_file(Rc::new(swc_common::FileName::Url(specifier)), source);
map
}

Expand All @@ -49,11 +26,11 @@ impl SourceMap {

pub fn new_source_file(
&self,
file_name: impl IntoSwcFileName,
specifier: ModuleSpecifier,
source: String,
) -> Rc<SourceFile> {
self
.inner
.new_source_file(file_name.into_file_name(), source)
.new_source_file(Rc::new(swc_common::FileName::Url(specifier)), source)
}
}
43 changes: 25 additions & 18 deletions src/transpiling/jsx_precompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::swc::atoms::Atom;
use crate::swc::parser::lexer::util::CharExt;
use swc_common::SyntaxContext;
use swc_common::DUMMY_SP;
use swc_ecma_ast::*;
use swc_ecma_utils::prepend_stmt;
Expand Down Expand Up @@ -558,7 +559,7 @@ impl JsxPrecompile {
match &self.import_jsx {
Some(ident) => ident.clone(),
None => {
let ident = Ident::new("_jsx".into(), DUMMY_SP);
let ident = new_ident("_jsx".into());
self.import_jsx = Some(ident.clone());
ident
}
Expand All @@ -570,7 +571,7 @@ impl JsxPrecompile {
match &self.import_jsx_ssr {
Some(ident) => ident.clone(),
None => {
let ident = Ident::new("_jsxTemplate".into(), DUMMY_SP);
let ident = new_ident("_jsxTemplate".into());
self.import_jsx_ssr = Some(ident.clone());
ident
}
Expand All @@ -582,7 +583,7 @@ impl JsxPrecompile {
match &self.import_jsx_attr {
Some(ident) => ident.clone(),
None => {
let ident = Ident::new("_jsxAttr".into(), DUMMY_SP);
let ident = new_ident("_jsxAttr".into());
self.import_jsx_attr = Some(ident.clone());
ident
}
Expand All @@ -594,7 +595,7 @@ impl JsxPrecompile {
match &self.import_jsx_escape {
Some(ident) => ident.clone(),
None => {
let ident = Ident::new("_jsxEscape".into(), DUMMY_SP);
let ident = new_ident("_jsxEscape".into());
self.import_jsx_escape = Some(ident.clone());
ident
}
Expand All @@ -609,6 +610,7 @@ impl JsxPrecompile {

Expr::Call(CallExpr {
span: DUMMY_SP,
ctxt: Default::default(),
callee: Callee::Expr(Box::new(Expr::Ident(
self.get_jsx_escape_fn_identifier(),
))),
Expand Down Expand Up @@ -970,6 +972,7 @@ impl JsxPrecompile {

CallExpr {
span: DUMMY_SP,
ctxt: Default::default(),
callee: Callee::Expr(Box::new(Expr::Ident(self.get_jsx_identifier()))),
args,
type_args: None,
Expand All @@ -990,6 +993,7 @@ impl JsxPrecompile {

CallExpr {
span: DUMMY_SP,
ctxt: Default::default(),
callee: Callee::Expr(Box::new(Expr::Ident(
self.get_jsx_attr_identifier(),
))),
Expand Down Expand Up @@ -1332,7 +1336,7 @@ impl JsxPrecompile {
Vec::with_capacity(1 + dynamic_exprs.len());
args.push(ExprOrSpread {
spread: None,
expr: Box::new(Expr::Ident(Ident::new(name.into(), DUMMY_SP))),
expr: Box::new(Expr::Ident(new_ident(name.into()))),
});
for dynamic_expr in dynamic_exprs.into_iter() {
args.push(ExprOrSpread {
Expand All @@ -1346,6 +1350,7 @@ impl JsxPrecompile {

Expr::Call(CallExpr {
span: DUMMY_SP,
ctxt: Default::default(),
callee: Callee::Expr(Box::new(Expr::Ident(jsx_ident))),
args,
type_args: Default::default(),
Expand Down Expand Up @@ -1377,25 +1382,19 @@ impl JsxPrecompile {
let mut imports: Vec<(Ident, Ident)> = vec![];

if let Some(jsx_ident) = &self.import_jsx {
imports.push((jsx_ident.clone(), Ident::new("jsx".into(), DUMMY_SP)))
imports.push((jsx_ident.clone(), new_ident("jsx".into())))
}

if let Some(jsx_ssr_ident) = &self.import_jsx_ssr {
imports.push((
jsx_ssr_ident.clone(),
Ident::new("jsxTemplate".into(), DUMMY_SP),
))
imports.push((jsx_ssr_ident.clone(), new_ident("jsxTemplate".into())))
}

if let Some(jsx_attr_ident) = &self.import_jsx_attr {
imports.push((
jsx_attr_ident.clone(),
Ident::new("jsxAttr".into(), DUMMY_SP),
))
imports.push((jsx_attr_ident.clone(), new_ident("jsxAttr".into())))
}

if let Some(espace_ident) = self.import_jsx_escape.take() {
imports.push((espace_ident, Ident::new("jsxEscape".into(), DUMMY_SP)))
imports.push((espace_ident, new_ident("jsxEscape".into())))
}

if !imports.is_empty() {
Expand Down Expand Up @@ -1472,12 +1471,13 @@ impl VisitMut for JsxPrecompile {
non_mod_stmt_idx,
ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(VarDecl {
span: DUMMY_SP,
ctxt: SyntaxContext::default(),
kind: VarDeclKind::Const,
declare: false,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: Pat::Ident(BindingIdent {
id: Ident::new(create_tpl_binding_name(*idx).into(), DUMMY_SP),
id: new_ident(create_tpl_binding_name(*idx).into()),
type_ann: None,
}),
init: Some(Box::new(Expr::Array(ArrayLit {
Expand Down Expand Up @@ -1571,6 +1571,10 @@ impl VisitMut for JsxPrecompile {
}
}

fn new_ident(name: Atom) -> Ident {
Ident::new(name, DUMMY_SP, SyntaxContext::default())
}

#[cfg(test)]
mod tests {
use crate::swc::ast::Module;
Expand All @@ -1580,6 +1584,7 @@ mod tests {
use crate::swc::parser::TsSyntax;
use crate::swc::visit::FoldWith;
use crate::EmitOptions;
use crate::ModuleSpecifier;
use crate::SourceMap;
use pretty_assertions::assert_eq;
use swc_common::comments::SingleThreadedComments;
Expand Down Expand Up @@ -2782,8 +2787,10 @@ const a = _jsxTemplate($$_tpl_1, _jsxAttr("class", "foo"), _jsxAttr("className",

fn parse(src: &str) -> (SourceMap, Module) {
let source_map = SourceMap::default();
let source_file =
source_map.new_source_file("file:///test.ts", src.to_string());
let source_file = source_map.new_source_file(
ModuleSpecifier::parse("file:///test.ts").unwrap(),
src.to_string(),
);
let input = StringInput::from(&*source_file);
let syntax = Syntax::Typescript(TsSyntax {
tsx: true,
Expand Down
7 changes: 6 additions & 1 deletion src/transpiling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,11 @@ pub fn fold_program(
options.var_decl_imports
),
Optional::new(
typescript::typescript(options.as_typescript_config(), marks.top_level),
typescript::typescript(
options.as_typescript_config(),
marks.unresolved,
marks.top_level
),
!options.transform_jsx
),
Optional::new(
Expand All @@ -422,6 +426,7 @@ pub fn fold_program(
options.as_typescript_config(),
options.as_tsx_config(),
comments,
marks.unresolved,
marks.top_level,
),
options.transform_jsx
Expand Down
Loading

0 comments on commit e43c6c0

Please sign in to comment.