Skip to content

Commit

Permalink
chore: refactor arg counter into __yarn_count_args compat function
Browse files Browse the repository at this point in the history
  • Loading branch information
dsifford committed May 12, 2017
1 parent 1151654 commit 0ef7233
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions yarn-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ __yarn_filedir() {
compopt -o nospace
}

# `_count_args` backwards compatibility
# Be sure to set `args` and `counter` locally before calling
__yarn_count_args() {
args=0
counter=1
while [[ $counter -lt $cword ]]; do
[[ ${words[$counter]} != -* ]] && (( args++ ))
(( counter++ ))
done
}

_yarn_add() {
local flags=(
--dev
Expand Down Expand Up @@ -184,20 +195,15 @@ _yarn_info() {

[[ "$prev" == info ]] && return

# `_count_args` backwards compatibility
local args=1
local counter=1
while [[ $counter -lt $cword ]]; do
[[ ${words[$counter]} != -* ]] && (( args++ ))
(( counter++ ))
done
local args counter
__yarn_count_args

case "$cur" in
-*)
COMPREPLY=( $( compgen -W "${flags[*]}" -- "$cur" ) )
;;
*)
if [[ $args -eq 3 ]]; then
if [[ $args -eq 2 ]]; then
COMPREPLY=( $( compgen -W "${standard_fields[*]}" -- "$cur" ) )
fi
;;
Expand Down Expand Up @@ -423,21 +429,15 @@ _yarn_why() {
}

_yarn_yarn() {
local args=1
local counter=1

# `_count_args` backwards compatibility
while [[ $counter -lt $cword ]]; do
[[ ${words[$counter]} != -* ]] && (( args++ ))
(( counter++ ))
done
local args counter
__yarn_count_args

case "$cur" in
-*)
COMPREPLY=( $( compgen -W "${global_flags[*]}" -- "$cur" ) )
;;
*)
if [[ $args -eq 1 ]]; then
if [[ $args -eq 0 ]]; then
COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
fi
;;
Expand Down

0 comments on commit 0ef7233

Please sign in to comment.