Skip to content

Commit

Permalink
new-lines: refactor to reduce duplicate code
Browse files Browse the repository at this point in the history
Both options where using identical code.  Now the newline character
is determined beforehand depending on the selected option and then
the same code can be used for all options
  • Loading branch information
Cube707 authored and adrienverge committed Jun 23, 2022
1 parent af843b6 commit 157b068
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions yamllint/rules/new_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@


def check(conf, line):
if conf['type'] == 'unix':
newline_char = '\n'
elif conf['type'] == 'dos':
newline_char = '\r\n'

if line.start == 0 and len(line.buffer) > line.end:
if conf['type'] == 'dos':
if (line.end + 2 > len(line.buffer) or
line.buffer[line.end:line.end + 2] != '\r\n'):
yield LintProblem(1, line.end - line.start + 1,
'wrong new line character: expected \\r\\n')
else:
if line.buffer[line.end] != '\n':
yield LintProblem(1, line.end - line.start + 1,
'wrong new line character: expected \\n')
if line.buffer[line.end:line.end + len(newline_char)] != newline_char:
yield LintProblem(1, line.end - line.start + 1,
'wrong new line character: expected {}'
.format(repr(newline_char).strip('\'')))

0 comments on commit 157b068

Please sign in to comment.