From 5b98cd205333a64230ea30beb84efd96db2a997f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Fri, 12 Aug 2016 11:58:57 +0200 Subject: [PATCH] feat(comments): Allow comments with multiple hash chars This change make the `comments` rule accept comments that start with multiple pound signs, e.g.: ############################## ## This is some documentation Closes: #12 --- tests/rules/test_comments.py | 34 ++++++++++++++++++++++++++++++---- yamllint/rules/comments.py | 22 ++++++++++++++++------ 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/tests/rules/test_comments.py b/tests/rules/test_comments.py index f6c96b9..ee8a62c 100644 --- a/tests/rules/test_comments.py +++ b/tests/rules/test_comments.py @@ -35,6 +35,10 @@ class CommentsTestCase(RuleTestCase): ' #comment 3 bis\n' ' # comment 3 ter\n' '\n' + '################################\n' + '## comment 4\n' + '##comment 5\n' + '\n' 'string: "Une longue phrase." # this is French\n', conf) def test_starting_space(self): @@ -52,7 +56,11 @@ class CommentsTestCase(RuleTestCase): '# comment 2\n' '# comment 3\n' ' # comment 3 bis\n' - ' # comment 3 ter\n', conf) + ' # comment 3 ter\n' + '\n' + '################################\n' + '## comment 4\n' + '## comment 5\n', conf) self.check('---\n' '#comment\n' '\n' @@ -63,9 +71,14 @@ class CommentsTestCase(RuleTestCase): '# comment 2\n' '#comment 3\n' ' #comment 3 bis\n' - ' # comment 3 ter\n', conf, + ' # comment 3 ter\n' + '\n' + '################################\n' + '## comment 4\n' + '##comment 5\n', conf, problem1=(2, 2), problem2=(6, 13), - problem4=(9, 2), problem5=(10, 4)) + problem3=(9, 2), problem4=(10, 4), + problem5=(15, 3)) def test_spaces_from_content(self): conf = ('comments:\n' @@ -106,13 +119,18 @@ class CommentsTestCase(RuleTestCase): ' #comment 3 bis\n' ' # comment 3 ter\n' '\n' + '################################\n' + '## comment 4\n' + '##comment 5\n' + '\n' 'string: "Une longue phrase." # this is French\n', conf, problem1=(2, 2), problem2=(4, 7), problem3=(6, 11), problem4=(6, 12), problem5=(9, 2), problem6=(10, 4), - problem7=(13, 30)) + problem7=(15, 3), + problem8=(17, 30)) def test_empty_comment(self): conf = ('comments:\n' @@ -132,6 +150,14 @@ class CommentsTestCase(RuleTestCase): ' min-spaces-from-content: 2\n') self.check('# comment\n', conf) + def test_last_line(self): + conf = ('comments:\n' + ' require-starting-space: yes\n' + ' min-spaces-from-content: 2\n' + 'new-line-at-end-of-file: disable\n') + self.check('# comment with no newline char:\n' + '#', conf) + def test_multi_line_scalar(self): conf = ('comments:\n' ' require-starting-space: yes\n' diff --git a/yamllint/rules/comments.py b/yamllint/rules/comments.py index e848a4c..33fbcbe 100644 --- a/yamllint/rules/comments.py +++ b/yamllint/rules/comments.py @@ -35,6 +35,12 @@ Use this rule to control the position and formatting of comments. # This sentence # is a block comment + the following code snippet would **PASS**: + :: + + ############################## + ## This is some documentation + the following code snippet would **FAIL**: :: @@ -71,9 +77,13 @@ def check(conf, comment): yield LintProblem(comment.line_no, comment.column_no, 'too few spaces before comment') - if (conf['require-starting-space'] and - comment.pointer + 1 < len(comment.buffer) and - comment.buffer[comment.pointer + 1] != ' ' and - comment.buffer[comment.pointer + 1] != '\n'): - yield LintProblem(comment.line_no, comment.column_no + 1, - 'missing starting space in comment') + if conf['require-starting-space']: + text_start = comment.pointer + 1 + while (comment.buffer[text_start] == '#' and + text_start < len(comment.buffer)): + text_start += 1 + if (text_start < len(comment.buffer) and + comment.buffer[text_start] not in (' ', '\n', '\0')): + yield LintProblem(comment.line_no, + comment.column_no + text_start - comment.pointer, + 'missing starting space in comment')