Skip to content
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

a?.b.c() produces different result than a?.b!.c() #10959

Closed
1 task
alex-kinokon opened this issue Jan 4, 2020 · 5 comments · Fixed by #10961
Closed
1 task

a?.b.c() produces different result than a?.b!.c() #10959

alex-kinokon opened this issue Jan 4, 2020 · 5 comments · Fixed by #10961
Assignees
Labels
area: typescript Has PR i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser Spec: Optional Chaining

Comments

@alex-kinokon
Copy link

Bug Report

  • I would like to work on a fix!

Input Code

  • REPL or Repo link if applicable:
a?.b.c()
a?.b!.c()

Actual behavior

"use strict";

var _a, _a2;

(_a = a) === null || _a === void 0 ? void 0 : _a.b.c();
((_a2 = a) === null || _a2 === void 0 ? void 0 : _a2.b).c();

Expected behavior/code
The ! should have no effect on the produced code.

Problem: Suppose a is null, then the second line can be reduced to:

   ((_a2 = a) === null || _a2 === void 0 ? void 0 : _a2.b).c()
=> ((a2 = null) === null || _a2 === void 0 ? void 0 : _a2.b).c()
=> (null === null || _a2 === void 0 ? void 0 : _a2.b).c()
=> (true ? void 0 : _a2.b).c()
=> undefined.c()

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js
{
  "presets": ["@babel/preset-typescript", "@babel/preset-stage-3"]
}

Environment

Possible Solution
¯\_(ツ)_/

Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.

@babel-bot
Copy link
Collaborator

Hey @proteriax! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@macabeus
Copy link
Contributor

macabeus commented Jan 4, 2020

I'm working in a fix to this bug.

@macabeus
Copy link
Contributor

macabeus commented Jan 4, 2020

PR opened: #10961

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Mar 22, 2020

EDIT: This is fixed in TS 3.9

This is correct. For reference, this is what the TypeScript compiler (verision 3.8) does: [TS Playground]

a?.b.c
a?.b!.c

a?.b.c()
a?.b!.c()
a === null || a === void 0 ? void 0 : a.b.c;
(a === null || a === void 0 ? void 0 : a.b).c;
a === null || a === void 0 ? void 0 : a.b.c();
(a === null || a === void 0 ? void 0 : a.b).c();

And this is Babel's output: [Babel repl]

(_a = a) === null || _a === void 0 ? void 0 : _a.b.c;
((_a2 = a) === null || _a2 === void 0 ? void 0 : _a2.b).c;
(_a3 = a) === null || _a3 === void 0 ? void 0 : _a3.b.c();
((_a4 = a) === null || _a4 === void 0 ? void 0 : _a4.b).c();

@alex-kinokon
Copy link
Author

@nicolo-ribaudo There’s a bug filed at the TypeScript repo.

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 24, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript Has PR i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser Spec: Optional Chaining
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants