You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In strict mode, function statements are only allowed at the top level of a script or function (source). Currently acorn accepts code that does not follow this rule.
For example, this code should throw a SyntaxError :
@getify, if i copy/paste your foo function in the console in Chrome (or Firefox), I get "SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function".
First ES5 and ES6 are different.
In ES5 strict mode, all non-top level functions declarations (by informal agreement among browser implementators, not by spec. ) should be illegal.
Thanks for the great clarifications, @allenwb. I suspected exactly that, but you're the authority. :)
From your perspective, do you think a parser should disallow things that the spec allows (or allow things the spec disallows) simply because browsers/annex-b cause divergence?
My take is that a parser should follow the spec strictly, and not concern itself with browser variances.
If you're building a browser (or processing web content scripts some other way) you need to implement Annex B in order to be compatible with existing web content. But, generally, stuff in Annex B is stuff a JS programmer should ever intentionally code. That's why it's in Annex B rather than the main body of the spec.
If you are building something other than a web browser and don't need to worry about compatibility will all existing web content, then you should just ignore Annex B and fully conform to the main body of the spec.
Building off what @allenwb said, I guess I would clarify my perspective as: a JS parser (not attached to a browser, mind-you) should take the more lenient (less errors) of either two positions if there's a divergence between the spec and browsers/annexB. If browsers throw an error but standard spec JS doesn't, the JS parser shouldn't. If the spec throws an error, but the browsers don't, the JS parser shouldn't. Only if both agree on throwing an error should the JS parser throw it. Just my 2 cents.
Agree with @getify here. I think parser is more generic and can be used by any kind of tools, so should choose to throw less errors whenever it's allowed by spec.
Activity
getify commentedon Aug 28, 2014
I don't think that's accurate. In fact, I believe as of ES6, this is officially allowed.
Trying it in Chrome:
I think the difference is that
if (a) function ...
is not allowed, butif (a) { function ... }
is.RReverser commentedon Aug 28, 2014
@getify Agree, but weird that MDN page and ES5 standard mention that this should be prohibited (while browsers don't).
getify commentedon Aug 28, 2014
Let's ask @allenwb or @rwaldron for clarification. :)
r-e-d commentedon Aug 28, 2014
@getify, if i copy/paste your foo function in the console in Chrome (or Firefox), I get "SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function".
getify commentedon Aug 28, 2014
@r-e-d
Could be that I'm running Chrome canary with ES6 stuff turned on? Shrugs.
allenwb commentedon Aug 28, 2014
First ES5 and ES6 are different.
In ES5 strict mode, all non-top level functions declarations (by informal agreement among browser implementators, not by spec. ) should be illegal.
In ES6, function declarations are legal (regardless of mode) in blocks. In strict mode, function declarations are still illegal as single statements such as
if (true) function foo() {};
. In theory, the same applies to non-strict mode, but for browsers specifically, Annex B rules apply: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-block-level-function-declarations-web-legacy-compatibility-semantics and http://people.mozilla.org/~jorendorff/es6-draft.html#sec-functiondeclarations-in-ifstatement-statement-clauses.There is no particular reason to assume, at this point in time, that Chrome, FF, MDN, or anything else has yet to get this all correct.
getify commentedon Aug 28, 2014
Thanks for the great clarifications, @allenwb. I suspected exactly that, but you're the authority. :)
From your perspective, do you think a parser should disallow things that the spec allows (or allow things the spec disallows) simply because browsers/annex-b cause divergence?
My take is that a parser should follow the spec strictly, and not concern itself with browser variances.
allenwb commentedon Aug 28, 2014
If you're building a browser (or processing web content scripts some other way) you need to implement Annex B in order to be compatible with existing web content. But, generally, stuff in Annex B is stuff a JS programmer should ever intentionally code. That's why it's in Annex B rather than the main body of the spec.
If you are building something other than a web browser and don't need to worry about compatibility will all existing web content, then you should just ignore Annex B and fully conform to the main body of the spec.
getify commentedon Aug 28, 2014
Building off what @allenwb said, I guess I would clarify my perspective as: a JS parser (not attached to a browser, mind-you) should take the more lenient (less errors) of either two positions if there's a divergence between the spec and browsers/annexB. If browsers throw an error but standard spec JS doesn't, the JS parser shouldn't. If the spec throws an error, but the browsers don't, the JS parser shouldn't. Only if both agree on throwing an error should the JS parser throw it. Just my 2 cents.
RReverser commentedon Sep 24, 2014
Agree with @getify here. I think parser is more generic and can be used by any kind of tools, so should choose to throw less errors whenever it's allowed by spec.
marijnh commentedon Sep 24, 2014
Agreed. Let's close this, and reconsider when it becomes clear what browsers will do in ES6-mode.