mirror of
https://github.com/adrienverge/yamllint.git
synced 2025-02-25 18:55:20 -06:00
Support ignoring shebangs
Some usages of YAML (like Ansible) supports running the file as a script. Support (by default) an ignore-shebangs setting for the comments module. Fixes #116 - comments rule with require-starting-space: true should special case shebang
This commit is contained in:
parent
0f073f7a09
commit
b77f78f677
@ -80,6 +80,33 @@ class CommentsTestCase(RuleTestCase):
|
||||
problem3=(9, 2), problem4=(10, 4),
|
||||
problem5=(15, 3))
|
||||
|
||||
def test_shebang(self):
|
||||
conf = ('comments:\n'
|
||||
' require-starting-space: true\n'
|
||||
' ignore-shebangs: false\n'
|
||||
'comments-indentation: disable\n')
|
||||
self.check('#!/bin/env my-interpreter\n',
|
||||
conf, problem1=(1, 2))
|
||||
self.check('#!/bin/env my-interpreter\n'
|
||||
'---\n'
|
||||
'#comment\n'
|
||||
'#!/bin/env my-interpreter\n'
|
||||
'', conf,
|
||||
problem1=(1, 2), problem2=(3, 2), problem3=(4, 2))
|
||||
|
||||
def test_ignore_shebang(self):
|
||||
conf = ('comments:\n'
|
||||
' require-starting-space: true\n'
|
||||
' ignore-shebangs: true\n'
|
||||
'comments-indentation: disable\n')
|
||||
self.check('#!/bin/env my-interpreter\n', conf)
|
||||
self.check('#!/bin/env my-interpreter\n'
|
||||
'---\n'
|
||||
'#comment\n'
|
||||
'#!/bin/env my-interpreter\n'
|
||||
'', conf,
|
||||
problem2=(3, 2), problem3=(4, 2))
|
||||
|
||||
def test_spaces_from_content(self):
|
||||
conf = ('comments:\n'
|
||||
' require-starting-space: false\n'
|
||||
|
@ -21,6 +21,9 @@ Use this rule to control the position and formatting of comments.
|
||||
|
||||
* Use ``require-starting-space`` to require a space character right after the
|
||||
``#``. Set to ``true`` to enable, ``false`` to disable.
|
||||
* Use ``ignore-shebangs`` to ignore a
|
||||
`shebang <https://en.wikipedia.org/wiki/Shebang_(Unix)>`_ at the beginning of
|
||||
the file when ``require-starting-space`` is set.
|
||||
* ``min-spaces-from-content`` is used to visually separate inline comments from
|
||||
content. It defines the minimal required number of spaces between a comment
|
||||
and its preceding content.
|
||||
@ -67,8 +70,10 @@ from yamllint.linter import LintProblem
|
||||
ID = 'comments'
|
||||
TYPE = 'comment'
|
||||
CONF = {'require-starting-space': bool,
|
||||
'ignore-shebangs': bool,
|
||||
'min-spaces-from-content': int}
|
||||
DEFAULT = {'require-starting-space': True,
|
||||
'ignore-shebangs': True,
|
||||
'min-spaces-from-content': 2}
|
||||
|
||||
|
||||
@ -84,8 +89,13 @@ def check(conf, comment):
|
||||
while (comment.buffer[text_start] == '#' and
|
||||
text_start < len(comment.buffer)):
|
||||
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')
|
||||
if text_start < len(comment.buffer):
|
||||
if (conf['ignore-shebangs'] and
|
||||
comment.line_no == 1 and
|
||||
comment.buffer[text_start] == '!'):
|
||||
return
|
||||
elif comment.buffer[text_start] not in (' ', '\n', '\0'):
|
||||
column = comment.column_no + text_start - comment.pointer
|
||||
yield LintProblem(comment.line_no,
|
||||
column,
|
||||
'missing starting space in comment')
|
||||
|
Loading…
Reference in New Issue
Block a user