Skip to content

Commit 178d6e6

Browse files
authored
feat: swc_ecma_parser 27 (#326)
1 parent 216d546 commit 178d6e6

File tree

12 files changed

+217
-191
lines changed

12 files changed

+217
-191
lines changed

Cargo.lock

Lines changed: 118 additions & 91 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ deno_media_type = "0.3.0"
3838
deno_terminal = "0.2.2"
3939
deno_error = "0.7.0"
4040

41-
dprint-swc-ext = "0.25.1"
41+
dprint-swc-ext = "0.26.0"
4242
percent-encoding = "2.3.1"
4343
serde = { version = "1.0.219", features = ["derive"] }
4444
text_lines = { version = "0.6.0", features = ["serialization"] }
@@ -50,32 +50,32 @@ unicode-width = "0.2.0"
5050
# pulling in new versions of swc crates
5151
#
5252
# NOTE: You can automatically update these dependencies by running ./scripts/update_swc_deps.ts
53-
swc_atoms = "=7.0.0"
54-
swc_common = "=14.0.4"
53+
swc_atoms = "=9.0.0"
54+
swc_common = "=17.0.1"
5555
swc_config = { version = "=3.1.2", optional = true }
5656
swc_config_macro = { version = "=1.0.1", optional = true }
57-
swc_ecma_ast = { version = "=15.0.0", features = ["serde-impl"] }
58-
swc_ecma_codegen = { version = "=17.0.2", optional = true }
57+
swc_ecma_ast = { version = "=18.0.0", features = ["serde-impl"] }
58+
swc_ecma_codegen = { version = "=20.0.2", optional = true }
5959
swc_ecma_codegen_macros = { version = "=2.0.2", optional = true }
60-
swc_ecma_loader = { version = "=14.0.0", optional = true }
61-
swc_ecma_lexer = "=23.0.2"
62-
swc_ecma_parser = "=24.0.3"
63-
swc_ecma_transforms_base = { version = "=27.0.0", features = ["inline-helpers"], optional = true }
64-
swc_ecma_transforms_classes = { version = "=27.0.0", optional = true }
65-
swc_ecma_transforms_compat = { version = "=31.0.0", optional = true }
60+
swc_ecma_loader = { version = "=17.0.0", optional = true }
61+
swc_ecma_lexer = "=26.0.0"
62+
swc_ecma_parser = "=27.0.7"
63+
swc_ecma_transforms_base = { version = "=30.0.1", features = ["inline-helpers"], optional = true }
64+
swc_ecma_transforms_classes = { version = "=30.0.0", optional = true }
65+
swc_ecma_transforms_compat = { version = "=35.0.0", optional = true }
6666
swc_ecma_transforms_macros = { version = "=1.0.1", optional = true }
67-
swc_ecma_transforms_optimization = { version = "=29.0.0", optional = true }
68-
swc_ecma_transforms_proposal = { version = "=27.0.0", optional = true }
69-
swc_ecma_transforms_react = { version = "=30.0.2", optional = true }
70-
swc_ecma_transforms_typescript = { version = "=30.0.1", optional = true }
71-
swc_ecma_utils = { version = "=21.0.0", optional = true }
72-
swc_ecma_visit = { version = "=15.0.0", optional = true }
67+
swc_ecma_transforms_optimization = { version = "=32.0.0", optional = true }
68+
swc_ecma_transforms_proposal = { version = "=30.0.0", optional = true }
69+
swc_ecma_transforms_react = { version = "=33.0.0", optional = true }
70+
swc_ecma_transforms_typescript = { version = "=33.0.0", optional = true }
71+
swc_ecma_utils = { version = "=24.0.0", optional = true }
72+
swc_ecma_visit = { version = "=18.0.1", optional = true }
7373
swc_eq_ignore_macros = "=1.0.1"
74-
swc_bundler = { version = "=32.0.0", optional = true }
74+
swc_bundler = { version = "=35.0.0", optional = true }
7575
swc_graph_analyzer = { version = "=14.0.1", optional = true }
7676
swc_macros_common = { version = "=1.0.1", optional = true }
7777
swc_sourcemap = { version = "=9.3.4", optional = true }
78-
swc_ts_fast_strip = { version = "=33.0.0", optional = true }
78+
swc_ts_fast_strip = { version = "=36.0.0", optional = true }
7979
swc_trace_macro = { version = "=2.0.2", optional = true }
8080
swc_visit = { version = "=2.0.1", optional = true }
8181
thiserror = "2.0.12"

scripts/01_setup.ts

100644100755
File mode changed.

scripts/02_build.ts

100644100755
File mode changed.

scripts/03_test.ts

100644100755
File mode changed.

scripts/04_confirm.ts

100644100755
File mode changed.

src/cjs_parse.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ impl CjsVisitor {
9797
Expr::Lit(Lit::Str(str)) => {
9898
let lit_value = &*str.value;
9999
if is_valid_object_define_obj_lit(&call_expr.args[2].expr) {
100-
self.add_export(lit_value);
100+
self.add_export(&lit_value.to_string_lossy());
101101
} else {
102-
self.add_unsafe_getter(lit_value);
102+
self.add_unsafe_getter(&lit_value.to_string_lossy());
103103
}
104104
}
105105
// Object.defineProperty(exports, key, { ... });
@@ -335,7 +335,7 @@ fn get_call_expr_require_value(call_expr: &CallExpr) -> Option<&str> {
335335
let arg = call_expr.args.first()?;
336336
let lit = arg.expr.as_lit()?;
337337
if let Lit::Str(str) = lit {
338-
return Some(&*str.value);
338+
return str.value.as_str();
339339
}
340340
}
341341
// _interopRequireWildcard(require(...))
@@ -524,7 +524,7 @@ fn get_member_prop_text(member_prop: &MemberProp) -> Option<&str> {
524524
match member_prop {
525525
MemberProp::Ident(ident) => Some(&ident.sym),
526526
MemberProp::Computed(computed) => match &*computed.expr {
527-
Expr::Lit(Lit::Str(str)) => Some(&str.value),
527+
Expr::Lit(Lit::Str(str)) => str.value.as_str(),
528528
_ => None,
529529
},
530530
_ => None,
@@ -546,7 +546,7 @@ fn get_prop_name(prop: &Prop) -> Option<&str> {
546546
fn prop_name_from_key(prop: &PropName) -> Option<&str> {
547547
match prop {
548548
PropName::Ident(ident) => Some(&*ident.sym),
549-
PropName::Str(str) => Some(&*str.value),
549+
PropName::Str(str) => str.value.as_str(),
550550
PropName::BigInt(_) | PropName::Computed(_) | PropName::Num(_) => None,
551551
}
552552
}

src/diagnostics.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,15 +563,18 @@ fn specifier_to_file_path(specifier: &ModuleSpecifier) -> Option<PathBuf> {
563563
}
564564
}
565565

566+
#[cfg(any(feature = "transpiling", feature = "type_strip"))]
566567
pub(crate) type DiagnosticsCell = crate::swc::common::sync::Lrc<
567568
crate::swc::common::sync::Lock<Vec<SwcDiagnostic>>,
568569
>;
569570

571+
#[cfg(any(feature = "transpiling", feature = "type_strip"))]
570572
#[derive(Default, Clone)]
571573
pub(crate) struct DiagnosticCollector {
572574
diagnostics: DiagnosticsCell,
573575
}
574576

577+
#[cfg(any(feature = "transpiling", feature = "type_strip"))]
575578
impl DiagnosticCollector {
576579
pub fn into_handler_and_cell(
577580
self,
@@ -588,6 +591,7 @@ impl DiagnosticCollector {
588591
}
589592
}
590593

594+
#[cfg(any(feature = "transpiling", feature = "type_strip"))]
591595
impl crate::swc::common::errors::Emitter for DiagnosticCollector {
592596
fn emit(
593597
&mut self,
@@ -618,7 +622,7 @@ impl std::fmt::Display for SwcFoldDiagnosticsError {
618622
}
619623
}
620624

621-
pub(crate) fn ensure_no_fatal_swc_diagnostics(
625+
pub fn ensure_no_fatal_swc_diagnostics(
622626
source_map: &swc_common::SourceMap,
623627
diagnostics: impl Iterator<Item = SwcDiagnostic>,
624628
) -> Result<(), SwcFoldDiagnosticsError> {

src/exports.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ impl ParsedSource {
3232
ModuleItem::ModuleDecl(m) => match m {
3333
ModuleDecl::Import(_) => {}
3434
ModuleDecl::ExportAll(n) => {
35-
result.reexports.push(n.src.value.to_string());
35+
result
36+
.reexports
37+
.push(n.src.value.to_string_lossy().into_owned());
3638
}
3739
ModuleDecl::ExportDecl(d) => {
3840
match &d.decl {

src/transpiling/jsx_precompile.rs

Lines changed: 60 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22

33
use crate::swc::atoms::Atom;
4+
use swc_atoms::Wtf8Atom;
45
use swc_common::DUMMY_SP;
56
use swc_common::SyntaxContext;
67
use swc_ecma_ast::*;
@@ -325,27 +326,22 @@ fn get_attr_name(jsx_attr: &JSXAttr, normalize: bool) -> String {
325326
}
326327
}
327328

328-
fn normalize_lit_str(lit: &Lit) -> Lit {
329-
match lit {
330-
Lit::Str(lit_str) => {
331-
let value: &str = &lit_str.value;
332-
let mut replaced = "".to_string();
329+
fn normalize_lit_str(lit_str: &Str) -> Lit {
330+
let value = lit_str.value.to_string_lossy();
331+
let mut replaced = "".to_string();
333332

334-
for (i, line) in value.lines().enumerate() {
335-
if i > 0 {
336-
replaced.push(' ');
337-
}
338-
replaced.push_str(line.trim_start());
339-
}
340-
341-
Lit::Str(Str {
342-
span: lit_str.span,
343-
value: replaced.into(),
344-
raw: None,
345-
})
333+
for (i, line) in value.lines().enumerate() {
334+
if i > 0 {
335+
replaced.push(' ');
346336
}
347-
_ => lit.clone(),
337+
replaced.push_str(line.trim_start());
348338
}
339+
340+
Lit::Str(Str {
341+
span: lit_str.span,
342+
value: replaced.into(),
343+
raw: None,
344+
})
349345
}
350346

351347
fn jsx_text_to_str(
@@ -451,7 +447,7 @@ fn is_text_valid_identifier(string_value: &str) -> bool {
451447
true
452448
}
453449

454-
fn string_lit_expr(value: Atom) -> Expr {
450+
fn string_lit_expr(value: Wtf8Atom) -> Expr {
455451
Expr::Lit(Lit::Str(Str {
456452
span: DUMMY_SP,
457453
value,
@@ -524,7 +520,7 @@ fn merge_serializable_children(
524520
}
525521
// Can be flattened
526522
Lit::Str(str_lit) => {
527-
buf.push_str(&escape_html(str_lit.value.as_ref()));
523+
buf.push_str(&escape_html(&str_lit.value.to_string_lossy()));
528524
continue;
529525
}
530526
_ => {}
@@ -673,8 +669,8 @@ impl JsxPrecompile {
673669
Lit::Bool(_) => expr,
674670
Lit::Null(_) => expr,
675671
Lit::Str(string_lit) => {
676-
let escaped_value = escape_html(string_lit.value.as_ref());
677-
if string_lit.value != escaped_value {
672+
let escaped_value = escape_html(&string_lit.value.to_string_lossy());
673+
if string_lit.value.to_string_lossy() != escaped_value {
678674
self.wrap_with_jsx_escape_call(expr)
679675
} else {
680676
expr
@@ -771,7 +767,7 @@ impl JsxPrecompile {
771767
JSXElementChild::JSXText(jsx_text) => {
772768
elems.push(Some(ExprOrSpread {
773769
spread: None,
774-
expr: Box::new(string_lit_expr(jsx_text.value)),
770+
expr: Box::new(string_lit_expr(jsx_text.value.into())),
775771
}));
776772
}
777773
// Case: <div>{2 + 2}</div>
@@ -856,7 +852,7 @@ impl JsxPrecompile {
856852
is_component = true;
857853
Expr::Ident(ident.clone())
858854
} else {
859-
string_lit_expr(name.clone())
855+
string_lit_expr(name.clone().into())
860856
}
861857
}
862858
// Case: <ctx.Provider />
@@ -920,8 +916,8 @@ impl JsxPrecompile {
920916

921917
if attr_name == "key" {
922918
key_value = match attr_value {
923-
JSXAttrValue::Lit(lit) => {
924-
let normalized_lit = normalize_lit_str(lit);
919+
JSXAttrValue::Str(str) => {
920+
let normalized_lit = normalize_lit_str(str);
925921
Some(Expr::Lit(normalized_lit))
926922
}
927923
JSXAttrValue::JSXExprContainer(jsx_expr_container) => {
@@ -944,8 +940,8 @@ impl JsxPrecompile {
944940
// Case: <Foo class={true}>
945941
// Case: <Foo class={null}>
946942
match attr_value {
947-
JSXAttrValue::Lit(lit) => {
948-
let normalized_lit = normalize_lit_str(lit);
943+
JSXAttrValue::Str(str) => {
944+
let normalized_lit = normalize_lit_str(str);
949945

950946
props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(
951947
KeyValueProp {
@@ -1023,7 +1019,11 @@ impl JsxPrecompile {
10231019
}
10241020
}
10251021

1026-
fn convert_to_jsx_attr_call(&mut self, name: Atom, expr: Expr) -> CallExpr {
1022+
fn convert_to_jsx_attr_call(
1023+
&mut self,
1024+
name: Wtf8Atom,
1025+
expr: Expr,
1026+
) -> CallExpr {
10271027
let args = vec![
10281028
ExprOrSpread {
10291029
spread: None,
@@ -1157,7 +1157,7 @@ impl JsxPrecompile {
11571157

11581158
let value = match &jsx_attr.value {
11591159
Some(attr_value) => match attr_value.clone() {
1160-
JSXAttrValue::Lit(lit) => Expr::Lit(lit),
1160+
JSXAttrValue::Str(lit) => Expr::Lit(Lit::Str(lit)),
11611161
JSXAttrValue::JSXExprContainer(_) => todo!(),
11621162
JSXAttrValue::JSXElement(jsx_element) => {
11631163
Expr::JSXElement(jsx_element)
@@ -1210,44 +1210,34 @@ impl JsxPrecompile {
12101210
// Case: <div class={true}>
12111211
// Case: <div class={null}>
12121212
match attr_value {
1213-
JSXAttrValue::Lit(lit) => match lit {
1214-
Lit::Str(string_lit) => {
1215-
// Edge Case: Both "key" and "ref" attributes are
1216-
// special attributes in most frameworks. Some
1217-
// frameworks may want to serialize it, other's don't.
1218-
// To support both use cases we'll always pass them to
1219-
// `jsxAttr()` so that frameowrks can decide for
1220-
// themselves what to do with it.
1221-
// Case: <div key="123" />
1222-
// Case: <div ref="123" />
1223-
if attr_name == "key" || attr_name == "ref" {
1224-
strings.last_mut().unwrap().push(' ');
1225-
strings.push("".to_string());
1226-
let expr = self.convert_to_jsx_attr_call(
1227-
attr_name.into(),
1228-
string_lit_expr(string_lit.value.clone()),
1229-
);
1230-
dynamic_exprs.push(Expr::Call(expr));
1231-
continue;
1232-
}
1213+
JSXAttrValue::Str(string_lit) => {
1214+
// Edge Case: Both "key" and "ref" attributes are
1215+
// special attributes in most frameworks. Some
1216+
// frameworks may want to serialize it, other's don't.
1217+
// To support both use cases we'll always pass them to
1218+
// `jsxAttr()` so that frameowrks can decide for
1219+
// themselves what to do with it.
1220+
// Case: <div key="123" />
1221+
// Case: <div ref="123" />
1222+
if attr_name == "key" || attr_name == "ref" {
1223+
strings.last_mut().unwrap().push(' ');
1224+
strings.push("".to_string());
1225+
let expr = self.convert_to_jsx_attr_call(
1226+
attr_name.into(),
1227+
string_lit_expr(string_lit.value.clone()),
1228+
);
1229+
dynamic_exprs.push(Expr::Call(expr));
1230+
continue;
1231+
}
12331232

1234-
let serialized_attr =
1235-
serialize_attr(&attr_name, &string_lit.value);
1233+
let serialized_attr =
1234+
serialize_attr(&attr_name, &string_lit.value.to_string_lossy());
12361235

1237-
strings
1238-
.last_mut()
1239-
.unwrap()
1240-
.push_str(serialized_attr.as_str());
1241-
}
1242-
// I've never seen this being possible as it would
1243-
// always be treated as an expression.
1244-
Lit::Bool(_) => {}
1245-
Lit::Null(_) => {}
1246-
Lit::Num(_) => {}
1247-
Lit::BigInt(_) => {}
1248-
Lit::Regex(_) => {}
1249-
Lit::JSXText(_) => {}
1250-
},
1236+
strings
1237+
.last_mut()
1238+
.unwrap()
1239+
.push_str(serialized_attr.as_str());
1240+
}
12511241
JSXAttrValue::JSXExprContainer(jsx_expr_container) => {
12521242
match &jsx_expr_container.expr {
12531243
// This is treated as a syntax error in attributes
@@ -1281,8 +1271,10 @@ impl JsxPrecompile {
12811271
continue;
12821272
}
12831273
Lit::Str(str_lit) => {
1284-
let serialized_attr =
1285-
serialize_attr(&attr_name, &str_lit.value);
1274+
let serialized_attr = serialize_attr(
1275+
&attr_name,
1276+
&str_lit.value.to_string_lossy(),
1277+
);
12861278

12871279
strings
12881280
.last_mut()

0 commit comments

Comments
 (0)