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

E206: Avoid false positives with multilines #1300

Merged
merged 1 commit into from Feb 2, 2021
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
11 changes: 11 additions & 0 deletions examples/playbooks/206.yml
Expand Up @@ -35,3 +35,14 @@
- name: JSON inside jinja is valid
debug:
msg: "{{ {'test': {'subtest': variable}} }}"
- name: Avoid false positive on multiline
vars:
cases:
case1: >-
http://example.com/{{
case1 }}
case2: >-
http://example.com/{{
case2 }}
debug:
var: cases
14 changes: 8 additions & 6 deletions src/ansiblelint/rules/VariableHasSpacesRule.py
Expand Up @@ -11,21 +11,23 @@

class VariableHasSpacesRule(AnsibleLintRule):
id = '206'
shortdesc = 'Variables should have spaces before and after: {{ var_name }}'
base_msg = 'Variables should have spaces before and after: '
shortdesc = base_msg + ' {{ var_name }}'
description = 'Variables should have spaces before and after: ``{{ var_name }}``'
severity = 'LOW'
tags = ['formatting']
version_added = 'v4.0.0'

variable_syntax = re.compile(r"{{.*}}")
bracket_regex = re.compile(r"{{[^{' -]|[^ '}-]}}")
bracket_regex = re.compile(
r"{{[^{\n' -]|[^ '\n}-]}}", re.MULTILINE | re.DOTALL)
exclude_json_re = re.compile(r"[^{]{'\w+': ?[^{]{.*?}}")

def matchtask(self, task: Dict[str, Any]) -> Union[bool, str]:
for k, v in nested_items(task):
if isinstance(v, str):
line_exclude_json = re.sub(r"[^{]{'\w+': ?[^{]{.*?}}", "", v)
if bool(self.bracket_regex.search(line_exclude_json)):
return True
cleaned = self.exclude_json_re.sub("", v)
if bool(self.bracket_regex.search(cleaned)):
return self.base_msg + v
return False


Expand Down