Skip to content

Commit 027d1b0

Browse files
directives: Fix DOS lines messing with rule IDs
Fixes #325 The linter allows a directive to contain trailing whitespace characters like \r, but does not trim them before iterating on the rules. As a result, the last rule in the list contains the trailing whitespace characters and never matches any existing rule. I added the necessary trimming, as well as a test with 2 checks to go along with it.
1 parent 67cb4eb commit 027d1b0

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

tests/test_yamllint_directives.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,34 @@ def test_disable_line_directive_with_rules(self):
232232
problem1=(3, 18, 'trailing-spaces'),
233233
problem2=(4, 8, 'colons'))
234234

235+
def test_disable_directive_with_rules_and_dos_lines(self):
236+
conf = self.conf + 'new-lines: {type: dos}\n'
237+
self.check('---\r\n'
238+
'- [valid , YAML]\r\n'
239+
'# yamllint disable rule:trailing-spaces\r\n'
240+
'- trailing spaces \r\n'
241+
'- bad : colon\r\n'
242+
'- [valid , YAML]\r\n'
243+
'# yamllint enable rule:trailing-spaces\r\n'
244+
'- bad : colon and spaces \r\n'
245+
'- [valid , YAML]\r\n',
246+
conf,
247+
problem1=(5, 8, 'colons'),
248+
problem2=(8, 7, 'colons'),
249+
problem3=(8, 26, 'trailing-spaces'))
250+
self.check('---\r\n'
251+
'- [valid , YAML]\r\n'
252+
'- trailing spaces \r\n'
253+
'- bad : colon\r\n'
254+
'- [valid , YAML]\r\n'
255+
'# yamllint disable-line rule:colons\r\n'
256+
'- bad : colon and spaces \r\n'
257+
'- [valid , YAML]\r\n',
258+
conf,
259+
problem1=(3, 18, 'trailing-spaces'),
260+
problem2=(4, 8, 'colons'),
261+
problem3=(7, 26, 'trailing-spaces'))
262+
235263
def test_directive_on_last_line(self):
236264
conf = 'new-line-at-end-of-file: {}'
237265
self.check('---\n'

yamllint/linter.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def process_comment(self, comment):
8787
return # this certainly wasn't a yamllint directive comment
8888

8989
if re.match(r'^# yamllint disable( rule:\S+)*\s*$', comment):
90-
rules = [item[5:] for item in comment[18:].split(' ')][1:]
90+
items = comment[18:].rstrip().split(' ')
91+
rules = [item[5:] for item in items][1:]
9192
if len(rules) == 0:
9293
self.rules = self.all_rules.copy()
9394
else:
@@ -96,7 +97,8 @@ def process_comment(self, comment):
9697
self.rules.add(id)
9798

9899
elif re.match(r'^# yamllint enable( rule:\S+)*\s*$', comment):
99-
rules = [item[5:] for item in comment[17:].split(' ')][1:]
100+
items = comment[17:].rstrip().split(' ')
101+
rules = [item[5:] for item in items][1:]
100102
if len(rules) == 0:
101103
self.rules.clear()
102104
else:
@@ -114,7 +116,8 @@ def process_comment(self, comment):
114116
return # this certainly wasn't a yamllint directive comment
115117

116118
if re.match(r'^# yamllint disable-line( rule:\S+)*\s*$', comment):
117-
rules = [item[5:] for item in comment[23:].split(' ')][1:]
119+
items = comment[23:].rstrip().split(' ')
120+
rules = [item[5:] for item in items][1:]
118121
if len(rules) == 0:
119122
self.rules = self.all_rules.copy()
120123
else:

0 commit comments

Comments
 (0)