Skip to content

Commit 49823bf

Browse files
authored
fix: multi-line string indent was broken in some cases (#566)
1 parent cfb13dc commit 49823bf

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

src/generation/generate.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3992,22 +3992,22 @@ mod string_literal {
39923992
use super::*;
39933993

39943994
pub fn gen_non_jsx_text(string_value: &str, context: &mut Context) -> PrintItems {
3995-
gen_from_raw_string(&match context.config.quote_style {
3995+
match context.config.quote_style {
39963996
QuoteStyle::AlwaysDouble => format_with_double(string_value),
39973997
QuoteStyle::AlwaysSingle => format_with_single(string_value),
39983998
QuoteStyle::PreferDouble => handle_prefer_double(string_value),
39993999
QuoteStyle::PreferSingle => handle_prefer_single(string_value),
4000-
})
4000+
}
40014001
}
40024002

40034003
pub fn gen_jsx_text(string_value: &str, context: &mut Context) -> PrintItems {
40044004
// JSX attributes cannot contain escaped quotes so regardless of
40054005
// configuration, allow changing the quote style to single or
40064006
// double depending on if it contains the opposite quote
4007-
gen_from_raw_string(&match context.config.jsx_quote_style {
4007+
match context.config.jsx_quote_style {
40084008
JsxQuoteStyle::PreferDouble => handle_prefer_double(string_value),
40094009
JsxQuoteStyle::PreferSingle => handle_prefer_single(string_value),
4010-
})
4010+
}
40114011
}
40124012

40134013
pub fn get_value(node: &Str, context: &mut Context) -> String {
@@ -4049,28 +4049,36 @@ mod string_literal {
40494049
}
40504050
}
40514051

4052-
fn handle_prefer_double(string_value: &str) -> String {
4052+
fn handle_prefer_double(string_value: &str) -> PrintItems {
40534053
if double_to_single(string_value) <= 0 {
40544054
format_with_double(string_value)
40554055
} else {
40564056
format_with_single(string_value)
40574057
}
40584058
}
40594059

4060-
fn handle_prefer_single(string_value: &str) -> String {
4060+
fn handle_prefer_single(string_value: &str) -> PrintItems {
40614061
if double_to_single(string_value) >= 0 {
40624062
format_with_single(string_value)
40634063
} else {
40644064
format_with_double(string_value)
40654065
}
40664066
}
40674067

4068-
fn format_with_double(string_value: &str) -> String {
4069-
format!("\"{}\"", string_value.replace('"', "\\\""))
4068+
fn format_with_double(string_value: &str) -> PrintItems {
4069+
let mut items = PrintItems::new();
4070+
items.push_str("\"");
4071+
items.extend(gen_from_raw_string(&string_value.replace('"', "\\\"")));
4072+
items.push_str("\"");
4073+
items
40704074
}
40714075

4072-
fn format_with_single(string_value: &str) -> String {
4073-
format!("'{}'", string_value.replace('\'', "\\'"))
4076+
fn format_with_single(string_value: &str) -> PrintItems {
4077+
let mut items = PrintItems::new();
4078+
items.push_str("'");
4079+
items.extend(gen_from_raw_string(&string_value.replace('\'', "\\'")));
4080+
items.push_str("'");
4081+
items
40744082
}
40754083

40764084
fn double_to_single(string_value: &str) -> i32 {

tests/specs/issues/issue0565.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
== should format multi-line string and keep start of string indented ==
2+
asdf(
3+
"Testing \
4+
this out",
5+
async () => {
6+
// test
7+
}
8+
);
9+
10+
[expect]
11+
asdf(
12+
"Testing \
13+
this out",
14+
async () => {
15+
// test
16+
},
17+
);

0 commit comments

Comments
 (0)