Skip to content
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
18 changes: 13 additions & 5 deletions src/printer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const {
indent,
dedent,
ifBreak,
indentIfBreak,
hardline,
hardlineWithoutBreakParent,
softline,
Expand Down Expand Up @@ -2885,17 +2886,24 @@ function printNode(path, options, print) {
? hardline
: "";

const condsGroupId = Symbol("match-arm-conds");

return [
"",
hardline,
maybeEmptyLineBetweenArms,
...maybeLeadingComment,
group([
group([conds, indent(line)]),
"=> ",
body,
maybeTrailingComma,
...maybeTrailingComment,
group([conds, indent(line)], { id: condsGroupId }),
// When the conditions break onto multiple lines, the `=>` moves to
// its own indented line - the body then needs the same extra
// indent level for its own line breaks (see #2435).
indentIfBreak(
["=> ", body, maybeTrailingComma, ...maybeTrailingComment],
{
groupId: condsGroupId,
}
),
]),
].slice(!path.isFirst ? 0 : 1);
}, "arms");
Expand Down
178 changes: 177 additions & 1 deletion tests/match/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`match.php 1`] = `
====================================options=====================================
Expand Down Expand Up @@ -70,6 +70,49 @@ match($really_really_really_really_really_really_really_really_long_variable_nam
default => null
};

// https://github.com/prettier/plugin-php/issues/2435
// When the conditions of a match arm break onto multiple lines, the body
// on the indented \`=>\` line must keep the extra indent for its own breaks.
echo match ($operation) {
"short", "really very long option 2" => SomeClass::fairlyLongMethodNameThatRunsOn($variable, ["short", "short"]),
};

// Same, with a method chain as the body and a trailing comment on the arm.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> $service
->firstStep($input)
->secondStep(outerFunction(innerFunction(deepestFunction($value))))
->thirdStep(fn($item) => $item + 1), // trailing comment
default => null,
};

// Same, with a method chain whose last step takes a nested chain argument.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> $service
->firstStep($input)
->secondStep($second)
->thirdStep(makeThing($inner->levelFour($deep))->levelTwo($second)),
default => null,
};

// Same, with an array as the body.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> [
"first entry of the array body",
"second entry of the array body",
],
default => null,
};

=====================================output=====================================
<?php

Expand Down Expand Up @@ -158,6 +201,51 @@ match (
default => null
};

// https://github.com/prettier/plugin-php/issues/2435
// When the conditions of a match arm break onto multiple lines, the body
// on the indented \`=>\` line must keep the extra indent for its own breaks.
echo match ($operation) {
"short",
"really very long option 2"
=> SomeClass::fairlyLongMethodNameThatRunsOn($variable, [
"short",
"short"
])
};

// Same, with a method chain as the body and a trailing comment on the arm.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> $service
->firstStep($input)
->secondStep(outerFunction(innerFunction(deepestFunction($value))))
->thirdStep(fn($item) => $item + 1), // trailing comment
default => null
};

// Same, with a method chain whose last step takes a nested chain argument.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> $service
->firstStep($input)
->secondStep($second)
->thirdStep(makeThing($inner->levelFour($deep))->levelTwo($second)),
default => null
};

// Same, with an array as the body.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> ["first entry of the array body", "second entry of the array body"],
default => null
};

================================================================================
`;

Expand Down Expand Up @@ -231,6 +319,49 @@ match($really_really_really_really_really_really_really_really_long_variable_nam
default => null
};

// https://github.com/prettier/plugin-php/issues/2435
// When the conditions of a match arm break onto multiple lines, the body
// on the indented \`=>\` line must keep the extra indent for its own breaks.
echo match ($operation) {
"short", "really very long option 2" => SomeClass::fairlyLongMethodNameThatRunsOn($variable, ["short", "short"]),
};

// Same, with a method chain as the body and a trailing comment on the arm.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> $service
->firstStep($input)
->secondStep(outerFunction(innerFunction(deepestFunction($value))))
->thirdStep(fn($item) => $item + 1), // trailing comment
default => null,
};

// Same, with a method chain whose last step takes a nested chain argument.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> $service
->firstStep($input)
->secondStep($second)
->thirdStep(makeThing($inner->levelFour($deep))->levelTwo($second)),
default => null,
};

// Same, with an array as the body.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> [
"first entry of the array body",
"second entry of the array body",
],
default => null,
};

=====================================output=====================================
<?php

Expand Down Expand Up @@ -319,5 +450,50 @@ match (
default => null,
};

// https://github.com/prettier/plugin-php/issues/2435
// When the conditions of a match arm break onto multiple lines, the body
// on the indented \`=>\` line must keep the extra indent for its own breaks.
echo match ($operation) {
"short",
"really very long option 2"
=> SomeClass::fairlyLongMethodNameThatRunsOn($variable, [
"short",
"short",
]),
};

// Same, with a method chain as the body and a trailing comment on the arm.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> $service
->firstStep($input)
->secondStep(outerFunction(innerFunction(deepestFunction($value))))
->thirdStep(fn($item) => $item + 1), // trailing comment
default => null,
};

// Same, with a method chain whose last step takes a nested chain argument.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> $service
->firstStep($input)
->secondStep($second)
->thirdStep(makeThing($inner->levelFour($deep))->levelTwo($second)),
default => null,
};

// Same, with an array as the body.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> ["first entry of the array body", "second entry of the array body"],
default => null,
};

================================================================================
`;
43 changes: 43 additions & 0 deletions tests/match/match.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,46 @@
match($really_really_really_really_really_really_really_really_long_variable_name) {
default => null
};

// https://github.com/prettier/plugin-php/issues/2435
// When the conditions of a match arm break onto multiple lines, the body
// on the indented `=>` line must keep the extra indent for its own breaks.
echo match ($operation) {
"short", "really very long option 2" => SomeClass::fairlyLongMethodNameThatRunsOn($variable, ["short", "short"]),
};

// Same, with a method chain as the body and a trailing comment on the arm.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> $service
->firstStep($input)
->secondStep(outerFunction(innerFunction(deepestFunction($value))))
->thirdStep(fn($item) => $item + 1), // trailing comment
default => null,
};

// Same, with a method chain whose last step takes a nested chain argument.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> $service
->firstStep($input)
->secondStep($second)
->thirdStep(makeThing($inner->levelFour($deep))->levelTwo($second)),
default => null,
};

// Same, with an array as the body.
echo match ($operation) {
"a very long first option text",
"a very long second option text",
"a third option"
=> [
"first entry of the array body",
"second entry of the array body",
],
default => null,
};
Loading