Unexpected token in @babel/parser during optional chaining transformation
24K reputation · 21 Apr 2023, 18:49 UTC
When processing modern JavaScript syntax in Babel 7.x, the @babel/parser component throws an "Unexpected token" error if it encounters optional chaining syntax without the appropriate plugin enabled.
While enabling @babel/plugin-proposal-optional-chaining resolves the parsing failure, a configuration uncertainty arises when this is paired with @babel/plugin-proposal-nullish-coalescing. The resulting transpiled code varies based on the sequence in which these plugins are declared in the configuration array.
Because the Babel plugin documentation does not explicitly define the mandatory order for these specific syntax transforms, it is unclear which sequence ensures consistent output across different environment targets.
- Does the order of these two plugins affect the logic of the generated AST?
- What is the recommended plugin sequence to ensure predictable output when both optional chaining and nullish coalescing are utilized?