Skip to content

Commit

Permalink
Suggest keyword argument based on spelling in UnpackArgs (google#393)
Browse files Browse the repository at this point in the history
Users of tools such as Bazel and ours arguably edit mostly
the kwargs of functions the tool provides. Typos often slip
through and spelling suggestion can save some headache.

This change may affect the message string of some errors.

Signed-off-by: Pierre Fenoll <[email protected]>
  • Loading branch information
fenollp authored Feb 13, 2022
1 parent bb14e15 commit c55a923
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions starlark/testdata/function.star
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,9 @@ b(1, y=2, )
#c(1, *[], )
#d(1, *[], z=None, )
#e(1, *[], z=None, *{}, )

---
# Unpack provides spell check for argument names.
load("assert.star", "assert")

assert.fails(lambda: min([], keg=1), ".+did you mean key\\?")
13 changes: 12 additions & 1 deletion starlark/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"log"
"reflect"
"strings"

"go.starlark.net/internal/spell"
)

// An Unpacker defines custom argument unpacking behavior.
Expand Down Expand Up @@ -149,7 +151,16 @@ kwloop:
continue kwloop
}
}
return fmt.Errorf("%s: unexpected keyword argument %s", fnname, name)
err := fmt.Errorf("%s: unexpected keyword argument %s", fnname, name)
names := make([]string, 0, nparams)
for i := 0; i < nparams; i += 2 {
param, _ := paramName(pairs[i])
names = append(names, param)
}
if n := spell.Nearest(string(name), names); n != "" {
err = fmt.Errorf("%s (did you mean %s?)", err.Error(), n)
}
return err
}

// Check that all non-optional parameters are defined.
Expand Down

0 comments on commit c55a923

Please sign in to comment.