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

quoted-strings false positive on strings containing brackets #516

Closed
sxlijin opened this issue Nov 4, 2022 · 2 comments · Fixed by #615
Closed

quoted-strings false positive on strings containing brackets #516

sxlijin opened this issue Nov 4, 2022 · 2 comments · Fixed by #615

Comments

@sxlijin
Copy link

sxlijin commented Nov 4, 2022

When using brackets to define lists, string literals in that list can't contain brackets;

$ cat foo.yaml
++ 2022-11-04T11:24:52-0700 ++ cat foo.yaml
# invalid
field: [string[bracket]]

but when you include brackets:

$ cat foo.yaml
++ 2022-11-04T11:24:52-0700 ++ cat foo.yaml
# valid
field: ["string[bracket]"]

yamllint complains about the string being quoted:

$ trunk check --filter=yamllint foo.yaml
++ 2022-11-04T11:24:54-0700 ++ trunk check --filter=yamllint foo.yaml
Checking 100% [=======================================================================================================================================================>]  5/5  0.3s

  ISSUES

foo.yaml:2:9
 2:9  high  string value is redundantly quoted with any quotes  yamllint/quoted-strings

✖ 1 blocking issue
@adrienverge
Copy link
Owner

Hello @sxlijin, thanks for reporting this.

A simpler reproduction is:

$ yamllint -d 'rules: {quoted-strings: {required: only-when-needed}}' - <<<'field: [string[bracket]]'
  1:15      error    syntax error: expected ',' or ']', but got '[' (syntax)

$ 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)

@ruipinge you contributed required: only-when-needed in #235, would you like to have a look into this?

In the meantine, a YAML block sequence can be used to workaround the problem:

field: 
- string[bracket]

@robherring
Copy link
Contributor

There's also a related case with commas in bracketed sequences:

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

robherring added a commit to robherring/yamllint that referenced this issue Nov 21, 2023
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.

Resolves Issue adrienverge#516

Signed-off-by: Rob Herring <robh@kernel.org>
robherring added a commit to robherring/yamllint that referenced this issue Nov 21, 2023
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 adrienverge#516
robherring added a commit to robherring/yamllint that referenced this issue Nov 22, 2023
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 adrienverge#516
robherring added a commit to robherring/yamllint that referenced this issue Nov 22, 2023
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 adrienverge#516
adrienverge pushed a commit to robherring/yamllint that referenced this issue Nov 22, 2023
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 adrienverge#516
adrienverge pushed a commit that referenced this issue Nov 22, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants