robertkrimen / otto

A JavaScript interpreter in Go (golang)
http://godoc.org/github.com/robertkrimen/otto
MIT License
8.01k stars 584 forks source link

otto/repl: autocomplete of chained expressions #342

Open robfig opened 5 years ago

robfig commented 5 years ago

I noticed that chained expressions do not work with the autocompleter. I was curious if there was any thought on how to support that?

For example, assuming a() returns an object, I would want a().<TAB> to show me that object's properties, but currently the autocompleter shows me globals instead. Looking at the code, I think it would work if lastExpressionRegex changed to select the entire expression. That would result in invoking the function multiple times, but that could be ok..

cc @deoxxa

Asday commented 5 years ago

Consider the following:

function a() {
  var arr = [ { a: 1, b: 2 }, [ 3, 4, 5 ] ];
  return arr[Math.floor(Math.random() * 2)];
}

Without a strict typing system, autocompletion of chaining is not reliable.

Interestingly, chromium tries to guess, it looks like based on the last invocation of the function:

image

robfig commented 5 years ago

Yes, that's true, but I think in most cases the type returned is stable, especially without changing the arguments to the function. At any rate, it would do the right thing much more often than the current behavior, which never does the right thing in that scenario?