Add rule: truthy, to forbid truthy values that are not quoted

This commit is contained in:
Peter Ericson
2016-09-28 21:56:45 +10:00
committed by Adrien Vergé
parent c163135ee5
commit 1f472bc144
7 changed files with 105 additions and 2 deletions

View File

@@ -43,3 +43,5 @@ rules:
new-lines:
type: unix
trailing-spaces: enable
truthy:
level: warning

View File

@@ -26,3 +26,4 @@ rules:
line-length:
level: warning
allow-non-breakable-inline-mappings: yes
truthy: disable

View File

@@ -31,6 +31,7 @@ from yamllint.rules import (
new_line_at_end_of_file,
new_lines,
trailing_spaces,
truthy,
)
_RULES = {
@@ -50,6 +51,7 @@ _RULES = {
new_line_at_end_of_file.ID: new_line_at_end_of_file,
new_lines.ID: new_lines,
trailing_spaces.ID: trailing_spaces,
truthy.ID: truthy,
}

56
yamllint/rules/truthy.py Normal file
View File

@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2016 Peter Ericson
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Use this rule to forbid truthy values that are not quoted.
.. rubric:: Examples
#. With ``truthy: {}``
the following code snippet would **PASS**:
::
object: {"True": 1, 1: "True"}
the following code snippet would **FAIL**:
::
object: {True: 1, 1: True}
"""
import yaml
from yamllint.linter import LintProblem
ID = 'truthy'
TYPE = 'token'
CONF = {}
TRUTHY = ['YES', 'Yes', 'yes',
'NO', 'No', 'no',
'TRUE', 'True', # true is a boolean
'FALSE', 'False', # false is a boolean
'ON', 'On', 'on',
'OFF', 'Off', 'off']
def check(conf, token, prev, next, nextnext, context):
if isinstance(token, yaml.tokens.ScalarToken):
if token.value in TRUTHY and token.style is None:
yield LintProblem(token.start_mark.line + 1,
token.start_mark.column + 1,
"truthy value is not quoted")