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

refactor: do not rely on AST extra properties in plugins #11662

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Jun 2, 2020

Q                       A
Fixed Issues? See explainers below
Patch: Bug Fix? Yes
License MIT

As a follow-up to #11623, this PR refines (a?.b)() detection and ensures that plugins never rely on extra AST properties. Also fixes a regression introduced in #11623 that f((a?.b)) is incorrectly transformed into f((a == null ? void 0 : a.b.bind(a))).

@JLHwung JLHwung added PR: Spec Compliance 👓 A type of pull request used for our changelog categories i: regression Spec: Optional Chaining labels Jun 2, 2020
@@ -32,8 +32,8 @@ class Foo {
(o == null ? void 0 : o.Foo.m.bind(o.Foo))().toString();
((_o$Foo$self$getSelf = ((_o$Foo = o.Foo) == null ? void 0 : _o$Foo.self.getSelf.bind(_o$Foo.self))()) == null ? void 0 : _o$Foo$self$getSelf.m.bind(_o$Foo$self$getSelf))();
((_o$Foo$self$getSelf2 = ((_o$Foo$self = o.Foo.self) == null ? void 0 : _o$Foo$self.getSelf.bind(_o$Foo$self))()) == null ? void 0 : _o$Foo$self$getSelf2.m.bind(_o$Foo$self$getSelf2))();
((_fn$Foo$self$getSelf = ((_fn = fn()) == null ? void 0 : (_fn$Foo = _fn.Foo) == null ? void 0 : _fn$Foo.self.getSelf.bind(_fn.Foo.self))()) == null ? void 0 : _fn$Foo$self$getSelf.m.bind(_fn$Foo$self$getSelf))();
((_fn$Foo$self$getSelf2 = (fn == null ? void 0 : (_fn$Foo$self2 = _fn$Foo$self = fn().Foo.self) == null ? void 0 : _fn$Foo$self2.getSelf.bind(_fn$Foo$self))()) == null ? void 0 : _fn$Foo$self$getSelf2.m.bind(_fn$Foo$self$getSelf2))();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✌️Unnecessary memoizers removed.

@babel-bot
Copy link
Collaborator

babel-bot commented Jun 2, 2020

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/23457/

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 2, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 99f7986:

Sandbox Source
patient-cdn-4xxdb Configuration
sad-fermi-53jx1 Configuration

@JLHwung JLHwung force-pushed the refactor-parenthesized-optional-member-call branch from 8426858 to 71972b5 Compare June 2, 2020 15:29
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
replacementPath.parentPath !== parentPath) &&
parentPath.isCallExpression()
) {
if (i === 0 && parentIsCall) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if we remove the i check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question!

Since parentIsCall is computed only once for the baseRef, if we don't limit this branch with i === 0, which means applying to the baseRef only, it will generate multiple memoizing assignment during looping all the optional chain elements.

This change fixes an issue in https://github.com/babel/babel/pull/11662/files#diff-778d6015479bf103795200e754865e9dL30 where fn().Foo.self is memoized twice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to add this in a comment?

Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@nicolo-ribaudo nicolo-ribaudo merged commit c9da3bc into babel:master Jun 4, 2020
@nicolo-ribaudo nicolo-ribaudo deleted the refactor-parenthesized-optional-member-call branch June 4, 2020 23:12
@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 Sep 4, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Spec Compliance 👓 A type of pull request used for our changelog categories Spec: Optional Chaining
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants