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.1k stars 336 forks source link

syntax: comments before `case` are incorrectly indented #917

Closed zx8 closed 1 year ago

zx8 commented 1 year ago
$ shfmt --version
v3.3.1

Notice # baz is indented 2 extra spaces, when it shouldn't be.

Actual

case foo in
  # bar
  "$bar") ;;
    # baz
  "$baz") ;;
esac

Expected

case foo in
  # bar
  "$bar") ;;
  # baz
  "$baz") ;;
esac
mvdan commented 1 year ago

Thanks, this looks like a valid bug. The printer should probably be a bit smarter.

mvdan commented 1 year ago

Another very similar case below minimized from https://github.com/mvdan/sh/discussions/950; I think they are all the same bug:

$ shfmt -version
v3.6.0-0.dev.0.20221125121205-436da662a80b
$ cat f.sh
case x in
# doesn't move
c)
    foo
    ;;

# does move!
"")
    foo
    ;;
esac
$ shfmt f.sh
case x in
# doesn't move
c)
    foo
    ;;

    # does move!
"")
    foo
    ;;
esac