adrienverge / yamllint

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

Question: Is it possible to make it so it forces blank lines between elements? #683

Open Stasky745 opened 3 months ago

Stasky745 commented 3 months ago

So to make it that this is right:

# For all rules
ignore: |
  *.dont-lint-me.yaml
  /bin/
  !/bin/*.lint-me-anyway.yaml

rules:
  key-duplicates:
    ignore: |
      generated
      *.template.yaml

  trailing-spaces:
    ignore: |
      *.ignore-trailing-spaces.yaml
      /ascii-art/*

and this is wrong:

# For all rules
ignore: |
  *.dont-lint-me.yaml
  /bin/
  !/bin/*.lint-me-anyway.yaml
rules:
  key-duplicates:
    ignore: |
      generated
      *.template.yaml
  trailing-spaces:
    ignore: |
      *.ignore-trailing-spaces.yaml
      /ascii-art/*

I'd say when the following line has less identation than the previous one to force a blank space. Is this possible?

victoraugustolls commented 1 month ago

@adrienverge would you be open to a PR for this? I was thinking maybe a line_break rule that you can pass the root option, so you need to skip a line between root elements and level to say up until what level you need to jump a line between elements.

adrienverge commented 1 month ago

Hello Victor, I like the idea and would be open to such a contribution.

Instead of a new rule, maybe it would make sense to reuse the empty-lines rule?

Such a feature needs to be well designed and future-proof, so before developing any code (+ tests), we need a clear proposal of new options to be able to achieve complex configurations like:

# 2 empty lines required at top level
# 1 empty line required at levels 2, 3 and 4, for mappings only
# 1 empty line required at levels 2, for sequences only
a:
  k1: v1

  k2:
    x: 1
      s: 1

      t:
        no: line
        breaks: here
        but: it's OK

      u: 3

    y: 2

    z: 3

  k3: v2

b:
  - item

  - item

  - items:
    - subitem
    - subitem
    - subitem

c: 3

Ideally, we would also need a way to tell whether an empty line is required before first items in sequences and mappings, e.g. in the first comment's example:

# No empty line before first item:
rules:
  key-duplicates: …

  trailing-spaces: …

# versus:
rules:

  key-duplicates: …

  trailing-spaces: …

Also, should lines with comments be accounted for?

# comment
a: 1

b: 2

# vs.

# comment

a: 1

b: 2