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

Do not hide unnecessary escape characters #4199

Merged
merged 1 commit into from
Aug 21, 2024
Merged

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented Aug 12, 2024

Follow up of #4176, #4123 and #2712.

Now that we have deprecated unnecessary escape in v3, we can change the behavior in v4.

With this change, unnecessary will no longer be ignored, but handled like any other character.

This allows for writing fully qualified class names like this:

{{ constant('App\Entity\User::SOME_CONSTANT') }}

Instead of having to escape the \ in v3:

{{ constant('App\\Entity\\User::SOME_CONSTANT') }}

/cc @fabpot @stof

@ruudk ruudk changed the title Print unnecessary escape characters Return unnecessary escape characters Aug 12, 2024
@ruudk ruudk changed the title Return unnecessary escape characters Do not hide unnecessary escape characters Aug 12, 2024
@ruudk
Copy link
Contributor Author

ruudk commented Aug 12, 2024

I don't think the integration test failure is related to this PR.

@ruudk
Copy link
Contributor Author

ruudk commented Aug 13, 2024

@fabpot @stof Is there anything I need to change?

@stof
Copy link
Member

stof commented Aug 13, 2024

this needs some changelog entry

@ruudk
Copy link
Contributor Author

ruudk commented Aug 13, 2024

@stof @fabpot Done 😊

src/Lexer.php Outdated Show resolved Hide resolved
CHANGELOG Outdated Show resolved Hide resolved
tests/LexerTest.php Show resolved Hide resolved
CHANGELOG Outdated
@@ -7,6 +7,7 @@
* Change the compilation of `for` loops to throw an exception when a `loop.*` variable is not defined
* Make `Environment::getGlobals()` private
* Drop support for PHP < 8.2
* Unnecessary escape characters are no longer ignored
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the meaning. Can you clarify?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to explain it better.

Doing App\Test tries to escape T which is unnecessary. In v3 this escape \ is ignored and reported as deprecation. In v4 it's no longer ignored, and results in a literal \. That means that App\Test will show up as App\Test as you expect.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this, as it will be hard to explain simply with a short sentence ?

 * Change tokenisation of escape sequence in strings

 Before::  
  // ... 
 {# foo equals "FooBar" #}
 
After:
 // 
 {# foo equals "Foo\Bar" #}

As example I would probably use a function call (instead of a print expression for instance), to support the idea it's a lexer thing and thus is not related to how context vars are printed.. wdyt ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe just "Escape characters are no longer ignored".

src/Lexer.php Outdated Show resolved Hide resolved
src/Lexer.php Outdated Show resolved Hide resolved
CHANGELOG Outdated
@@ -7,6 +7,7 @@
* Change the compilation of `for` loops to throw an exception when a `loop.*` variable is not defined
* Make `Environment::getGlobals()` private
* Drop support for PHP < 8.2
* Unnecessary escape characters are no longer ignored
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe just "Escape characters are no longer ignored".

ruudk added a commit to ruudk/Twig that referenced this pull request Aug 21, 2024
@ruudk
Copy link
Contributor Author

ruudk commented Aug 21, 2024

@fabpot Rebased the PR and applied the fixes.

Also created #4219 for V3.

@ruudk ruudk requested a review from fabpot August 21, 2024 17:37
fabpot added a commit that referenced this pull request Aug 21, 2024
This PR was merged into the 3.x branch.

Discussion
----------

Improve deprecation message

See #4199 (comment)

/cc `@stof` `@fabpot`

Commits
-------

9010792 Improve deprecation message
@fabpot
Copy link
Contributor

fabpot commented Aug 21, 2024

Thank you @ruudk.

@fabpot fabpot merged commit 8da936a into twigphp:4.x Aug 21, 2024
5 of 6 checks passed
@ruudk ruudk deleted the v4-escape branch August 21, 2024 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants