adrienverge / yamllint

A linter for YAML files.
GNU General Public License v3.0
2.86k stars 273 forks source link

Fix quoted-strings checks in flow maps and sequences #615

Closed robherring closed 11 months ago

robherring commented 11 months ago

Flow maps and sequences need quotes if the values contain any of the flow tokens ({}, [], ','). However, yamllint generates false positives in these cases:

$ yamllint -d 'rules: {quoted-strings: {required: only-when-needed}}' - <<<'field: ["string[bracket]"]' 1:9 error string value is redundantly quoted with any quotes (quoted-strings)

To fix this, track when inside a flow map/sequence and skip the quoting checks except for the quoting type.

Closes #516

coveralls commented 11 months ago

Coverage Status

coverage: 99.399% (+0.002%) from 99.397% when pulling a962b4101ade797eaae75d746f31f3463278181d on robherring:flow-quote-fix into ca7f8b972cbbc6058e8d532e82a05d34a9897cd7 on adrienverge:master.

robherring commented 11 months ago

Hello and thanks for tackling this!

It looks good at first glance, but could you add more tests with nested flows, to prove the functionnality of flow_cnt? (By the way you can rename it to flow_count or flow_nest_count for clarity)

Sure.

Does this address the field: ["foo,bar"] case too?

Yes

From what I see in the new test cases it reports [foo, "foo,bar"] as a failure, which is wrong, isn't it? 🤔

That one is testing the quotes are the right type.