From 2345a340952d7b29e2d835fff4062f9412140eb0 Mon Sep 17 00:00:00 2001 From: XuehaiPan Date: Fri, 12 Nov 2021 19:07:55 +0800 Subject: [PATCH 1/3] utils/shfmt.sh: rename regex variables --- Library/Homebrew/utils/shfmt.sh | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Library/Homebrew/utils/shfmt.sh b/Library/Homebrew/utils/shfmt.sh index a0f66a2f47..8a5758d538 100755 --- a/Library/Homebrew/utils/shfmt.sh +++ b/Library/Homebrew/utils/shfmt.sh @@ -229,8 +229,8 @@ align_multiline_if_condition() { local lastline='' local base_indent='' # indents before `if` local extra_indent='' # 2 extra spaces for `elif` - local multiline_if_begin_regex='^( *)(el)?if ' - local multiline_then_end_regex='^(.*)\; (then( *#.*)?)$' + local multiline_if_then_begin_regex='^( *)(el)?if ' + local multiline_if_then_end_regex='^(.*)\; (then( *#.*)?)$' local within_test_regex='^( *)(((! )?-[fdLrwxeszn] )|([^\[]+ == ))' trim() { @@ -238,7 +238,7 @@ align_multiline_if_condition() { printf "%s" "${BASH_REMATCH[0]}" } - if [[ "$1" =~ ${multiline_if_begin_regex} ]] + if [[ "$1" =~ ${multiline_if_then_begin_regex} ]] then base_indent="${BASH_REMATCH[1]}" [[ -n "${BASH_REMATCH[2]}" ]] && extra_indent=' ' @@ -250,7 +250,7 @@ align_multiline_if_condition() { do line="$1" shift - if [[ "${line}" =~ ${multiline_then_end_regex} ]] + if [[ "${line}" =~ ${multiline_if_then_end_regex} ]] then line="${BASH_REMATCH[1]}" lastline="${base_indent}${BASH_REMATCH[2]}" @@ -287,24 +287,24 @@ wrap_then_do() { local -a processed=() local -a buffer=() local line - local singleline_then_regex='^( *)(el)?if (.+)\; (then( *#.*)?)$' - local singleline_do_regex='^( *)(for|while) (.+)\; (do( *#.*)?)$' - local multiline_if_begin_regex='^( *)(el)?if ' - local multiline_then_end_regex='^(.*)\; (then( *#.*)?)$' + local singleline_if_then_regex='^( *)(el)?if (.+)\; (then( *#.*)?)$' + local singleline_for_do_regex='^( *)(for|while) (.+)\; (do( *#.*)?)$' + local multiline_if_then_begin_regex='^( *)(el)?if ' + local multiline_if_then_end_regex='^(.*)\; (then( *#.*)?)$' while IFS='' read -r line do if [[ "${#buffer[@]}" == 0 ]] then - if [[ "${line}" =~ ${singleline_then_regex} ]] + if [[ "${line}" =~ ${singleline_if_then_regex} ]] then processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[2]}if ${BASH_REMATCH[3]}") processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[4]}") - elif [[ "${line}" =~ ${singleline_do_regex} ]] + elif [[ "${line}" =~ ${singleline_for_do_regex} ]] then processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[2]} ${BASH_REMATCH[3]}") processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[4]}") - elif [[ "${line}" =~ ${multiline_if_begin_regex} ]] + elif [[ "${line}" =~ ${multiline_if_then_begin_regex} ]] then buffer=("${line}") else @@ -312,7 +312,7 @@ wrap_then_do() { fi else buffer+=("${line}") - if [[ "${line}" =~ ${multiline_then_end_regex} ]] + if [[ "${line}" =~ ${multiline_if_then_end_regex} ]] then while IFS='' read -r line do @@ -351,7 +351,7 @@ format() { return 1 fi - tempfile="$(dirname "${file}")/.${file##*/}.temp" + tempfile="$(dirname "${file}")/.${file##*/}.formatted~" cp -af "${file}" "${tempfile}" fi trap 'rm -f "${tempfile}" 2>/dev/null' RETURN @@ -396,6 +396,8 @@ format() { then cp -af "${tempfile}" "${file}" else + # Show a linebreak between outputs + [[ "${RETCODE}" != 0 ]] && onoe # Show differences "${DIFF}" "${DIFF_ARGS[@]}" "${file}" "${tempfile}" 1>&2 fi @@ -434,7 +436,6 @@ do 4) onoe "${0##*/}: Fixable bad styles detected in file \"${file}\", run \`brew style --fix\` to apply. Formatter exited with code 4." ;; *) onoe "${0##*/}: Failed to format file \"${file}\". Formatter exited with code ${retcode}." ;; esac - onoe RETCODE=1 fi done From 391e7e22f8bad3686a2d0978a00eaa17f3788d12 Mon Sep 17 00:00:00 2001 From: XuehaiPan Date: Fri, 12 Nov 2021 19:22:40 +0800 Subject: [PATCH 2/3] utils/shfmt.sh: add `!=` and `=~` to within test regex --- Library/Homebrew/utils/shfmt.sh | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/utils/shfmt.sh b/Library/Homebrew/utils/shfmt.sh index 8a5758d538..ca280f7871 100755 --- a/Library/Homebrew/utils/shfmt.sh +++ b/Library/Homebrew/utils/shfmt.sh @@ -227,11 +227,11 @@ no_forbidden_styles() { align_multiline_if_condition() { local line local lastline='' - local base_indent='' # indents before `if` - local extra_indent='' # 2 extra spaces for `elif` + local base_indent='' # indents before `if` + local elif_extra_indent='' # 2 extra spaces for `elif` local multiline_if_then_begin_regex='^( *)(el)?if ' local multiline_if_then_end_regex='^(.*)\; (then( *#.*)?)$' - local within_test_regex='^( *)(((! )?-[fdLrwxeszn] )|([^\[]+ == ))' + local within_test_regex='^( *)(((! )?-[fdLrwxeszn] )|([^\[]+ (==|!=|=~) ))' trim() { [[ "$1" =~ [^[:space:]](.*[^[:space:]])? ]] @@ -241,7 +241,7 @@ align_multiline_if_condition() { if [[ "$1" =~ ${multiline_if_then_begin_regex} ]] then base_indent="${BASH_REMATCH[1]}" - [[ -n "${BASH_REMATCH[2]}" ]] && extra_indent=' ' + [[ -n "${BASH_REMATCH[2]}" ]] && elif_extra_indent=' ' # 2 extra spaces for `elif` echo "$1" shift fi @@ -257,9 +257,14 @@ align_multiline_if_condition() { fi if [[ "${line}" =~ ${within_test_regex} ]] then - echo "${base_indent}${extra_indent} $(trim "${line}")" + # Add 3 extra spaces (6 spaces in total) to multiline test conditions + # before: after: + # if [[ -n ... || if [[ -n ... || + # -n ... ]] -n ... ]] + # then then + echo "${base_indent}${elif_extra_indent} $(trim "${line}")" else - echo "${base_indent}${extra_indent} $(trim "${line}")" + echo "${base_indent}${elif_extra_indent} $(trim "${line}")" fi done From 50cde98fd393a8faf8937af3177c2ec58486d07f Mon Sep 17 00:00:00 2001 From: XuehaiPan Date: Fri, 12 Nov 2021 19:23:10 +0800 Subject: [PATCH 3/3] utils/shfmt.sh: allow single line if blocks --- Library/Homebrew/utils/shfmt.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/utils/shfmt.sh b/Library/Homebrew/utils/shfmt.sh index ca280f7871..3359a6adf4 100755 --- a/Library/Homebrew/utils/shfmt.sh +++ b/Library/Homebrew/utils/shfmt.sh @@ -292,6 +292,7 @@ wrap_then_do() { local -a processed=() local -a buffer=() local line + local singleline_if_then_fi_regex='^( *)if (.+)\; then (.+)\; fi( *#.*)?$' local singleline_if_then_regex='^( *)(el)?if (.+)\; (then( *#.*)?)$' local singleline_for_do_regex='^( *)(for|while) (.+)\; (do( *#.*)?)$' local multiline_if_then_begin_regex='^( *)(el)?if ' @@ -301,7 +302,10 @@ wrap_then_do() { do if [[ "${#buffer[@]}" == 0 ]] then - if [[ "${line}" =~ ${singleline_if_then_regex} ]] + if [[ "${line}" =~ ${singleline_if_then_fi_regex} ]] + then + processed+=("${line}") + elif [[ "${line}" =~ ${singleline_if_then_regex} ]] then processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[2]}if ${BASH_REMATCH[3]}") processed+=("${BASH_REMATCH[1]}${BASH_REMATCH[4]}")