jquery / esprima

ECMAScript parsing infrastructure for multipurpose analysis
http://esprima.org
BSD 2-Clause "Simplified" License
7.02k stars 784 forks source link

Function declaration name is mistakenly treated as function code #1707

Open ariya opened 7 years ago

ariya commented 7 years ago

(As reported by @michaelficarra and explained by @bakkot as case 14,15,18-25 in #1502).

Test case:

esprima.parse('function eval() {"use strict"; }');

Actual: An exception "Function name may not be eval or arguments in strict mode".

Expected: A valid syntax tree.


A similar test case:

esprima.parse('function static() {"use strict"; }');

Actual: An exception "Use of future reserved word in strict mode".

Expected: A valid syntax tree.


A reference for this in the specification is on Types of Source Code:

Function code is source text that is parsed to supply the value of the [[ECMAScriptCode]] and [[FormalParameters]] internal slots (see 9.2) of an ECMAScript function object.

While the formal parameters are part of the function code, the declaration name however is not.

bakkot commented 7 years ago

It's probably worth noting that all major engines also reject such programs, contra spec.

bakkot commented 7 years ago

Oh, and see old spec bug relating to this, which never got resolved.

ariya commented 7 years ago

@bakkot Confirmed. I checked the behavior of V8, SpiderMonkey, and ChakraCore.

It would be interesting to find out (1) the reason that most web browser's engine continues to violate the specification (compatibility etc) and (2) if there is any plan for those engines to start to comply to the specification in the future.

bakkot commented 6 years ago

The spec question has finally been resolved in favor of making a normative change to match the behavior in most implementations, including esprima. See https://github.com/tc39/ecma262/pull/1158. I think this can be closed with no further action.