Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add newline when the right-hand-side of an assignment is multiple lines #389

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
26 changes: 24 additions & 2 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8444,7 +8444,29 @@ fn gen_assignment_like_with_token<'a>(expr: Node<'a>, op: &str, op_token: Option
} else {
conditions::indent_if_start_of_line(assignment).into()
};
let assignment = if use_new_line_group { new_line_group(assignment) } else { assignment };

let assignment = if use_new_line_group {
let mut items = PrintItems::new();

let start_assignment_ln = LineNumber::new("startAssignment");
let end_assignment_ln = LineNumber::new("endAssignment");

items.push_condition(if_true_or(
"isMultipleLines",
Rc::new(move |context| condition_helpers::is_multiple_lines(context, start_assignment_ln, end_assignment_ln)),
Signal::NewLine.into(),
Signal::PossibleNewLine.into(),
));

items.push_info(start_assignment_ln);
items.extend(new_line_group(assignment));
items.push_info(end_assignment_ln);

items
} else {
assignment
};

items.extend(assignment);
items
}
Expand All @@ -8464,7 +8486,7 @@ fn gen_assignment_like_with_token<'a>(expr: Node<'a>, op: &str, op_token: Option
return items;

fn get_use_new_line_group(expr: &Node) -> bool {
matches!(expr, Node::MemberExpr(_))
matches!(expr, Node::MemberExpr(_)) || matches!(expr, Node::MetaPropExpr(_)) || matches!(expr, Node::BinExpr(_))
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/specs/common/configuration/UseTabs_IndentWidth.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const test = testingThisOutt + testtt + testingThisOut + testingthi + outt;

[expect]
const test = testingThisOutt + testtt
const test =
testingThisOutt + testtt
+ testingThisOut + testingthi
+ outt;
3 changes: 2 additions & 1 deletion tests/specs/declarations/enum/members/EnumMember_All.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum Test {
enum Test {
memberThatIsLong =
52364363463463,
member2 = "some" + "test"
member2 =
"some" + "test"
+ "withLotsOfThese",
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ testingtesting()
.this = 523452346236 + 234345234652465246;

[expect]
testingtesting()
.this = 523452346236
+ 234345234652465246;
testingtesting().this =
523452346236 + 234345234652465246;

== should format if the right side goes over the line width ==
testing.this = testingthisouttttttttttttt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const t = this.context.currentGroup &&
!== test;

[expect]
const t = this.context.currentGroup
const t =
this.context.currentGroup
&& this.context.currentGroup.name
!== test;
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const t = this.context.currentGroup &&
test;

[expect]
const t = this.context.currentGroup &&
const t =
this.context.currentGroup &&
this.context.currentGroup.name !==
test;
9 changes: 5 additions & 4 deletions tests/specs/expressions/MetaProperty/MetaProperty_All.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const t = import
.other;

[expect]
const t = import
.meta
.test
.other;
const t =
import
.meta
.test
.other;
3 changes: 2 additions & 1 deletion tests/specs/general/Parentheses_All.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ possiblyTrue || (
);

[expect]
const binaryResult = true || false
const binaryResult =
true || false
&& possiblyTrue
|| (
true && false || maybeTrue
Expand Down
14 changes: 8 additions & 6 deletions tests/specs/issues/deno/issue014291.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ let workerId: number;
function spawnGateway(shardId: number, options: Partial<any>) {
gateway = createGatewayManager({
handleDiscordPayload: async function handleDiscordPayload(_: any, data: any, shardId: any) {
const id = (data.t && ["GUILD_CREATE", "GUILD_DELETE", "GUILD_UPDATE"].includes(data.t)
? (data.d as any)?.id
: (data.d as any)?.guild_id) ?? "000000000000000000";
const id =
(data.t && ["GUILD_CREATE", "GUILD_DELETE", "GUILD_UPDATE"].includes(data.t)
? (data.d as any)?.id
: (data.d as any)?.guild_id) ?? "000000000000000000";

// IF FINAL SHARD BECAME READY TRIGGER NEXT WORKER
if (data.t === "READY") {
Expand All @@ -29,9 +30,10 @@ let workerId: number;
function spawnGateway(shardId: number, options: Partial<any>) {
gateway = createGatewayManager({
handleDiscordPayload: async function handleDiscordPayload(_: any, data: any, shardId: any) {
const id = (data.t && ["GUILD_CREATE", "GUILD_DELETE", "GUILD_UPDATE"].includes(data.t)
? (data.d as any)?.id
: (data.d as any)?.guild_id) ?? "000000000000000000";
const id =
(data.t && ["GUILD_CREATE", "GUILD_DELETE", "GUILD_UPDATE"].includes(data.t)
? (data.d as any)?.id
: (data.d as any)?.guild_id) ?? "000000000000000000";

// IF FINAL SHARD BECAME READY TRIGGER NEXT WORKER
if (data.t === "READY") {
Expand Down
72 changes: 72 additions & 0 deletions tests/specs/issues/issue0379.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
~~ indentWidth: 4, lineWidth: 80, memberExpression.linePerExpression: true, binaryExpression.linePerExpression: true, binaryExpression.operatorPosition: sameLine ~~
== should prioritize break after the assignment operator ==
const camelcasedName = defaultName.split('-').map((x) => x.charAt(0).toUpperCase() + x.substring(1)).join('');
const namespacedName = 'Foo' + defaultName.split('-').map((x) => x.charAt(0).toUpperCase() + x.substring(1)).join('');
const namespacedName2 =
"Foo" +
defaultName
.split("-")
.map((x) => x.charAt(0).toUpperCase() + x.substring(1))
.join("");

const myObject = {
long_concatenated_value: "Hello" + "World" + "this" + "is" + "a" + "long" + "string",
long_concatenated_value_two: "This" + "expression" + "exceeds" + "the" + "line" + "width" + "on" + "its" + "own"
};

const linebreak_test_1 = "Hello" + "World" + "this" + "is" + "a" + "long" + "string";
const linebreak_test_2 = "This" + "expression" + "exceeds" + "the" + "line" + "width" + "on" + "its" + "own";

[expect]
const camelcasedName = defaultName
.split("-")
.map((x) => x.charAt(0).toUpperCase() + x.substring(1))
.join("");
const namespacedName =
"Foo" +
defaultName
.split("-")
.map((x) => x.charAt(0).toUpperCase() + x.substring(1))
.join("");
const namespacedName2 =
"Foo" +
defaultName
.split("-")
.map((x) => x.charAt(0).toUpperCase() + x.substring(1))
.join("");

const myObject = {
long_concatenated_value:
"Hello" + "World" + "this" + "is" + "a" + "long" + "string",
long_concatenated_value_two:
"This" +
"expression" +
"exceeds" +
"the" +
"line" +
"width" +
"on" +
"its" +
"own",
};

const linebreak_test_1 =
"Hello" + "World" + "this" + "is" + "a" + "long" + "string";
const linebreak_test_2 =
"This" +
"expression" +
"exceeds" +
"the" +
"line" +
"width" +
"on" +
"its" +
"own";

== should prioritize break after the assignment operator ==
const linebreak_test_1 =
"Hello World this" +
"is a long string to column sixty-five.";
[expect]
const linebreak_test_1 =
"Hello World this" + "is a long string to column sixty-five.";
4 changes: 2 additions & 2 deletions tests/specs/jsx/JsxElement/JsxElement_All.txt
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ const t = (
</div>
);
// this has the && right at the line width max so the newline will happen before
const testing = someConditionLongEnoughTestTest
&& (
const testing =
someConditionLongEnoughTestTest && (
<td className="some-class-name">
{someValue}
</td>
Expand Down
19 changes: 9 additions & 10 deletions tests/specs/statements/variableStatement/VariableStatement_All.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ const t: Testing,
const testing = testtttttttt && testtttttttt, other = testtttttttt && testttttttttttt;

[expect]
const testing = testtttttttt
&& testtttttttt,
other = testtttttttt
&& testttttttttttt;
const testing =
testtttttttt && testtttttttt,
other =
testtttttttt && testttttttttttt;

== should handle multiple declarations that span multiple lines with an object ==
const varA = { testttttttttttttttttttttttttttt }, varB = testtttttttt && testttttttttttt;
Expand All @@ -50,8 +50,8 @@ const varA = { testttttttttttttttttttttttttttt }, varB = testtttttttt && testttt
const varA = {
testttttttttttttttttttttttttttt,
},
varB = testtttttttt
&& testttttttttttt;
varB =
testtttttttt && testttttttttttt;

== should handle multiple declarations that span multiple lines with comments in between ==
const varA = /* test*/
Expand All @@ -60,8 +60,7 @@ const varA = /* test*/
testttttttttttttttttttttttttttt,
}, // test
// testing
varB = testtttttttt
&& testttttttttttt;
varB = testtttttttt && testttttttttttt;

[expect]
const varA = /* test*/
Expand All @@ -70,8 +69,8 @@ const varA = /* test*/
testttttttttttttttttttttttttttt,
}, // test
// testing
varB = testtttttttt
&& testttttttttttt;
varB =
testtttttttt && testttttttttttt;

== should handle multiple declarations that span multiple lines with object destructuring ==
const { testing, othertestingggggggggggggggg } = {
Expand Down