Skip to content

Commit 468babf

Browse files
committed
console, internal/jsre: fix autocomplete issues (ethereum#26518)
1 parent 5a49853 commit 468babf

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

console/console.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,17 +271,13 @@ func (c *Console) AutoCompleteInput(line string, pos int) (string, []string, str
271271
if len(line) == 0 || pos == 0 {
272272
return "", nil, ""
273273
}
274-
// Chunck data to relevant part for autocompletion
274+
// Chunk data to relevant part for autocompletion
275275
// E.g. in case of nested lines eth.getBalance(eth.coinb<tab><tab>
276276
start := pos - 1
277277
for ; start > 0; start-- {
278278
// Skip all methods and namespaces (i.e. including the dot)
279-
if line[start] == '.' || (line[start] >= 'a' && line[start] <= 'z') || (line[start] >= 'A' && line[start] <= 'Z') {
280-
continue
281-
}
282-
// Handle web3 in a special way (i.e. other numbers aren't auto completed)
283-
if start >= 3 && line[start-3:start] == "web3" {
284-
start -= 3
279+
c := line[start]
280+
if c == '.' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '1' && c <= '9') {
285281
continue
286282
}
287283
// We've hit an unexpected character, autocomplete form here

internal/jsre/completion.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
package jsre
1818

1919
import (
20+
"regexp"
2021
"sort"
2122
"strings"
2223

2324
"github.com/dop251/goja"
2425
)
2526

27+
// JS numerical token
28+
var numerical = regexp.MustCompile(`^(NaN|-?((\d*\.\d+|\d+)([Ee][+-]?\d+)?|Infinity))$`)
29+
2630
// CompleteKeywords returns potential continuations for the given line. Since line is
2731
// evaluated, callers need to make sure that evaluating line does not have side effects.
2832
func (jsre *JSRE) CompleteKeywords(line string) []string {
@@ -43,8 +47,11 @@ func getCompletions(vm *goja.Runtime, line string) (results []string) {
4347
// and "x.y" is an object, obj will reference "x.y".
4448
obj := vm.GlobalObject()
4549
for i := 0; i < len(parts)-1; i++ {
50+
if numerical.MatchString(parts[i]) {
51+
return nil
52+
}
4653
v := obj.Get(parts[i])
47-
if v == nil {
54+
if v == nil || goja.IsNull(v) || goja.IsUndefined(v) {
4855
return nil // No object was found
4956
}
5057
obj = v.ToObject(vm)
@@ -67,7 +74,11 @@ func getCompletions(vm *goja.Runtime, line string) (results []string) {
6774
// Append opening parenthesis (for functions) or dot (for objects)
6875
// if the line itself is the only completion.
6976
if len(results) == 1 && results[0] == line {
70-
obj := obj.Get(parts[len(parts)-1])
77+
// Accessing the property will cause it to be evaluated.
78+
// This can cause an error, e.g. in case of web3.eth.protocolVersion
79+
// which has been dropped from geth. Ignore the error for autocompletion
80+
// purposes.
81+
obj := SafeGet(obj, parts[len(parts)-1])
7182
if obj != nil {
7283
if _, isfunc := goja.AssertFunction(obj); isfunc {
7384
results[0] += "("

0 commit comments

Comments
 (0)