new-lines: refactor to reduce duplicate code

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
This commit is contained in:
Jan Wille
2022-06-20 19:09:42 +02:00
parent 237eefba73
commit 49f029ea99

View File

@@ -41,13 +41,13 @@ DEFAULT = {'type': 'unix'}
def check(conf, line): 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 line.start == 0 and len(line.buffer) > line.end:
if conf['type'] == 'dos': if line.buffer[line.end:line.end + len(newline_char)] != newline_char:
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, yield LintProblem(1, line.end - line.start + 1,
'wrong new line character: expected \\r\\n') 'wrong new line character: expected {}'
else: .format(repr(newline_char).strip('\'')))
if line.buffer[line.end] != '\n':
yield LintProblem(1, line.end - line.start + 1,
'wrong new line character: expected \\n')