Skip to content

Commit

Permalink
fix: Ensure non-top most conditional expr is indented.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jul 6, 2020
1 parent e716d97 commit f648e87
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/parsing/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,21 @@ fn parse_conditional_expr<'a>(node: &'a CondExpr, context: &mut Context<'a>) ->
if top_most_data.is_top_most {
items.push_condition(conditions::indent_if_start_of_line(cons_and_alt_items));
} else {
items.extend(cons_and_alt_items);
let cons_and_alt_items = cons_and_alt_items.into_rc_path();
let top_most_info = top_most_data.top_most_info;
items.push_condition(if_true_or(
"indentIfSameIndentationAsTopMostAndStartOfLine",
move |context| {
if context.writer_info.is_start_of_line() {
let top_most_info = context.get_resolved_info(&top_most_info)?;
Some(context.writer_info.indent_level == top_most_info.indent_level)
} else {
Some(false)
}
},
with_indent(cons_and_alt_items.clone().into()),
cons_and_alt_items.into(),
));
}

return items;
Expand Down
28 changes: 28 additions & 0 deletions tests/specs/issues/issue0022.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
~~ deno: true ~~
== should format as-is. This could probably be better and note that the entire assignment is the conditional expression ==
const es5Bundle = test === asdf ? true : false ||
target === ts.ScriptTarget.ES2016
? true
: false

[expect]
const es5Bundle = test === asdf ? true : false ||
target === ts.ScriptTarget.ES2016
? true
: false;

== should format as-is ==
const es5Bundle = target === ts.ScriptTarget.ES3 ||
target === ts.ScriptTarget.ES5 ||
target === ts.ScriptTarget.ES2015 ||
target === ts.ScriptTarget.ES2016
? true
: false;

[expect]
const es5Bundle = target === ts.ScriptTarget.ES3 ||
target === ts.ScriptTarget.ES5 ||
target === ts.ScriptTarget.ES2015 ||
target === ts.ScriptTarget.ES2016
? true
: false;

0 comments on commit f648e87

Please sign in to comment.