diff --git a/src/djlint/formatter/indent.py b/src/djlint/formatter/indent.py index d486a3bb..9c4709a4 100644 --- a/src/djlint/formatter/indent.py +++ b/src/djlint/formatter/indent.py @@ -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) @@ -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, diff --git a/tests/test_nunjucks/test_filters.py b/tests/test_nunjucks/test_filters.py new file mode 100644 index 00000000..6b7f8b07 --- /dev/null +++ b/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 diff --git a/tests/test_nunjucks/test_functions.py b/tests/test_nunjucks/test_functions.py index d3e0ac7e..49cb4070 100644 --- a/tests/test_nunjucks/test_functions.py +++ b/tests/test_nunjucks/test_functions.py @@ -37,6 +37,7 @@ ( '{{ item.split("/")[1] }}\n' '{{ item.split("/").123 }}\n' + # https://github.com/Riverside-Healthcare/djLint/issues/704 '{{ item.split("/").bar }}\n' ), ({}), @@ -44,12 +45,14 @@ ), 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",