diff --git a/src/generation/generate.rs b/src/generation/generate.rs index 23374f77..bf49107b 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -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 } @@ -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(_)) } } diff --git a/tests/specs/common/configuration/UseTabs_IndentWidth.txt b/tests/specs/common/configuration/UseTabs_IndentWidth.txt index 22e623c2..53501ad0 100644 --- a/tests/specs/common/configuration/UseTabs_IndentWidth.txt +++ b/tests/specs/common/configuration/UseTabs_IndentWidth.txt @@ -3,6 +3,7 @@ const test = testingThisOutt + testtt + testingThisOut + testingthi + outt; [expect] -const test = testingThisOutt + testtt +const test = + testingThisOutt + testtt + testingThisOut + testingthi + outt; diff --git a/tests/specs/declarations/enum/members/EnumMember_All.txt b/tests/specs/declarations/enum/members/EnumMember_All.txt index 895cda93..7b0dbcc1 100644 --- a/tests/specs/declarations/enum/members/EnumMember_All.txt +++ b/tests/specs/declarations/enum/members/EnumMember_All.txt @@ -25,6 +25,7 @@ enum Test { enum Test { memberThatIsLong = 52364363463463, - member2 = "some" + "test" + member2 = + "some" + "test" + "withLotsOfThese", } diff --git a/tests/specs/expressions/AssignmentExpression/AssignmentExpression_All.txt b/tests/specs/expressions/AssignmentExpression/AssignmentExpression_All.txt index 70e6af12..7c460231 100644 --- a/tests/specs/expressions/AssignmentExpression/AssignmentExpression_All.txt +++ b/tests/specs/expressions/AssignmentExpression/AssignmentExpression_All.txt @@ -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; diff --git a/tests/specs/expressions/BinaryExpression/BinaryExpression_OperatorPosition_NextLine.txt b/tests/specs/expressions/BinaryExpression/BinaryExpression_OperatorPosition_NextLine.txt index faf48484..4c1f53d9 100644 --- a/tests/specs/expressions/BinaryExpression/BinaryExpression_OperatorPosition_NextLine.txt +++ b/tests/specs/expressions/BinaryExpression/BinaryExpression_OperatorPosition_NextLine.txt @@ -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; diff --git a/tests/specs/expressions/BinaryExpression/BinaryExpression_OperatorPosition_SameLine.txt b/tests/specs/expressions/BinaryExpression/BinaryExpression_OperatorPosition_SameLine.txt index ead93ccc..fdd89298 100644 --- a/tests/specs/expressions/BinaryExpression/BinaryExpression_OperatorPosition_SameLine.txt +++ b/tests/specs/expressions/BinaryExpression/BinaryExpression_OperatorPosition_SameLine.txt @@ -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; diff --git a/tests/specs/expressions/MetaProperty/MetaProperty_All.txt b/tests/specs/expressions/MetaProperty/MetaProperty_All.txt index 5245d2eb..40bc3def 100644 --- a/tests/specs/expressions/MetaProperty/MetaProperty_All.txt +++ b/tests/specs/expressions/MetaProperty/MetaProperty_All.txt @@ -11,7 +11,8 @@ const t = import .other; [expect] -const t = import - .meta - .test - .other; +const t = + import + .meta + .test + .other; diff --git a/tests/specs/general/Parentheses_All.txt b/tests/specs/general/Parentheses_All.txt index 60552175..e8363a1e 100644 --- a/tests/specs/general/Parentheses_All.txt +++ b/tests/specs/general/Parentheses_All.txt @@ -109,7 +109,8 @@ possiblyTrue || ( ); [expect] -const binaryResult = true || false +const binaryResult = + true || false && possiblyTrue || ( true && false || maybeTrue diff --git a/tests/specs/issues/deno/issue014291.txt b/tests/specs/issues/deno/issue014291.txt index 54ea7940..fed88966 100644 --- a/tests/specs/issues/deno/issue014291.txt +++ b/tests/specs/issues/deno/issue014291.txt @@ -8,9 +8,10 @@ let workerId: number; function spawnGateway(shardId: number, options: Partial) { 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") { @@ -29,9 +30,10 @@ let workerId: number; function spawnGateway(shardId: number, options: Partial) { 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") { diff --git a/tests/specs/issues/issue0379.txt b/tests/specs/issues/issue0379.txt new file mode 100644 index 00000000..34932c2a --- /dev/null +++ b/tests/specs/issues/issue0379.txt @@ -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."; diff --git a/tests/specs/jsx/JsxElement/JsxElement_All.txt b/tests/specs/jsx/JsxElement/JsxElement_All.txt index 5b9d0ce2..2b009906 100644 --- a/tests/specs/jsx/JsxElement/JsxElement_All.txt +++ b/tests/specs/jsx/JsxElement/JsxElement_All.txt @@ -283,8 +283,8 @@ const t = ( ); // this has the && right at the line width max so the newline will happen before -const testing = someConditionLongEnoughTestTest - && ( +const testing = + someConditionLongEnoughTestTest && ( {someValue} diff --git a/tests/specs/statements/variableStatement/VariableStatement_All.txt b/tests/specs/statements/variableStatement/VariableStatement_All.txt index b0c67e94..39e2a20b 100644 --- a/tests/specs/statements/variableStatement/VariableStatement_All.txt +++ b/tests/specs/statements/variableStatement/VariableStatement_All.txt @@ -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; @@ -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*/ @@ -60,8 +60,7 @@ const varA = /* test*/ testttttttttttttttttttttttttttt, }, // test // testing - varB = testtttttttt - && testttttttttttt; + varB = testtttttttt && testttttttttttt; [expect] const varA = /* test*/ @@ -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 } = {