Skip to content

Commit 1a9330c

Browse files
authored
feat: flexible heredoc support (#1574)
1 parent 74ad643 commit 1a9330c

3 files changed

Lines changed: 58 additions & 53 deletions

File tree

src/printer.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2785,12 +2785,14 @@ function printNode(path, options, print) {
27852785
if (parent.kind === "encapsedpart") {
27862786
const parentParent = path.getParentNode(1);
27872787
let closingTagIndentation = 0;
2788+
const flexible = isMinVersion(options.phpVersion, "7.3");
2789+
const linebreak = flexible ? hardline : literalline;
27882790
if (parentParent.type === "heredoc") {
27892791
const lines = parentParent.raw.split(/\r?\n/g);
27902792
closingTagIndentation = lines[lines.length - 1].search(/\S/);
27912793
}
27922794
return join(
2793-
literalline,
2795+
linebreak,
27942796
node.raw
27952797
.split(/\r?\n/g)
27962798
.map((s) => s.substring(closingTagIndentation))
@@ -2837,17 +2839,20 @@ function printNode(path, options, print) {
28372839
switch (node.type) {
28382840
case "string":
28392841
case "shell":
2840-
case "heredoc":
2842+
case "heredoc": {
2843+
const flexible = isMinVersion(options.phpVersion, "7.3");
2844+
const linebreak = flexible ? hardline : literalline;
28412845
return concat([
28422846
getEncapsedQuotes(node),
28432847
// Respect `indent` for `heredoc` nodes
2844-
node.type === "heredoc" ? literalline : "",
2848+
node.type === "heredoc" ? linebreak : "",
28452849
concat(path.map(print, "value")),
28462850
getEncapsedQuotes(node, { opening: false }),
28472851
node.type === "heredoc" && docShouldHaveTrailingNewline(path)
28482852
? hardline
28492853
: "",
28502854
]);
2855+
}
28512856
// istanbul ignore next
28522857
default:
28532858
return `Have not implemented kind ${node.type} yet.`;

tests/nowdoc/__snapshots__/jsfmt.spec.js.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3105,35 +3105,35 @@ $str = sprintf(
31053105
31063106
$str = sprintf(
31073107
<<<EOD
3108-
foo
3109-
EOD
3108+
foo
3109+
EOD
31103110
,
31113111
true,
31123112
);
31133113
31143114
$str = sprintf(
31153115
sprintf(
31163116
<<<END
3117-
foo
3118-
END
3117+
foo
3118+
END
31193119
,
31203120
true,
31213121
),
31223122
);
31233123
31243124
$str = call([
31253125
<<<END
3126-
foo
3127-
END
3126+
foo
3127+
END
31283128
,
31293129
true,
31303130
]);
31313131
31323132
$str = [
31333133
[
31343134
<<<END
3135-
foo
3136-
END
3135+
foo
3136+
END
31373137
,
31383138
true,
31393139
],
@@ -3142,8 +3142,8 @@ END
31423142
$str = [
31433143
call(
31443144
<<<END
3145-
foo
3146-
END
3145+
foo
3146+
END
31473147
,
31483148
true,
31493149
),

tests/trailing_commas/__snapshots__/jsfmt.spec.js.snap

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,30 +1597,30 @@ $expected = [
15971597
// heredoc
15981598
$expected = [
15991599
<<<EOT
1600-
foo
1601-
# bar
1602-
baz
1600+
foo
1601+
# bar
1602+
baz
16031603
1604-
EOT
1604+
EOT
16051605
,
16061606
];
16071607
$expected = [
16081608
'foo',
16091609
<<<EOT
1610-
foo
1611-
# bar
1612-
baz
1610+
foo
1611+
# bar
1612+
baz
16131613
1614-
EOT
1614+
EOT
16151615
,
16161616
];
16171617
$expected = [
16181618
<<<EOT
1619-
foo
1620-
# bar
1621-
baz
1619+
foo
1620+
# bar
1621+
baz
16221622
1623-
EOT
1623+
EOT
16241624
,
16251625
'bar',
16261626
];
@@ -1659,30 +1659,30 @@ $expected = [
16591659
// Associative array with heredoc
16601660
$expected = [
16611661
'key' => <<<EOT
1662-
foo
1663-
# bar
1664-
baz
1662+
foo
1663+
# bar
1664+
baz
16651665
1666-
EOT
1666+
EOT
16671667
,
16681668
];
16691669
$expected = [
16701670
'foo',
16711671
'key' => <<<EOT
1672-
foo
1673-
# bar
1674-
baz
1672+
foo
1673+
# bar
1674+
baz
16751675
1676-
EOT
1676+
EOT
16771677
,
16781678
];
16791679
$expected = [
16801680
'key' => <<<EOT
1681-
foo
1682-
# bar
1683-
baz
1681+
foo
1682+
# bar
1683+
baz
16841684
1685-
EOT
1685+
EOT
16861686
,
16871687
'bar',
16881688
];
@@ -1720,28 +1720,28 @@ $expected = [
17201720
// Nested with heredoc
17211721
$expected = [
17221722
'test' => <<<EOT
1723-
foo
1724-
# bar
1725-
baz
1723+
foo
1724+
# bar
1725+
baz
17261726
1727-
EOT
1727+
EOT
17281728
,
17291729
'collection' => [
17301730
[
17311731
'one' => <<<EOT
1732-
foo
1733-
# bar
1734-
baz
1732+
foo
1733+
# bar
1734+
baz
17351735
1736-
EOT
1736+
EOT
17371737
,
17381738
],
17391739
[
17401740
'two' => <<<EOT
1741-
foo
1742-
# bar
1743-
baz
1744-
EOT
1741+
foo
1742+
# bar
1743+
baz
1744+
EOT
17451745
,
17461746
],
17471747
],
@@ -3327,10 +3327,10 @@ call(
33273327
33283328
call(
33293329
<<<EOT
3330-
My name is "$name". I am printing some $foo->foo.
3331-
Now, I am printing some {$foo->bar[1]}.
3332-
This should not print a capital 'A': \\x41
3333-
EOT
3330+
My name is "$name". I am printing some $foo->foo.
3331+
Now, I am printing some {$foo->bar[1]}.
3332+
This should not print a capital 'A': \\x41
3333+
EOT
33343334
,
33353335
);
33363336

0 commit comments

Comments
 (0)