babel / babel/babel

[beta.55] private fields transform produces unnecessary WeakMaps, and not-private output.

Open
#8,421 18 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
44k
Forks
6k
Avg merge
5d 15h
Merged PRs (30d)
23

Description

## Bug Report

I played around with the new private fields [in the repl](https://babeljs.io/repl/build/master/#?babili=false&browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=MYGwhgzhAEBiD29oG8BQ1oGIBmjoF5oBWdLAIzACcDoA2UgFwFMIGAKAShVI2HgDsI8EEwB0IeAHM2DABYBLCKJyIANNDmLlFSh1IBfVIdR9BDaNhr8mAdziJU2Uc1acgA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&sourceType=module&lineWrap=true&presets=es2015%2Creact%2Cstage-2&prettier=false&targets=&version=7.0.0-beta.55&envVersion=) and discovered some things:

- The fields aren't truly private. The output code can be imported by script tag (for example) and it leaks the private fields into public space.
- They are only "private" strictly to the Babel output code.
- See this [codepen example](https://codepen.io/trusktr/project/editor/82a736a7537498166dac1ad31a5a554c) of public code accessing private fields.
- The `_foo` and `_bar` variables shouldn't be accessible. Babel should expose only the variables explicitly defined in the original source.
- Unnecessary number of WeakMaps are created, one for each field.
- If the fields aren't truly private, there's no need to create `WeakMaps` at all.
- We don't know where users will stick output code (f.e. a `` tag, not necessarily a closure), so output should be truly-private, otherwise the performance hit of making `WeakMap`s is entirely defeated.
- In many cases people might use Node `require` or `Webpack`, which automatically wraps code in a `function` thus creating encapsulation, but this isn't always the case.

See my truly-private implementation idea: https://github.com/babel/proposals/issues/12#issuecomment-410115104

Please feel free to borrow any ideas from there.

Contributor guide

Open the contributing guide

Research direction

Start with the linked Babel REPL example and CodePen, reproduce the beta.55 private-fields transform, and inspect the generated output. Read the linked proposals discussion for the intended privacy semantics. Done means the output does not expose private names and avoids unnecessary per-field WeakMaps.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.