Skip to content

Commit

Permalink
jinja[spacing]: use black for formatting (#2375)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Sep 1, 2022
1 parent 37e04ea commit adc699b
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 216 deletions.
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ toctree
toidentifier
tomli
toolset
uncook
ungrouped
unignored
unimported
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,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: 698
PYTEST_REQPASS: 702

steps:
- name: Activate WSL1
Expand Down
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ repos:
additional_dependencies:
- ansible-compat>=2.2.0
- ansible-core
- black
- enrich
- filelock
- flaky
Expand All @@ -167,6 +168,7 @@ repos:
additional_dependencies:
- ansible-compat>=2.2.0
- ansible-core
- black
- docutils
- enrich
- filelock
Expand Down
2 changes: 1 addition & 1 deletion examples/playbooks/jinja-spacing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
- name: Invalid multiline nested JSON
ansible.builtin.debug:
# <-- 8
# not an error currently because current implementation skips multiline expressions
msg: >-
{{ {'dummy_2': {'nested_dummy_1': value_1,
'nested_dummy_2': value_2}} |
Expand Down
2 changes: 1 addition & 1 deletion examples/playbooks/vars/jinja-spacing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ valid_multiline_nested_json: >-
{{ {'dummy_2': {'nested_dummy_1': value_1,
'nested_dummy_2': value_2}} |
combine(dummy_1) }}
invalid_multiline_nested_json: >- # <-- 8
invalid_multiline_nested_json: >- # ignored multiline expression, for now
{{ {'dummy_2': {'nested_dummy_1': value_1,
'nested_dummy_2': value_2}} |
combine(dummy_1)}}
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ zip_safe = False
install_requires =
ansible-compat>=2.2.0 # GPLv3
ansible-core>=2.12.0 # GPLv3
black>=22.1.0 # MIT
enrich>=1.2.6
filelock # The Unlicense
jsonschema>=4.9.0 # MIT, version needed for improved errors
Expand Down
29 changes: 25 additions & 4 deletions src/ansiblelint/rules/jinja.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,39 @@ curious how black would reformat a small sniped feel free to visit
include the entire jinja2 template, so instead of `{{ 1+2==3 }}`, do paste
only `1+2==3`.

In ansible, `changed_when`, `failed_when`, `until`, `when` are considered to
use implicit jinja2 templating, meaning that they do not require `{{ }}`. Our
rule will suggest the removal of the braces for these fields.

## Problematic code

```yaml
---
foo: "{{some|dict2items}}" # <-- jinja[spacing]
bar: "{{ & }}" # <-- jinja[invalid]
- name: Some task
vars:
foo: "{{some|dict2items}}" # <-- jinja[spacing]
bar: "{{ & }}" # <-- jinja[invalid]
when: "{{ foo | bool }}" # <-- jinja[spacing] - 'when' has implicit templating
```
## Correct code
```yaml
---
foo: "{{ some | dict2items }}"
bar: "{{ '&' }}"
- name: Some task
vars:
foo: "{{ some | dict2items }}"
bar: "{{ '&' }}"
when: foo | bool
```
## Current limitations
In its current form, this rule presents the following limitations:
- Jinja2 blocks that have newlines in them will not be reformatted because we
consider that the user deliberately wanted to format them in a particular way.
- Jinja2 blocks that use tilde as a binary operation are ignored because black
does not support tilde as a binary operator. Example: `{{ a ~ b }}`.
- Jinja2 blocks that use dot notation with numbers are ignored because python
and black do not allow it. Example: `{{ foo.0.bar }}`
Loading

0 comments on commit adc699b

Please sign in to comment.