Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console, internal/jsre: fix autocomplete issues #26518

Merged
merged 4 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,8 @@ func (c *Console) AutoCompleteInput(line string, pos int) (string, []string, str
start := pos - 1
for ; start > 0; start-- {
// Skip all methods and namespaces (i.e. including the dot)
if line[start] == '.' || (line[start] >= 'a' && line[start] <= 'z') || (line[start] >= 'A' && line[start] <= 'Z') {
continue
}
// Handle web3 in a special way (i.e. other numbers aren't auto completed)
if start >= 3 && line[start-3:start] == "web3" {
start -= 3
c := line[start]
if c == '.' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '1' && c <= '9') {
continue
}
// We've hit an unexpected character, autocomplete form here
Expand Down
13 changes: 12 additions & 1 deletion internal/jsre/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
package jsre

import (
"regexp"
"sort"
"strings"

"github.com/dop251/goja"
)

// JS numerical token
var numerical = regexp.MustCompile(`^(NaN|-?((\d*\.\d+|\d+)([Ee][+-]?\d+)?|Infinity))$`)

// CompleteKeywords returns potential continuations for the given line. Since line is
// evaluated, callers need to make sure that evaluating line does not have side effects.
func (jsre *JSRE) CompleteKeywords(line string) []string {
Expand All @@ -43,6 +47,9 @@ func getCompletions(vm *goja.Runtime, line string) (results []string) {
// and "x.y" is an object, obj will reference "x.y".
obj := vm.GlobalObject()
for i := 0; i < len(parts)-1; i++ {
if numerical.MatchString(parts[i]) {
return nil
}
v := obj.Get(parts[i])
if v == nil || goja.IsNull(v) || goja.IsUndefined(v) {
return nil // No object was found
Expand All @@ -67,7 +74,11 @@ func getCompletions(vm *goja.Runtime, line string) (results []string) {
// Append opening parenthesis (for functions) or dot (for objects)
// if the line itself is the only completion.
if len(results) == 1 && results[0] == line {
obj := obj.Get(parts[len(parts)-1])
// Accessing the property will cause it to be evaluated.
// This can cause an error, e.g. in case of web3.eth.protocolVersion
// which has been dropped from geth. Ignore the error for autocompletion
// purposes.
obj := SafeGet(obj, parts[len(parts)-1])
if obj != nil {
if _, isfunc := goja.AssertFunction(obj); isfunc {
results[0] += "("
Expand Down