Skip to content

Different behavior for a?.b<c>.d depending on the compilation targetΒ #49293

Closed
@nicolo-ribaudo

Description

@nicolo-ribaudo

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 😬)

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions