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
fix: privateFieldsAsProperties global counter (#15389) #15393
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/53886/ |
Next, I would like to add tests, but there are some specialties involved to create a reproducer (see my codesandbox.io example):
|
b49fc7f
to
a2656af
Compare
This comment was marked as outdated.
This comment was marked as outdated.
It might be a bit early to work on this PR now. It's best to let us agree on which method to choose in the end first, to be able to use the best method and avoid wasting some time. |
This is why this PR is a "draft". I think a (draft) PR is the better place to discuss and try out concrete solutions, because it literally contains the code changes. I felt the issue was getting too long and into too much detail. But if you want, we can continue the discussion there. |
This comment was marked as outdated.
This comment was marked as outdated.
a2656af
to
c5b62b3
Compare
c5b62b3
to
367a6e4
Compare
When babel-helpers are inlined, classPrivateFieldLooseKey() defines a global variable `id` and initialized it to zero in every generated file. This can lead to name clashes between #-private members in subclass and superclass, which again leads to incorrect semantics. In the new solution, instead of trying to produce a globally unique private property name string, a Symbol is created. The helper function classPrivateFieldLooseKey() is no longer used, but kept for backwards-compatibility. In environments where Symbol is not available (Internet Explorer), a polyfill must be included. For details and discussion of other solutions, see babel#15389 For easier review, the test output updates follow in a separate commit.
In all test output files (output.js and output.mjs), replaced 'babelHelpers.classPrivateFieldLooseKey' by 'Symbol'. Tests are green.
367a6e4
to
34245f2
Compare
All tests are green, ready for review. |
@liuxingbaoyu what do you think? In the discussion in the corresponding issue, it seemed to me that everyone agrees that emitting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My take on this PR is that we can try it out (see if anyone reports this after it's released, as it's technically a breaking change)
Even though there are two approvals now, if you want to review it! @nicolo-ribaudo
@@ -1,4 +1,4 @@ | |||
var _privateMethod = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("privateMethod"); | |||
var _privateMethod = /*#__PURE__*/Symbol("privateMethod"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like /*#__PURE__*/
is no longer needed.
https://github.com/babel/babel/pull/15393/files#diff-328fd0199b81baa72df150181152575e627b62d082281c6b5ac8f0075a7174b6R88
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just about to remove the PURE comment for the new Symbol
output and wondered whether it is needed for the existing standard solution, which generates new WeakMap()
also with a PURE annotation. Shouldn't optimizer tools like Uglify know that creating a (built-in) WeakMap
has no side-effects?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on the fence with this. In theory it should be good, but technically it's a breaking change. Maybe we could introduce this under a new We will release a new minor roughly at the end of the week, and we could inclue the new assumption in that version. |
Will the new assumptions be enabled by default? I'm afraid a lot of people haven't noticed it. |
Well, not even the old
|
Sounds good! |
Feel free to do it! |
Feel free to make the changes here! If you need an example of how to define a new assumption option, you can check 7e50ee2. |
I can work on docs for this PR. |
What about the |
Yes. Or we might even just remove the |
More questions: If in 7.21.0/7.220, both |
Since the new approach to introduce a dedicated assumption for using Symbol properties is quite different, I started a fresh branch and created a new PR #15435. I copied all relevant information (e.g. link to documentation PR), I hope I didn't miss anything. Let's continue there. |
When babel-helpers are inlined, the global variable
id
was defined and initialized to zero in every generated file. This could lead to name clashes between #-private members in subclass and superclass, which leads to incorrect semantics.The new solution uses
Symbol
s to create a unique private slot that cannot name-clash. I assume that in old environments that don't supportSymbol
natively, (babel-)polyfills must be loaded.For details and discussion of other solutions, see #15389.