In my code, I have something that boils down to this:
var x = y || ()=>{};
(In case you are wondering, I am later calling x()
try this var x =( y || (()=>{}));
This is normal. Unlike a function
expression, which is a PrimaryExpression like other literals and identifiers, and arrow function specifically is an AssignmentExpression and can only appear inside grouping, comma, assignment, conditional (ternary) and yield expressions. Any other operator than those would lead to ambiguities.
For example, if you expect y || ()=>z
to work, then also ()=>z || y
should be expected to work (assuming symmetric precedence). That however clearly collides with our expectation that we can use any operators inside concise bodies of arrow functions. Therefore, it's a syntax error and requires explicit grouping to work and stay maintainable.
This is not a bug in the JavaScript engine. This behavior is documented.
Although the arrow in an arrow function is not an operator, arrow functions have special parsing rules that interact differently with operator precedence compared to regular functions.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Parsing_order