-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
Bug Report
🔎 Search Terms
instantiation expression optional chaining target
🕗 Version & Regression Information
This feature has been introduced in 4.7. I tested 4.7.0-dev.20220528
⏯ Playground Link
Playground link with relevant code
💻 Code
a?.b<c>.d🙁 Actual behavior
When targeting <=ES2019 it generates (a === null || a === void 0 ? void 0 : a.b).d, while when targeting >=ES2020 it generates a?.b.d.
They have different behaviors: in the ES2019 output it always reads .d, while in the ES2020 output it only reads .d if a is not nullish.
🙂 Expected behavior
They should have the same behavior.
The ES2020 behavior feels more useful, but "adding <c> to an optional chain ends the optional context" (as wrapping it in parentheses does) would be ok too as long as it's consistent. You would then have to write a?.b<c>?.d.
(Babel's implementation has the same different behavior depending on the target, so this is apparently something very easy to overlook while implementing instantiation expressions 😬)