Skip to content
Open
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
46 changes: 29 additions & 17 deletions src/printer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const {
literalline,
align,
dedentToRoot,
markAsRoot,
} = doc.builders;
const { willBreak } = doc.utils;
const {
Expand Down Expand Up @@ -866,6 +867,21 @@ function printLookupNodes(path, options, print) {
}
}

/**
* Join the body lines of a flexible heredoc or nowdoc. `hardline` trims
* trailing whitespace, which is part of the string, so a line ending in
* whitespace is followed by a `literalline` that keeps the indentation.
*/
function joinHeredocLines(lines) {
return markAsRoot(
lines.flatMap((line, index) =>
index === 0
? [line]
: [/[\t ]$/.test(lines[index - 1]) ? literalline : hardline, line]
)
);
}

function getEncapsedQuotes(node, { opening = true } = {}) {
if (node.type === "heredoc") {
return opening ? `<<<${node.label}` : node.label;
Expand Down Expand Up @@ -2866,25 +2882,23 @@ function printNode(path, options, print) {
const parentParent = path.grandparent;
let closingTagIndentation = 0;
const flexible = options.phpVersion >= 7.3;
let linebreak = literalline;
if (parentParent.type === "heredoc") {
linebreak = flexible ? hardline : literalline;
const lines = parentParent.raw.split("\n");
closingTagIndentation = lines[lines.length - 1].search(/\S/);
if (closingTagIndentation === -1) {
closingTagIndentation = lines[lines.length - 2].search(/\S/);
}
}
return join(
linebreak,
node.raw
.split("\n")
.map((s, i) =>
i > 0 || node.loc.start.column === 0
? s.substring(closingTagIndentation)
: s
)
);
const valueLines = node.raw
.split("\n")
.map((s, i) =>
i > 0 || node.loc.start.column === 0
? s.substring(closingTagIndentation)
: s
);
return parentParent.type === "heredoc" && flexible
? joinHeredocLines(valueLines)
: join(literalline, valueLines);
}

const quote = useDoubleQuote(node, options) ? '"' : "'";
Expand Down Expand Up @@ -2960,15 +2974,13 @@ function printNode(path, options, print) {
return node.value;
case "nowdoc": {
const flexible = options.phpVersion >= 7.3;
const linebreak = flexible ? hardline : literalline;
const lines = [...node.value.split("\n"), node.label];
return [
"<<<'",
node.label,
"'",
linebreak,
join(linebreak, node.value.split("\n")),
linebreak,
node.label,
flexible ? hardline : literalline,
flexible ? joinHeredocLines(lines) : join(literalline, lines),
docShouldHaveTrailingNewline(path) ? hardline : "",
];
}
Expand Down
90 changes: 90 additions & 0 deletions tests/encapsed/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2077,6 +2077,96 @@ printf(
================================================================================
`;

exports[`heredoc-trailing-whitespace.php 1`] = `
====================================options=====================================
parsers: ["php"]
phpVersion: "7.0"
printWidth: 80
| printWidth
=====================================input======================================
<?php
$a = <<<EOT
foo
bar

EOT;

function f()
{
$b = <<<EOT
foo

{$c}
bar
EOT;
}

=====================================output=====================================
<?php
$a = <<<EOT
foo
bar

EOT;

function f()
{
$b = <<<EOT
foo

{$c}
bar
EOT;
}

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

exports[`heredoc-trailing-whitespace.php 2`] = `
====================================options=====================================
parsers: ["php"]
phpVersion: "7.3"
printWidth: 80
| printWidth
=====================================input======================================
<?php
$a = <<<EOT
foo
bar

EOT;

function f()
{
$b = <<<EOT
foo

{$c}
bar
EOT;
}

=====================================output=====================================
<?php
$a = <<<EOT
foo
bar

EOT;

function f()
{
$b = <<<EOT
foo

{$c}
bar
EOT;
}

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

exports[`single.php 1`] = `
====================================options=====================================
parsers: ["php"]
Expand Down
16 changes: 16 additions & 0 deletions tests/encapsed/heredoc-trailing-whitespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
$a = <<<EOT
foo
bar

EOT;

function f()
{
$b = <<<EOT
foo

{$c}
bar
EOT;
}
82 changes: 82 additions & 0 deletions tests/nowdoc/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4193,3 +4193,85 @@ EOD;

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

exports[`trailing-whitespace.php 1`] = `
====================================options=====================================
parsers: ["php"]
phpVersion: "7.0"
printWidth: 80
| printWidth
=====================================input======================================
<?php
$a = <<<'EOT'
foo
bar
EOT;

function f()
{
$b = <<<'EOT'
foo

bar
EOT;
}

=====================================output=====================================
<?php
$a = <<<'EOT'
foo
bar
EOT;

function f()
{
$b = <<<'EOT'
foo

bar
EOT;
}

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

exports[`trailing-whitespace.php 2`] = `
====================================options=====================================
parsers: ["php"]
phpVersion: "7.3"
printWidth: 80
| printWidth
=====================================input======================================
<?php
$a = <<<'EOT'
foo
bar
EOT;

function f()
{
$b = <<<'EOT'
foo

bar
EOT;
}

=====================================output=====================================
<?php
$a = <<<'EOT'
foo
bar
EOT;

function f()
{
$b = <<<'EOT'
foo

bar
EOT;
}

================================================================================
`;
14 changes: 14 additions & 0 deletions tests/nowdoc/trailing-whitespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
$a = <<<'EOT'
foo
bar
EOT;

function f()
{
$b = <<<'EOT'
foo

bar
EOT;
}