-
-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proper support for flexible heredoc/nowdoc syntax #1274
Comments
This is a tricky one. I'd say, it would be The Prettier Way™ (and also the prettier way 🙃) to figure out one way to print heredocs and to enforce that. One process which sounds reasonable to me: If the starting tag goes on its own line, align the content with that. Otherwise indent it one Examples of the described ruleExample 1The heredoc-opening tag goes on its own line. Input: some_function(<<<'EOT'
Hello World!
EOT
); Current output: some_function(
<<<'EOT'
Hello World!
EOT
); Envisioned output: some_function(
<<<'EOT'
Hello World!
EOT
); Example 2The heredoc-opening tag goes on the same line as the preceding code. Input: $string = <<<'EOT'
Hello World!
EOT; Current output: $string = <<<'EOT'
Hello World!
EOT; Envisioned output: $string = <<<'EOT'
Hello World!
EOT; However, this is not doable consistently without an We could apply this rule to all heredocs that are already indented (and thus signaling PHP 7.4 availability), but while this would certainly work, I'd probably go with the more consistent way until we implement an option for the engine. (It's high time for that anyway I guess, but who's got that time. ¯\_(ツ)_/¯) Until then, I'd actually vote for keeping the current behavior. Resetting the heredoc indentation can be annoying, but it's the only way we can be consistent about this. Sure, we could just keep the indentation untouched, but Prettier is deliberately changing a lot of indentation around that already. If we keep the user's indentation (relative to the opening tag), this will lead to ugly cases like this: // Input:
some_function(<<<'EOT'
Hello World!
EOT
);
// Output:
some_function(
<<<'EOT'
Hello World!
EOT
); In my opinion, if we'd want to keep the user's indentation, we would need to figure out the user's most likely intention. I'd rather not open that Pandora's box. |
I agree with all the points you're making! 👍 |
Agreed. I'll go ahead and create a |
👍 Another question: Is |
Was thinking about that too. I don't really associate it with Zend (mostly because I've never came in touch with that), but a name with more clarity should be preferred anyways. |
|
I also prefer "runtime" over "engine", but I guess "version" ist indeed the most obvious one. And I'd prepend "PHP" to avoid future collisions with prettier core or other plugins. |
Collisions are a good point. I'd go with |
Totally agree 👍 |
Another question: What should the default be? The oldest supported version, i.e. 7.2? Or should there even be a default? |
I don't think there should be a default. There's no way we can make any reliable assumptions that people use a supported PHP version, and there's no reason to constrain the formatter to an old PHP version because nobody bothered to change the option's default. |
That's true, but I'm not sure if it's really worth giving up our zero-config goodness over it. Let's make a quick overview:
I think for those few features it's almost a pity to force users to configure something before they can get productive. If one does the initial formatting with an incorrect setting, it's trivial to resolve that issue. I'd propose to
This would allow to keep the plugin zero-config with optimal support from 5.4 to 7.1/7.2 and very minor drawbacks for 7.3 and beyond. Another idea to work around this issue was to read the PHP version from Update: The PHP community adopted trailing commas in multiline arrays pretty consistently. It might be best to change the default for trailing commas to |
I thought about reading from I'm pretty torn on the default thing. I definitely see the appeal of keeping the Prettier setup zero-config. So, what's the pros/cons here? Pro default value:
Contra default value:
Honestly, I don't feel like I could wholeheartedly weigh in on either side. But I'm bad at making decisions anyway. @evilebottnawi, thoughts? These are the things to be balanced based on opinion. There is also a very practical issue with the default version which I'm not sure how to solve: Would we still use the latest parser version? If not, we'd need to implement options for a PHP 7.1/7.2 parser (as far as I can tell, only switches for 7.0, 7.3 and 7.4 exist). This might also annoy users who use PHP 7.3/7.4 features and get a parser error. If we keep using the latest parser and settle for PHP 7.2 as the default, how would we handle encountering a PHP 7.3/7.4 feature? Would we magically upgrade the |
I think people who use PHP <= 5.4 in production are used to running into issues these days. This analysis show that usage is insignificant. I guess putting a note in our README concerning version support would solve this, something like:
According to the analysis, roughly two thirds of our users would be in group 1, one third in group 2, and almost no one in group 3. If one accounts for the fact that users of very old PHP version often don't use composer either and therefore wouldn't show up in the statistics, this might shift a little bit (*) - but in the future most users will move into groups 1 and 2. (*) This analysis reports that roughly 15% of all Websites still use PHP 5.3 and 5.4 - but I think that the analysis based on composer installs is more relevant to us because we're catering PHP developers, and most of the Websites using old PHP versions are probably just Wordpress sites that were never updated 😉 I think we should build as little version-dependent stuff into our plugin as possible:
Prettier for JS just prints modern features the same way they are found, and adapts formatting choices and option defaults very conservatively based on platform support - they're changing the default for trailing commas to the "es5" setting now (in Prettier 2.0)! prettier/prettier#68 Alternative idea When I think more about the approach Prettier for JS is taking, there might be another alternative: If the end tag of a heredoc/nowdoc statement is indented, print it according to the "modern style" suggested above - otherwise print it in the compatible style we're using now. The obvious downside with this approach is that you'd manually have to indent your end tag to get optimal formatting - very similar to how you currently have to make the manual change from ExamplesExample 1The indented end tag signals PHP 7.3 or above. Input: some_function(<<<'EOT'
Hello World!
EOT
); Current output: some_function(
<<<'EOT'
Hello World!
EOT
); Envisioned output: some_function(
<<<'EOT'
Hello World!
EOT
); Example 2The unindented end tag signals a version which is possibly lower than 7.3: Input: $string = <<<'EOT'
Hello World!
EOT; Output: $string = <<<'EOT'
Hello World!
EOT; I'm currently a bit torn on the whole story - maybe best to sleep on it. Might also an idea to boil the whole argument down a bit and then put it up for a vote (in a separate issue)? |
The points you made are pretty convincing, mostly because I didn't read this part thoroughly enough:
Which is to say I didn't realize that this really means that every version from as early as 5.5 up until as late as 7.2 is unproblematic. This would definitely be good enough for me. 👍 That said: Isn't it even from 5.4 to 7.2? PHP 5.5 has no new syntactic novelties (which we use), has it?
Absolutely. I'd assume that you only add Prettier to a maintained project, and maintained projects (especially maintained to the degree that someone adds a new formatter) running on PHP 5.4 should be the absolute exception. I also overestimated the problems when features newer than the default are encountered. I somehow supposed that we would be required to "upgrade" the formatter at that point — and didn't consider we could just format those features and then continue with the default version. (Which is to say that I like your "alternative idea" most. It's not optimal, but as I outlined above, the optimal solution seems to not be realizable.)
Yeah, let's do that — but I think your suggestion is very viable. |
You're absolutely right! After sleeping on it, I think we have basically have two ways to proceed: Option 1: Add
|
I agree with option 1. Option 2 is not too far from what we have right now. It's not bad, but IMO it keeps us too far away from the results we can achieve with 1. Prettier for JS may go well with guessing, but I think it would be hard for us to adopt that procedure one-to-one — not least of all because JS never gets breaking changes. The ones in PHP major versions are incredibly rare, but they exist, and if somebody runs into them because of Prettier, this may be very annoying to deal with. |
Totally agree! 👍 Also, in JS a I'll go ahead and create a new issue to outline the upcoming changes and link to it in all the issues that are affected by it, such that people can still give feedback. |
I am sorry, I am very busy right now, do not have time on this project, as soon as I deal with everything, I will return and continue to help so feel free to improve project 😄 |
@evilebottnawi 😅 No worries, wishing you success with your tasks and looking forward to continue working with you on this in the future! |
Now that the |
Hi @czosel and anyone else who can help, I can see that various commits have been merged relating to the flexible HEREDOC/NOWDOC syntax. The indentation before the end identifier is preserved when using the NOWDOC syntax, but not as I would have hoped for HEREDOC. I wonder if you can advice whether this is expected behaviour? Here's a playground example: NOWDOC/HEREDOC indentation example I was expecting the HEREDOC to be indented like it is for the NOWDOC when using --php-version=7.4 Thanks |
Thanks for bringing this up @mwchambers. I just took a look, and making heredoc behave exacly the same way as nowdoc was a simple change: #1574 |
@czosel Thank you, that is working perfectly now with that change applied. 👍 |
@mwchambers Great, thanks for testing! We'll make a new release once #1576 has landed. |
Released as v0.15.2 🎉 |
I'll close this in favor of #1590 to make it more clear what can still be improved. |
Currently, the new flexible heredoc/nowdoc syntax is "destroyed" during formatting:
is printed as
Also, there seems to be an extra linebreak before the end.
Concerning expected behavior, I'd propose to leave the indentation the same way we find it. Thoughts?
cc @evilebottnawi @loilo
The text was updated successfully, but these errors were encountered: