[document-end] adds min/max empty lines before

pull/393/head
Christian Krause 4 years ago
parent 0edff4fca0
commit 88d302e4c7
No known key found for this signature in database
GPG Key ID: 3CD613B9A8567F58

@ -72,3 +72,15 @@ class DocumentEndTestCase(RuleTestCase):
'---\n'
'third: document\n'
'...\n', conf, problem=(6, 1))
def test_empty_lines(self):
conf = ('document-end:\n'
' present: true\n'
' max-empty-lines-before: 0\n')
self.check('---\n'
'doc: ument\n'
'...\n', conf)
self.check('---\n'
'doc: ument\n'
'\n'
'...\n', conf, problem=(3, 0))

@ -21,6 +21,10 @@ Use this rule to require or forbid the use of document end marker (``...``).
* Set ``present`` to ``true`` when the document end marker is required, or to
``false`` when it is forbidden.
* ``min-empty-lines-before`` defines the minimal number of empty lines before
the document end marker.
* ``max-empty-lines-before`` defines the maximal number of empty lines before
the document end marker.
.. rubric:: Default values (when enabled)
@ -89,8 +93,12 @@ from yamllint.linter import LintProblem
ID = 'document-end'
TYPE = 'token'
CONF = {'present': bool}
DEFAULT = {'present': True}
CONF = {'present': bool,
'max-empty-lines-before': int,
'min-empty-lines-before': int}
DEFAULT = {'present': True,
'max-empty-lines-before': -1,
'min-empty-lines-before': 0}
def check(conf, token, prev, next, nextnext, context):
@ -108,6 +116,21 @@ def check(conf, token, prev, next, nextnext, context):
yield LintProblem(token.start_mark.line + 1, 1,
'missing document end "..."')
if isinstance(next, yaml.DocumentEndToken):
empty_lines = next.start_mark.line - prev.start_mark.line - 1
if (conf['max-empty-lines-before'] >= 0 and
empty_lines > conf['max-empty-lines-before']):
yield LintProblem(token.start_mark.line,
token.start_mark.column,
'too many empty lines before document end')
if (conf['min-empty-lines-before'] > 0 and
empty_lines < conf['min-empty-lines-before']):
yield LintProblem(token.start_mark.line,
token.start_mark.column,
'too few empty lines before document end')
else:
if isinstance(token, yaml.DocumentEndToken):
yield LintProblem(token.start_mark.line + 1,

Loading…
Cancel
Save