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:
committed by
Adrien Vergé
parent
0f073f7a09
commit
b77f78f677
@@ -80,6 +80,33 @@ class CommentsTestCase(RuleTestCase):
|
|||||||
problem3=(9, 2), problem4=(10, 4),
|
problem3=(9, 2), problem4=(10, 4),
|
||||||
problem5=(15, 3))
|
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):
|
def test_spaces_from_content(self):
|
||||||
conf = ('comments:\n'
|
conf = ('comments:\n'
|
||||||
' require-starting-space: false\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
|
* Use ``require-starting-space`` to require a space character right after the
|
||||||
``#``. Set to ``true`` to enable, ``false`` to disable.
|
``#``. 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
|
* ``min-spaces-from-content`` is used to visually separate inline comments from
|
||||||
content. It defines the minimal required number of spaces between a comment
|
content. It defines the minimal required number of spaces between a comment
|
||||||
and its preceding content.
|
and its preceding content.
|
||||||
@@ -67,8 +70,10 @@ from yamllint.linter import LintProblem
|
|||||||
ID = 'comments'
|
ID = 'comments'
|
||||||
TYPE = 'comment'
|
TYPE = 'comment'
|
||||||
CONF = {'require-starting-space': bool,
|
CONF = {'require-starting-space': bool,
|
||||||
|
'ignore-shebangs': bool,
|
||||||
'min-spaces-from-content': int}
|
'min-spaces-from-content': int}
|
||||||
DEFAULT = {'require-starting-space': True,
|
DEFAULT = {'require-starting-space': True,
|
||||||
|
'ignore-shebangs': True,
|
||||||
'min-spaces-from-content': 2}
|
'min-spaces-from-content': 2}
|
||||||
|
|
||||||
|
|
||||||
@@ -84,8 +89,13 @@ def check(conf, comment):
|
|||||||
while (comment.buffer[text_start] == '#' and
|
while (comment.buffer[text_start] == '#' and
|
||||||
text_start < len(comment.buffer)):
|
text_start < len(comment.buffer)):
|
||||||
text_start += 1
|
text_start += 1
|
||||||
if (text_start < len(comment.buffer) and
|
if text_start < len(comment.buffer):
|
||||||
comment.buffer[text_start] not in (' ', '\n', '\0')):
|
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,
|
yield LintProblem(comment.line_no,
|
||||||
comment.column_no + text_start - comment.pointer,
|
column,
|
||||||
'missing starting space in comment')
|
'missing starting space in comment')
|
||||||
|
|||||||
Reference in New Issue
Block a user