sh
sh copied to clipboard
cmd/shfmt: Add support for minify and simplify via EditorConfig
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.
I have added some tests following the structure of other editorconfig tests.