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
209 changes: 118 additions & 91 deletions Cargo.lock

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ deno_media_type = "0.3.0"
deno_terminal = "0.2.2"
deno_error = "0.7.0"

dprint-swc-ext = "0.25.1"
dprint-swc-ext = "0.26.0"
percent-encoding = "2.3.1"
serde = { version = "1.0.219", features = ["derive"] }
text_lines = { version = "0.6.0", features = ["serialization"] }
Expand All @@ -50,32 +50,32 @@ unicode-width = "0.2.0"
# pulling in new versions of swc crates
#
# NOTE: You can automatically update these dependencies by running ./scripts/update_swc_deps.ts
swc_atoms = "=7.0.0"
swc_common = "=14.0.4"
swc_atoms = "=9.0.0"
swc_common = "=17.0.1"
swc_config = { version = "=3.1.2", optional = true }
swc_config_macro = { version = "=1.0.1", optional = true }
swc_ecma_ast = { version = "=15.0.0", features = ["serde-impl"] }
swc_ecma_codegen = { version = "=17.0.2", optional = true }
swc_ecma_ast = { version = "=18.0.0", features = ["serde-impl"] }
swc_ecma_codegen = { version = "=20.0.2", optional = true }
swc_ecma_codegen_macros = { version = "=2.0.2", optional = true }
swc_ecma_loader = { version = "=14.0.0", optional = true }
swc_ecma_lexer = "=23.0.2"
swc_ecma_parser = "=24.0.3"
swc_ecma_transforms_base = { version = "=27.0.0", features = ["inline-helpers"], optional = true }
swc_ecma_transforms_classes = { version = "=27.0.0", optional = true }
swc_ecma_transforms_compat = { version = "=31.0.0", optional = true }
swc_ecma_loader = { version = "=17.0.0", optional = true }
swc_ecma_lexer = "=26.0.0"
swc_ecma_parser = "=27.0.7"
swc_ecma_transforms_base = { version = "=30.0.1", features = ["inline-helpers"], optional = true }
swc_ecma_transforms_classes = { version = "=30.0.0", optional = true }
swc_ecma_transforms_compat = { version = "=35.0.0", optional = true }
swc_ecma_transforms_macros = { version = "=1.0.1", optional = true }
swc_ecma_transforms_optimization = { version = "=29.0.0", optional = true }
swc_ecma_transforms_proposal = { version = "=27.0.0", optional = true }
swc_ecma_transforms_react = { version = "=30.0.2", optional = true }
swc_ecma_transforms_typescript = { version = "=30.0.1", optional = true }
swc_ecma_utils = { version = "=21.0.0", optional = true }
swc_ecma_visit = { version = "=15.0.0", optional = true }
swc_ecma_transforms_optimization = { version = "=32.0.0", optional = true }
swc_ecma_transforms_proposal = { version = "=30.0.0", optional = true }
swc_ecma_transforms_react = { version = "=33.0.0", optional = true }
swc_ecma_transforms_typescript = { version = "=33.0.0", optional = true }
swc_ecma_utils = { version = "=24.0.0", optional = true }
swc_ecma_visit = { version = "=18.0.1", optional = true }
swc_eq_ignore_macros = "=1.0.1"
swc_bundler = { version = "=32.0.0", optional = true }
swc_bundler = { version = "=35.0.0", optional = true }
swc_graph_analyzer = { version = "=14.0.1", optional = true }
swc_macros_common = { version = "=1.0.1", optional = true }
swc_sourcemap = { version = "=9.3.4", optional = true }
swc_ts_fast_strip = { version = "=33.0.0", optional = true }
swc_ts_fast_strip = { version = "=36.0.0", optional = true }
swc_trace_macro = { version = "=2.0.2", optional = true }
swc_visit = { version = "=2.0.1", optional = true }
thiserror = "2.0.12"
Expand Down
Empty file modified scripts/01_setup.ts
100644 → 100755
Empty file.
Empty file modified scripts/02_build.ts
100644 → 100755
Empty file.
Empty file modified scripts/03_test.ts
100644 → 100755
Empty file.
Empty file modified scripts/04_confirm.ts
100644 → 100755
Empty file.
10 changes: 5 additions & 5 deletions src/cjs_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ impl CjsVisitor {
Expr::Lit(Lit::Str(str)) => {
let lit_value = &*str.value;
if is_valid_object_define_obj_lit(&call_expr.args[2].expr) {
self.add_export(lit_value);
self.add_export(&lit_value.to_string_lossy());
} else {
self.add_unsafe_getter(lit_value);
self.add_unsafe_getter(&lit_value.to_string_lossy());
}
}
// Object.defineProperty(exports, key, { ... });
Expand Down Expand Up @@ -335,7 +335,7 @@ fn get_call_expr_require_value(call_expr: &CallExpr) -> Option<&str> {
let arg = call_expr.args.first()?;
let lit = arg.expr.as_lit()?;
if let Lit::Str(str) = lit {
return Some(&*str.value);
return str.value.as_str();
}
}
// _interopRequireWildcard(require(...))
Expand Down Expand Up @@ -524,7 +524,7 @@ fn get_member_prop_text(member_prop: &MemberProp) -> Option<&str> {
match member_prop {
MemberProp::Ident(ident) => Some(&ident.sym),
MemberProp::Computed(computed) => match &*computed.expr {
Expr::Lit(Lit::Str(str)) => Some(&str.value),
Expr::Lit(Lit::Str(str)) => str.value.as_str(),
_ => None,
},
_ => None,
Expand All @@ -546,7 +546,7 @@ fn get_prop_name(prop: &Prop) -> Option<&str> {
fn prop_name_from_key(prop: &PropName) -> Option<&str> {
match prop {
PropName::Ident(ident) => Some(&*ident.sym),
PropName::Str(str) => Some(&*str.value),
PropName::Str(str) => str.value.as_str(),
PropName::BigInt(_) | PropName::Computed(_) | PropName::Num(_) => None,
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,15 +563,18 @@ fn specifier_to_file_path(specifier: &ModuleSpecifier) -> Option<PathBuf> {
}
}

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

#[cfg(any(feature = "transpiling", feature = "type_strip"))]
#[derive(Default, Clone)]
pub(crate) struct DiagnosticCollector {
diagnostics: DiagnosticsCell,
}

#[cfg(any(feature = "transpiling", feature = "type_strip"))]
impl DiagnosticCollector {
pub fn into_handler_and_cell(
self,
Expand All @@ -588,6 +591,7 @@ impl DiagnosticCollector {
}
}

#[cfg(any(feature = "transpiling", feature = "type_strip"))]
impl crate::swc::common::errors::Emitter for DiagnosticCollector {
fn emit(
&mut self,
Expand Down Expand Up @@ -618,7 +622,7 @@ impl std::fmt::Display for SwcFoldDiagnosticsError {
}
}

pub(crate) fn ensure_no_fatal_swc_diagnostics(
pub fn ensure_no_fatal_swc_diagnostics(
source_map: &swc_common::SourceMap,
diagnostics: impl Iterator<Item = SwcDiagnostic>,
) -> Result<(), SwcFoldDiagnosticsError> {
Expand Down
4 changes: 3 additions & 1 deletion src/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ impl ParsedSource {
ModuleItem::ModuleDecl(m) => match m {
ModuleDecl::Import(_) => {}
ModuleDecl::ExportAll(n) => {
result.reexports.push(n.src.value.to_string());
result
.reexports
.push(n.src.value.to_string_lossy().into_owned());
}
ModuleDecl::ExportDecl(d) => {
match &d.decl {
Expand Down
128 changes: 60 additions & 68 deletions src/transpiling/jsx_precompile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use crate::swc::atoms::Atom;
use swc_atoms::Wtf8Atom;
use swc_common::DUMMY_SP;
use swc_common::SyntaxContext;
use swc_ecma_ast::*;
Expand Down Expand Up @@ -325,27 +326,22 @@ fn get_attr_name(jsx_attr: &JSXAttr, normalize: bool) -> String {
}
}

fn normalize_lit_str(lit: &Lit) -> Lit {
match lit {
Lit::Str(lit_str) => {
let value: &str = &lit_str.value;
let mut replaced = "".to_string();
fn normalize_lit_str(lit_str: &Str) -> Lit {
let value = lit_str.value.to_string_lossy();
let mut replaced = "".to_string();

for (i, line) in value.lines().enumerate() {
if i > 0 {
replaced.push(' ');
}
replaced.push_str(line.trim_start());
}

Lit::Str(Str {
span: lit_str.span,
value: replaced.into(),
raw: None,
})
for (i, line) in value.lines().enumerate() {
if i > 0 {
replaced.push(' ');
}
_ => lit.clone(),
replaced.push_str(line.trim_start());
}

Lit::Str(Str {
span: lit_str.span,
value: replaced.into(),
raw: None,
})
}

fn jsx_text_to_str(
Expand Down Expand Up @@ -451,7 +447,7 @@ fn is_text_valid_identifier(string_value: &str) -> bool {
true
}

fn string_lit_expr(value: Atom) -> Expr {
fn string_lit_expr(value: Wtf8Atom) -> Expr {
Expr::Lit(Lit::Str(Str {
span: DUMMY_SP,
value,
Expand Down Expand Up @@ -524,7 +520,7 @@ fn merge_serializable_children(
}
// Can be flattened
Lit::Str(str_lit) => {
buf.push_str(&escape_html(str_lit.value.as_ref()));
buf.push_str(&escape_html(&str_lit.value.to_string_lossy()));
continue;
}
_ => {}
Expand Down Expand Up @@ -673,8 +669,8 @@ impl JsxPrecompile {
Lit::Bool(_) => expr,
Lit::Null(_) => expr,
Lit::Str(string_lit) => {
let escaped_value = escape_html(string_lit.value.as_ref());
if string_lit.value != escaped_value {
let escaped_value = escape_html(&string_lit.value.to_string_lossy());
if string_lit.value.to_string_lossy() != escaped_value {
self.wrap_with_jsx_escape_call(expr)
} else {
expr
Expand Down Expand Up @@ -771,7 +767,7 @@ impl JsxPrecompile {
JSXElementChild::JSXText(jsx_text) => {
elems.push(Some(ExprOrSpread {
spread: None,
expr: Box::new(string_lit_expr(jsx_text.value)),
expr: Box::new(string_lit_expr(jsx_text.value.into())),
}));
}
// Case: <div>{2 + 2}</div>
Expand Down Expand Up @@ -856,7 +852,7 @@ impl JsxPrecompile {
is_component = true;
Expr::Ident(ident.clone())
} else {
string_lit_expr(name.clone())
string_lit_expr(name.clone().into())
}
}
// Case: <ctx.Provider />
Expand Down Expand Up @@ -920,8 +916,8 @@ impl JsxPrecompile {

if attr_name == "key" {
key_value = match attr_value {
JSXAttrValue::Lit(lit) => {
let normalized_lit = normalize_lit_str(lit);
JSXAttrValue::Str(str) => {
let normalized_lit = normalize_lit_str(str);
Some(Expr::Lit(normalized_lit))
}
JSXAttrValue::JSXExprContainer(jsx_expr_container) => {
Expand All @@ -944,8 +940,8 @@ impl JsxPrecompile {
// Case: <Foo class={true}>
// Case: <Foo class={null}>
match attr_value {
JSXAttrValue::Lit(lit) => {
let normalized_lit = normalize_lit_str(lit);
JSXAttrValue::Str(str) => {
let normalized_lit = normalize_lit_str(str);

props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
Expand Down Expand Up @@ -1023,7 +1019,11 @@ impl JsxPrecompile {
}
}

fn convert_to_jsx_attr_call(&mut self, name: Atom, expr: Expr) -> CallExpr {
fn convert_to_jsx_attr_call(
&mut self,
name: Wtf8Atom,
expr: Expr,
) -> CallExpr {
let args = vec![
ExprOrSpread {
spread: None,
Expand Down Expand Up @@ -1157,7 +1157,7 @@ impl JsxPrecompile {

let value = match &jsx_attr.value {
Some(attr_value) => match attr_value.clone() {
JSXAttrValue::Lit(lit) => Expr::Lit(lit),
JSXAttrValue::Str(lit) => Expr::Lit(Lit::Str(lit)),
JSXAttrValue::JSXExprContainer(_) => todo!(),
JSXAttrValue::JSXElement(jsx_element) => {
Expr::JSXElement(jsx_element)
Expand Down Expand Up @@ -1210,44 +1210,34 @@ impl JsxPrecompile {
// Case: <div class={true}>
// Case: <div class={null}>
match attr_value {
JSXAttrValue::Lit(lit) => match lit {
Lit::Str(string_lit) => {
// Edge Case: Both "key" and "ref" attributes are
// special attributes in most frameworks. Some
// frameworks may want to serialize it, other's don't.
// To support both use cases we'll always pass them to
// `jsxAttr()` so that frameowrks can decide for
// themselves what to do with it.
// Case: <div key="123" />
// Case: <div ref="123" />
if attr_name == "key" || attr_name == "ref" {
strings.last_mut().unwrap().push(' ');
strings.push("".to_string());
let expr = self.convert_to_jsx_attr_call(
attr_name.into(),
string_lit_expr(string_lit.value.clone()),
);
dynamic_exprs.push(Expr::Call(expr));
continue;
}
JSXAttrValue::Str(string_lit) => {
// Edge Case: Both "key" and "ref" attributes are
// special attributes in most frameworks. Some
// frameworks may want to serialize it, other's don't.
// To support both use cases we'll always pass them to
// `jsxAttr()` so that frameowrks can decide for
// themselves what to do with it.
// Case: <div key="123" />
// Case: <div ref="123" />
if attr_name == "key" || attr_name == "ref" {
strings.last_mut().unwrap().push(' ');
strings.push("".to_string());
let expr = self.convert_to_jsx_attr_call(
attr_name.into(),
string_lit_expr(string_lit.value.clone()),
);
dynamic_exprs.push(Expr::Call(expr));
continue;
}

let serialized_attr =
serialize_attr(&attr_name, &string_lit.value);
let serialized_attr =
serialize_attr(&attr_name, &string_lit.value.to_string_lossy());

strings
.last_mut()
.unwrap()
.push_str(serialized_attr.as_str());
}
// I've never seen this being possible as it would
// always be treated as an expression.
Lit::Bool(_) => {}
Lit::Null(_) => {}
Lit::Num(_) => {}
Lit::BigInt(_) => {}
Lit::Regex(_) => {}
Lit::JSXText(_) => {}
},
strings
.last_mut()
.unwrap()
.push_str(serialized_attr.as_str());
}
JSXAttrValue::JSXExprContainer(jsx_expr_container) => {
match &jsx_expr_container.expr {
// This is treated as a syntax error in attributes
Expand Down Expand Up @@ -1281,8 +1271,10 @@ impl JsxPrecompile {
continue;
}
Lit::Str(str_lit) => {
let serialized_attr =
serialize_attr(&attr_name, &str_lit.value);
let serialized_attr = serialize_attr(
&attr_name,
&str_lit.value.to_string_lossy(),
);

strings
.last_mut()
Expand Down
4 changes: 2 additions & 2 deletions src/transpiling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,8 +1169,8 @@ function App() {
.unwrap()
.into_source()
.text;
let expected = r#"/** @jsxImportSource jsx_lib */ import { jsx as _jsx, Fragment as _Fragment } from "jsx_lib/jsx-runtime";
function App() {
let expected = r#"import { jsx as _jsx, Fragment as _Fragment } from "jsx_lib/jsx-runtime";
/** @jsxImportSource jsx_lib */ function App() {
return /*#__PURE__*/ _jsx("div", {
children: /*#__PURE__*/ _jsx(_Fragment, {})
});
Expand Down
Loading