From f22a49af2b37e826db200ad896266326e92f3c6a Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 26 Nov 2025 15:22:40 -0500 Subject: [PATCH 1/3] fix: deno_ast 0.52 --- Cargo.lock | 57 ++++++++++++++++------------------ Cargo.toml | 4 ++- src/generation/generate.rs | 35 +++++++++++++++------ src/generation/node_helpers.rs | 21 ++++++++++--- 4 files changed, 72 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0e61a8f3..38315012 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,17 +35,11 @@ version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0674a1ddeecb70197781e945de4b3b8ffb61fa939a5597bcf48503737663100" -[[package]] -name = "arrayvec" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" - [[package]] name = "ast_node" -version = "3.0.4" +version = "5.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a184645bcc6f52d69d8e7639720699c6a99efb711f886e251ed1d16db8dd90e" +checksum = "2eb025ef00a6da925cf40870b9c8d008526b6004ece399cb0974209720f0b194" dependencies = [ "quote", "swc_macros_common", @@ -176,8 +170,6 @@ checksum = "be1e0bca6c3637f992fc1cc7cbc52a78c1ef6db076dbf1059c4323d6a2048376" [[package]] name = "deno_ast" version = "0.51.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c72e0409b3dbd60a5bf296cbc273a8e36bb3a3aab6abac389f1891e187d5ce14" dependencies = [ "capacity_builder", "deno_error", @@ -354,9 +346,9 @@ dependencies = [ [[package]] name = "dprint-swc-ext" -version = "0.25.1" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf592ae6a864437e98ef9c6ae7936b822077e9d038a3a48ee081ab92313afad4" +checksum = "33175ddb7a6d418589cab2966bd14a710b3b1139459d3d5ca9edf783c4833f4c" dependencies = [ "allocator-api2", "bumpalo", @@ -419,9 +411,9 @@ dependencies = [ [[package]] name = "from_variant" -version = "2.0.2" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "308530a56b099da144ebc5d8e179f343ad928fa2b3558d1eb3db9af18d6eff43" +checksum = "e5ff35a391aef949120a0340d690269b3d9f63460a6106e99bd07b961f345ea9" dependencies = [ "swc_macros_common", "syn 2.0.106", @@ -462,14 +454,15 @@ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "hstr" -version = "2.0.1" +version = "3.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ced1416104790052518d199e753d49a7d8130d476c664bc9e53f40cfecb8e615" +checksum = "0c43c0a9e8fbdb3bb9dc8eee85e1e2ac81605418b4c83b6b7413cbf14d56ca5c" dependencies = [ "hashbrown 0.14.5", "new_debug_unreachable", "once_cell", "rustc-hash 2.1.1", + "serde", "triomphe", ] @@ -1114,9 +1107,9 @@ dependencies = [ [[package]] name = "swc_atoms" -version = "7.0.0" +version = "9.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3500dcf04c84606b38464561edc5e46f5132201cb3e23cf9613ed4033d6b1bb2" +checksum = "d4ccbe2ecad10ad7432100f878a107b1d972a8aee83ca53184d00c23a078bb8a" dependencies = [ "hstr", "once_cell", @@ -1125,9 +1118,9 @@ dependencies = [ [[package]] name = "swc_common" -version = "14.0.4" +version = "17.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2bb772b3a26b8b71d4e8c112ced5b5867be2266364b58517407a270328a2696" +checksum = "259b675d633a26d24efe3802a9d88858c918e6e8f062d3222d3aa02d56a2cf4c" dependencies = [ "anyhow", "ast_node", @@ -1145,15 +1138,15 @@ dependencies = [ "swc_eq_ignore_macros", "swc_visit", "tracing", - "unicode-width 0.1.14", + "unicode-width 0.2.1", "url", ] [[package]] name = "swc_ecma_ast" -version = "15.0.0" +version = "18.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65c25af97d53cf8aab66a6c68f3418663313fc969ad267fc2a4d19402c329be1" +checksum = "a573a0c72850dec8d4d8085f152d5778af35a2520c3093b242d2d1d50776da7c" dependencies = [ "bitflags", "is-macro", @@ -1171,15 +1164,13 @@ dependencies = [ [[package]] name = "swc_ecma_lexer" -version = "23.0.2" +version = "26.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "017d06ea85008234aa9fb34d805c7dc563f2ea6e03869ed5ac5a2dc27d561e4d" +checksum = "5e82f7747e052c6ff6e111fa4adeb14e33b46ee6e94fe5ef717601f651db48fc" dependencies = [ - "arrayvec", "bitflags", "either", "num-bigint", - "phf", "rustc-hash 2.1.1", "seq-macro", "serde", @@ -1189,22 +1180,28 @@ dependencies = [ "swc_atoms", "swc_common", "swc_ecma_ast", + "swc_ecma_parser", "tracing", ] [[package]] name = "swc_ecma_parser" -version = "24.0.3" +version = "27.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e9011783c975ba592ffc09cd208ced92b1dfabb2e5e0ef453559e2e25286127" +checksum = "7f1a51af1a92cd4904c073b293e491bbc0918400a45d58227b34c961dd6f52d7" dependencies = [ + "bitflags", "either", "num-bigint", + "phf", + "rustc-hash 2.1.1", + "seq-macro", "serde", + "smartstring", + "stacker", "swc_atoms", "swc_common", "swc_ecma_ast", - "swc_ecma_lexer", "tracing", ] diff --git a/Cargo.toml b/Cargo.toml index 4916edb7..1911434e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "dprint-plugin-typescript" version = "0.95.12" authors = ["David Sherret "] -edition = "2021" +edition = "2024" homepage = "https://github.com/dprint/dprint-plugin-typescript" keywords = ["formatting", "formatter", "typescript", "javascript"] license = "MIT" @@ -48,3 +48,5 @@ markup_fmt = "0.19.0" pretty_assertions = "1.3.0" serde_json = { version = "1.0" } +[patch.crates-io.deno_ast] +path = "../deno_ast" diff --git a/src/generation/generate.rs b/src/generation/generate.rs index d5090dc9..568b6259 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -3639,7 +3639,15 @@ fn gen_property_signature<'a>(node: &TsPropertySignature<'a>, context: &mut Cont fn gen_quotable_prop<'a>(node: Node<'a>, context: &mut Context<'a>) -> PrintItems { match context.config.quote_props { QuoteProps::AsNeeded => match node { - Node::Str(str_lit) if is_text_valid_identifier(str_lit.value()) => gen_from_raw_string(str_lit.value()), + Node::Str(str_lit) => { + if let Some(text) = str_lit.value().as_str() + && is_text_valid_identifier(text) + { + gen_from_raw_string(text) + } else { + gen_node(node, context) + } + } _ => gen_node(node, context), }, QuoteProps::Consistent => match context.use_consistent_quote_props() { @@ -3651,7 +3659,10 @@ fn gen_quotable_prop<'a>(node: Node<'a>, context: &mut Context<'a>) -> PrintItem }, Some(false) => match node { // remove quotes - Node::Str(str_lit) => gen_from_raw_string(str_lit.value()), + Node::Str(str_lit) => match str_lit.value().as_str() { + Some(text) => gen_from_raw_string(text), + None => gen_node(node, context), + }, _ => gen_node(node, context), }, None => { @@ -3698,7 +3709,10 @@ fn use_consistent_quotes_for_members<'a>(mut members: impl Iterator bool { match expr { Expr::Ident(ident) => !is_text_valid_identifier(ident.sym()), - Expr::Lit(Lit::Str(str)) => !is_text_valid_identifier(str.value()), + Expr::Lit(Lit::Str(str)) => match str.value().as_str() { + Some(text) => !is_text_valid_identifier(text), + None => true, + }, _ => false, } } @@ -3706,7 +3720,10 @@ fn use_consistent_quotes_for_members<'a>(mut members: impl Iterator bool { match name { PropName::Ident(ident) => !is_text_valid_identifier(ident.sym()), - PropName::Str(str) => !is_text_valid_identifier(str.value()), + PropName::Str(str) => match str.value().as_str() { + Some(text) => !is_text_valid_identifier(text), + None => true, + }, _ => false, } } @@ -9117,7 +9134,7 @@ fn gen_jsx_children<'a>(opts: GenJsxChildrenOptions<'a>, context: &mut Context<' let mut found_non_space_child = false; // include space expressions at the start for child in real_children.into_iter() { - if found_non_space_child && node_helpers::has_jsx_space_expr_text(child) { + if found_non_space_child && node_helpers::has_jsx_space_expr_text(child, context.program) { current_jsx_space_exprs.push(child); continue; } @@ -9239,19 +9256,19 @@ fn gen_jsx_children<'a>(opts: GenJsxChildrenOptions<'a>, context: &mut Context<' /// If the node has a "JSX space expression" between or text that's only spaces between. fn has_jsx_space_between<'a>(previous_node: Node<'a>, next_node: Node<'a>, program: Program<'a>) -> bool { - return node_helpers::nodes_have_only_spaces_between(previous_node, next_node, program) || has_jsx_space_expr_between(previous_node, next_node); - - fn has_jsx_space_expr_between(previous_node: Node, next_node: Node) -> bool { + fn has_jsx_space_expr_between(previous_node: Node, next_node: Node, program: Program) -> bool { let nodes_between = node_helpers::get_siblings_between(previous_node, next_node); for node_between in nodes_between { - if node_helpers::has_jsx_space_expr_text(node_between) { + if node_helpers::has_jsx_space_expr_text(node_between, program) { return true; } } false } + + node_helpers::nodes_have_only_spaces_between(previous_node, next_node, program) || has_jsx_space_expr_between(previous_node, next_node, program) } } diff --git a/src/generation/node_helpers.rs b/src/generation/node_helpers.rs index 3bc0007b..4a0f7f5c 100644 --- a/src/generation/node_helpers.rs +++ b/src/generation/node_helpers.rs @@ -115,11 +115,11 @@ pub fn get_siblings_between<'a, 'b>(node_a: Node<'a>, node_b: Node<'b>) -> Vec bool { - get_jsx_space_expr_space_count(node) > 0 +pub fn has_jsx_space_expr_text(node: Node, program: Program) -> bool { + get_jsx_space_expr_space_count(node, program) > 0 } -pub fn get_jsx_space_expr_space_count(node: Node) -> usize { +pub fn get_jsx_space_expr_space_count(node: Node, program: Program) -> usize { // A "JSX space expression" is a JSXExprContainer with // a string literal containing only spaces. // * {" "} @@ -130,7 +130,8 @@ pub fn get_jsx_space_expr_space_count(node: Node) -> usize { .. }) => { let mut space_count = 0; - for c in text.value().chars() { + let text = remove_quotes_from_str(text.text_fast(program)); + for c in text.chars() { if c == ' ' { space_count += 1; } else { @@ -143,6 +144,16 @@ pub fn get_jsx_space_expr_space_count(node: Node) -> usize { } } +fn remove_quotes_from_str(text: &str) -> &str { + if text.is_empty() { + text + } else if text.starts_with("'") && text.ends_with("'") || text.starts_with("\"") && text.ends_with("\"") { + &text[1..text.len() - 1] + } else { + text + } +} + pub fn count_spaces_between_jsx_children<'a>(previous_node: Node<'a>, next_node: Node<'a>, program: Program<'a>) -> usize { let all_siblings_between = get_siblings_between(previous_node, next_node); let siblings_between = all_siblings_between @@ -155,7 +166,7 @@ pub fn count_spaces_between_jsx_children<'a>(previous_node: Node<'a>, next_node: let mut previous_node = previous_node; for node in siblings_between { - count += get_jsx_space_expr_space_count(node); + count += get_jsx_space_expr_space_count(node, program); if nodes_have_only_spaces_between(previous_node, node, program) { count += 1; From 663f8ea3ee3964cc040e83b98048f7a21c8a2160 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 26 Nov 2025 21:28:35 -0500 Subject: [PATCH 2/3] use released crate --- Cargo.lock | 4 +++- Cargo.toml | 5 +---- src/format_text.rs | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 38315012..1f237d4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -169,7 +169,9 @@ checksum = "be1e0bca6c3637f992fc1cc7cbc52a78c1ef6db076dbf1059c4323d6a2048376" [[package]] name = "deno_ast" -version = "0.51.0" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30c2f6f65154faed61e45d6578566f9fab9d2a330c35c87366706883701cce51" dependencies = [ "capacity_builder", "deno_error", diff --git a/Cargo.toml b/Cargo.toml index 1911434e..af2a8169 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ harness = false [dependencies] anyhow = "1.0.64" capacity_builder = "0.5.0" -deno_ast = { version = "0.51.0", features = ["view"] } +deno_ast = { version = "0.52.0", features = ["view"] } dprint-core = { version = "0.67.4", features = ["formatting"] } dprint-core-macros = "0.1.0" percent-encoding = "2.3.1" @@ -47,6 +47,3 @@ malva = "0.11.2" markup_fmt = "0.19.0" pretty_assertions = "1.3.0" serde_json = { version = "1.0" } - -[patch.crates-io.deno_ast] -path = "../deno_ast" diff --git a/src/format_text.rs b/src/format_text.rs index 17127352..f13ec55f 100644 --- a/src/format_text.rs +++ b/src/format_text.rs @@ -118,7 +118,7 @@ fn inner_format(parsed_source: &ParsedSource, config: &Configuration, external_f pub fn trace_file(file_path: &Path, file_text: &str, config: &Configuration) -> dprint_core::formatting::TracingResult { let parsed_source = parse_swc_ast(file_path, None, file_text.into()).unwrap(); ensure_no_specific_syntax_errors(&parsed_source).unwrap(); - dprint_core::formatting::trace_printing(|| generate(&parsed_source, config, None), config_to_print_options(file_text, config)) + dprint_core::formatting::trace_printing(|| generate(&parsed_source, config, None).unwrap(), config_to_print_options(file_text, config)) } fn config_to_print_options(file_text: &str, config: &Configuration) -> PrintOptions { From 373d25bc7b2df726b13401ae7f02eea6e8add40f Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 26 Nov 2025 21:43:26 -0500 Subject: [PATCH 3/3] add some tests --- .../literals/StringLiteral/StringLiteral_All.txt | 2 ++ .../StringLiteral_QuoteProps_Consistent.txt | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/tests/specs/literals/StringLiteral/StringLiteral_All.txt b/tests/specs/literals/StringLiteral/StringLiteral_All.txt index efdc2a68..a839b52c 100644 --- a/tests/specs/literals/StringLiteral/StringLiteral_All.txt +++ b/tests/specs/literals/StringLiteral/StringLiteral_All.txt @@ -2,11 +2,13 @@ const t = "\t" ; const u = ""; const v = "1"; +const w = "\uD800"; [expect] const t = "\t"; const u = ""; const v = "1"; +const w = "\uD800"; == should not replace actual character with escaped character == const t = " "; diff --git a/tests/specs/literals/StringLiteral/StringLiteral_QuoteProps_Consistent.txt b/tests/specs/literals/StringLiteral/StringLiteral_QuoteProps_Consistent.txt index 39407868..eb35bb0e 100644 --- a/tests/specs/literals/StringLiteral/StringLiteral_QuoteProps_Consistent.txt +++ b/tests/specs/literals/StringLiteral/StringLiteral_QuoteProps_Consistent.txt @@ -174,3 +174,15 @@ const x = { }), "/src/shared/index.ts": `export const a: Unrestricted = 1;`, }; + +== should add quotes for this == +const objectLiteral = { + "\uD800": true, + test: 1, +}; + +[expect] +const objectLiteral = { + "\uD800": true, + "test": 1, +};