From 74276cd7c1660b18afcf36c867ac95dba6be6f9e Mon Sep 17 00:00:00 2001 From: Clayton Carter Date: Sun, 13 Aug 2023 10:11:51 -0400 Subject: [PATCH 1/4] refactor(match): Extract var and remove ifBreak Match arms are never combined onto a single line, so ifBreak is not needed. --- src/printer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/printer.js b/src/printer.js index e9059dfd6..85941548c 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2887,8 +2887,9 @@ function printNode(path, options, print) { } case "match": { const arms = path.map((armPath, armIdx) => { + const armNode = armPath.getValue(); const conds = - armPath.getValue().conds === null + armNode.conds === null ? "default" : armPath.map( (condPath, condIdx) => @@ -2906,7 +2907,7 @@ function printNode(path, options, print) { "match (", group([softline, indent(path.call(print, "cond")), softline]), ") {", - group(indent([...arms, options.trailingCommaPHP ? ifBreak(",") : ""])), + group(indent([...arms, options.trailingCommaPHP ? "," : ""])), " ", softline, "}", From b8a57a9813d1a06d2eabdbab3d6efc14b4592286 Mon Sep 17 00:00:00 2001 From: Clayton Carter Date: Sun, 13 Aug 2023 20:16:01 -0400 Subject: [PATCH 2/4] feat(match): Support empty lines between arms --- src/printer.js | 7 +++ tests/match/__snapshots__/jsfmt.spec.js.snap | 48 ++++++++++++++++++++ tests/match/match.php | 15 +++++- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/printer.js b/src/printer.js index 85941548c..aec26930c 100644 --- a/src/printer.js +++ b/src/printer.js @@ -53,6 +53,7 @@ const { normalizeMagicMethodName, getNextNonSpaceNonCommentCharacterIndex, isNextLineEmpty, + isPreviousLineEmpty, } = require("./util"); function isMinVersion(actualVersion, requiredVersion) { @@ -2897,9 +2898,15 @@ function printNode(path, options, print) { "conds" ); const body = armPath.call(print, "body"); + const maybeEmptyLineBetweenArms = + armIdx > 0 && + isPreviousLineEmpty(options.originalText, armNode, options) + ? hardline + : ""; return [ ",", hardline, + maybeEmptyLineBetweenArms, group([group([conds, indent(line)]), "=> ", body]), ].slice(armIdx > 0 ? 0 : 1); }, "arms"); diff --git a/tests/match/__snapshots__/jsfmt.spec.js.snap b/tests/match/__snapshots__/jsfmt.spec.js.snap index 92f1e46aa..7a17b335d 100644 --- a/tests/match/__snapshots__/jsfmt.spec.js.snap +++ b/tests/match/__snapshots__/jsfmt.spec.js.snap @@ -52,6 +52,20 @@ $extraLongMatch = match($a) { 'cd' => [], default => [], }; + +// Whitespace in match expressions is handled like in function/method arguments: +// - none above the first arm or below the last arm +// - empty lines are preserved between arms +// - multiple empty lines are collapsed into one +match ($a) { + + 'a' => 1, + + + 'b' => 2 + +}; + =====================================output===================================== [] }; +// Whitespace in match expressions is handled like in function/method arguments: +// - none above the first arm or below the last arm +// - empty lines are preserved between arms +// - multiple empty lines are collapsed into one +match ($a) { + "a" => 1, + + "b" => 2 +}; + ================================================================================ `; @@ -179,6 +203,20 @@ $extraLongMatch = match($a) { 'cd' => [], default => [], }; + +// Whitespace in match expressions is handled like in function/method arguments: +// - none above the first arm or below the last arm +// - empty lines are preserved between arms +// - multiple empty lines are collapsed into one +match ($a) { + + 'a' => 1, + + + 'b' => 2 + +}; + =====================================output===================================== [], }; +// Whitespace in match expressions is handled like in function/method arguments: +// - none above the first arm or below the last arm +// - empty lines are preserved between arms +// - multiple empty lines are collapsed into one +match ($a) { + "a" => 1, + + "b" => 2, +}; + ================================================================================ `; diff --git a/tests/match/match.php b/tests/match/match.php index 25206cb90..d88c953a9 100644 --- a/tests/match/match.php +++ b/tests/match/match.php @@ -41,4 +41,17 @@ => 'some really long value in the return part of the match statement', 'cd' => [], default => [], -}; \ No newline at end of file +}; + +// Whitespace in match expressions is handled like in function/method arguments: +// - none above the first arm or below the last arm +// - empty lines are preserved between arms +// - multiple empty lines are collapsed into one +match ($a) { + + 'a' => 1, + + + 'b' => 2 + +}; From c770afb73a33e41552792e882365cd2457b877b3 Mon Sep 17 00:00:00 2001 From: Clayton Carter Date: Sun, 13 Aug 2023 20:37:05 -0400 Subject: [PATCH 3/4] feat(match): Print leading comments --- src/printer.js | 4 ++ .../comments/__snapshots__/jsfmt.spec.js.snap | 43 +++++++++++++++++++ tests/comments/match.php | 19 ++++++++ 3 files changed, 66 insertions(+) create mode 100644 tests/comments/match.php diff --git a/src/printer.js b/src/printer.js index aec26930c..b0248607f 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2889,6 +2889,9 @@ function printNode(path, options, print) { case "match": { const arms = path.map((armPath, armIdx) => { const armNode = armPath.getValue(); + const maybeLeadingComment = comments.hasLeadingComment(armNode) + ? [comments.printComments(armNode.leadingComments, options), hardline] + : []; const conds = armNode.conds === null ? "default" @@ -2907,6 +2910,7 @@ function printNode(path, options, print) { ",", hardline, maybeEmptyLineBetweenArms, + ...maybeLeadingComment, group([group([conds, indent(line)]), "=> ", body]), ].slice(armIdx > 0 ? 0 : 1); }, "arms"); diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index d467f1e46..a8a49a454 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -4824,6 +4824,49 @@ list( ================================================================================ `; +exports[`match.php 1`] = ` +====================================options===================================== +parsers: ["php"] +printWidth: 80 + | printWidth +=====================================input====================================== + 1, + + + + /* + * second comment + */ + + + + 'b' => 2, + default => null +}; + +=====================================output===================================== + 1, + + /* + * second comment + */ + "b" => 2, + default => null, +}; + +================================================================================ +`; + exports[`method.php 1`] = ` ====================================options===================================== parsers: ["php"] diff --git a/tests/comments/match.php b/tests/comments/match.php new file mode 100644 index 000000000..633e05588 --- /dev/null +++ b/tests/comments/match.php @@ -0,0 +1,19 @@ + 1, + + + + /* + * second comment + */ + + + + 'b' => 2, + default => null +}; From 727ce186095982233199d81b3509e60f909932c1 Mon Sep 17 00:00:00 2001 From: Clayton Carter Date: Tue, 15 Aug 2023 06:26:10 -0400 Subject: [PATCH 4/4] feat(match): Print trailing comments --- src/printer.js | 27 ++++++++++++++++--- .../comments/__snapshots__/jsfmt.spec.js.snap | 12 +++++++-- tests/comments/match.php | 6 ++++- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/printer.js b/src/printer.js index b0248607f..43079e3d1 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2887,11 +2887,25 @@ function printNode(path, options, print) { return path.call(print, "name"); } case "match": { + const lastArmIndex = node.arms.length - 1; const arms = path.map((armPath, armIdx) => { const armNode = armPath.getValue(); + const maybeLeadingComment = comments.hasLeadingComment(armNode) ? [comments.printComments(armNode.leadingComments, options), hardline] : []; + const maybeTrailingComma = + armIdx < lastArmIndex || options.trailingCommaPHP ? "," : ""; + const maybeTrailingComment = comments.hasTrailingComment(armNode) + ? [ + " ", + comments.printComments( + armNode.comments.filter((c) => c.trailing), + options + ), + ] + : []; + const conds = armNode.conds === null ? "default" @@ -2906,19 +2920,26 @@ function printNode(path, options, print) { isPreviousLineEmpty(options.originalText, armNode, options) ? hardline : ""; + return [ - ",", + "", hardline, maybeEmptyLineBetweenArms, ...maybeLeadingComment, - group([group([conds, indent(line)]), "=> ", body]), + group([ + group([conds, indent(line)]), + "=> ", + body, + maybeTrailingComma, + ...maybeTrailingComment, + ]), ].slice(armIdx > 0 ? 0 : 1); }, "arms"); return group([ "match (", group([softline, indent(path.call(print, "cond")), softline]), ") {", - group(indent([...arms, options.trailingCommaPHP ? "," : ""])), + group(indent([...arms])), " ", softline, "}", diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index a8a49a454..e7ed09089 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -4847,7 +4847,11 @@ $withComments = match($v) { 'b' => 2, - default => null + // leading comment ... + 'c' => 3, /* ... and trailing comment */ + 'd' // fourth comment + => 4, + default => null // final comment }; =====================================output===================================== @@ -4861,7 +4865,11 @@ $withComments = match ($v) { * second comment */ "b" => 2, - default => null, + // leading comment ... + "c" => 3, /* ... and trailing comment */ + "d" // fourth comment + => 4, + default => null, // final comment }; ================================================================================ diff --git a/tests/comments/match.php b/tests/comments/match.php index 633e05588..2c8c95d8a 100644 --- a/tests/comments/match.php +++ b/tests/comments/match.php @@ -15,5 +15,9 @@ 'b' => 2, - default => null + // leading comment ... + 'c' => 3, /* ... and trailing comment */ + 'd' // fourth comment + => 4, + default => null // final comment };