139139
140140 foo: 'bar"baz'
141141
142+ #. With ``quoted-strings: {quote-type: consistent}``
143+
144+ the following code snippet would **PASS**:
145+ ::
146+
147+ foo: 'bar'
148+ baz: 'quux'
149+
150+ the following code snippet would **FAIL**:
151+ ::
152+
153+ foo: 'bar'
154+ baz: "quux"
155+
142156#. With ``quoted-strings: {required: only-when-needed, check-keys: true,
143157 extra-required: ["[:]"]}``
144158
161175
162176ID = 'quoted-strings'
163177TYPE = 'token'
164- CONF = {'quote-type' : ('any' , 'single' , 'double' ),
178+ CONF = {'quote-type' : ('any' , 'single' , 'double' , 'consistent' ),
165179 'required' : (True , False , 'only-when-needed' ),
166180 'extra-required' : [str ],
167181 'extra-allowed' : [str ],
@@ -198,7 +212,14 @@ def VALIDATE(conf):
198212 list ('-+0123456789' ))
199213
200214
201- def _quote_match (quote_type , token_style ):
215+ def _quote_match (quote_type , token_style , context ):
216+ if quote_type == 'consistent' and token_style is not None :
217+ # The canonical token style in a document is assumed to be the first
218+ # one found for the purpose of 'consistent'
219+ if 'quoted_strings_consistent_token_style' not in context :
220+ context ['quoted_strings_consistent_token_style' ] = token_style
221+ return context ['quoted_strings_consistent_token_style' ] == token_style
222+
202223 return ((quote_type == 'any' ) or
203224 (quote_type == 'single' and token_style == "'" ) or
204225 (quote_type == 'double' and token_style == '"' ))
@@ -294,15 +315,15 @@ def check(conf, token, prev, next, nextnext, context):
294315
295316 # Quotes are mandatory and need to match config
296317 if (token .style is None or
297- not (_quote_match (quote_type , token .style ) or
318+ not (_quote_match (quote_type , token .style , context ) or
298319 (conf ['allow-quoted-quotes' ] and _has_quoted_quotes (token )))):
299320 msg = f"string { node } is not quoted with { quote_type } quotes"
300321
301322 elif conf ['required' ] is False :
302323
303324 # Quotes are not mandatory but when used need to match config
304325 if (token .style and
305- not _quote_match (quote_type , token .style ) and
326+ not _quote_match (quote_type , token .style , context ) and
306327 not (conf ['allow-quoted-quotes' ] and
307328 _has_quoted_quotes (token ))):
308329 msg = f"string { node } is not quoted with { quote_type } quotes"
@@ -328,7 +349,7 @@ def check(conf, token, prev, next, nextnext, context):
328349
329350 # But when used need to match config
330351 elif (token .style and
331- not _quote_match (quote_type , token .style ) and
352+ not _quote_match (quote_type , token .style , context ) and
332353 not (conf ['allow-quoted-quotes' ] and _has_quoted_quotes (token ))):
333354 msg = f"string { node } is not quoted with { quote_type } quotes"
334355
0 commit comments