With babel-preset-react
(EDIT: babel version: v6.26.0
), the following expression fails with the error "Invalid left-hand side in arrow function parameters
" (try on babel repl):
true ? ("test") : x => 42 //FAILS, but only with `babel-preset-react`
Yet it works given a small modification:
true ? "test" : x => 42 //works fine
Both of these scenarios work as expected in seemingly any other configuration.
Is this a bug? Or is there something as a part of JSX parsing that causes this to happen?
Arrow functions are parsed differently than regular ones see Parsing order
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.
let callback;
callback = callback || function() {}; // ok
callback = callback || () => {};
// SyntaxError: invalid arrow-function arguments
callback = callback || (() => {}); // ok
It must have something to do with associativity of ternary operator (right-to-left) see link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With