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 5, 2021
1 parent 4374490 commit 4c86d3f
Show file tree
Hide file tree
Showing 2 changed files with 88 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))
28 changes: 26 additions & 2 deletions yamllint/rules/document_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@

ID = 'document-start'
TYPE = 'token'
CONF = {'present': bool}
DEFAULT = {'present': True}
CONF = {'present': bool,
'max-empty-lines-after': (int, -1),
'min-empty-lines-after': (int, 0)}
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 +98,26 @@ def check(conf, token, prev, next, nextnext, context):
yield LintProblem(token.start_mark.line + 1, 1,
'missing document start "---"')

if (conf['max-empty-lines-after'] >= 0 and
isinstance(prev, yaml.DocumentStartToken)):

empty_lines = token.start_mark.line - prev.start_mark.line - 1

if 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
isinstance(prev, yaml.DocumentStartToken)):

empty_lines = token.start_mark.line - prev.start_mark.line - 1

if 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 4c86d3f

Please sign in to comment.