From b77f78f677f2714269cc8957fd0c85df9cc0c391 Mon Sep 17 00:00:00 2001 From: Mattias Bengtsson Date: Thu, 6 Dec 2018 13:50:24 +0100 Subject: [PATCH] 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 --- tests/rules/test_comments.py | 27 +++++++++++++++++++++++++++ yamllint/rules/comments.py | 20 +++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/tests/rules/test_comments.py b/tests/rules/test_comments.py index ded31fa..742650b 100644 --- a/tests/rules/test_comments.py +++ b/tests/rules/test_comments.py @@ -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' diff --git a/yamllint/rules/comments.py b/yamllint/rules/comments.py index 7ecce39..4777d02 100644 --- a/yamllint/rules/comments.py +++ b/yamllint/rules/comments.py @@ -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 `_ 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')