Autocompletion on arrays include numbers

RESOLVED WORKSFORME

Status

()

Firefox
Developer Tools: Console
P3
normal
RESOLVED WORKSFORME
6 years ago
4 years ago

People

(Reporter: espadrine, Assigned: espadrine)

Tracking

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: [autocomplete])

Attachments

(1 attachment)

(Assignee)

Description

6 years ago
> window.a = [1, 2];
> window.a.<TAB>

The autocompletion request above yields two possibilities,
"0" and "1", corresponding to the indices of the array.
However, selecting one of those inserts a syntactically
invalid JS statement.

The best way to deal with this is to autocomplete with

> window.a[0]

instead of

> window.a.0
This is probably also a problem with property names that have spaces in their names or other special characters, not just numbers in arrays.

Thanks for the report!
(Assignee)

Comment 2

6 years ago
Mihai: Property names that have spaces in their names should use a JS parser
to check whether the property needs to be in a string.

Then again, the use of a state machine to parse JS in the WebConsole
is not great. For starters, having spaces around the dot in a property lookup
is valid JS (see example below). Yet the state machine rejects it.

> window . a [ 0 ]

We should use a JS parser there, too. I have a patch for bug 759837,
but I have yet to write tests for it. That shouldn't take too long.
(Assignee)

Comment 3

6 years ago
Created attachment 649075 [details] [diff] [review]
Don't include numbers when completing objects and arrays.

This patch does not include numbers in autocompletion of objects and arrays.

It leaves room for bracket completion (eg, completion of |window[|).

It would require a JS parser to check for properties
that are not valid identifiers.
Assignee: nobody → thaddee.tyl
Attachment #649075 - Flags: review?(mihai.sucan)
Comment on attachment 649075 [details] [diff] [review]
Don't include numbers when completing objects and arrays.

Review of attachment 649075 [details] [diff] [review]:
-----------------------------------------------------------------

What should I review here?

I see the new completeAfter property which is never changed. I also see checks for +foo != +foo and specific behavior for such cases, but you have an outer if that skips those cases. Sorry, I am confused about the patch.

Do we really need a parser? Can you please check if the spec tells us what makes a valid identifier? Perhaps we could have a regex that would work for most-cases that determines an invalid identifier (it doesn't have to be 100% accurate). When the identifier is invalid we would wrap it in quotes, like ["foo identifier"].

Thanks!
Attachment #649075 - Flags: review?(mihai.sucan)
(Assignee)

Updated

6 years ago
Blocks: 780564
(Assignee)

Comment 5

6 years ago
(In reply to Mihai Sucan [:msucan] from comment #4)
> I see the new completeAfter property which is never changed. I also see
> checks for +foo != +foo and specific behavior for such cases, but you have
> an outer if that skips those cases. Sorry, I am confused about the patch.

As the code comment explain, dot completion does not add plain numbers
as properties.

It checks for those string numbers by converting the string to a number,
and checking whether the conversion failed. That's what the |+foo != +foo| is.

> Do we really need a parser? Can you please check if the spec tells us what
> makes a valid identifier? Perhaps we could have a regex that would work for
> most-cases that determines an invalid identifier (it doesn't have to be 100%
> accurate). When the identifier is invalid we would wrap it in quotes, like
> ["foo identifier"].

The spec is hard to implement in JS. Esprima does it, though [1].
It achieves that by listing all valid UTF-16 characters in a huge regex,
and checking whether every character of the identifier validates against it.
Of course, it has two huge regexes, one for the first character of the identifier,
the other for the following characters of the identifier.

Then, it has a different, big string to interpret what is a valid whitespace.


  [1] https://github.com/ariya/esprima/blob/master/esprima.js#L233


This algorithm is easy to get wrong; that's why I'd rather rely on a JS parser…

Anyway, we'll get a parser for everyone soon! :)
Thanks for your explanation.

Since all these patches work on the same parts of the code, please make one patch in one bug. Thanks!
Filter on GUNGNIR.
Priority: -- → P3
Whiteboard: [autocomplete]
This bug was fixed long ago. I dont remember which bug.
Status: NEW → RESOLVED
Last Resolved: 4 years ago
Resolution: --- → WORKSFORME
You need to log in before you can comment on or make changes to this bug.