Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@
"description": "Forces no braces when the header is one line and body is one line. Otherwise forces braces."
}]
},
"useParentheses": {
"description": "Controls how parentheses are used around expression statements.",
"type": "string",
"default": "disambiguation",
"oneOf": [{
"const": "maintain",
"description": "Maintains parentheses as written in the source code."
}, {
"const": "disambiguation",
"description": "Forces parentheses for disambiguation (object literals, function expressions, class expressions)."
}, {
"const": "preferNone",
"description": "Prefers no parentheses - only uses them when absolutely necessary to avoid breaking code."
}]
},
"bracePosition": {
"description": "Where to place the opening brace.",
"type": "string",
Expand Down Expand Up @@ -1184,6 +1199,9 @@
"conditionalType.operatorPosition": {
"$ref": "#/definitions/operatorPosition"
},
"useParentheses": {
"$ref": "#/definitions/useParentheses"
},
"arguments.preferHanging": {
"$ref": "#/definitions/preferHangingGranular"
},
Expand Down
14 changes: 10 additions & 4 deletions src/configuration/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use dprint_core::configuration::*;
/// .prefer_hanging(true)
/// .prefer_single_line(false)
/// .quote_style(QuoteStyle::PreferSingle)
/// .use_parentheses(UseParentheses::PreferNone)
/// .next_control_flow_position(NextControlFlowPosition::SameLine)
/// .build();
/// ```
Expand Down Expand Up @@ -58,7 +59,7 @@ impl ConfigurationBuilder {
.comment_line_force_space_after_slashes(false)
.construct_signature_space_after_new_keyword(true)
.constructor_type_space_after_new_keyword(true)
.arrow_function_use_parentheses(UseParentheses::Force)
.arrow_function_use_parentheses(ArrowFunctionUseParentheses::Force)
.new_line_kind(NewLineKind::LineFeed)
.function_expression_space_after_function_keyword(true)
.tagged_template_space_before_literal(false)
Expand Down Expand Up @@ -481,7 +482,7 @@ impl ConfigurationBuilder {
/// Whether to use parentheses for arrow functions.
///
/// Default: `UseParentheses::Maintain`
pub fn arrow_function_use_parentheses(&mut self, value: UseParentheses) -> &mut Self {
pub fn arrow_function_use_parentheses(&mut self, value: ArrowFunctionUseParentheses) -> &mut Self {
self.insert("arrowFunction.useParentheses", value.to_string().into())
}

Expand Down Expand Up @@ -1077,6 +1078,10 @@ impl ConfigurationBuilder {
self.insert("whileStatement.spaceAround", value.into())
}

pub fn use_parentheses(&mut self, value: UseParentheses) -> &mut Self {
self.insert("useParentheses", value.to_string().into())
}

#[cfg(test)]
pub(super) fn get_inner_config(&self) -> ConfigKeyMap {
self.config.clone()
Expand Down Expand Up @@ -1119,7 +1124,8 @@ mod tests {
.quote_props(QuoteProps::AsNeeded)
.prefer_hanging(false)
/* situational */
.arrow_function_use_parentheses(UseParentheses::Maintain)
.arrow_function_use_parentheses(ArrowFunctionUseParentheses::Maintain)
.use_parentheses(UseParentheses::PreferNone)
.binary_expression_line_per_expression(false)
.conditional_expression_line_per_expression(true)
.member_expression_line_per_expression(false)
Expand Down Expand Up @@ -1297,7 +1303,7 @@ mod tests {
.while_statement_space_around(true);

let inner_config = config.get_inner_config();
assert_eq!(inner_config.len(), 182);
assert_eq!(inner_config.len(), 183);
let diagnostics = resolve_config(inner_config, &Default::default()).diagnostics;
assert_eq!(diagnostics.len(), 0);
}
Expand Down
8 changes: 7 additions & 1 deletion src/configuration/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
semi_colons,
file_indent_level: get_value(&mut config, "fileIndentLevel", 0, &mut diagnostics),
/* situational */
arrow_function_use_parentheses: get_value(&mut config, "arrowFunction.useParentheses", UseParentheses::Maintain, &mut diagnostics),
arrow_function_use_parentheses: get_value(
&mut config,
"arrowFunction.useParentheses",
ArrowFunctionUseParentheses::Maintain,
&mut diagnostics,
),
binary_expression_line_per_expression: get_value(&mut config, "binaryExpression.linePerExpression", false, &mut diagnostics),
conditional_expression_line_per_expression: get_value(&mut config, "conditionalExpression.linePerExpression", true, &mut diagnostics),
jsx_quote_style: get_value(&mut config, "jsx.quoteStyle", quote_style.to_jsx_quote_style(), &mut diagnostics),
Expand Down Expand Up @@ -333,6 +338,7 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
switch_statement_space_around: get_value(&mut config, "switchStatement.spaceAround", space_around, &mut diagnostics),
tuple_type_space_around: get_value(&mut config, "tupleType.spaceAround", space_around, &mut diagnostics),
while_statement_space_around: get_value(&mut config, "whileStatement.spaceAround", space_around, &mut diagnostics),
use_parentheses: get_value(&mut config, "useParentheses", UseParentheses::Disambiguation, &mut diagnostics),
};

diagnostics.extend(get_unknown_property_diagnostics(config));
Expand Down
33 changes: 30 additions & 3 deletions src/configuration/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,29 @@ generate_str_to_from![
[PreferNone, "preferNone"]
];

/// Whether to use parentheses around a single parameter in an arrow function.
/// Controls how parentheses are used around expressions.
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum UseParentheses {
/// Maintains parentheses as written in the source code.
Maintain,
/// Forces parentheses for disambiguation (object literals, function expressions, class expressions at statement position).
Disambiguation,
/// Prefers no parentheses - only uses them when absolutely necessary to avoid breaking code.
PreferNone,
}

generate_str_to_from![
UseParentheses,
[Maintain, "maintain"],
[Disambiguation, "disambiguation"],
[PreferNone, "preferNone"]
];

/// Whether to use parentheses around a single parameter in an arrow function.
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ArrowFunctionUseParentheses {
/// Maintains the current state of the parentheses.
Maintain,
/// Forces parentheses.
Expand All @@ -185,7 +204,12 @@ pub enum UseParentheses {
PreferNone,
}

generate_str_to_from![UseParentheses, [Maintain, "maintain"], [Force, "force"], [PreferNone, "preferNone"]];
generate_str_to_from![
ArrowFunctionUseParentheses,
[Maintain, "maintain"],
[Force, "force"],
[PreferNone, "preferNone"]
];

/// How to decide to use single or double quotes.
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
Expand Down Expand Up @@ -320,7 +344,7 @@ pub struct Configuration {
pub file_indent_level: u32,
/* situational */
#[serde(rename = "arrowFunction.useParentheses")]
pub arrow_function_use_parentheses: UseParentheses,
pub arrow_function_use_parentheses: ArrowFunctionUseParentheses,
#[serde(rename = "binaryExpression.linePerExpression")]
pub binary_expression_line_per_expression: bool,
#[serde(rename = "conditionalExpression.linePerExpression")]
Expand Down Expand Up @@ -662,4 +686,7 @@ pub struct Configuration {
pub tuple_type_space_around: bool,
#[serde(rename = "whileStatement.spaceAround")]
pub while_statement_space_around: bool,
/* expression parentheses */
#[serde(rename = "useParentheses")]
pub use_parentheses: UseParentheses,
}
Loading
Loading