From 0acaa2315fcd4064f0ec980c833d0ace03b45331 Mon Sep 17 00:00:00 2001 From: Tom Coleman <15375218+ColemanTom@users.noreply.github.com> Date: Fri, 24 May 2024 09:41:35 +1000 Subject: [PATCH] cmd/shfmt: Add support for minify and simplify via EditorConfig Closes #819. --- cmd/shfmt/main.go | 4 ++++ cmd/shfmt/testdata/script/editorconfig.txtar | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/cmd/shfmt/main.go b/cmd/shfmt/main.go index 78a6b35c..ffc527c4 100644 --- a/cmd/shfmt/main.go +++ b/cmd/shfmt/main.go @@ -380,6 +380,10 @@ func propsOptions(lang syntax.LangVariant, props editorconfig.Section) { syntax.KeepPadding(props.Get("keep_padding") == "true")(printer) // TODO(v4): rename to func_next_line for consistency with flags syntax.FunctionNextLine(props.Get("function_next_line") == "true")(printer) + minify_opt := minify.val || props.Get("minify") == "true" + syntax.Minify(minify_opt)(printer) + // Keep the original value if passed by CLI, or minify is enabled, or it is in the config file + simplify.val = simplify.val || minify_opt || props.Get("simplify") == "true" } func formatPath(path string, checkShebang bool) error { diff --git a/cmd/shfmt/testdata/script/editorconfig.txtar b/cmd/shfmt/testdata/script/editorconfig.txtar index 7a84c401..238f3c8c 100644 --- a/cmd/shfmt/testdata/script/editorconfig.txtar +++ b/cmd/shfmt/testdata/script/editorconfig.txtar @@ -159,6 +159,12 @@ keep_padding = true [function_next_line.sh] function_next_line = true +[simplify.sh] +simplify = true + +[minify.sh] +minify = true + -- otherknobs/shell_variant_bash.sh -- array=(elem) -- otherknobs/shell_variant_mksh.sh -- @@ -183,6 +189,14 @@ foo() { echo foo } +-- otherknobs/simplify.sh -- +foo() { + ((bar)) +} +-- otherknobs/minify.sh -- +foo(){ +((bar)) +} -- ignored/.editorconfig -- root = true