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

jinja[spacing]: fix negative numbers inside arrays #2328

Merged
merged 1 commit into from
Aug 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:TOX_PARALLEL_NO_SPINNER
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 694
PYTEST_REQPASS: 696

steps:
- name: Activate WSL1
Expand Down
18 changes: 16 additions & 2 deletions src/ansiblelint/rules/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def in_expression(tokens: list[Any]) -> str:
if token[1] in begin_types:
avoid_spacing = True
break
if token[1] == "operator" and token[2] in (":", ""):
if token[1] == "operator" and token[2] in (":", "", "["):
avoid_spacing = True
break
if token[1] in ("operator", "integer", "string", "name"):
Expand Down Expand Up @@ -299,7 +299,7 @@ def in_expression(tokens: list[Any]) -> str:
"(",
):
tokens.pop()
elif tokens[-2][2] == ":" and in_expression(tokens) == "[":
elif tokens[-2][2] != "," and in_expression(tokens) == "[":
tokens.pop()
else:
if tokens[-2][1] == "operator" and tokens[-2][2] in ("-", "+"):
Expand Down Expand Up @@ -525,6 +525,20 @@ def test_jinja_spacing_vars() -> None:
"spacing",
id="33",
),
pytest.param(
# negative array index
"{{ foo[-1] }}",
"{{ foo[-1] }}",
"spacing",
id="34",
),
pytest.param(
# negative array index, repair
"{{ foo[- 1] }}",
"{{ foo[-1] }}",
"spacing",
id="35",
),
),
)
def test_jinja(text: str, expected: str, tag: str) -> None:
Expand Down