invalid label error in {"label":"value"} syntax

RESOLVED INVALID

Status

()

RESOLVED INVALID
13 years ago
13 years ago

People

(Reporter: ja.doma, Unassigned)

Tracking

Trunk
x86
Windows XP
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

13 years ago
User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4

This code causes error:
var el = document.createElement("input");	
{"~type":"hidden", "~value":v, "~name":"sel[]"}.each(function(k, v) { el.setAttribute(k, v); });
document.getElementById("chnl").appendChild(el);

From console:
Error: invalid label
.....
Source Code:
{"~type":"hidden", "~value":v, "~name":"sel[]"}.each(function(k, v) { el.setAttribute(k, v); });

With the arrow between "~type" and "hidden";

Looks like the parser treat the object literal as a block with usual label (used to mark loops). So if I write it:

var dummy = {"~type":"hidden", "~value":v, "~name":"sel[]"}.each(function(k, v) { el.setAttribute(k, v); });

Everything works properly. It looks like the JS syntax disambiguation in some cases, but in this context the block couldn't be declared, so it is a proper bug in firefox parser. IE accepts the code without errors.

P.S. 'each' method call is just to let you know the code is real, remove or replace it by any existing in Object method.
P.P.S maybe the code is more Ruby-like, but I like it that way =)

Reproducible: Always

Steps to Reproduce:
1. copy code from the details, place in a function, save into file
2. remove .each method call, or replace it by .toString()
3. open the document, see the parser error, no execution was made actually

Actual Results:  
Error from the parser (before code execution).

Expected Results:  
N/A

Here:

if(a) {test: "value"}.each(function(k, v){ alert(v); });

The parser may sound the error, because {test: looks like normal if body with label test: (next statement should be any loop). But if you get the expression (something that gives results back) after ':' and not the statement (something tha t dosn't return anything, or more strict 'loop statements only'), then the code {test:expression should be read as a literal object.

Example:

if(a) {test: while(...  //this is a code block, not a literal object!

So the grammar has no collisions, I don't know why yo guys have implemented it that way... =)
(Reporter)

Comment 1

13 years ago
If you enclose the line in (), you will explicitly declare it as a expression, so this works also properly:

var el = document.createElement("input");       
({"~type":"hidden", "~value":v, "~name":"sel[]"}).each(function(k, v) {
el.setAttribute(k, v); });
document.getElementById("chnl").appendChild(el);
Assignee: nobody → general
Component: General → JavaScript Engine
Product: Firefox → Core
QA Contact: general → general
Version: unspecified → Trunk

Comment 2

13 years ago
I think this is invalid.

BoneEcho
{"test": "value"}.hasOwnProperty("test")
SyntaxError on line 1: invalid label

({"test": "value"}).hasOwnProperty("test")
true

if (true) {test: while(false) ; }

MSIE6
{"test": "value"}.hasOwnProperty("test")
SyntaxError: Expected ';'

({"test": "value"}).hasOwnProperty("test")
true

if (true) {test: while(false) ; }
Status: UNCONFIRMED → RESOLVED
Last Resolved: 13 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.