Skip to content

Commit 8b719f4

Browse files
authored
jinja[spacing]: fix negative numbers inside arrays (#2328)
Fixes: #2317
1 parent a39f8cb commit 8b719f4

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

.github/workflows/tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ jobs:
165165
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:TOX_PARALLEL_NO_SPINNER
166166
# Number of expected test passes, safety measure for accidental skip of
167167
# tests. Update value if you add/remove tests.
168-
PYTEST_REQPASS: 694
168+
PYTEST_REQPASS: 696
169169

170170
steps:
171171
- name: Activate WSL1

src/ansiblelint/rules/jinja.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def in_expression(tokens: list[Any]) -> str:
243243
if token[1] in begin_types:
244244
avoid_spacing = True
245245
break
246-
if token[1] == "operator" and token[2] in (":", ""):
246+
if token[1] == "operator" and token[2] in (":", "", "["):
247247
avoid_spacing = True
248248
break
249249
if token[1] in ("operator", "integer", "string", "name"):
@@ -299,7 +299,7 @@ def in_expression(tokens: list[Any]) -> str:
299299
"(",
300300
):
301301
tokens.pop()
302-
elif tokens[-2][2] == ":" and in_expression(tokens) == "[":
302+
elif tokens[-2][2] != "," and in_expression(tokens) == "[":
303303
tokens.pop()
304304
else:
305305
if tokens[-2][1] == "operator" and tokens[-2][2] in ("-", "+"):
@@ -525,6 +525,20 @@ def test_jinja_spacing_vars() -> None:
525525
"spacing",
526526
id="33",
527527
),
528+
pytest.param(
529+
# negative array index
530+
"{{ foo[-1] }}",
531+
"{{ foo[-1] }}",
532+
"spacing",
533+
id="34",
534+
),
535+
pytest.param(
536+
# negative array index, repair
537+
"{{ foo[- 1] }}",
538+
"{{ foo[-1] }}",
539+
"spacing",
540+
id="35",
541+
),
528542
),
529543
)
530544
def test_jinja(text: str, expected: str, tag: str) -> None:

0 commit comments

Comments
 (0)