Repository: aristanetworks/j2lint (language: python)

Issue:
single-space-decorator: False positive for curly brackets in strings
Required code for python format function:
````jinja
{% set row.line = row.line ~ "| {t:{t_width}} ".format(t=text, t_width=table.column_width[key]) %}
````
fails with message:
````code
template.j2:58 A single space shall be added between Jinja2 curly brackets and a variable name: {{ ethernet_interface }} (single-space-decorator)
````



Discussion / hints:

Fix patch:
diff --git a/j2lint/rules/jinja_variable_has_space_rule.py b/j2lint/rules/jinja_variable_has_space_rule.py
index 80d63f0..0cd55e5 100644
--- a/j2lint/rules/jinja_variable_has_space_rule.py
+++ b/j2lint/rules/jinja_variable_has_space_rule.py
@@ -24,7 +24,7 @@ class JinjaVariableHasSpaceRule(Rule):
     severity = "LOW"
 
     regex = re.compile(
-        r"{{[^ \-\+\d]|{{[-\+][^ ]|[^ \-\+\d]}}|[^ {][-\+\d]}}|{{ \s+[^ \-\+]|[^ \-\+] \s+}}"
+        r"{{[^ \-\+\d][^}]+}}|{{[-\+][^ ][^}]+}}|{{[^}]+[^ \-\+\d]}}|{{[^}]+[^ {][-\+\d]}}|{{ \s+[^ \-\+]}}|{{[^}]+[^ \-\+] \s+}}"
     )
 
     def check(self, line: str) -> re.Match[str] | None:


Test patch:
diff --git a/tests/test_rules/data/jinja_variable_has_space_rule.j2 b/tests/test_rules/data/jinja_variable_has_space_rule.j2
index a55d702..5f73e8e 100644
--- a/tests/test_rules/data/jinja_variable_has_space_rule.j2
+++ b/tests/test_rules/data/jinja_variable_has_space_rule.j2
@@ -1,3 +1,13 @@
 {{ethernet}}
 {{ ethernet}}
+# Too many spaces
+{{      ethernet}}
+{{ ethernet     }}
 {{ethernet }}
+{{+ethernet }}
+{{-ethernet }}
+{{ ethernet-}}
+# Next ones should not fail
+{{ ethernet }}
+# cf issue/62
+{% set row.line = row.line ~ "| {t:{t_width}} ".format(t=text, t_width=table.column_width[key]) %}
diff --git a/tests/test_rules/test_rules.py b/tests/test_rules/test_rules.py
index ef1752b..905da7c 100644
--- a/tests/test_rules/test_rules.py
+++ b/tests/test_rules/test_rules.py
@@ -15,7 +15,17 @@
     ),
     pytest.param(
         f"{TEST_DATA_DIR}/jinja_variable_has_space_rule.j2",
-        [("S1", 1), ("S1", 2), ("S1", 3)],
+        [
+            ("S1", 1),
+            ("S1", 2),
+            ("S1", 4),
+            ("S1", 5),
+            ("S1", 6),
+            ("S1", 7),
+            ("S2", 7),  # the faulty {{+ethernet }} triggers S2 as well
+            ("S1", 8),
+            ("S1", 9),
+        ],
         [],
         [],
         id="jinja_variable_has_space_rule",