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
6.99k stars 333 forks source link

syntax: Minify inserting backslashes between `)` and `=` #932

Open LucasLarson opened 1 year ago

LucasLarson commented 1 year ago

shfmt --minify is inserting a backslash \ and a newline if there’s an assignment following a closing parenthesis ). For example:

$ cat example.sh
  #!/bin/sh
  case $1 in
  option)
  assignment=not_ok
  echo "${assignment}"
  ;;
  *)
  echo ok
  esac

but then

$ cat example.sh | shfmt -mn
  #!/bin/sh
  case $1 in
  option)\ 
  assignment=not_ok
  echo "$assignment"
  ;;
  *)echo ok
  esac

but what I expected, and what would still work in zsh, bash, ksh, mksh, oksh, posh, yash, and dash, at least on my Mac using Homebrew installations – and still pass shellcheck – is:

$ cat example.sh | shfmt -mn
  #!/bin/sh
  case $1 in
  option)assignment=not_ok
  echo "$assignment"
  ;;
  *)echo ok
  esac
mvdan commented 1 year ago

Thanks, this looks like a minor bug. If there's a way for the minify option to use fewer bytes and still result in valid shell, then it should probably be doing that. Happy to review a patch if someone is up for it.