plugin-php
plugin-php copied to clipboard
Heredoc containing \n cannot be parsed for indenting
Input:
<?php
function foo() {
$message .= <<<EOT
Please read this message.\n We apologize for the inconvenience.
EOT;
}
Output:
Invalid body indentation level (expecting an indentation at least 4) on line 5
3 | $message .= <<<EOT
4 | Please read this message.\n We apologize for the inconvenience.
> 5 | EOT;
| ^
6 | }
7 |
Expected:
<?php
function foo() {
$message .= <<<EOT
Please read this message.\n We apologize for the inconvenience.
EOT;
}
Basically, the \n
in the middle of the heredoc appears to confuse either the parser or prettier.
This is definitely an issue in the parser, it's seeing the \n
in the middle of the string as a newline when calculating the indent level of the heredoc.
I am not sure how to fix this one, it seems the \n is being turned into a new line, but I am not sure how it keeps that value independent from the actual newline.
I just ran into this, too, and was able to reduce it to \n
followed by a character. If the \n
is the last char on the line, it's not an issue.
<?php
if (true) {
echo <<<STR
\na
STR;
}
❯ node_modules/.bin/prettier --check foo.php
Checking formatting...
foo.php
[error] foo.php: SyntaxError: Invalid body indentation level (expecting an indentation at least 4) on line 6
[error] 4 | echo <<<STR
[error] 5 | \na
[error] > 6 | STR;
[error] | ^
[error] 7 | }
[error] 8 |
All matched files use Prettier code style!
❯ node_modules/.bin/prettier --version
3.2.5
❯ npm list | grep plugin-php
├── @prettier/[email protected]
The original string that caused this for us was an SQL statement that involved the line LINES TERMINATED BY '\n'