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

Fix(j2lint): Fix spelling delimeter -> delimiter #39

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ __pycache__
build
dist
*.egg-info
**/.python-version
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Syntax and code style issues detected by Jinja2 Linter are:
- To close a control, end tag must have same indentation level
- Indentation are 4 spaces and NOT tabulation
5. S7 Jinja statements should be on separate lines
6. S8 Jinja statements should not have {%- or {%+ or -%} as delimeters
6. S8 Jinja statements should not have {%- or {%+ or -%} as delimiters
7. VAR-1 All variables shall use lower case
8. VAR-2 If variable is multi-words, underscore _ shall be used as a separator

Expand Down Expand Up @@ -109,7 +109,7 @@ j2lint <path-to-directory-of-templates> --json

### Ignoring rules

1. The --ignore option can have one or more of these values: syntax-error, single-space-decorator, filter-enclosed-by-spaces, jinja-statement-single-space, jinja-statements-indentation, no-tabs, single-statement-per-line, jinja-delimeter, jinja-variable-lower-case, jinja-variable-format.
1. The --ignore option can have one or more of these values: syntax-error, single-space-decorator, filter-enclosed-by-spaces, jinja-statement-single-space, jinja-statements-indentation, no-tabs, single-statement-per-line, jinja-delimiter, jinja-variable-lower-case, jinja-variable-format.
2. If multiple rules are to be ignored, use the --ignore option along with rule descriptions separated by space.

```bash
Expand All @@ -122,13 +122,13 @@ j2lint <path-to-directory-of-templates> --ignore <rule_description1> <rule_desc>
{# j2lint: disable=S8}

# OR
{# j2lint: disable=jinja-delimeter #}
{# j2lint: disable=jinja-delimiter #}
```

4. Disabling multiple rules

```jinja2
{# j2lint: disable=jinja-delimeter j2lint: disable=S1 #}
{# j2lint: disable=jinja-delimiter j2lint: disable=S1 #}
```

### Adding custom rules
Expand Down Expand Up @@ -162,7 +162,7 @@ j2lint --stdin
- repo: https://github.com/aristanetworks/j2lint.git
rev: <release_tag/sha>
hooks:
- id: j2lint
- id: j2lint
```

2. Run pre-commit -> ```pre-commit run --all-files ```
Expand Down
2 changes: 1 addition & 1 deletion j2lint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
'jinja-statements-indentation',
'jinja-statements-no-tabs',
'single-statement-per-line',
'jinja-statements-delimeter',
'jinja-statements-delimiter',
'jinja-variable-lower-case',
'jinja-variable-format',
'S0', 'S1', 'S2', 'S3', 'S4',
Expand Down
20 changes: 18 additions & 2 deletions j2lint/linter/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,26 @@ def create_from_directory(clazz, rules_dir, ignore_rules, warn_rules):
"""
result = clazz()
result.rules = load_plugins(os.path.expanduser(rules_dir))
# FIXME: once the first version of j2lint is tagged and publish,
# remove the deprecated_short_description
for rule in result.rules:
if (rule.short_description in ignore_rules) or (rule.id in ignore_rules):
if (
rule.short_description in ignore_rules
or rule.id in ignore_rules
or (
hasattr(rule, "deprecated_short_description")
and rule.deprecated_short_description in ignore_rules
)
):
rule.ignore = True
if (rule.short_description in warn_rules) or (rule.id in warn_rules):
if (
rule.short_description in warn_rules
or rule.id in warn_rules
or (
hasattr(rule, "deprecated_short_description")
and rule.deprecated_short_description in warn_rules
)
):
rule.warn.append(rule)
logger.info(
"Created collection from rules directory {}".format(rules_dir))
Expand Down
14 changes: 7 additions & 7 deletions j2lint/linter/indenter/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
INDENT_SHIFT = 4
DEFAULT_WHITESPACES = 1
BLOCK_START_INDENT = 0
JINJA_START_DELIMETERS = ['{%-', '{%+']
JINJA_START_DELIMITERS = ['{%-', '{%+']

jinja_node_stack = []
jinja_delimeter_stack = []
jinja_delimiter_stack = []


class Node:
Expand Down Expand Up @@ -62,8 +62,8 @@ def create_indentation_error(self, node, message):
"""
return (node.statement.start_line_no,
delimit_jinja_statement(node.statement.line,
node.statement.start_delimeter,
node.statement.end_delimeter),
node.statement.start_delimiter,
node.statement.end_delimiter),
message)

def check_indent_level(self, result, node):
Expand All @@ -74,14 +74,14 @@ def check_indent_level(self, result, node):
node (Node): Node object for which to check the level is correct
"""
actual = node.statement.begin
if len(jinja_node_stack) and jinja_node_stack[0].statement.start_delimeter in JINJA_START_DELIMETERS:
if len(jinja_node_stack) and jinja_node_stack[0].statement.start_delimiter in JINJA_START_DELIMITERS:
BLOCK_START_INDENT = 1
elif node.expected_indent == 0 and node.statement.start_delimeter in JINJA_START_DELIMETERS:
elif node.expected_indent == 0 and node.statement.start_delimiter in JINJA_START_DELIMITERS:
BLOCK_START_INDENT = 1
else:
BLOCK_START_INDENT = 0

if node.statement.start_delimeter in JINJA_START_DELIMETERS:
if node.statement.start_delimiter in JINJA_START_DELIMITERS:
expected = node.expected_indent + BLOCK_START_INDENT
else:
expected = node.expected_indent + DEFAULT_WHITESPACES + BLOCK_START_INDENT
Expand Down
4 changes: 2 additions & 2 deletions j2lint/linter/indenter/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ def __init__(self, line):
self.words = line[0].split()
self.start_line_no = line[1]
self.end_line_no = line[2]
self.start_delimeter = line[3]
self.end_delimeter = line[4]
self.start_delimiter = line[3]
self.end_delimiter = line[4]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""JinjaStatementDelimeterRule.py - Rule class to check if jinja delimeters
"""JinjaStatementDelimiterRule.py - Rule class to check if jinja delimiters
are wrong.
"""
import re
Expand All @@ -7,16 +7,21 @@
from j2lint.utils import get_jinja_statements


class JinjaStatementDelimeterRule(Rule):
"""Rule class to check if jinja delimeters are wrong.
class JinjaStatementDelimiterRule(Rule):
"""Rule class to check if jinja delimiters are wrong.
"""
id = 'S6'
short_description = 'jinja-statements-delimeter'
description = "Jinja statements should not have {%- or {%+ or -%} as delimeters"
# FIXME - for now supporting both syntax for the short_description to be backward
# compatible.
# The doc and READMEs are fixed to show only the new syntax.
# This will be removed
short_description = 'jinja-statements-delimiter'
deprecated_short_description = 'jinja-statements-delimeter'
description = "Jinja statements should not have {%- or {%+ or -%} as delimiters"
severity = 'LOW'

def check(self, file, line):
"""Checks if the given line matches the wrong delimeters
"""Checks if the given line matches the wrong delimiters

Args:
file (string): file path
Expand Down
4 changes: 2 additions & 2 deletions j2lint/rules/JinjaStatementHasSpacesRule.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""JinjaStatementHasSpacesRule.py - Rule class to check if jinja statement has
atleast a single space surrounding the
delimeter.
delimiter.
"""
import re
from j2lint.linter.rule import Rule


class JinjaStatementHasSpacesRule(Rule):
"""Rule class to check if jinja statement has atleast a single space
surrounding the delimeter.
surrounding the delimiter.
"""
id = 'S4'
short_description = 'jinja-statements-single-space'
Expand Down
2 changes: 1 addition & 1 deletion j2lint/rules/JinjaTemplateIndentationRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def checktext(self, file, text):

with open(file['path']) as template:
text = template.read()
# Collect only Jinja Statements within delimeters {% and %} and ignore the other statements
# Collect only Jinja Statements within delimiters {% and %} and ignore the other statements
lines = get_jinja_statements(text, indentation=True)

# Build a tree out of Jinja Statements to get the expected indentation level for each statement
Expand Down
10 changes: 7 additions & 3 deletions j2lint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def get_tuple(l, item):


def get_jinja_statements(text, indentation=False):
"""Gets jinja statements with {%[-/+] [-]%} delimeters
"""Gets jinja statements with {%[-/+] [-]%} delimiters

Args:
text (string): multiline text to search the jinja statements in
Expand All @@ -158,13 +158,13 @@ def get_jinja_statements(text, indentation=False):


def delimit_jinja_statement(line, start="{%", end="%}"):
"""Adds end delimeters for a jinja statement
"""Adds end delimiters for a jinja statement

Args:
line (string): text line

Returns:
[string]: jinja statement with jinja start and end delimeters
[string]: jinja statement with jinja start and end delimiters
"""
return start + line + end

Expand Down Expand Up @@ -219,6 +219,10 @@ def is_rule_disabled(text, rule):
for line in regex.finditer(comment):
if rule.short_description == line.group(1):
return True
# FIXME - remove next release
if (hasattr(rule, "deprecated_short_description") and
rule.deprecated_short_description == line.group(1)):
return True
if rule.id == line.group(1):
return True
return False