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

logical-assignment: Do not assign names to anonymous functions #11370

Merged

Conversation

arku
Copy link
Contributor

@arku arku commented Apr 2, 2020

Q                       A
Fixed Issues? Fixes #11362
Patch: Bug Fix? Yes
Major: Breaking Change? No
Minor: New Feature? No
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

@arku arku force-pushed the fix/logical-assignment-anonymous-functions branch from b626107 to cbc73c0 Compare April 3, 2020 00:24
@Andarist
Copy link
Member

Andarist commented Apr 3, 2020

A bunch of stuff can be introduced to make the function anonymous, the easiest ones would be:

  1. unwrap from array
var _f;

var f;
((_f = f) !== null && _f !== void 0 ? _f : f = [function () {}][0]).name;
  1. introduce a helper
function ensureAnonymous(fn) { return fn }
var _f;

var f;
((_f = f) !== null && _f !== void 0 ? _f : f = ensureAnonymous(function () {})).name;

Both of those are valid es5 so I would consider them being better alternatives. I don't think that all current transforms (but I might be wrong) are that much concerned about .name, it's more of a "best effort" from what I remember. So maybe it's not worth doing crazy stuff to cover this? Or maybe a crazy hack could be hidden behind spec: true option?

Copy link
Member

@jridgewell jridgewell left a comment

Choose a reason for hiding this comment

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

We actually could update the spec to give anonymous closures a name. We don't have it for any of the math assignment operators, because the function just gets coerced into a number. I'll open an issue.

introduce a helper

Using a IIFE sounds good to me. I don't think we need a helper, though, it's not that much code.

@jridgewell
Copy link
Member

tc39/proposal-logical-assignment#23 (comment) mentions that we can also use a SequenceExpression to do this:

// input
foo ??= function() {};

// output
foo ?? (foo = (0, function() {}));

@arku arku force-pushed the fix/logical-assignment-anonymous-functions branch from cbc73c0 to fd88b0f Compare April 7, 2020 08:46
@arku
Copy link
Contributor Author

arku commented Apr 7, 2020

@jridgewell I have updated my PR with the suggested changes. Do you mind taking another look?

@nicolo-ribaudo nicolo-ribaudo added PR: Spec Compliance 👓 A type of pull request used for our changelog categories Spec: Logical Assignment labels Apr 7, 2020
@arku arku force-pushed the fix/logical-assignment-anonymous-functions branch from fd88b0f to b09826c Compare April 7, 2020 21:52
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Thanks!

{
"plugins": [
"proposal-logical-assignment-operators",
"proposal-nullish-coalescing-operator"
Copy link
Member

Choose a reason for hiding this comment

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

Note for reviewers: the "exec" and "transform" tests are in different folders because they have different configs.

@jridgewell
Copy link
Member

@babel/babel Any idea what's going on with the tests?

@nicolo-ribaudo
Copy link
Member

@JLHwung It is related to exports, maybe you know 🤔

@JLHwung JLHwung force-pushed the fix/logical-assignment-anonymous-functions branch from 7997fb2 to a17e23a Compare May 5, 2020 13:27
@JLHwung
Copy link
Contributor

JLHwung commented May 5, 2020

Rebased on upstream. The exports error should be fixed in recent releases.

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 5, 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 a17e23a:

Sandbox Source
reverent-matsumoto-q1ej6 Configuration
jolly-firefly-x51ml Configuration

@babel-bot
Copy link
Collaborator

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

@nicolo-ribaudo nicolo-ribaudo changed the title fix(proposal-logical-assignment): Do not assign names to anonymous functions logical-assignment: Do not assign names to anonymous functions May 5, 2020
@nicolo-ribaudo nicolo-ribaudo merged commit a8061ae into babel:master May 5, 2020
@arku arku deleted the fix/logical-assignment-anonymous-functions branch May 5, 2020 15:00
jridgewell added a commit to jridgewell/babel that referenced this pull request Jun 1, 2020
This is a partial revert of babel#11370, updating the tests to match the [new consensus](babel/proposals#66 (comment)).
nicolo-ribaudo pushed a commit that referenced this pull request Jun 3, 2020
This is a partial revert of #11370, updating the tests to match the [new consensus](babel/proposals#66 (comment)).
@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 5, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 5, 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: Logical Assignment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

proposal-logical-assignment should not assign anonymous function name
6 participants