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

fix(formatter): reverted chained function formatting #721

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 11 additions & 62 deletions src/djlint/formatter/indent.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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