First off, the correct place to report a V8 bug is crbug.com/v8/new. StackOverflow is not the right venue for bug reports.
Secondly, this is not a bug; it is JavaScript's "Automatic Semicolon Insertion" working as specified. You can read the specification here. What it boils down to is that a = {x:1}[b, c] = [1, 2]
is valid JavaScript (nonsensical maybe, but valid syntax), so no semicolon is inserted automatically.
The best way to avoid falling into this and similar traps is to always write explicit semicolons. Or you can prefer the visual cleanliness of semicolon-free code, but then you'll have to familiarize yourself with the rules of ASI, and know in which cases semicolons are required.
EDIT: Since you asked below what a = {x:1}[b, c] = [1, 2]
does, here's an explanation:
Firstly, you can chain assignments, like x = y = z = 0;
, so one effect of this expression is that the variable a
will point at the array [1, 2]
afterwards.
Looking at the middle term {x:1}[b, c]
, the first half of that is, of course, an object literal. The second half is an index expression to access a property of this object. As a simplified version, consider: {x:1}["x"]
would return 1
. (Note: if you evaluate this in e.g. the console, you'll have to wrap the object literal into ()
, i.e. ({x:1})["x"]
, because the {}
will otherwise be interpreted as a block instead of a literal. One of the many traps of JavaScript...)
Now the specific index expression here is b, c
, which is a comma-separated sequence of expressions, similar to how you could write for (x = 0, y = 0; ...; ...) {...}
. So the b
part is evaluated for side effects; since it's just a reference to a variable, that doesn't do anything, but you could e.g. write console.log(b)
there and then it would do something. c
is then the overall result value of the sequence; in your example it's an uninitialized variable so its value is undefined
, which in the context of an index expression is converted to the string "undefined"
.
Putting it all together, here's what happens, in order:
[1, 2]
is evaluated and creates a two-element array. Let's call this array A
, just for descriptive purposes.
{x:1}
is evaluated and creates an object with an x
property. Let's call this object O
, again just for descriptive purposes.
b
is evaluated (to undefined
), and the result of the evaluation is discarded.
c
is evaluated (to undefined
), converted to the string "undefined"
, and used to store A
as a property on O
, as in O["undefined"] = A
, or as if you had written {x:1, undefined:[1, 2]}
.
- Since nothing refers to
O
, it becomes unreachable and eligible for garbage collection. You can't see that step (4) happened to it.
A
will be assigned to a
.
In summary, a = {x:1}[b, c] = [1, 2]
is certainly a weird line of code to write, but it would be a bug if a JavaScript engine decided for you "I'm sure you meant something else, let me give you the behavior that I think you wanted". As long as you write valid JavaScript, the engine has to evaluate it exactly by the rules of the JavaScript language.