Skip to content

Commit 334d979

Browse files
s1nashekhirin
authored andcommitted
console, internal/jsre: fix autocomplete issues (ethereum#26518)
Fixes ethereum#26505 where the console crashed when a property getter raised an exception during autocompletion. I also noticed while fixing this issue that autocomplete wasn't working for objects/fields with numbers in them (most importantly web3.<tab><tab>) which is also now fixed.
1 parent 49b8044 commit 334d979

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

console/console.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,8 @@ func (c *Console) AutoCompleteInput(line string, pos int) (string, []string, str
305305
start := pos - 1
306306
for ; start > 0; start-- {
307307
// Skip all methods and namespaces (i.e. including the dot)
308-
if line[start] == '.' || (line[start] >= 'a' && line[start] <= 'z') || (line[start] >= 'A' && line[start] <= 'Z') {
309-
continue
310-
}
311-
// Handle web3 in a special way (i.e. other numbers aren't auto completed)
312-
if start >= 3 && line[start-3:start] == "web3" {
313-
start -= 3
308+
c := line[start]
309+
if c == '.' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '1' && c <= '9') {
314310
continue
315311
}
316312
// We've hit an unexpected character, autocomplete form here

internal/jsre/completion.go

Lines changed: 12 additions & 1 deletion
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,6 +47,9 @@ 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])
4754
if v == nil || goja.IsNull(v) || goja.IsUndefined(v) {
4855
return nil // No object was found
@@ -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)