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

[BUG] [Formatter] Attribute access after function calls in Jinja gets extra space #704

Open
3 tasks done
klette opened this issue Jun 30, 2023 · 6 comments
Open
3 tasks done
Labels

Comments

@klette
Copy link

klette commented Jun 30, 2023

System Info

  • OS: Fedora 38
  • Python Version 3.11
  • djLint Version HEAD
  • template language: jinja

Issue

When function formatting is enabled the
following template is formated wrongly
breaking the template.

{{ foo('foo').bar }}

becomes

{{ foo("foo") .bar }}

The simple case of attribute access directly after the function call is easy
enough to fix by adding \w to the \d in the "index" capture group (matching group 5).

Here with some comments on the regex added during testing the issue:

if config.no_function_formatting is False:
        func = partial(format_function, config, beautified_code)
        # format function contents
        beautified_code = re.sub(
            re.compile(
                r"""
                ([ ]*)    # Matching Group 1: Matches any number of spaces at the beginning
                ({{-?\+?) # Matching Group 2: Matches '{{' followed by an optional '-' or '+' sign
                [ ]*?     # Matches any number of spaces after '{{-?+' (non-greedy)
                ((?:(?!}}).)*?\w) # Matching Group 3: Matches any character except '}}' followed by a word character (\w)
                (\((?:\"[^\"]*\"|'[^']*'|[^\)])*?\)[ ]*)  # Matching Group 4: Matches parentheses and their contents within '{{...}}'
                ((?:\[[^\]]*?\]|\.[\d\w]+)[ ]*)? # Matching Group 5: Matches square brackets or dot notation within '{{...}}'
                ((?:(?!}}).)*?-?\+?}}) # Matching Group 6: Matches any character except '}}' followed by an optional '-' or '+' sign and '}}'
                """,
                flags=re.IGNORECASE | re.MULTILINE | re.VERBOSE | re.DOTALL,
            ),
            func,
            beautified_code,
        )

But the repeated case requires a bit more work to fix I think.

How To Reproduce

Add the following test cases to tests/test_jinja/test_parenthesis.py:

    pytest.param(
        ("{{ url('foo').foo }}"),
        ('{{ url("foo").foo }}\n'),
        id="function_call_attribute_access",
    ),
    pytest.param(
        ("{{ url('foo').foo().bar[1] }}"),
        ('{{ url("foo").foo().bar[1] }}\n'),
        id="function_call_attribute_access_multiple",
    ),
@klette klette added 🦠 bug Something isn't working 🧽 formatter labels Jun 30, 2023
@welcome
Copy link

welcome bot commented Jun 30, 2023

Thanks for opening your first issue here!

@christopherpickering
Copy link
Contributor

Thanks for reporting! You are welcome to do a pr with this, otherwise I will get it in a release next week.

@christopherpickering
Copy link
Contributor

chained was pretty rough :D

christopherpickering pushed a commit that referenced this issue Jul 19, 2023
# [1.32.0](v1.31.1...v1.32.0) (2023-07-19)

### Bug Fixes

* **formatter:** fixed formatting on chained function ([7c2756d](7c2756d)), closes [#704](#704)
* **linter:** fixed false positive on h026 data-id ([400882a](400882a)), closes [#711](#711)
* **linter:** fixed golang false positive on T027 ([232f4bc](232f4bc)), closes [#712](#712)

### Features

* **formatter:** added support for jazzband's sorl-thumbnail tags ([86122dc](86122dc)), closes [#714](#714)
@christopherpickering
Copy link
Contributor

🎉 This issue has been resolved in version 1.32.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@christopherpickering
Copy link
Contributor

I don't have time to get to #720 in the next few days so I will roll back this change until then.

christopherpickering added a commit that referenced this issue Jul 20, 2023
This update reverts the formatting on of chained functions as it broke filter strings, but at the
same time leaves in a fix for extra spaces that the original issue found.

re #720, #704
@christopherpickering
Copy link
Contributor

I did leave in a fix for the extra spaces, even in chained functions, but only the first function will be formatted currently.

christopherpickering pushed a commit that referenced this issue Jul 20, 2023
## [1.32.1](v1.32.0...v1.32.1) (2023-07-20)

### Bug Fixes

* **formatter:** reverted chained function formatting ([2ba6e3b](2ba6e3b)), closes [#720](#720) [#704](#704)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants