Skip to content
Open
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
16 changes: 4 additions & 12 deletions tests/rules/test_new_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,8 @@ def test_platform_type(self):
self.check('---\ntext\n', conf)
self.check('---\r\ntext\r\n', conf, problem=(1, 4))
self.check('---\r\ntext\n', conf, problem=(1, 4))
# FIXME: the following tests currently don't work
# because only the first line is checked for line-endings
# see: issue #475
# ---
# self.check('---\ntext\r\nfoo\n', conf, problem=(2, 4))
# self.check('---\ntext\r\n', conf, problem=(2, 4))
self.check('---\ntext\r\nfoo\n', conf, problem=(2, 5))
self.check('---\ntext\r\n', conf, problem=(2, 5))

# mock the Windows new-line-character
with mock.patch('yamllint.rules.new_lines.linesep', '\r\n'):
Expand All @@ -88,9 +84,5 @@ def test_platform_type(self):
self.check('---\r\ntext\r\n', conf)
self.check('---\ntext\n', conf, problem=(1, 4))
self.check('---\ntext\r\n', conf, problem=(1, 4))
# FIXME: the following tests currently don't work
# because only the first line is checked for line-endings
# see: issue #475
# ---
# self.check('---\r\ntext\nfoo\r\n', conf, problem=(2, 4))
# self.check('---\r\ntext\n', conf, problem=(2, 4))
self.check('---\r\ntext\nfoo\r\n', conf, problem=(2, 5))
self.check('---\r\ntext\n', conf, problem=(2, 5))
4 changes: 3 additions & 1 deletion yamllint/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def get_cosmetic_problems(buffer, conf, filepath):
context = {}
for rule in token_rules:
context[rule.ID] = {}
for rule in line_rules:
context[rule.ID] = {}

class DisableDirective:
def __init__(self):
Expand Down Expand Up @@ -156,7 +158,7 @@ def process_comment(self, comment):
elif isinstance(elem, parser.Line):
for rule in line_rules:
rule_conf = conf.rules[rule.ID]
for problem in rule.check(rule_conf, elem):
for problem in rule.check(rule_conf, elem, context[rule.ID]):
problem.rule = rule.ID
problem.level = rule_conf['level']
cache.append(problem)
Expand Down
2 changes: 1 addition & 1 deletion yamllint/rules/empty_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
'max-end': 0}


def check(conf, line):
def check(conf, line, context):
if line.start == line.end and line.end < len(line.buffer):
# Only alert on the last blank line of a series
if (line.end + 2 <= len(line.buffer) and
Expand Down
2 changes: 1 addition & 1 deletion yamllint/rules/line_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def check_inline_mapping(line):
return False


def check(conf, line):
def check(conf, line, context):
max_length = conf['max']
length = line.end - line.start
if length > max_length:
Expand Down
2 changes: 1 addition & 1 deletion yamllint/rules/new_line_at_end_of_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
TYPE = 'line'


def check(conf, line):
def check(conf, line, context):
if line.end == len(line.buffer) and line.end > line.start:
yield LintProblem(line.line_no, line.end - line.start + 1,
'no new line character at the end of file')
10 changes: 7 additions & 3 deletions yamllint/rules/new_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,20 @@
DEFAULT = {'type': 'unix'}


def check(conf, line):
def check(conf, line, context):
if context.get("has_shown_problem") is True:
return

if conf['type'] == 'unix':
newline_char = '\n'
elif conf['type'] == 'platform':
newline_char = linesep
elif conf['type'] == 'dos':
newline_char = '\r\n'

if line.start == 0 and len(line.buffer) > line.end:
if len(line.buffer) > line.end:
if line.buffer[line.end:line.end + len(newline_char)] != newline_char:
context["has_shown_problem"] = True
c = repr(newline_char).strip('\'')
yield LintProblem(1, line.end - line.start + 1,
yield LintProblem(line.line_no, line.end - line.start + 1,
f'wrong new line character: expected {c}')
2 changes: 1 addition & 1 deletion yamllint/rules/trailing_spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
TYPE = 'line'


def check(conf, line):
def check(conf, line, context):
if line.end == 0:
return

Expand Down