Skip to content

Commit a74f510

Browse files
authored
fix: deno_ast 0.52 (#758)
1 parent 00760d2 commit a74f510

7 files changed

Lines changed: 88 additions & 48 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "dprint-plugin-typescript"
33
version = "0.95.12"
44
authors = ["David Sherret <dsherret@gmail.com>"]
5-
edition = "2021"
5+
edition = "2024"
66
homepage = "https://github.com/dprint/dprint-plugin-typescript"
77
keywords = ["formatting", "formatter", "typescript", "javascript"]
88
license = "MIT"
@@ -32,7 +32,7 @@ harness = false
3232
[dependencies]
3333
anyhow = "1.0.64"
3434
capacity_builder = "0.5.0"
35-
deno_ast = { version = "0.51.0", features = ["view"] }
35+
deno_ast = { version = "0.52.0", features = ["view"] }
3636
dprint-core = { version = "0.67.4", features = ["formatting"] }
3737
dprint-core-macros = "0.1.0"
3838
percent-encoding = "2.3.1"
@@ -47,4 +47,3 @@ malva = "0.11.2"
4747
markup_fmt = "0.19.0"
4848
pretty_assertions = "1.3.0"
4949
serde_json = { version = "1.0" }
50-

src/format_text.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ fn inner_format(parsed_source: &ParsedSource, config: &Configuration, external_f
118118
pub fn trace_file(file_path: &Path, file_text: &str, config: &Configuration) -> dprint_core::formatting::TracingResult {
119119
let parsed_source = parse_swc_ast(file_path, None, file_text.into()).unwrap();
120120
ensure_no_specific_syntax_errors(&parsed_source).unwrap();
121-
dprint_core::formatting::trace_printing(|| generate(&parsed_source, config, None), config_to_print_options(file_text, config))
121+
dprint_core::formatting::trace_printing(|| generate(&parsed_source, config, None).unwrap(), config_to_print_options(file_text, config))
122122
}
123123

124124
fn config_to_print_options(file_text: &str, config: &Configuration) -> PrintOptions {

src/generation/generate.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3639,7 +3639,15 @@ fn gen_property_signature<'a>(node: &TsPropertySignature<'a>, context: &mut Cont
36393639
fn gen_quotable_prop<'a>(node: Node<'a>, context: &mut Context<'a>) -> PrintItems {
36403640
match context.config.quote_props {
36413641
QuoteProps::AsNeeded => match node {
3642-
Node::Str(str_lit) if is_text_valid_identifier(str_lit.value()) => gen_from_raw_string(str_lit.value()),
3642+
Node::Str(str_lit) => {
3643+
if let Some(text) = str_lit.value().as_str()
3644+
&& is_text_valid_identifier(text)
3645+
{
3646+
gen_from_raw_string(text)
3647+
} else {
3648+
gen_node(node, context)
3649+
}
3650+
}
36433651
_ => gen_node(node, context),
36443652
},
36453653
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
36513659
},
36523660
Some(false) => match node {
36533661
// remove quotes
3654-
Node::Str(str_lit) => gen_from_raw_string(str_lit.value()),
3662+
Node::Str(str_lit) => match str_lit.value().as_str() {
3663+
Some(text) => gen_from_raw_string(text),
3664+
None => gen_node(node, context),
3665+
},
36553666
_ => gen_node(node, context),
36563667
},
36573668
None => {
@@ -3698,15 +3709,21 @@ fn use_consistent_quotes_for_members<'a>(mut members: impl Iterator<Item = Node<
36983709
fn check_expr(expr: Expr) -> bool {
36993710
match expr {
37003711
Expr::Ident(ident) => !is_text_valid_identifier(ident.sym()),
3701-
Expr::Lit(Lit::Str(str)) => !is_text_valid_identifier(str.value()),
3712+
Expr::Lit(Lit::Str(str)) => match str.value().as_str() {
3713+
Some(text) => !is_text_valid_identifier(text),
3714+
None => true,
3715+
},
37023716
_ => false,
37033717
}
37043718
}
37053719

37063720
fn check_prop_name(name: PropName) -> bool {
37073721
match name {
37083722
PropName::Ident(ident) => !is_text_valid_identifier(ident.sym()),
3709-
PropName::Str(str) => !is_text_valid_identifier(str.value()),
3723+
PropName::Str(str) => match str.value().as_str() {
3724+
Some(text) => !is_text_valid_identifier(text),
3725+
None => true,
3726+
},
37103727
_ => false,
37113728
}
37123729
}
@@ -9117,7 +9134,7 @@ fn gen_jsx_children<'a>(opts: GenJsxChildrenOptions<'a>, context: &mut Context<'
91179134
let mut found_non_space_child = false; // include space expressions at the start
91189135

91199136
for child in real_children.into_iter() {
9120-
if found_non_space_child && node_helpers::has_jsx_space_expr_text(child) {
9137+
if found_non_space_child && node_helpers::has_jsx_space_expr_text(child, context.program) {
91219138
current_jsx_space_exprs.push(child);
91229139
continue;
91239140
}
@@ -9239,19 +9256,19 @@ fn gen_jsx_children<'a>(opts: GenJsxChildrenOptions<'a>, context: &mut Context<'
92399256

92409257
/// If the node has a "JSX space expression" between or text that's only spaces between.
92419258
fn has_jsx_space_between<'a>(previous_node: Node<'a>, next_node: Node<'a>, program: Program<'a>) -> bool {
9242-
return node_helpers::nodes_have_only_spaces_between(previous_node, next_node, program) || has_jsx_space_expr_between(previous_node, next_node);
9243-
9244-
fn has_jsx_space_expr_between(previous_node: Node, next_node: Node) -> bool {
9259+
fn has_jsx_space_expr_between(previous_node: Node, next_node: Node, program: Program) -> bool {
92459260
let nodes_between = node_helpers::get_siblings_between(previous_node, next_node);
92469261

92479262
for node_between in nodes_between {
9248-
if node_helpers::has_jsx_space_expr_text(node_between) {
9263+
if node_helpers::has_jsx_space_expr_text(node_between, program) {
92499264
return true;
92509265
}
92519266
}
92529267

92539268
false
92549269
}
9270+
9271+
node_helpers::nodes_have_only_spaces_between(previous_node, next_node, program) || has_jsx_space_expr_between(previous_node, next_node, program)
92559272
}
92569273
}
92579274

src/generation/node_helpers.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ pub fn get_siblings_between<'a, 'b>(node_a: Node<'a>, node_b: Node<'b>) -> Vec<N
115115
parent_children.drain(node_a.child_index() + 1..node_b.child_index()).collect()
116116
}
117117

118-
pub fn has_jsx_space_expr_text(node: Node) -> bool {
119-
get_jsx_space_expr_space_count(node) > 0
118+
pub fn has_jsx_space_expr_text(node: Node, program: Program) -> bool {
119+
get_jsx_space_expr_space_count(node, program) > 0
120120
}
121121

122-
pub fn get_jsx_space_expr_space_count(node: Node) -> usize {
122+
pub fn get_jsx_space_expr_space_count(node: Node, program: Program) -> usize {
123123
// A "JSX space expression" is a JSXExprContainer with
124124
// a string literal containing only spaces.
125125
// * {" "}
@@ -130,7 +130,8 @@ pub fn get_jsx_space_expr_space_count(node: Node) -> usize {
130130
..
131131
}) => {
132132
let mut space_count = 0;
133-
for c in text.value().chars() {
133+
let text = remove_quotes_from_str(text.text_fast(program));
134+
for c in text.chars() {
134135
if c == ' ' {
135136
space_count += 1;
136137
} else {
@@ -143,6 +144,16 @@ pub fn get_jsx_space_expr_space_count(node: Node) -> usize {
143144
}
144145
}
145146

147+
fn remove_quotes_from_str(text: &str) -> &str {
148+
if text.is_empty() {
149+
text
150+
} else if text.starts_with("'") && text.ends_with("'") || text.starts_with("\"") && text.ends_with("\"") {
151+
&text[1..text.len() - 1]
152+
} else {
153+
text
154+
}
155+
}
156+
146157
pub fn count_spaces_between_jsx_children<'a>(previous_node: Node<'a>, next_node: Node<'a>, program: Program<'a>) -> usize {
147158
let all_siblings_between = get_siblings_between(previous_node, next_node);
148159
let siblings_between = all_siblings_between
@@ -155,7 +166,7 @@ pub fn count_spaces_between_jsx_children<'a>(previous_node: Node<'a>, next_node:
155166
let mut previous_node = previous_node;
156167

157168
for node in siblings_between {
158-
count += get_jsx_space_expr_space_count(node);
169+
count += get_jsx_space_expr_space_count(node, program);
159170

160171
if nodes_have_only_spaces_between(previous_node, node, program) {
161172
count += 1;

tests/specs/literals/StringLiteral/StringLiteral_All.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
const t = "\t" ;
33
const u = "";
44
const v = "1";
5+
const w = "\uD800";
56

67
[expect]
78
const t = "\t";
89
const u = "";
910
const v = "1";
11+
const w = "\uD800";
1012

1113
== should not replace actual character with escaped character ==
1214
const t = " ";

tests/specs/literals/StringLiteral/StringLiteral_QuoteProps_Consistent.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,15 @@ const x = {
174174
}),
175175
"/src/shared/index.ts": `export const a: Unrestricted = 1;`,
176176
};
177+
178+
== should add quotes for this ==
179+
const objectLiteral = {
180+
"\uD800": true,
181+
test: 1,
182+
};
183+
184+
[expect]
185+
const objectLiteral = {
186+
"\uD800": true,
187+
"test": 1,
188+
};

0 commit comments

Comments
 (0)