Skip to content

Commit f648e87

Browse files
committed
fix: Ensure non-top most conditional expr is indented.
1 parent e716d97 commit f648e87

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/parsing/parser.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,21 @@ fn parse_conditional_expr<'a>(node: &'a CondExpr, context: &mut Context<'a>) ->
17631763
if top_most_data.is_top_most {
17641764
items.push_condition(conditions::indent_if_start_of_line(cons_and_alt_items));
17651765
} else {
1766-
items.extend(cons_and_alt_items);
1766+
let cons_and_alt_items = cons_and_alt_items.into_rc_path();
1767+
let top_most_info = top_most_data.top_most_info;
1768+
items.push_condition(if_true_or(
1769+
"indentIfSameIndentationAsTopMostAndStartOfLine",
1770+
move |context| {
1771+
if context.writer_info.is_start_of_line() {
1772+
let top_most_info = context.get_resolved_info(&top_most_info)?;
1773+
Some(context.writer_info.indent_level == top_most_info.indent_level)
1774+
} else {
1775+
Some(false)
1776+
}
1777+
},
1778+
with_indent(cons_and_alt_items.clone().into()),
1779+
cons_and_alt_items.into(),
1780+
));
17671781
}
17681782

17691783
return items;

tests/specs/issues/issue0022.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
~~ deno: true ~~
2+
== should format as-is. This could probably be better and note that the entire assignment is the conditional expression ==
3+
const es5Bundle = test === asdf ? true : false ||
4+
target === ts.ScriptTarget.ES2016
5+
? true
6+
: false
7+
8+
[expect]
9+
const es5Bundle = test === asdf ? true : false ||
10+
target === ts.ScriptTarget.ES2016
11+
? true
12+
: false;
13+
14+
== should format as-is ==
15+
const es5Bundle = target === ts.ScriptTarget.ES3 ||
16+
target === ts.ScriptTarget.ES5 ||
17+
target === ts.ScriptTarget.ES2015 ||
18+
target === ts.ScriptTarget.ES2016
19+
? true
20+
: false;
21+
22+
[expect]
23+
const es5Bundle = target === ts.ScriptTarget.ES3 ||
24+
target === ts.ScriptTarget.ES5 ||
25+
target === ts.ScriptTarget.ES2015 ||
26+
target === ts.ScriptTarget.ES2016
27+
? true
28+
: false;

0 commit comments

Comments
 (0)