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

Apply new-lines check to all lines of a file, not just the first one #693

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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))
6 changes: 6 additions & 0 deletions yamllint/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ def process_comment(self, comment):
for id in rules:
self.rules.discard(id)

def disable_by_force(self, rule):
if rule in self.all_rules:
self.rules.add(rule)

def is_disabled_by_directive(self, problem):
return problem.rule in self.rules

Expand Down Expand Up @@ -166,6 +170,8 @@ def process_comment(self, comment):
for problem in cache:
if not (disabled_for_line.is_disabled_by_directive(problem) or
disabled.is_disabled_by_directive(problem)):
if problem.rule == 'new-lines':
disabled.disable_by_force(problem.rule)
yield problem

disabled_for_line = disabled_for_next_line
Expand Down
4 changes: 2 additions & 2 deletions yamllint/rules/new_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def check(conf, line):
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:
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}')
Loading