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
pull/17/head
Adrien Vergé 9 years ago
parent 82dd7dbf16
commit 5b98cd2053

@ -35,6 +35,10 @@ class CommentsTestCase(RuleTestCase):
' #comment 3 bis\n' ' #comment 3 bis\n'
' # comment 3 ter\n' ' # comment 3 ter\n'
'\n' '\n'
'################################\n'
'## comment 4\n'
'##comment 5\n'
'\n'
'string: "Une longue phrase." # this is French\n', conf) 'string: "Une longue phrase." # this is French\n', conf)
def test_starting_space(self): def test_starting_space(self):
@ -52,7 +56,11 @@ class CommentsTestCase(RuleTestCase):
'# comment 2\n' '# comment 2\n'
'# comment 3\n' '# comment 3\n'
' # comment 3 bis\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' self.check('---\n'
'#comment\n' '#comment\n'
'\n' '\n'
@ -63,9 +71,14 @@ class CommentsTestCase(RuleTestCase):
'# comment 2\n' '# comment 2\n'
'#comment 3\n' '#comment 3\n'
' #comment 3 bis\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), 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): def test_spaces_from_content(self):
conf = ('comments:\n' conf = ('comments:\n'
@ -106,13 +119,18 @@ class CommentsTestCase(RuleTestCase):
' #comment 3 bis\n' ' #comment 3 bis\n'
' # comment 3 ter\n' ' # comment 3 ter\n'
'\n' '\n'
'################################\n'
'## comment 4\n'
'##comment 5\n'
'\n'
'string: "Une longue phrase." # this is French\n', conf, 'string: "Une longue phrase." # this is French\n', conf,
problem1=(2, 2), problem1=(2, 2),
problem2=(4, 7), problem2=(4, 7),
problem3=(6, 11), problem4=(6, 12), problem3=(6, 11), problem4=(6, 12),
problem5=(9, 2), problem5=(9, 2),
problem6=(10, 4), problem6=(10, 4),
problem7=(13, 30)) problem7=(15, 3),
problem8=(17, 30))
def test_empty_comment(self): def test_empty_comment(self):
conf = ('comments:\n' conf = ('comments:\n'
@ -132,6 +150,14 @@ class CommentsTestCase(RuleTestCase):
' min-spaces-from-content: 2\n') ' min-spaces-from-content: 2\n')
self.check('# comment\n', conf) 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): def test_multi_line_scalar(self):
conf = ('comments:\n' conf = ('comments:\n'
' require-starting-space: yes\n' ' require-starting-space: yes\n'

@ -35,6 +35,12 @@ Use this rule to control the position and formatting of comments.
# This sentence # This sentence
# is a block comment # is a block comment
the following code snippet would **PASS**:
::
##############################
## This is some documentation
the following code snippet would **FAIL**: the following code snippet would **FAIL**:
:: ::
@ -71,9 +77,13 @@ def check(conf, comment):
yield LintProblem(comment.line_no, comment.column_no, yield LintProblem(comment.line_no, comment.column_no,
'too few spaces before comment') 'too few spaces before comment')
if (conf['require-starting-space'] and if conf['require-starting-space']:
comment.pointer + 1 < len(comment.buffer) and text_start = comment.pointer + 1
comment.buffer[comment.pointer + 1] != ' ' and while (comment.buffer[text_start] == '#' and
comment.buffer[comment.pointer + 1] != '\n'): text_start < len(comment.buffer)):
yield LintProblem(comment.line_no, comment.column_no + 1, 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') 'missing starting space in comment')

Loading…
Cancel
Save