Skip to content

Commit

Permalink
feat(formatter): added support for nunjucks async loop tags
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherpickering committed Sep 18, 2023
1 parent 11da1b6 commit 5a32bb3
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/djlint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ def __init__(
+ r""" (?:if
| ifchanged
| for
| asyncEach
| asyncAll
| block(?!trans|translate)
| spaceless
| compress
Expand Down Expand Up @@ -602,9 +604,7 @@ def __init__(
+ f"Error: Invalid pyproject.toml max_attribute_length value {djlint_settings['max_attribute_length']}"
)

self.template_if_for_pattern = (
r"(?:{%-?\s?(?:if|for)[^}]*?%}(?:.*?{%\s?end(?:if|for)[^}]*?-?%})+?)"
)
self.template_if_for_pattern = r"(?:{%-?\s?(?:if|for|asyncAll|asyncEach)[^}]*?%}(?:.*?{%\s?end(?:if|for|each|all)[^}]*?-?%})+?)"

self.attribute_pattern: str = (
rf"""
Expand Down Expand Up @@ -673,6 +673,8 @@ def __init__(
+ r"""
(?:if
| for
| asyncEach
| asyncAll
| block(?!trans)
| spaceless
| compress
Expand Down Expand Up @@ -707,6 +709,10 @@ def __init__(
| endif
| for
| endfor
| asyncEach
| endeach
| asyncAll
| endall
| block(?!trans)
| endblock(?!trans)
| else
Expand Down Expand Up @@ -902,6 +908,8 @@ def __init__(
| for
| block
| with
| asyncEach
| asyncAll
"""

self.break_html_tags: str = (
Expand Down
122 changes: 122 additions & 0 deletions tests/test_nunjucks/test_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
"""Test nunjucks ascyn tags.
poetry run pytest tests/test_nunjucks/test_async.py
"""
import pytest

from src.djlint.reformat import formatter
from tests.conftest import printer

test_data = [
pytest.param(
(
"<ul>{% asyncEach athlete in athlete_list %}<li>{{ athlete.name }}</li>{% empty %}<li>Sorry, no athletes in this list.</li>{% endeach %}</ul>"
),
(
"<ul>\n"
" {% asyncEach athlete in athlete_list %}\n"
" <li>{{ athlete.name }}</li>\n"
" {% empty %}\n"
" <li>Sorry, no athletes in this list.</li>\n"
" {% endeach %}\n"
"</ul>\n"
),
id="eachAsync",
),
pytest.param(
(
"<ul>{% asyncAll athlete in athlete_list %}<li>{{ athlete.name }}</li>{% empty %}<li>Sorry, no athletes in this list.</li>{% endall %}</ul>"
),
(
"<ul>\n"
" {% asyncAll athlete in athlete_list %}\n"
" <li>{{ athlete.name }}</li>\n"
" {% empty %}\n"
" <li>Sorry, no athletes in this list.</li>\n"
" {% endall %}\n"
"</ul>\n"
),
id="eachAll",
),
pytest.param(
(
"{% asyncEach i in items %}\n"
" <div>{% formfield i %}</div>\n"
"{% endeach %}"
),
(
"{% asyncEach i in items %}\n"
" <div>{% formfield i %}</div>\n"
"{% endeach %}\n"
),
id="each test nested formfield",
),
pytest.param(
(
"{% asyncAll i in items %}\n"
" <div>{% formfield i %}</div>\n"
"{% endall %}"
),
(
"{% asyncAll i in items %}\n"
" <div>{% formfield i %}</div>\n"
"{% endall %}\n"
),
id="all test nested formfield",
),
pytest.param(
(
'<div class="form-inputs plans-form">\n'
' <div class="trans-wrapper pos-comparator-flex">\n'
" {% asyncEach field in POSOptimizer %}\n"
" <div>\n"
" <label>{{ field.label }}</label>\n"
" {% formfield field show_label=False %}\n"
" </div>\n"
" {% endeach %}\n"
" {% asyncAll field in POSOptimizer %}\n"
" <div>\n"
" <label>{{ field.label }}</label>\n"
" {% formfield field show_label=False %}\n"
" </div>\n"
" {% endall %}\n"
" <div>\n"
" <label>&nbsp</label>\n"
' <button type="submit">{% trans "Calcola" %}</button>\n'
" </div>\n"
" </div>\n"
"</div>\n"
),
(
'<div class="form-inputs plans-form">\n'
' <div class="trans-wrapper pos-comparator-flex">\n'
" {% asyncEach field in POSOptimizer %}\n"
" <div>\n"
" <label>{{ field.label }}</label>\n"
" {% formfield field show_label=False %}\n"
" </div>\n"
" {% endeach %}\n"
" {% asyncAll field in POSOptimizer %}\n"
" <div>\n"
" <label>{{ field.label }}</label>\n"
" {% formfield field show_label=False %}\n"
" </div>\n"
" {% endall %}\n"
" <div>\n"
" <label>&nbsp</label>\n"
' <button type="submit">{% trans "Calcola" %}</button>\n'
" </div>\n"
" </div>\n"
"</div>\n"
),
id="each test nested formfield inside for",
),
]


@pytest.mark.parametrize(("source", "expected"), test_data)
def test_base(source, expected, django_config):
output = formatter(django_config, source)

printer(expected, source, output)
assert expected == output

0 comments on commit 5a32bb3

Please sign in to comment.