sh icon indicating copy to clipboard operation
sh copied to clipboard

allow preserving/adding opening parenthesis in case statements

Open Villemoes opened this issue 2 months ago • 2 comments

The posix standard (https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_09_04_05) explicitly allows spelling the case statements using opening parentheses:

case "$foo" in
    (x) echo "foo is x" ;;
    (y) echo "foo is y" ;;
    (*) echo "foo is neither x or y" ;;
esac

However, AFAICT, shfmt consistently strips the opening (, with no option for leaving it alone (or adding it when missing). As I use that spelling quite consistently, because unbalanced parentheses are really annoying, I can't use shfmt.

As this isn't a bashism or zshism, could shfmt grow support for keeping or adding ( to case statements?

Villemoes avatar Sep 29 '25 09:09 Villemoes

Previously: https://github.com/mvdan/sh/issues/1168

As I mentioned there, the bar for adding options is very high. This does not meet the requirements for it.

The only alternative that comes to mind is for shfmt to obey the user with their existing preference - i.e. if all switch cases use the opening parenthesis, then leave them there. If any of them are missing the opening parenthesis, then remove them all for consistency. I'd be fine with this approach - it wouldn't require more options and complexity, and it shouldn't affect existing users either.

cc @Artoria2e5

mvdan avatar Sep 29 '25 09:09 mvdan

Thanks for the quick reply. And sorry for not doing my homework properly, I did try to search for an existing issue, but obviously not well enough.

I would really appreciate it if the "auto" option you mention could be implemented, as the inability to leave the open parens alone is a show-stopper for me to make use of shfmt. I don't really need a knob to add those; the diff stripping them in case of inconsistency would be enough for me.

Villemoes avatar Sep 29 '25 10:09 Villemoes