Repository navigation
Conversation
Flexible heredocs and nowdocs (PHP >= 7.3) are re-indented with `hardline`, and the doc printer trims trailing spaces and tabs before every `hardline`. That whitespace is part of the string, so formatting changed the value: `foo ` became `foo`, and a whitespace-only line became empty. After a line that ends in whitespace, use a `literalline` under `markAsRoot` instead, so the next line keeps the same indentation and nothing is trimmed. Other lines still use `hardline`, so output is unchanged for heredocs without trailing whitespace.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug: with
phpVersion7.3 or later, trailing spaces and tabs on heredoc and nowdoc lines are removed, which changes the string (#2026):is printed with
fooand no trailing spaces. A whitespace-only line inside an indented heredoc also becomes empty.Cause: flexible heredocs and nowdocs are re-indented by joining their lines with
hardline, and Prettier's doc printer trims trailing whitespace before everyhardline. Before 7.3 they're joined withliteralline, which doesn't trim, so the bug only shows up with flexible heredocs.Fix: a new
joinHeredocLineshelper insrc/printer.mjsjoins the lines of a flexible heredoc string part or nowdoc body. A line that ends in a space or tab is followed by aliterallineinsidemarkAsRoot, so the next line still gets the current indentation and nothing is trimmed. All other lines are still joined withhardline, so heredocs without trailing whitespace print exactly as before.Tests: added
tests/encapsed/heredoc-trailing-whitespace.phpandtests/nowdoc/trailing-whitespace.php, each with a top-level and an indented heredoc/nowdoc that have trailing spaces, a trailing tab and a whitespace-only line (the heredoc one also has an interpolation followed by spaces). Both run under the existing 7.0 and 7.3 specs. Without the fix, the two 7.3 snapshots fail. With it,yarn test:nodepasses (603 tests),AST_COMPARE=1 yarn test:nodepasses andyarn lintis clean.I also formatted the
--FILE--sections of php-src'sZend/tests(5703 files) with and without the change. The only output that differs isZend/tests/heredoc_nowdoc/nowdoc_013.phpt: it lost the trailing spaces ofsome stringbefore and keeps them now, so its nikic/PHP-Parser AST matches the input's. Every other file is byte-identical.Fixes #2026