Skip to content
Merged
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
31 changes: 29 additions & 2 deletions deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,30 @@
"description": ""
}]
},
"preferSingleLineDefaultTrue": {
"description": "If code should revert back from being on multiple lines to being on a single line when able. Defaults to the global `preferSingleLine` value when it's set and otherwise `true`.",
"type": "boolean",
"default": true,
"oneOf": [{
"const": true,
"description": ""
}, {
"const": false,
"description": ""
}]
},
"jsxExpressionContainer.preferSingleLine": {
"description": "Whether to keep a JSX expression container's braces beside its expression when there's a newline between the open brace and the expression. Only the newline after the open brace is checked, so when this is `false` the close brace goes on its own line even if it was beside the expression. Defaults to the global `preferSingleLine` value when it's set and otherwise `true`.",
"type": "boolean",
"default": true,
"oneOf": [{
"const": true,
"description": "Keeps the braces beside the expression even when there's a newline after the open brace. Ex. `{value}`"
}, {
"const": false,
"description": "Puts the braces on their own lines with the expression indented between them when there's a newline after the open brace."
}]
},
"forceSingleLine": {
"description": "If code should be forced to be on a single line if able.",
"type": "boolean",
Expand Down Expand Up @@ -1733,20 +1757,23 @@
"$ref": "#/definitions/preferSingleLine"
},
"exportDeclaration.preferSingleLine": {
"$ref": "#/definitions/preferSingleLine"
"$ref": "#/definitions/preferSingleLineDefaultTrue"
},
"forStatement.preferSingleLine": {
"$ref": "#/definitions/preferSingleLine"
},
"importDeclaration.preferSingleLine": {
"$ref": "#/definitions/preferSingleLine"
"$ref": "#/definitions/preferSingleLineDefaultTrue"
},
"jsxAttributes.preferSingleLine": {
"$ref": "#/definitions/preferSingleLine"
},
"jsxElement.preferSingleLine": {
"$ref": "#/definitions/preferSingleLine"
},
"jsxExpressionContainer.preferSingleLine": {
"$ref": "#/definitions/jsxExpressionContainer.preferSingleLine"
},
"mappedType.preferSingleLine": {
"$ref": "#/definitions/preferSingleLine"
},
Expand Down
14 changes: 13 additions & 1 deletion src/configuration/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,17 @@ impl ConfigurationBuilder {
self.insert("jsxElement.preferSingleLine", value.into())
}

/// Whether to keep a JSX expression container's braces beside its expression
/// when there's a newline after the open brace.
///
/// * `true` - Keeps the braces beside the expression. Ex. `{value}`
/// * `false` - Puts the braces on their own lines when there's a newline after the open brace.
///
/// Default: The global `preferSingleLine` value when set, otherwise `true`.
pub fn jsx_expression_container_prefer_single_line(&mut self, value: bool) -> &mut Self {
self.insert("jsxExpressionContainer.preferSingleLine", value.into())
}

pub fn mapped_type_prefer_single_line(&mut self, value: bool) -> &mut Self {
self.insert("mappedType.preferSingleLine", value.into())
}
Expand Down Expand Up @@ -1635,6 +1646,7 @@ mod tests {
.import_declaration_prefer_single_line(false)
.jsx_attributes_prefer_single_line(false)
.jsx_element_prefer_single_line(false)
.jsx_expression_container_prefer_single_line(false)
.mapped_type_prefer_single_line(false)
.member_expression_prefer_single_line(false)
.object_expression_prefer_single_line(false)
Expand Down Expand Up @@ -1705,7 +1717,7 @@ mod tests {
.with_statement_space_around(true);

let inner_config = config.get_inner_config();
assert_eq!(inner_config.len(), 231);
assert_eq!(inner_config.len(), 232);
let diagnostics = resolve_config(inner_config, &Default::default()).diagnostics;
assert_eq!(diagnostics.len(), 0);
}
Expand Down
29 changes: 29 additions & 0 deletions src/configuration/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
),
jsx_attributes_prefer_single_line: get_value(&mut config, "jsxAttributes.preferSingleLine", prefer_single_line, &mut diagnostics),
jsx_element_prefer_single_line: get_value(&mut config, "jsxElement.preferSingleLine", prefer_single_line, &mut diagnostics),
jsx_expression_container_prefer_single_line: get_value(
&mut config,
"jsxExpressionContainer.preferSingleLine",
prefer_single_line_nullable.unwrap_or(true),
&mut diagnostics,
),
mapped_type_prefer_single_line: get_value(&mut config, "mappedType.preferSingleLine", prefer_single_line, &mut diagnostics),
member_expression_prefer_single_line: get_value(&mut config, "memberExpression.preferSingleLine", prefer_single_line, &mut diagnostics),
object_expression_prefer_single_line: get_value(&mut config, "objectExpression.preferSingleLine", prefer_single_line, &mut diagnostics),
Expand Down Expand Up @@ -825,6 +831,29 @@ mod tests {
assert_eq!(result.diagnostics.len(), 0);
}

#[test]
fn handle_jsx_expression_container_prefer_single_line() {
let resolve = |values: &[(&str, bool)]| {
let mut config = ConfigKeyMap::new();
for (key, value) in values {
config.insert(key.to_string(), ConfigKeyValue::from_bool(*value));
}
let result = resolve_config(config, &GlobalConfiguration::default());
assert_eq!(result.diagnostics.len(), 0);
result.config.jsx_expression_container_prefer_single_line
};

// defaults to true when the global value isn't set
assert!(resolve(&[]));
// falls back to the global value when it's set
assert!(!resolve(&[("preferSingleLine", false)]));
assert!(resolve(&[("preferSingleLine", true)]));
// the per-node value wins over the global value
assert!(resolve(&[("preferSingleLine", false), ("jsxExpressionContainer.preferSingleLine", true)]));
assert!(!resolve(&[("preferSingleLine", true), ("jsxExpressionContainer.preferSingleLine", false)]));
assert!(!resolve(&[("jsxExpressionContainer.preferSingleLine", false)]));
}

#[test]
fn handle_sort_type_only_imports_exports() {
for (value, expected) in [
Expand Down
2 changes: 2 additions & 0 deletions src/configuration/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,8 @@ pub struct Configuration {
pub jsx_attributes_prefer_single_line: bool,
#[serde(rename = "jsxElement.preferSingleLine")]
pub jsx_element_prefer_single_line: bool,
#[serde(rename = "jsxExpressionContainer.preferSingleLine")]
pub jsx_expression_container_prefer_single_line: bool,
#[serde(rename = "mappedType.preferSingleLine")]
pub mapped_type_prefer_single_line: bool,
#[serde(rename = "memberExpression.preferSingleLine")]
Expand Down
49 changes: 46 additions & 3 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4594,7 +4594,13 @@ fn handle_jsx_surrounding_parens<'a>(inner_items: PrintItems<'a>, context: &mut
}
}

if context.parent().is::<JSXExprContainer>() && context.config.jsx_multi_line_parens != JsxMultiLineParens::Always {
if let Some(container) = context.parent().to::<JSXExprContainer>()
&& context.config.jsx_multi_line_parens != JsxMultiLineParens::Always
{
// the container already puts the jsx on its own lines
if should_surround_jsx_expr_container_expr_with_newlines(container, context) {
return inner_items;
}
return surround_with_newlines_indented_if_multi_line(inner_items, context.config.indent_width);
}

Expand Down Expand Up @@ -4648,7 +4654,11 @@ fn handle_jsx_surrounding_parens<'a>(inner_items: PrintItems<'a>, context: &mut
parent = parent.parent().unwrap();
}

parent.is::<JSXExprContainer>()
match parent.to::<JSXExprContainer>() {
// the container already puts the jsx on its own lines
Some(container) => !should_surround_jsx_expr_container_expr_with_newlines(container, context),
None => false,
}
}
}

Expand Down Expand Up @@ -4800,7 +4810,7 @@ fn gen_jsx_expr_container<'a>(node: &JSXExprContainer<'a>, context: &mut Context
gen_as_jsx_expr_container_with_new_lines(node.expr.range(), gen_inner, surround_with_new_lines, surround_with_space, context)
}
JSXExpr::Expr(expr) => {
let surround_with_new_lines = should_surround_jsx_expr_container_with_newlines(node.expr.range(), context.program);
let surround_with_new_lines = should_surround_jsx_expr_container_expr_with_newlines(node, context);
// keep an object or array literal's brackets beside the braces (ex. `{{ value: true }}`)
let surround_with_space =
context.config.jsx_expression_container_space_surrounding_expression && !is_jsx_expr_container_object_or_array_literal(*expr, context);
Expand Down Expand Up @@ -4868,6 +4878,39 @@ fn should_surround_jsx_expr_container_with_newlines(expr: SourceRange, program:
false
}

/// Gets if the braces of a container with an expression go on their own lines
/// because of comments or a newline after the open brace that's maintained.
fn should_surround_jsx_expr_container_expr_with_newlines<'a>(node: &JSXExprContainer<'a>, context: &Context<'a>) -> bool {
should_surround_jsx_expr_container_with_newlines(node.expr.range(), context.program)
|| !context.config.jsx_expression_container_prefer_single_line
&& has_new_line_after_jsx_expr_container_open_brace(node, context.program)
// the parens already put multi-line jsx on its own lines (ex. `{(` and `)}`)
&& !is_jsx_surrounded_by_multi_line_parens(node, context)
}

/// Gets if the container's expression starts on a line after its open brace.
fn has_new_line_after_jsx_expr_container_open_brace(node: &JSXExprContainer, program: Program) -> bool {
match node.expr {
JSXExpr::Expr(expr) => node.start_line_fast(program) != expr.start_line_fast(program),
JSXExpr::JSXEmptyExpr(_) => false,
}
}

/// Gets if the container's expression is jsx that `jsx.multiLineParens: always`
/// surrounds with parens when it's on multiple lines.
fn is_jsx_surrounded_by_multi_line_parens<'a>(node: &JSXExprContainer<'a>, context: &Context<'a>) -> bool {
if context.config.jsx_multi_line_parens != JsxMultiLineParens::Always {
return false;
}
let JSXExpr::Expr(mut expr) = node.expr else {
return false;
};
while let Expr::Paren(paren_expr) = expr {
expr = paren_expr.expr;
}
is_jsx_paren_expr_handled_node(expr.into(), context)
}

/// Gets if the expression will be generated as an object or array literal
/// with no comments between it and the braces of its container.
fn is_jsx_expr_container_object_or_array_literal<'a>(expr: Expr<'a>, context: &Context<'a>) -> bool {
Expand Down
69 changes: 69 additions & 0 deletions tests/specs/issues/issue0577.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
-- file.tsx --
~~ indentWidth: 4, jsxExpressionContainer.preferSingleLine: false ~~
== should keep the braces of a jsx expression container on their own lines ==
function Ok() {
return (
<div>
{
isThisConditionOk
? (
<p><strong>Yes</strong></p>
)
: (
<p><strong>No</strong></p>
)
}
</div>
);
}

[expect]
function Ok() {
return (
<div>
{
isThisConditionOk
? (
<p>
<strong>Yes</strong>
</p>
)
: (
<p>
<strong>No</strong>
</p>
)
}
</div>
);
}

== should keep the braces of a jsx attribute value on their own lines ==
function Ok() {
return (
<div
render={
work(
nice,
to,
have
)
}
/>
);
}

[expect]
function Ok() {
return (
<div
render={
work(
nice,
to,
have,
)
}
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
-- file.tsx --
== should keep the braces beside the expression when there's a newline after the open brace ==
const t = <div>
{
value
}
</div>;
const u = <div
render={
work(
nice,
to,
have
)
}
/>;

[expect]
const t = (
<div>
{value}
</div>
);
const u = (
<div
render={work(
nice,
to,
have,
)}
/>
);

== should keep the braces beside a conditional with jsx ==
const t = <div>
{
isThisConditionOk
? <p><strong>Yes</strong></p>
: <p><strong>No</strong></p>
}
</div>;

[expect]
const t = (
<div>
{isThisConditionOk
? (
<p>
<strong>Yes</strong>
</p>
)
: (
<p>
<strong>No</strong>
</p>
)}
</div>
);

== should not add blank lines around multi-line jsx after a comment ==
const t = <div>{
// comment
<span>Yes<b>yes</b></span>
}</div>;
const u = <div attr={
// comment
<span>Yes<b>yes</b></span>
} />;

[expect]
const t = (
<div>
{
// comment
<span>
Yes<b>yes</b>
</span>
}
</div>
);
const u = (
<div
attr={
// comment
<span>
Yes<b>yes</b>
</span>
}
/>
);
Loading
Loading