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

Change `${foo}` into `$foo` when simplifying, not just minifying #943

Closed quad closed 1 year ago

quad commented 1 year ago

It would be nice to minify the code to simplify parameter expressions even when simplifying.

Perhaps what I really want is a way to turn on/off individual minifiation rules.

Would you be open to a PR implementing either?

mvdan commented 1 year ago

I don't think this should be part of "simplify" - that feature is for changes which should always be reasonable to make. I only kept it behind a flag because, technically, they are not formatting changes. This change in particular could be a bad idea in some cases. In some, like ${foo}bar, it would change the program. In others, it could make code less consistent - I personally prefer using the longer form everywhere.

As for the minification rules, you're right that currently it's all-or-nothing. What are you thinking in terms of API? At least we'd have to somehow number or identify all the types of changes. Then we'd need to allow selecting those via a Go API like https://github.com/mvdan/sh/blob/279b806d8f2417af933a40a726ad8299395d9183/syntax/printer.go#L78, and via shfmt -mn.

mvdan commented 1 year ago

I'm going to close this for now, per my comment above. Always happy to entertain adding extra "minify" rules, but there's also no reason why these need to exist inside shfmt. The parsing and printing logic is a library, so other tools can easily be spun up.