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

Improve output when privateFieldsAsProperties or privateFieldsAsSymbols #16313

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

liuxingbaoyu
Copy link
Member

@liuxingbaoyu liuxingbaoyu commented Feb 28, 2024

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

attempted to use private field on non-instance is now replaced by throwing TypeError: this[_privateMethod] is not a function, which I think does not matter since this is not in spec mode.

Contains #16312, please only view the last commit, it does not require blocking release.

@babel-bot
Copy link
Collaborator

babel-bot commented Feb 28, 2024

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

@liuxingbaoyu liuxingbaoyu force-pushed the improve-classPrivateFieldLooseBase branch from 30d7bd8 to 9c4e868 Compare February 28, 2024 02:41
@liuxingbaoyu liuxingbaoyu added the PR: Output optimization 🔬 A type of pull request used for our changelog categories label Feb 28, 2024
@liuxingbaoyu liuxingbaoyu force-pushed the improve-classPrivateFieldLooseBase branch from c1f929e to b7ae29b Compare February 29, 2024 00:41
@@ -893,6 +893,15 @@ helpers.classPrivateFieldLooseBase = helper("7.0.0-beta.0")`
}
`;

helpers.classPrivateFieldGetLoose = helper("7.24.0")`
Copy link
Member

Choose a reason for hiding this comment

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

Can you move it to a standalone file? :)

babelHelpers.classPrivateFieldLooseBase(this, _foo)[_foo] = 2;
babelHelpers.classPrivateFieldLooseBase(other.obj, _foo)[_foo] += 1;
babelHelpers.classPrivateFieldLooseBase(other.obj, _foo)[_foo] = 2;
babelHelpers.classPrivateFieldGetLoose(this, _foo, 1)[_foo] += 1;
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this a size increase in general? It looks shorter because the helepr name is shorter, but once you minify the helper name to 1 char this has ,1 more.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can pass ,1 to mean "return the property" rather than "return the receiver", so that we are sure that it's an improbement rather than depending on the ratio of get to set.

@@ -839,6 +839,13 @@ const privateNameHandlerLoose: Handler<PrivateNameState> = {
const { object } = member.node;
const { name } = (member.node.property as t.PrivateName).id;

if (process.env.BABEL_8_BREAKING || newHelpers(file)) {
Copy link
Member

Choose a reason for hiding this comment

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

newHelpers doesn't work here anymore, because it returns true in 7.24.0

Copy link
Member Author

Choose a reason for hiding this comment

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

ah. I have modified newHelpers to return true in v7.24.1. Maybe the changes were lost for some reason.
What do you think of this approach?

@@ -3,7 +3,7 @@ var _privateStaticMethod = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("
class Cl {
constructor() {
if (exfiltrated === undefined) {
exfiltrated = babelHelpers.classPrivateFieldLooseBase(Cl, _privateStaticMethod)[_privateStaticMethod];
exfiltrated = babelHelpers.classPrivateFieldGetLoose(Cl, _privateStaticMethod);
Copy link
Member

Choose a reason for hiding this comment

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

For methods, when the receiver is the class name, we can skip the helper call (similarly to how we do for the non-loose transform)

Copy link
Member Author

Choose a reason for hiding this comment

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

image
Unfortunately this will cause the test to fail. :)

@liuxingbaoyu liuxingbaoyu force-pushed the improve-classPrivateFieldLooseBase branch from b7ae29b to c701ff0 Compare March 5, 2024 18:24
@liuxingbaoyu liuxingbaoyu force-pushed the improve-classPrivateFieldLooseBase branch from c701ff0 to b6821f7 Compare March 15, 2024 23:02
@liuxingbaoyu liuxingbaoyu force-pushed the improve-classPrivateFieldLooseBase branch from b6821f7 to 005dd59 Compare March 15, 2024 23:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Output optimization 🔬 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants