Skip to content

Commit 681090e

Browse files
authored
feat: add doWhileStatement.nextControlFlowPosition (#434)
1 parent 9f086d0 commit 681090e

File tree

8 files changed

+106
-8
lines changed

8 files changed

+106
-8
lines changed

deployment/schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,9 @@
10491049
"tryStatement.nextControlFlowPosition": {
10501050
"$ref": "#/definitions/nextControlFlowPosition"
10511051
},
1052+
"doWhileStatement.nextControlFlowPosition": {
1053+
"$ref": "#/definitions/nextControlFlowPosition"
1054+
},
10521055
"arguments.trailingCommas": {
10531056
"$ref": "#/definitions/trailingCommas"
10541057
},

src/configuration/builder.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,10 @@ impl ConfigurationBuilder {
749749
self.insert("tryStatement.nextControlFlowPosition", value.to_string().into())
750750
}
751751

752+
pub fn do_while_statement_next_control_flow_position(&mut self, value: NextControlFlowPosition) -> &mut Self {
753+
self.insert("doWhileStatement.nextControlFlowPosition", value.to_string().into())
754+
}
755+
752756
/* operator position */
753757

754758
pub fn binary_expression_operator_position(&mut self, value: OperatorPosition) -> &mut Self {
@@ -1112,6 +1116,7 @@ mod tests {
11121116
/* next control flow position */
11131117
.if_statement_next_control_flow_position(NextControlFlowPosition::SameLine)
11141118
.try_statement_next_control_flow_position(NextControlFlowPosition::SameLine)
1119+
.do_while_statement_next_control_flow_position(NextControlFlowPosition::SameLine)
11151120
/* operator position */
11161121
.binary_expression_operator_position(OperatorPosition::SameLine)
11171122
.conditional_expression_operator_position(OperatorPosition::SameLine)
@@ -1214,7 +1219,7 @@ mod tests {
12141219
.while_statement_space_around(true);
12151220

12161221
let inner_config = config.get_inner_config();
1217-
assert_eq!(inner_config.len(), 171);
1222+
assert_eq!(inner_config.len(), 172);
12181223
let diagnostics = resolve_config(inner_config, &resolve_global_config(ConfigKeyMap::new(), &Default::default()).config).diagnostics;
12191224
assert_eq!(diagnostics.len(), 0);
12201225
}

src/configuration/resolve_config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
162162
next_control_flow_position,
163163
&mut diagnostics,
164164
),
165+
do_while_statement_next_control_flow_position: get_value(
166+
&mut config,
167+
"doWhileStatement.nextControlFlowPosition",
168+
next_control_flow_position,
169+
&mut diagnostics,
170+
),
165171
/* operator position */
166172
binary_expression_operator_position: get_value(&mut config, "binaryExpression.operatorPosition", operator_position, &mut diagnostics),
167173
conditional_expression_operator_position: get_value(&mut config, "conditionalExpression.operatorPosition", operator_position, &mut diagnostics),

src/configuration/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ pub struct Configuration {
409409
pub if_statement_next_control_flow_position: NextControlFlowPosition,
410410
#[serde(rename = "tryStatement.nextControlFlowPosition")]
411411
pub try_statement_next_control_flow_position: NextControlFlowPosition,
412+
#[serde(rename = "doWhileStatement.nextControlFlowPosition")]
413+
pub do_while_statement_next_control_flow_position: NextControlFlowPosition,
412414
/* operator position */
413415
#[serde(rename = "binaryExpression.operatorPosition")]
414416
pub binary_expression_operator_position: OperatorPosition,

src/generation/generate.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4287,7 +4287,15 @@ fn gen_do_while_stmt<'a>(node: &'a DoWhileStmt, context: &mut Context<'a>) -> Pr
42874287
context,
42884288
));
42894289
items.extend(gen_node(node.body.into(), context));
4290-
items.push_str(" while");
4290+
items.extend(gen_control_flow_separator(
4291+
context.config.do_while_statement_next_control_flow_position,
4292+
&node.body.range(),
4293+
"while",
4294+
None,
4295+
None,
4296+
context,
4297+
));
4298+
items.push_str("while");
42914299
if context.config.do_while_statement_space_after_while_keyword {
42924300
items.push_str(" ");
42934301
}
@@ -4738,7 +4746,7 @@ fn gen_if_stmt<'a>(node: &'a IfStmt, context: &mut Context<'a>) -> PrintItems {
47384746
context.config.if_statement_next_control_flow_position,
47394747
&cons_range,
47404748
"else",
4741-
if_stmt_start_ln,
4749+
Some(if_stmt_start_ln),
47424750
Some(result.close_brace_condition_ref),
47434751
context,
47444752
));
@@ -4995,7 +5003,7 @@ fn gen_try_stmt<'a>(node: &'a TryStmt, context: &mut Context<'a>) -> PrintItems
49955003
next_control_flow_position,
49965004
&last_block_range,
49975005
"catch",
4998-
last_block_start_ln,
5006+
Some(last_block_start_ln),
49995007
None,
50005008
context,
50015009
));
@@ -5011,7 +5019,7 @@ fn gen_try_stmt<'a>(node: &'a TryStmt, context: &mut Context<'a>) -> PrintItems
50115019
next_control_flow_position,
50125020
&last_block_range,
50135021
"finally",
5014-
last_block_start_ln,
5022+
Some(last_block_start_ln),
50155023
None,
50165024
context,
50175025
));
@@ -7892,7 +7900,7 @@ fn gen_control_flow_separator(
78927900
next_control_flow_position: NextControlFlowPosition,
78937901
previous_node_block: &SourceRange,
78947902
token_text: &str,
7895-
previous_start_ln: LineNumber,
7903+
previous_start_ln: Option<LineNumber>,
78967904
previous_close_brace_condition_ref: Option<ConditionReference>,
78977905
context: &mut Context,
78987906
) -> PrintItems {
@@ -7903,8 +7911,10 @@ fn gen_control_flow_separator(
79037911
"newLineOrSpace",
79047912
Rc::new(move |condition_context| {
79057913
// newline if on the same line as the previous
7906-
if condition_helpers::is_on_same_line(condition_context, previous_start_ln)? {
7907-
return Some(true);
7914+
if let Some(previous_start_ln) = previous_start_ln {
7915+
if condition_helpers::is_on_same_line(condition_context, previous_start_ln)? {
7916+
return Some(true);
7917+
}
79087918
}
79097919

79107920
// newline if the previous did not have a close brace
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
~~ doWhileStatement.nextControlFlowPosition: maintain, lineWidth: 40 ~~
2+
== should maintain the position when on same line ==
3+
do {
4+
console.log("hello")
5+
} while (1 === 1);
6+
7+
[expect]
8+
do {
9+
console.log("hello");
10+
} while (1 === 1);
11+
12+
== should maintain the position when else if on next line ==
13+
do {
14+
console.log("hello");
15+
}
16+
while (1 === 1);
17+
18+
[expect]
19+
do {
20+
console.log("hello");
21+
}
22+
while (1 === 1);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
~~ doWhileStatement.nextControlFlowPosition: nextLine, lineWidth: 40 ~~
2+
== should use the next line for the new control flow position ==
3+
do {
4+
console.log("hello")
5+
} while (1 === 1);
6+
7+
[expect]
8+
do {
9+
console.log("hello");
10+
}
11+
while (1 === 1);
12+
13+
== should stay on next line ==
14+
do {
15+
console.log("hello")
16+
}
17+
while (1 === 1);
18+
19+
[expect]
20+
do {
21+
console.log("hello");
22+
}
23+
while (1 === 1);
24+
25+
== should keep comments before while ==
26+
do {
27+
console.log("hello")
28+
}
29+
// good enough for now
30+
while (1 === 1);
31+
32+
[expect]
33+
do {
34+
console.log("hello");
35+
}
36+
while (
37+
// good enough for now
38+
1 === 1
39+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
~~ doWhileStatement.nextControlFlowPosition: sameLine, lineWidth: 40 ~~
2+
== should use the same line for the new control flow position ==
3+
do {
4+
console.log("hello")
5+
}
6+
while (1 === 1);
7+
8+
[expect]
9+
do {
10+
console.log("hello");
11+
} while (1 === 1);

0 commit comments

Comments
 (0)