Skip to content

Commit

Permalink
[document-start] adds min/max empty lines after document start/end
Browse files Browse the repository at this point in the history
  • Loading branch information
wookietreiber committed Jul 13, 2021
1 parent 4374490 commit c9c6bdc
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
62 changes: 62 additions & 0 deletions tests/rules/test_document_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,65 @@ def test_directives(self):
'---\n'
'doc: 2\n'
'...\n', conf)

def test_empty_lines(self):
conf = ('document-start:\n'
' present: true\n'
' max-empty-lines-after: 0\n')

self.check('%YAML 1.2\n'
'---\n'
'doc: ument\n', conf)

self.check('%YAML 1.2\n'
'---\n'
'\n'
'doc: ument\n', conf, problem=(4, 1))

conf = ('document-start:\n'
' present: true\n'
' max-empty-lines-after: 1\n')

self.check('%YAML 1.2\n'
'---\n'
'\n'
'doc: ument\n', conf)

self.check('%YAML 1.2\n'
'---\n'
'\n'
'\n'
'doc: ument\n', conf, problem=(5, 1))

conf = ('document-start:\n'
' present: true\n'
' min-empty-lines-after: 1\n')

self.check('%YAML 1.2\n'
'---\n'
'\n'
'doc: ument\n', conf)

self.check('%YAML 1.2\n'
'---\n'
'doc: ument\n', conf, problem=(3, 1))

conf = ('document-start:\n'
' present: true\n'
' max-empty-lines-after: 1\n'
' min-empty-lines-after: 1\n')

self.check('%YAML 1.2\n'
'---\n'
'\n'
'doc: ument\n', conf)

self.check('%YAML 1.2\n'
'---\n'
'doc: ument\n', conf, problem=(3, 1))

self.check('%YAML 1.2\n'
'---\n'
'\n'
'\n'
'doc: ument\n', conf, problem=(5, 1))
27 changes: 25 additions & 2 deletions yamllint/rules/document_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
* Set ``present`` to ``true`` when the document start marker is required, or to
``false`` when it is forbidden.
* ``min-empty-lines-after`` defines the minimal number of empty lines after the
document start marker.
* ``max-empty-lines-after`` defines the maximal number of empty lines after the
document start marker.
.. rubric:: Default values (when enabled)
Expand Down Expand Up @@ -79,8 +83,12 @@

ID = 'document-start'
TYPE = 'token'
CONF = {'present': bool}
DEFAULT = {'present': True}
CONF = {'present': bool,
'max-empty-lines-after': int,
'min-empty-lines-after': int}
DEFAULT = {'present': True,
'max-empty-lines-after': -1,
'min-empty-lines-after': 0}


def check(conf, token, prev, next, nextnext, context):
Expand All @@ -94,6 +102,21 @@ def check(conf, token, prev, next, nextnext, context):
yield LintProblem(token.start_mark.line + 1, 1,
'missing document start "---"')

if isinstance(prev, yaml.DocumentStartToken):
empty_lines = token.start_mark.line - prev.start_mark.line - 1

if (conf['max-empty-lines-after'] >= 0 and
empty_lines > conf['max-empty-lines-after']):
yield LintProblem(token.start_mark.line + 1,
token.start_mark.column + 1,
'too many empty lines after document start')

if (conf['min-empty-lines-after'] > 0 and
empty_lines < conf['min-empty-lines-after']):
yield LintProblem(token.start_mark.line + 1,
token.start_mark.column + 1,
'too few empty lines after document start')

else:
if isinstance(token, yaml.DocumentStartToken):
yield LintProblem(token.start_mark.line + 1,
Expand Down

0 comments on commit c9c6bdc

Please sign in to comment.