Skip to content

Commit

Permalink
fix(formatter): reverted chained function formatting
Browse files Browse the repository at this point in the history
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
  • Loading branch information
christopherpickering committed Jul 20, 2023
1 parent 41fa758 commit 2ba6e3b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 62 deletions.
73 changes: 11 additions & 62 deletions src/djlint/formatter/indent.py
Expand Up @@ -382,51 +382,22 @@ def format_set(config: Config, html: str, match: re.Match) -> str:
return f"{leading_space}{open_bracket} {tag} {contents} {close_bracket}"

def format_function(config: Config, html: str, match: re.Match) -> str:
# will accept stuff like ` url("foo").foo().bar[1] `
if inside_ignored_block(config, html, match) or not match.group(3):
if inside_ignored_block(config, html, match):
return match.group()

leading_space = match.group(1)
open_bracket = match.group(2)
tag = match.group(3).strip()
close_bracket = match.group(4)

functions = ""
for function in tag.split("."):
parts = re.search(
r"""
((?:\w|\|)*) # function name including pipe: id|default()
(?:
(
\((?:\"[^\"]*\"|'[^']*'|[^\)])*?\) # ()
| \[(?:\"[^\"]*\"|'[^']*'|[^\]])*?\] # []
)
(\[(?:\"[^\"]*\"|'[^']*'|[^\]])*?\])? # [] trailing
)?
""",
function,
re.I | re.DOTALL | re.VERBOSE,
)
if functions != "":
functions += "."
functions += parts.group(1)

if parts.group(2):
functions += (
parts.group(2)[0]
+ format_data(
config,
parts.group(2).strip()[1:-1],
len(f"{open_bracket} {tag} {close_bracket}"),
leading_space,
)
+ parts.group(2)[-1]
)

if parts.group(3):
functions += parts.group(3)
index = (match.group(5) or "").strip()
close_bracket = match.group(6)
contents = format_data(
config,
match.group(4).strip()[1:-1],
len(f"{open_bracket} {tag}() {close_bracket}"),
leading_space,
)

return f"{leading_space}{open_bracket} {functions} {close_bracket}"
return f"{leading_space}{open_bracket} {tag}({contents}){index} {close_bracket}"

if config.no_set_formatting is False:
func = partial(format_set, config, beautified_code)
Expand All @@ -445,29 +416,7 @@ def format_function(config: Config, html: str, match: re.Match) -> str:
# format function contents
beautified_code = re.sub(
re.compile(
r"""
([ ]*)
({{-?\+?)
[ ]*?
(
(?:
(?:
(?:(?:(?!}}).)*?\w) # function name
(?:
(?:\((?:\"[^\"]*\"|'[^']*'|[^\)])*?\)) # (stuff)
| (?:\[(?:\"[^\"]*\"|'[^']*'|[^\]])*?\]) # [stuff]
)
)
\.?)+
(?:
(?:\[(?:\"[^\"]*\"|'[^']*'|[^\]])*?\]) # [] following ()
| [a-z\d]* # .stuff
)?
[ ]*
)?
((?:(?!}}).)*?-?\+?}})
""",
r"([ ]*)({{-?\+?)[ ]*?((?:(?!}}).)*?\w)(\((?:\"[^\"]*\"|'[^']*'|[^\)])*?\)[ ]*)((?:\[[^\]]*?\]|\.[^\s]+)[ ]*)?((?:(?!}}).)*?-?\+?}})",
flags=re.IGNORECASE | re.MULTILINE | re.VERBOSE | re.DOTALL,
),
func,
Expand Down
42 changes: 42 additions & 0 deletions tests/test_nunjucks/test_filters.py
@@ -0,0 +1,42 @@
"""Test nunjucks filters.
poetry run pytest tests/test_nunjucks/test_filters.py
"""
import pytest

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

test_data = [
pytest.param(
(
"{% set absoluteUrl %}{{ page.url | htmlBaseUrl(metadata.url) }}{% endset %}\n"
),
(
"{% set absoluteUrl %}\n"
" {{ page.url | htmlBaseUrl(metadata.url) }}\n"
"{% endset %}\n"
),
({}),
id="one",
),
pytest.param(
(
"{{ post.templateContent | transformWithHtmlBase(absolutePostUrl, post.url) | dump | safe }}"
),
(
"{{ post.templateContent | transformWithHtmlBase(absolutePostUrl, post.url) | dump | safe }}\n"
),
({}),
id="two",
),
]


@pytest.mark.parametrize(("source", "expected", "args"), test_data)
def test_base(source, expected, args, nunjucks_config):
args["profile"] = "nunjucks"
output = formatter(config_builder(args), source)

printer(expected, source, output)
assert expected == output
3 changes: 3 additions & 0 deletions tests/test_nunjucks/test_functions.py
Expand Up @@ -37,19 +37,22 @@
(
'{{ item.split("/")[1] }}\n'
'{{ item.split("/").123 }}\n'
# https://github.com/Riverside-Healthcare/djLint/issues/704
'{{ item.split("/").bar }}\n'
),
({}),
id="test index",
),
pytest.param(
("{{ url('foo').foo }}"),
# https://github.com/Riverside-Healthcare/djLint/issues/704
('{{ url("foo").foo }}\n'),
({}),
id="function_call_attribute_access",
),
pytest.param(
("{{ url('foo').foo().bar[1] }}"),
# https://github.com/Riverside-Healthcare/djLint/issues/704
('{{ url("foo").foo().bar[1] }}\n'),
({}),
id="function_call_attribute_access_multiple",
Expand Down

0 comments on commit 2ba6e3b

Please sign in to comment.