From 157b06871da1864a0c42c347e86dd1c511bdd035 Mon Sep 17 00:00:00 2001 From: Jan Wille Date: Mon, 20 Jun 2022 19:09:42 +0200 Subject: [PATCH] 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 --- yamllint/rules/new_lines.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/yamllint/rules/new_lines.py b/yamllint/rules/new_lines.py index fc3d399..9c5a022 100644 --- a/yamllint/rules/new_lines.py +++ b/yamllint/rules/new_lines.py @@ -41,13 +41,13 @@ DEFAULT = {'type': 'unix'} 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('\'')))