Add the "needed-extra-regex" option
This commit is contained in:
@@ -344,3 +344,24 @@ class QuotedTestCase(RuleTestCase):
|
|||||||
' "word 1\\\n'
|
' "word 1\\\n'
|
||||||
' word 2"\n',
|
' word 2"\n',
|
||||||
conf, problem1=(9, 3))
|
conf, problem1=(9, 3))
|
||||||
|
|
||||||
|
def test_needed_extra_regex(self):
|
||||||
|
conf1 = 'quoted-strings: {quote-type: single, ' + \
|
||||||
|
'required: only-when-needed, ' + \
|
||||||
|
'needed-extra-regex: ""}\n'
|
||||||
|
|
||||||
|
self.check('---\n'
|
||||||
|
'string1: foo\n'
|
||||||
|
'string2: \'foo\'\n' # fails
|
||||||
|
'string3: \'%foo\'\n', # fails
|
||||||
|
conf1, problem1=(3, 10), problem2=(4, 10))
|
||||||
|
|
||||||
|
conf2 = 'quoted-strings: {quote-type: single, ' + \
|
||||||
|
'required: only-when-needed, ' + \
|
||||||
|
'needed-extra-regex: ^%.*$}\n'
|
||||||
|
|
||||||
|
self.check('---\n'
|
||||||
|
'string1: foo\n'
|
||||||
|
'string2: \'foo\'\n' # fails
|
||||||
|
'string3: \'%foo\'\n',
|
||||||
|
conf2, problem1=(3, 10))
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ used.
|
|||||||
foo: 'bar'
|
foo: 'bar'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from yamllint.linter import LintProblem
|
from yamllint.linter import LintProblem
|
||||||
@@ -72,9 +73,11 @@ from yamllint.linter import LintProblem
|
|||||||
ID = 'quoted-strings'
|
ID = 'quoted-strings'
|
||||||
TYPE = 'token'
|
TYPE = 'token'
|
||||||
CONF = {'quote-type': ('any', 'single', 'double'),
|
CONF = {'quote-type': ('any', 'single', 'double'),
|
||||||
'required': (True, False, 'only-when-needed')}
|
'required': (True, False, 'only-when-needed'),
|
||||||
|
'needed-extra-regex': str}
|
||||||
DEFAULT = {'quote-type': 'any',
|
DEFAULT = {'quote-type': 'any',
|
||||||
'required': True}
|
'required': True,
|
||||||
|
'needed-extra-regex': ''}
|
||||||
|
|
||||||
DEFAULT_SCALAR_TAG = u'tag:yaml.org,2002:str'
|
DEFAULT_SCALAR_TAG = u'tag:yaml.org,2002:str'
|
||||||
START_TOKENS = {'#', '*', '!', '?', '@', '`', '&',
|
START_TOKENS = {'#', '*', '!', '?', '@', '`', '&',
|
||||||
@@ -132,8 +135,11 @@ def check(conf, token, prev, next, nextnext, context):
|
|||||||
# Quotes are disallowed when not needed
|
# Quotes are disallowed when not needed
|
||||||
if (tag == DEFAULT_SCALAR_TAG and token.value
|
if (tag == DEFAULT_SCALAR_TAG and token.value
|
||||||
and token.value[0] not in START_TOKENS):
|
and token.value[0] not in START_TOKENS):
|
||||||
msg = "string value is redundantly quoted with %s quotes" % (
|
extra_regex = conf['needed-extra-regex']
|
||||||
quote_type)
|
|
||||||
|
if extra_regex == '' or not re.match(extra_regex, token.value):
|
||||||
|
msg = "string value is redundantly quoted with %s quotes" % (
|
||||||
|
quote_type)
|
||||||
|
|
||||||
# But when used need to match config
|
# But when used need to match config
|
||||||
elif token.style and not quote_match(quote_type, token.style):
|
elif token.style and not quote_match(quote_type, token.style):
|
||||||
|
|||||||
Reference in New Issue
Block a user