yamllint icon indicating copy to clipboard operation
yamllint copied to clipboard

Alias as a mapping key needs a space before the colon

Open perlpunk opened this issue 5 years ago • 4 comments

I am getting an error for a space that actually is required to be there. The following YAML has a an alias as a mapping key, and because aliases actually can contain colons, there must be a space after an alias in this case. libyaml doesn't implement that, and so it will be parsed without the space, but it's not correct:

% cat foo.yaml
---
a: &alias key
*alias : b

% yamllint foo.yaml
foo.yaml
  3:7       error    too many spaces before colon  (colons)

% yamllint -v
yamllint 1.15.0

So it would be good to allow one space in this case.

perlpunk avatar Jan 29 '20 21:01 perlpunk

Just for completeness, the following YAML processors will fail to parse it without the space: Javascript yaml, C libfyaml, C++ yaml-cpp, Python ruamel.yaml, Nim NimYAML, Javascript js-yaml, Haskell HsYAML (which is based on the YAML 1.2 reference parser from Oren Ben-Kiki), Perl YAML::PP See YAML Runtimes

perlpunk avatar Jan 30 '20 10:01 perlpunk

Additional question: Is is possible to make yamllint ignore this line in our source?

*foo :   # yamllint disable-line rule:max-spaces-before

didn't make a difference

Edit: ok, this works:

*foo :   # yamllint disable-line rule:colons

perlpunk avatar Jan 30 '20 10:01 perlpunk

Hi @perlpunk, thanks for the report. I wasn't aware of this (aliases are rarely used).

I think the best would be, when the key is an alias:

  • to allow 1 space maximum if conf is max-spaces-before: 0,
  • to keep current behavior if max-spaces-before > 0.

Another possibility would be to add a new option like max-spaces-before-on-aliases: 1, it would give more flexibility to users, but I'm not sure it's worth it.

Contributions are welcome!

adrienverge avatar Jan 30 '20 13:01 adrienverge

Thanks, I think your first suggestion makes sense. As you said, aliases as keys are rare :)

Edit: It would be useful though, to actually enforce a space after the alias...

perlpunk avatar Jan 30 '20 13:01 perlpunk