Skip to content
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

fix: consistent formatting for @component #772

Merged
merged 3 commits into from
Jan 13, 2023

Conversation

hjanos1
Copy link
Contributor

@hjanos1 hjanos1 commented Jan 9, 2023

This PR makes formatting of the @component directive consistent with the @include directive.
These two directives are fairly similar, but before this change, their formatting behaviour was inconsistent.

Description

  • Extracted formatExpressionInsideBladeDirective method, changes:
    • only removes last trailing comma (that is the result of the helper function expression)
    • only removes linebreaks from the formatted expression if it can be inlined
  • Used formatExpressionInsideBladeDirective method in restoreConditions, this results in consistent formatting

Related Issue

Fixes #771

How Has This Been Tested?

yarn run test completes successfully
Note: some test cases has been changed, please review that they are consistent with the project's expectations
When making the changes, I have tested that they are consistent with the prettier php plugin's behaviour, except for removing the line breaks around ->, which was a change in this project, that I did not touch without discussing its purpose.

Prettier PHP Plugin Playground - includeFirst change
Prettier PHP Plugin Playground - nested conditions change

Changes:
- only remove last trailing comma (that is the result of the helper function expression)
- only remove linebreaks from the formatted expression if it can be inlined
- use formatExpressionInsideBladeDirective for conditionalTokens
- wrapping behaviour is consistent with prettier php plugin
- except line breaks being removed around `->`
@hjanos1 hjanos1 requested a review from shufo as a code owner January 9, 2023 01:01
@codecov
Copy link

codecov bot commented Jan 12, 2023

Codecov Report

Base: 77.85% // Head: 77.87% // Increases project coverage by +0.02% 🎉

Coverage data is based on head (83a3c18) compared to base (c767960).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #772      +/-   ##
==========================================
+ Coverage   77.85%   77.87%   +0.02%     
==========================================
  Files          13       13              
  Lines        1657     1659       +2     
  Branches      277      279       +2     
==========================================
+ Hits         1290     1292       +2     
  Misses        334      334              
  Partials       33       33              
Impacted Files Coverage Δ
src/formatter.ts 89.49% <100.00%> (+0.01%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@shufo
Copy link
Owner

shufo commented Jan 13, 2023

@hjanos1
Thanks for creating the PR.
Great!

The changes in title was no problem but I think the changed behaviour around nested condition in @if should not be multiple lines, because PSR-12 standard does not recommend about this (Allowed though) . And, multiple @if condition lines in the blade file feels bit weird to me because, unlike regular PHP, blade is an html/js/php polyglot language.

@if (
    count(
        auth('     (  )   ')->user()->currentXY->shopsXY()
    ) > 1
) {{-- <- feels ambiguous. Is this a html, js, or just a blade? --}}
    <span class="ml-24">Test</span>
@else
    <span class="ml-16">Test</span>
@endif

I think it's ok if it is a regular php file.

if (
    count(
        auth('     (  )   ')->user()->currentXY->shopsXY()
    ) > 1
) {
  echo 1;  
} else {
  echo 2;
}

Maybe we should add an option around this behaviour. (like breaking condition to multiple lines option)

After the merge, I'll fix the condition in @if to not have multiple lines.
Thank you!

@shufo shufo changed the title Feature: consistent formatting for @component fix: consistent formatting for @component Jan 13, 2023
@shufo shufo merged commit a3d3aaa into shufo:main Jan 13, 2023
@hjanos1 hjanos1 deleted the feature/consistent-formatting-for-component branch January 15, 2023 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@component directive formatting is inconsistent
2 participants