Skip to content

Commit

Permalink
jinja[spacing]: fix negative numbers inside arrays
Browse files Browse the repository at this point in the history
Fixes: #2317
  • Loading branch information
ssbarnea committed Aug 23, 2022
1 parent a39f8cb commit cbb0391
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
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

0 comments on commit cbb0391

Please sign in to comment.