Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow backslash-escaped special characters in double-quoted strings (#700) #703

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/rules/test_quoted_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from tests.common import RuleTestCase
from yaml.reader import ReaderError as yaml_reader_ReaderError
jmknoble marked this conversation as resolved.
Show resolved Hide resolved

from yamllint import config

Expand Down Expand Up @@ -475,6 +476,28 @@ def test_only_when_needed_extras(self):
'- "foo bar"\n',
conf, problem1=(3, 3), problem2=(7, 3), problem3=(11, 3))

def test_only_when_needed_special_characters(self):
conf = 'quoted-strings: {required: only-when-needed}\n'
self.check('---\n'
# double-quoted escaped special chars: ok
'k1: "\\u001b"\n',
conf)

def test_only_when_needed_special_characters_exceptions(self):
conf = 'quoted-strings: {required: only-when-needed}\n'
yamltext1 = ('---\n'
# double-quoted unescaped special chars: yuck"
'k1: "\u001b"\n')
yamltext2 = ('---\n'
# single-quoted unescaped special chars: yuck"
"k1: '\u001b'\n")
yamltext3 = ('---\n'
# unquoted unescaped special chars: yuck"
'k1: \u001b\n')
self.assertRaises(yaml_reader_ReaderError, self.check, yamltext1, conf)
self.assertRaises(yaml_reader_ReaderError, self.check, yamltext2, conf)
self.assertRaises(yaml_reader_ReaderError, self.check, yamltext3, conf)
jmknoble marked this conversation as resolved.
Show resolved Hide resolved

def test_octal_values(self):
conf = 'quoted-strings: {required: true}\n'

Expand Down
14 changes: 12 additions & 2 deletions yamllint/rules/quoted_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,21 @@ def _quote_match(quote_type, token_style):
(quote_type == 'double' and token_style == '"'))


def _quotes_are_needed(string, is_inside_a_flow):
def _quotes_are_needed(string, style, is_inside_a_flow):
# Quotes needed on strings containing flow tokens
if is_inside_a_flow and set(string) & {',', '[', ']', '{', '}'}:
return True

loader = yaml.BaseLoader('key: ' + string)
try:
loader = yaml.BaseLoader('key: ' + string)
except yaml.reader.ReaderError as e:
if e.reason == "special characters are not allowed" and style == '"':
# Special characters in a double-quoted string
# are assumed to be backslash-escaped
return True
else:
raise

# Remove the 5 first tokens corresponding to 'key: ' (StreamStartToken,
# BlockMappingStartToken, KeyToken, ScalarToken(value=key), ValueToken)
for _ in range(5):
Expand Down Expand Up @@ -299,6 +308,7 @@ def check(conf, token, prev, next, nextnext, context):
# Quotes are not strictly needed here
if (token.style and tag == DEFAULT_SCALAR_TAG and token.value and
not _quotes_are_needed(token.value,
token.style,
context['flow_nest_count'] > 0)):
is_extra_required = any(re.search(r, token.value)
for r in conf['extra-required'])
Expand Down
Loading