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

Remove excess spacing before multiline block %} #245

Closed
ruudk opened this issue Jul 3, 2024 · 4 comments · Fixed by #247
Closed

Remove excess spacing before multiline block %} #245

ruudk opened this issue Jul 3, 2024 · 4 comments · Fixed by #247

Comments

@ruudk
Copy link
Contributor

ruudk commented Jul 3, 2024

Rule(s) related to or rule(s) missing

Using the standard TwigCsFixer rules I notice something odd.

Actual behavior

{% trans 'my.key' with {
    faqUrl: 'http://faq',
    contactUrl: 'http://contact',
    }

%}

Expected behavior

{% trans 'my.key' with {
    faqUrl: 'http://faq',
    contactUrl: 'http://contact',
    } %}

Why isn't the excess newline and spacing not removed?

@VincentLanglet
Copy link
Owner

Why isn't the excess newline and spacing not removed?

Hi, the main reason is that all the spacing rules are ignoring new lines, because there is nothing about it in the twig standard. People might prefer:

{{
       foo
       + bar
}}

over

{{ foo
       + bar }}

cf
https://github.com/VincentLanglet/Twig-CS-Fixer/blob/main/src/Rules/AbstractSpacingRule.php#L45-L49
and https://github.com/VincentLanglet/Twig-CS-Fixer/blob/main/src/Rules/AbstractSpacingRule.php#L85-L89

The AbstractSpacingRule is used

  • for block delimiters
  • for the operators
  • for the punctuations (ie, {, (, :, ...)

I'm sure you won't exemple

{
    foo: bar,
    foo2: bar2,
    ...
}

to be fixed to { foo: bar, foo2: bar2, ... } automatically.

We could consider adding an option to the DelimiterSpacingRule (because it's maybe the only one where it make sens to remove newlines ?). Which means adding an option (in the construct of AbstractSpacingRule with) to skip or not newlines.

Wdyt ?

@ruudk
Copy link
Contributor Author

ruudk commented Jul 3, 2024

I agree that this might be a preference that shouldn't be enabled by default.

So I'm all ears for introducing a new option that defaults to the current behavior.

@VincentLanglet
Copy link
Owner

So I'm all ears for introducing a new option that defaults to the current behavior.

I'll be at least one week off so won't be able to help on this soon.

But if you want to try

  • AbstractSpacingRule will implements ConfigurableRuleInterface
  • AbstractSpacingRule will have a construct with skipNewLine = true
  • The getConfiguration need to be implemented
  • checkSpaceAfter and checkSpaceBefore need to be updated to handle the skipNewline option
// Ignore new line
$next = $this->findNext(Token::INDENT_TOKENS, $tokens, $tokenPosition + 1, true);

will certainly be Token::INDENT_TOKENS + Token::EOL_TOKENS when we don't skip EOL. But then maybe the count of whitespace and message will need to be modified (I dunno).

And test will be necessary.

@ruudk
Copy link
Contributor Author

ruudk commented Jul 3, 2024

I tried something in #247 but I couldn't get it to work.

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 a pull request may close this issue.

2 participants