Rules: Add the 'comments' rule

This commit is contained in:
Adrien Vergé
2016-01-13 21:36:35 +01:00
parent 5c4c208b98
commit 851b9ac42c
4 changed files with 259 additions and 6 deletions

View File

@@ -1,8 +1,8 @@
---
rules:
#block-sequence-indentation:
# present: yes
# block-sequence-indentation:
# present: yes
braces:
min-spaces-inside: 0
max-spaces-inside: 0
@@ -15,9 +15,10 @@ rules:
commas:
max-spaces-before: 0
max-spaces-after: 1
#comment:
# min-spaces-after: 1
# min-spaces-before: 2
comments:
level: warning
require-starting-space: yes
min-spaces-from-content: 2
document-end: disable
document-start:
level: warning
@@ -30,7 +31,8 @@ rules:
max-spaces-after: 1
indentation:
spaces: 2
#comments: yes
# indent-lists: yes
# check-comments: yes
line-length:
max: 80
new-line-at-end-of-file: {level: error}

View File

@@ -19,6 +19,7 @@ from yamllint.rules import (
brackets,
colons,
commas,
comments,
document_end,
document_start,
empty_lines,
@@ -35,6 +36,7 @@ _RULES = {
brackets.ID: brackets,
colons.ID: colons,
commas.ID: commas,
comments.ID: comments,
document_end.ID: document_end,
document_start.ID: document_start,
empty_lines.ID: empty_lines,

View File

@@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2016 Adrien Vergé
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from yamllint.errors import LintProblem
ID = 'comments'
TYPE = 'token'
CONF = {'require-starting-space': bool,
'min-spaces-from-content': int}
class Comment(object):
def __init__(self, line, column, buffer, pointer):
self.line = line
self.column = column
self.buffer = buffer
self.pointer = pointer
def __repr__(self):
end = self.buffer.find('\n', self.pointer)
if end == -1:
end = self.buffer.find('\0', self.pointer)
if end != -1:
return self.buffer[self.pointer:end]
return self.buffer[self.pointer:]
def __eq__(self, other):
return (self.line == other.line and
self.column == other.column and
str(self) == str(other))
def get_comments_until_next_token(token, next):
if next is None:
buf = token.end_mark.buffer[token.end_mark.pointer:]
elif token.end_mark.line == next.start_mark.line:
return
else:
buf = token.end_mark.buffer[token.end_mark.pointer:
next.start_mark.pointer]
line_no = token.end_mark.line + 1
column_no = token.end_mark.column + 1
pointer = token.end_mark.pointer
for line in buf.split('\n'):
pos = line.find('#')
if pos != -1:
yield Comment(line_no, column_no + pos,
token.end_mark.buffer, pointer + pos)
pointer += len(line) + 1
line_no += 1
column_no = 1
def check(conf, token, prev, next):
for comment in get_comments_until_next_token(token, next):
if (conf['min-spaces-from-content'] != -1 and
comment.line == token.end_mark.line + 1 and
comment.pointer - token.end_mark.pointer <
conf['min-spaces-from-content']):
yield LintProblem(comment.line, comment.column,
'too few spaces before comment')
if (conf['require-starting-space'] and
comment.pointer + 1 < len(comment.buffer) and
comment.buffer[comment.pointer + 1] != ' '):
yield LintProblem(comment.line, comment.column + 1,
'missing starting space in comment')