From 22714ebff2d6104b1059e48120133f8706143c36 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 3 Dec 2026 16:19:12 -0800 Subject: [PATCH] [Refactor] declare one variable per `local`, and assign it separately ksh93 has no `local`, so `local X=y` there never assigns `X`; `nvm_has_solaris_binary` always saw an empty version as a result. --- bash_completion | 74 +++++++++++++++++++++++++++++++++++++++---------- nvm.sh | 6 ++-- 2 files changed, 63 insertions(+), 17 deletions(-) diff --git a/bash_completion b/bash_completion index 4faffbbb..d38dcdae 100644 --- a/bash_completion +++ b/bash_completion @@ -46,7 +46,10 @@ EOF # Update the locals in __nvm_candidates from completed words only. A nonzero return # hands completion back to the shell (e.g. for an exec command or a -j value). __nvm_parse_context() { - local i word next rest + local i + local word + local next + local rest rest="${COMP_LINE-}" rest="${rest#*"${COMP_WORDS[0]}"}" rest="${rest#*"${subcommand}"}" @@ -118,7 +121,9 @@ __nvm_parse_context() { } __nvm_options() { - local flags lts_flags="${lts}" + local flags + local lts_flags + lts_flags="${lts}" if [[ -z "${only}" && ",${NVM_ONLY_SELECTED}," != *,lts,* ]]; then lts_flags='' fi @@ -149,7 +154,8 @@ __nvm_options() { *) return 0 ;; esac # Only split our fixed option lists, never user-supplied words. - local IFS=' ' + local IFS + IFS=' ' # shellcheck disable=SC2086 __nvm_complete ${flags} } @@ -157,7 +163,10 @@ __nvm_options() { # Bash needs literal prefix filtering; zsh's compadd matches the candidates itself. __nvm_candidates() { if [[ -n ${ZSH_VERSION-} ]]; then emulate -L sh; fi - local current_word="${1}" match_prefix="${2-}" + local current_word + current_word="${1}" + local match_prefix + match_prefix="${2-}" COMPREPLY=() if (( COMP_CWORD == 1 )); then case "${current_word}" in @@ -171,7 +180,8 @@ __nvm_candidates() { return 0 fi - local subcommand="${COMP_WORDS[1]}" + local subcommand + subcommand="${COMP_WORDS[1]}" case "${subcommand}" in help | -h | --help | --no-colors) if (( COMP_CWORD == 2 )); then @@ -184,11 +194,31 @@ __nvm_candidates() { ;; esac - local arg_count=0 end_options=0 - local only='--only=' lts='--lts --lts=' no_alias='--no-alias' - local build_mode='-s -b' save='--save' alias_target='--alias= --default' reinstall='--reinstall-packages-from=' - local delete_prefix='--delete-prefix' - local NVM_ONLY_SELECTED="${NVM_ONLY_SELECTED-}" + local arg_count + arg_count=0 + local end_options + end_options=0 + local only + only='--only=' + local lts + lts='--lts --lts=' + local no_alias + no_alias='--no-alias' + local build_mode + build_mode='-s -b' + local save + save='--save' + local alias_target + alias_target='--alias= --default' + local reinstall + reinstall='--reinstall-packages-from=' + local delete_prefix + delete_prefix='--delete-prefix' + # a bare `local` would discard the inherited value + local only_selected + only_selected="${NVM_ONLY_SELECTED-}" + local NVM_ONLY_SELECTED + NVM_ONLY_SELECTED="${only_selected}" __nvm_parse_context || return 0 if (( ! end_options )); then case "${current_word}" in @@ -232,7 +262,8 @@ __nvm_candidates() { } __nvm() { - local current_word="${2-${COMP_WORDS[COMP_CWORD]}}" + local current_word + current_word="${2-${COMP_WORDS[COMP_CWORD]}}" if [[ "${current_word}" == *\\* && "${COMP_WORDS[COMP_CWORD]}" == "${current_word}"* ]]; then # Readline returns the escapes it inserted for an unquoted common prefix. # shellcheck disable=SC2162 @@ -243,7 +274,12 @@ __nvm() { __nvm_complete_command() { __nvm "$@" - local i quoted single_quote_escape="'\\''" history_escape="\"'!'\"" + local i + local quoted + local single_quote_escape + single_quote_escape="'\\''" + local history_escape + history_escape="\"'!'\"" for (( i = 0; i < ${#COMPREPLY[@]}; i++ )); do # Quote for the argument Readline is inserting into, not as a path: # a same-named directory must not add a slash to an alias. @@ -282,12 +318,20 @@ if [[ -n ${ZSH_VERSION-} ]]; then # ZLE supplies words, CURRENT, and PREFIX when it invokes the completer. # shellcheck disable=SC2154 __nvm_zsh_complete() { - local COMP_CWORD=$((CURRENT - 1)) COMP_LINE="${words[*]}" - local -a COMP_WORDS COMPREPLY value_flags arguments + local COMP_CWORD + COMP_CWORD=$((CURRENT - 1)) + local COMP_LINE + COMP_LINE="${words[*]}" + local -a COMP_WORDS + local -a COMPREPLY + local -a value_flags + local -a arguments COMP_WORDS=("${words[@]}") __nvm_candidates "${PREFIX}" - local word ret=1 + local word + local ret + ret=1 for word in "${COMPREPLY[@]}"; do case "${word}" in -*=) value_flags+=("${word}") ;; diff --git a/nvm.sh b/nvm.sh index 0bfc2cdd..661e92b5 100755 --- a/nvm.sh +++ b/nvm.sh @@ -4951,7 +4951,8 @@ nvm_node_version_has_solaris_binary() { # Succeeds if $VERSION represents a version (node, io.js or merged) that has a # Solaris binary, fails otherwise. nvm_has_solaris_binary() { - local VERSION="${1-}" + local VERSION + VERSION="${1-}" if nvm_is_merged_node_version "${VERSION}"; then return 0 # All merged node versions have a Solaris binary elif nvm_is_iojs_version "${VERSION}"; then @@ -5586,7 +5587,8 @@ nvm() { nvm_err "wget: not found" fi - local NVM_TEST_TOOLS NVM_ADD_TEST_TOOLS + local NVM_TEST_TOOLS + local NVM_ADD_TEST_TOOLS NVM_TEST_TOOLS="git grep" NVM_ADD_TEST_TOOLS="sed cut basename rm mkdir xargs" if [ "darwin" != "$(nvm_get_os)" ] && [ "freebsd" != "$(nvm_get_os)" ]; then