From 09a16bcea115b3fe3927df7a6e0cdb202db42624 Mon Sep 17 00:00:00 2001 From: XuehaiPan Date: Wed, 15 Sep 2021 23:40:14 +0800 Subject: [PATCH] utils/shfmt.sh: implement checkers for forbidden styles --- Library/Homebrew/utils/shfmt.sh | 163 ++++++++++++++++++++------------ 1 file changed, 105 insertions(+), 58 deletions(-) diff --git a/Library/Homebrew/utils/shfmt.sh b/Library/Homebrew/utils/shfmt.sh index f1c305feae..5b00521c13 100755 --- a/Library/Homebrew/utils/shfmt.sh +++ b/Library/Homebrew/utils/shfmt.sh @@ -83,14 +83,23 @@ fi # no_tabs() { local file="$1" + local tempfile="$2" + local line + local num=0 + local retcode=0 + local regex_pos='^[[:space:]]+' + local regex_neg='^ +' - # TODO: use bash built-in regex match syntax instead - if grep -qE '^\t+' "${file}" - then - # TODO: add line number - onoe "Indent by tab detected." - return 1 - fi + while IFS='' read -r line + do + num="$((num + 1))" + if [[ "${line}" =~ ${regex_pos} && ! "${line}" =~ ${regex_neg} ]] + then + onoe "Indent by tab detected at \"${file}\", line ${num}." + retcode=1 + fi + done <"${file}" + return "${retcode}" } # Check pattern: @@ -98,56 +107,98 @@ no_tabs() { # ...; do # # Use the followings instead (keep for statements only one line): -# ARRAY=( -# ... -# ... -# ) -# for var in "${ARRAY[@]}" -# do +# ARRAY=( +# ... +# ) +# for var in "${ARRAY[@]}" +# do # no_multiline_for_statements() { local file="$1" + local tempfile="$2" + local line + local num=0 + local retcode=0 + local regex='^ *for [_[:alnum:]]+ in .*\\$' - # TODO: use bash built-in regex match syntax instead - if grep -qE '^\s*for .*\\\(#.*\)\?$' "${file}" - then - # TODO: add line number - onoe "Multi-line for statement detected." - return 1 - fi + while IFS='' read -r line + do + num="$((num + 1))" + if [[ "${line}" =~ ${regex} ]] + then + onoe "Multiline for statement detected at \"${file}\", line ${num}." + cat >&2 <&2 <"${file}" + printf "%s\n" "${processed[@]}" >"${tempfile}" } # TODO: it's hard to align multiline switch cases @@ -250,10 +302,6 @@ align_multiline_switch_cases() { true } -no_forbiddens() { - true -} - format() { local file="$1" if [[ ! -f "${file}" || ! -r "${file}" ]] @@ -282,17 +330,16 @@ format() { fi # Fail fast when forbidden patterns detected - if ! no_tabs "${tempfile}" || - ! no_multiline_for_statements "${tempfile}" || - ! no_IFS_newline "${tempfile}" + if ! no_forbiddens "${file}" "${tempfile}" then - return 1 + return 2 fi # Tweak it with custom shell script styles - wrap_then_do "${tempfile}" + wrap_then_do "${file}" "${tempfile}" + align_multiline_switch_cases "${file}" "${tempfile}" - if ! diff -q "${file}" "${tempfile}" + if ! diff -q "${file}" "${tempfile}" &>/dev/null then # Show differences diff -d -C 1 --color=auto "${file}" "${tempfile}" @@ -314,9 +361,9 @@ do then if [[ "$?" == 1 ]] then - onoe "${0##*/}: Failed to format file \"${file}\". Function exited with code $?." + onoe "${0##*/}: Failed to format file \"${file}\". Function exited with code 1." else - onoe "${0##*/}: Bad style for file \"${file}\". Function exited with code $?." + onoe "${0##*/}: Bad style for file \"${file}\". Function exited with code 2." fi onoe RETCODE=1