From 90b0df7a1715d44a6fdd27f45c97099cd12da227 Mon Sep 17 00:00:00 2001 From: MorganGeek Date: Tue, 14 Jul 2020 15:22:06 +0200 Subject: [PATCH] feature: (wip) code simplifications suggestions the purpose is to suggest refactoring opportunities in a given shellscript. some features would be : - Increase KISS / YAGNI - Remove code duplication / repetition - Improve code consistency / redability - Reduce LOC :-) (lines of code) - Encourage reuse of aliases --- dot_zsh_functions | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/dot_zsh_functions b/dot_zsh_functions index 31e1b26..258e8fd 100644 --- a/dot_zsh_functions +++ b/dot_zsh_functions @@ -134,6 +134,27 @@ function whichfunc() { whence -v $1 type -a $1 } +# TODO : suggest using long parameter names instead of short ones.... +function suggest_code_refactoring() { + #inspired by : \grep 'awk '\''{$1=$1};1'\' $HOME/Code/dotfiles/dot_zsh* + header "code refactoring suggestions" + while read -r line; do + local short_name=$(echo "$line" | sed -E "s/='.*//g") + local alias_value=$(echo "$line" | sed -E 's/[a-zA-Z_-]+=//g') + local alias_value_truncated=${alias_value:1:$((${#alias_value}-2))} + #arrow "looking for opportunities to use alias $short_name instead of $alias_value_truncated" + \grep "$alias_value_truncated" $HOME/Code/dotfiles/dot_zsh_funct* 2>/tmp/error.log #/dev/null + if [ $? -eq 0 ]; then + success "found $short_name --> $alias_value_truncated" + \grep "$alias_value_truncated" $HOME/Code/dotfiles/dot_zsh_funct* --color -n -H --line-buffered #2>/dev/null + else + local error_message=$(\cat /tmp/error.log) + if [ ! -z "$error_message" ]; then + error "issue occured when looking for $alias_value_truncated : $error_message" + fi + fi + done < <(alias | awk -v COUNT=1 'NF>COUNT') # list of all aliases, excluding the less interesting ones (where we use one word for another) +} function suggest_aliases() { local search_input_size=${1:-'50'} header "alias recommendations"