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.97k stars 332 forks source link

cmd/shfmt: Add support for minify and simplify via EditorConfig #1062

Open ColemanTom opened 4 months ago

ColemanTom commented 4 months ago

I've never used go before, so I this is a naive, but working approach to support simplify and minify in editorconfig files. I have not looked at adding tests yet, but will add tests if this code change approach is agreeable.

Manual testing below.

$ for opt in "" "simplify=true" "minify=true"; do echo $'[[shell]]\nindent=4\n'"$opt" > .editorconfig; echo "### $opt"; shfmt --diff f.sh; done
###
--- f.sh.orig
+++ f.sh
@@ -1,11 +1,11 @@
 #!/bin/bash

 ff() {
-local h=asdffdfd
+       local h=asdffdfd
 }
 g=a

-echo $(( 1 + 2 ));
+echo $((1 + 2))
 ff
 echo $h
 [[ "$HOME" == "$HELLO" ]]
### simplify=true
--- f.sh.orig
+++ f.sh
@@ -1,11 +1,11 @@
 #!/bin/bash

 ff() {
-local h=asdffdfd
+       local h=asdffdfd
 }
 g=a

-echo $(( 1 + 2 ));
+echo $((1 + 2))
 ff
 echo $h
-[[ "$HOME" == "$HELLO" ]]
+[[ $HOME == "$HELLO" ]]
### minify=true
--- f.sh.orig
+++ f.sh
@@ -1,11 +1,9 @@
 #!/bin/bash
-
-ff() {
+ff(){
 local h=asdffdfd
 }
 g=a
-
-echo $(( 1 + 2 ));
+echo $((1+2))
 ff
 echo $h
-[[ "$HOME" == "$HELLO" ]]
+[[ $HOME == "$HELLO" ]]

Adding, for e.g. -s or -mn to the shfmt call leads to consistent results (i.e. if -s is on, then for the opts=, opts="simplify=true", we get the second result above, but the opts="minify=true" option stays consistent with minify respected).

Closes #819.

ColemanTom commented 1 month ago

I have added some tests following the structure of other editorconfig tests.