fix wrong line numbers in normalized yaml

This commit is contained in:
Robert Kaussow 2019-01-28 11:04:44 +01:00
parent 51c5f43e5c
commit 961b965f96
3 changed files with 23 additions and 33 deletions

View File

@ -12,23 +12,21 @@ def check_braces_spaces(candidate, settings):
yamllines, errors = get_normalized_yaml(candidate, settings)
description = "no suitable numbers of spaces (required: 1)"
lineno = 1
matches = []
braces = re.compile("{{(.*?)}}")
if not errors:
for line in yamllines:
lineno += 1
for i, line in yamllines:
match = braces.findall(line)
if match:
for item in match:
matches.append((item, lineno))
matches.append((i, item))
for item, lineno in matches:
leading, trailing = count_spaces(item)
for i, line in matches:
leading, trailing = count_spaces(line)
if not leading == 1 or not trailing == 1:
errors.append(Error(lineno, description))
errors.append(Error(i, description))
return Result(candidate.path, errors)
@ -168,14 +166,12 @@ def check_empty_string_compare(candidate, settings):
description = 'use `when: var` rather than `when: var != ""` (or ' \
'conversely `when: not var` rather than `when: var == ""`)'
lineno = 0
empty_string_compare = re.compile("[=!]= ?[\"'][\"']")
if not errors:
for line in yamllines:
lineno += 1
for i, line in yamllines:
if empty_string_compare.findall(line):
errors.append(Error(lineno, description))
errors.append(Error(i, description))
return Result(candidate.path, errors)
@ -185,14 +181,12 @@ def check_compare_to_literal_bool(candidate, settings):
description = "use `when: var` rather than `when: var == True` " \
"(or conversely `when: not var`)"
lineno = 0
literal_bool_compare = re.compile("[=!]= ?(True|true|False|false)")
if not errors:
for line in yamllines:
lineno += 1
for i, line in yamllines:
if literal_bool_compare.findall(line):
errors.append(Error(lineno, description))
errors.append(Error(i, description))
return Result(candidate.path, errors)
@ -213,15 +207,13 @@ def check_delegate_to_localhost(candidate, settings):
def check_uppercase_literal_bool(candidate, settings):
yamllines, errors = get_normalized_yaml(candidate, settings)
description = "literal bools should be written as 'True|False' instead of 'true|false'"
description = "literal bools should be written as 'True/False' or 'yes/no'"
lineno = 1
uppercase_bool = re.compile(r"([=!]=|:)\s*(true|false)")
uppercase_bool = re.compile(r"([=!]=|:)\s*(true|false|TRUE|FALSE|Yes|No|YES|NO)\s*$")
if not errors:
for line in yamllines:
lineno += 1
for i, line in yamllines:
if uppercase_bool.findall(line):
errors.append(Error(lineno, description))
errors.append(Error(i, description))
return Result(candidate.path, errors)

View File

@ -15,18 +15,16 @@ def check_line_between_tasks(candidate, settings):
description = "missing task separation (required: 1 empty line)"
task_regex = re.compile(r"-\sname:.*")
lineno = 0
prevline = "#file_start_marker"
allowed_prevline = ["---", "tasks:", "pre_tasks:", "post_tasks:", "block:"]
if not errors:
for line in lines:
lineno += 1
for i, line in lines:
match = task_regex.search(line)
if match and prevline:
if not any(item in prevline for item in allowed_prevline):
errors.append(Error(lineno, description))
errors.append(Error(i, description))
prevline = line.strip()
return Result(candidate.path, errors)

View File

@ -486,25 +486,25 @@ def parse_yaml_linenumbers(data, filename):
def normalized_yaml(file, options):
# lines = []
lines = []
removes = []
try:
with codecs.open(file, mode='rb', encoding='utf-8') as f:
lines = enumerate(f.readlines())
print(lines)
lines = list(enumerate(f.readlines(), start=1))
for i, line in lines:
if line.strip().startswith("#"):
removes.append(line)
removes.append((i, line))
# remove document starter also
if options.get("remove_markers") and line.strip() == "---":
removes.append(line)
removes.append((i, line))
# remove empty lines
if options.get("remove_empty") and not line.strip():
removes.append(line)
removes.append((i, line))
# for line in removes:
# lines.remove(line)
for line in removes:
lines.remove(line)
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e:
raise LaterError("syntax error", e)
return lines