diff --git a/src/clean.mjs b/src/clean.mjs index 3622417ca..7262bf3f6 100644 --- a/src/clean.mjs +++ b/src/clean.mjs @@ -43,6 +43,20 @@ function clean(node, newObj) { newObj.value = newObj.value.replace(/\n/g, ""); } + // The parser injects `___PSEUDO_INLINE_PLACEHOLDER___` into the source text + // for top-level `?>\n continue 2; // continue 1; -> continue; if ((node.kind === "continue" || node.kind === "break") && node.level) { diff --git a/src/printer.mjs b/src/printer.mjs index f789a340b..cf358c093 100644 --- a/src/printer.mjs +++ b/src/printer.mjs @@ -66,6 +66,13 @@ const { isPreviousLineEmpty, } = prettierUtil; +// Sentinel injected by parser.mjs into the source text for top-level `?>\n i > 0 || node.loc.start.column === 0 @@ -2746,7 +2766,7 @@ function printNode(path, options, print) { const quote = useDoubleQuote(node, options) ? '"' : "'"; - let stringValue = node.raw; + let stringValue = node.raw.replaceAll(PSEUDO_INLINE_PLACEHOLDER, ""); if (node.raw[0] === "b") { stringValue = stringValue.slice(1); @@ -2811,7 +2831,7 @@ function printNode(path, options, print) { case "inline": return join( literalline, - node.raw.replace("___PSEUDO_INLINE_PLACEHOLDER___", "").split("\n") + node.raw.replaceAll(PSEUDO_INLINE_PLACEHOLDER, "").split("\n") ); case "magic": return node.value; @@ -2823,7 +2843,14 @@ function printNode(path, options, print) { node.label, "'", linebreak, - join(linebreak, node.value.split("\n")), + // The parser injects `___PSEUDO_INLINE_PLACEHOLDER___` into the raw + // source text for top-level `?>\n + + + + +EOD; +} + +$nowdoc = <<<'EOD' + + +EOD; + +$string = "\\n"; + +=====================================output===================================== + + + + + + EOD; +} +$nowdoc = <<<'EOD' + + +EOD; +$string = "\\n"; + +================================================================================ +`; diff --git a/tests/heredoc/heredoc.php b/tests/heredoc/heredoc.php new file mode 100644 index 000000000..716deadd7 --- /dev/null +++ b/tests/heredoc/heredoc.php @@ -0,0 +1,19 @@ + + + + + +EOD; +} + +$nowdoc = <<<'EOD' + + +EOD; + +$string = "\n"; diff --git a/tests/heredoc/jsfmt.spec.mjs b/tests/heredoc/jsfmt.spec.mjs new file mode 100644 index 000000000..8829c872f --- /dev/null +++ b/tests/heredoc/jsfmt.spec.mjs @@ -0,0 +1 @@ +run_spec(import.meta, ["php"]);