Skip to content

Incorrect code produced with ternaries, imported function calls, and ternaries #8281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marcins opened this issue Jul 5, 2022 · 1 comment · Fixed by #8276
Closed

Incorrect code produced with ternaries, imported function calls, and ternaries #8281

marcins opened this issue Jul 5, 2022 · 1 comment · Fixed by #8276

Comments

@marcins
Copy link
Contributor

marcins commented Jul 5, 2022

🐛 bug report

Code like this produces incorrect output:

import { a, b } from "./foo";

const c = a() ? undefined : b?.();

A bisect shows that this is related to the change in #7956.

The optional chaining is relevant, as without it in the Repl correct output is produced (without optimisation that would potentially simplify the code further).

🤔 Expected Behavior

Code is bundled correctly, the right hand side of the ternary should be wrapped in parens:
const c = (0, a)() ? undefined : (0, b)?.();

😯 Current Behavior

The bundler outputs:

const c = (0, a)() ? undefined : 0, b?.();

Code on right hand side of ternary is not wrapped, which changes the behaviour.

This manifests itself initially at build time as a Terser parsing error "Missing initializer in const declaration", because it the resulting JS tries to re-initialise b.

As per above, without the optional chaining in the Parcel repl the output is const c = (0, a)() ? undefined : (0, b)();

💻 Code Sample

Repro in repl

@mischnic
Copy link
Member

mischnic commented Jul 5, 2022

Is a bug in swc: swc-project/swc#5109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants