Compatibility Information

Browser Implementation Compatibility

In a certain specific case, Esprima intentially does not throw an exception (indicating a parsing error) although the input being parsed contains an invalid syntax. This is to achieve implementation compatibility with major web browsers. For further details, refer to the official ECMAScript 2015 Language Specification, Section B.3.3 on Block-Level Function Declarations Web Legacy Compatibility Semantics:

Prior to ECMAScript 2015, the ECMAScript specification did not define the occurrence of a FunctionDeclaration as an element of a Block statement’s StatementList. However, support for that form of FunctionDeclaration was an allowable extension and most browser-hosted ECMAScript implementations permitted them.

This is illustrated in the following simple example:

var syntax = esprima.parse('if (x) function y() {}');

In the above example, Esprima returns a syntax tree for the code (see the result).

According to the specification, declaring a function inside the block of an If statement is not possible. Yet, this is permitted in web browsers such as Firefox, Chrome, and many others.