Skip to content

Commit

Permalink
feat: add catchClause.spaceAround (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thristhart authored Feb 8, 2024
1 parent 90a5d64 commit 7ec9752
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
3 changes: 3 additions & 0 deletions deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,9 @@
"arrayPattern.spaceAround": {
"$ref": "#/definitions/spaceAround"
},
"catchClause.spaceAround": {
"$ref": "#/definitions/spaceAround"
},
"doWhileStatement.spaceAround": {
"$ref": "#/definitions/spaceAround"
},
Expand Down
7 changes: 6 additions & 1 deletion src/configuration/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,10 @@ impl ConfigurationBuilder {
self.insert("arrayPattern.spaceAround", value.into())
}

pub fn catch_clause_space_around(&mut self, value: bool) -> &mut Self {
self.insert("catchClause.spaceAround", value.into())
}

pub fn do_while_statement_space_around(&mut self, value: bool) -> &mut Self {
self.insert("doWhileStatement.spaceAround", value.into())
}
Expand Down Expand Up @@ -1252,6 +1256,7 @@ mod tests {
.arguments_space_around(true)
.array_expression_space_around(true)
.array_pattern_space_around(true)
.catch_clause_space_around(true)
.do_while_statement_space_around(true)
.for_in_statement_space_around(true)
.for_of_statement_space_around(true)
Expand All @@ -1264,7 +1269,7 @@ mod tests {
.while_statement_space_around(true);

let inner_config = config.get_inner_config();
assert_eq!(inner_config.len(), 178);
assert_eq!(inner_config.len(), 179);
let diagnostics = resolve_config(inner_config, &Default::default()).diagnostics;
assert_eq!(diagnostics.len(), 0);
}
Expand Down
1 change: 1 addition & 0 deletions src/configuration/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
arguments_space_around: get_value(&mut config, "arguments.spaceAround", space_around, &mut diagnostics),
array_expression_space_around: get_value(&mut config, "arrayExpression.spaceAround", space_around, &mut diagnostics),
array_pattern_space_around: get_value(&mut config, "arrayPattern.spaceAround", space_around, &mut diagnostics),
catch_clause_space_around: get_value(&mut config, "catchClause.spaceAround", space_around, &mut diagnostics),
do_while_statement_space_around: get_value(&mut config, "doWhileStatement.spaceAround", space_around, &mut diagnostics),
for_in_statement_space_around: get_value(&mut config, "forInStatement.spaceAround", space_around, &mut diagnostics),
for_of_statement_space_around: get_value(&mut config, "forOfStatement.spaceAround", space_around, &mut diagnostics),
Expand Down
2 changes: 2 additions & 0 deletions src/configuration/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ pub struct Configuration {
pub array_expression_space_around: bool,
#[serde(rename = "arrayPattern.spaceAround")]
pub array_pattern_space_around: bool,
#[serde(rename = "catchClause.spaceAround")]
pub catch_clause_space_around: bool,
#[serde(rename = "doWhileStatement.spaceAround")]
pub do_while_statement_space_around: bool,
#[serde(rename = "forInStatement.spaceAround")]
Expand Down
6 changes: 6 additions & 0 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,13 @@ fn gen_catch_clause<'a>(node: &CatchClause<'a>, context: &mut Context<'a>) -> Pr

if let Some(param) = &node.param {
items.push_str(" (");
if context.config.catch_clause_space_around {
items.push_str(" ");
}
items.extend(gen_node(param.into(), context));
if context.config.catch_clause_space_around {
items.push_str(" ");
}
items.push_str(")");
}
items.push_info(end_header_ln);
Expand Down
42 changes: 42 additions & 0 deletions tests/specs/clauses/catchClause/CatchClause_SpaceAround_True.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
~~ catchClause.spaceAround: true ~~
== should output without param ==
try{
a;
} catch
{
b;
}

[expect]
try {
a;
} catch {
b;
}

== should output with param ==
try{
a;
}
catch(ex){
b;
}

[expect]
try {
a;
} catch ( ex ) {
b;
}

== should output with type on param ==
try {
a
} catch ( ex :unknown ) {
}

[expect]
try {
a;
} catch ( ex: unknown ) {
}

0 comments on commit 7ec9752

Please sign in to comment.