mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
7.14k stars 338 forks source link

syntax: extra white-space before trailing comments after some compound commands #844

Open antichris opened 2 years ago

antichris commented 2 years ago

Some extra whitespace is added before trailing comments after

  1. brace groups ```sh { foo #inline } #trailing foo() { bar #inline } #trailing ``` ```sh { foo #inline } #trailing foo() { bar #inline } #trailing ```
  2. subshells ```sh ( foo #inline ) #trailing foo() ( bar #inline ) #trailing ``` ```sh ( foo #inline ) #trailing foo() ( bar #inline ) #trailing ```
  3. command substitutions ```sh foo=$( bar #inline ) #trailing ``` ```sh foo=$( bar #inline ) #trailing ```
  4. if clauses ```sh if foo; then bar #inline fi #trailing ``` ```sh if foo; then bar #inline fi #trailing ```
Altogether that looks like a weak effort to align with the above inline comment, as it is done for case. ```sh case $foo in ?) bar;; #inline esac #trailing ``` ```sh case $foo in ?) bar ;; #inline esac #trailing ```
Nothing of the sort is being done to loop constructs (for, while and until), for which output is the same as input. ```sh for foo; do bar #inline done #trailing while foo; do bar #inline done #trailing until foo; do bar #inline done #trailing ``` ```sh for foo; do bar #inline done #trailing while foo; do bar #inline done #trailing until foo; do bar #inline done #trailing ```

I'm not sure how often trailing comments on these constructs are used in practice, and whether they really are most commonly used for a continuation of the previous line (I'm inclined to doubt that is the case, but I may be biased).