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

feat: Implement privateFieldsAsSymbols assumption for classes #15435

Merged
merged 6 commits into from Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/babel-core/src/config/validation/options.ts
Expand Up @@ -277,6 +277,7 @@ const knownAssumptions = [
"noIncompleteNsImportDetection",
"noNewArrows",
"objectRestNoSymbols",
"privateFieldsAsSymbols",
"privateFieldsAsProperties",
"pureGetters",
"setClassMethods",
Expand Down
Expand Up @@ -58,6 +58,7 @@ export function buildPrivateNamesMap(props: PropPath[]) {
export function buildPrivateNamesNodes(
privateNamesMap: PrivateNamesMap,
privateFieldsAsProperties: boolean,
privateFieldsAsSymbols: boolean,
state: File,
) {
const initNodes: t.Statement[] = [];
Expand All @@ -67,6 +68,9 @@ export function buildPrivateNamesNodes(
// both static and instance fields are transpiled using a
// secret non-enumerable property. Hence, we also need to generate that
// key (using the classPrivateFieldLooseKey helper).
// When the privateFieldsAsSymbols assumption is enabled,
// both static and instance fields are transpiled using a
// secret Symbol to define a non-enumerable property.
// In spec mode, only instance fields need a "private name" initializer
// because static fields are directly assigned to a variable in the
// buildPrivateStaticFieldInitSpec function.
Expand All @@ -80,15 +84,18 @@ export function buildPrivateNamesNodes(
init = t.callExpression(state.addHelper("classPrivateFieldLooseKey"), [
t.stringLiteral(name),
]);
annotateAsPure(init);
} else if (privateFieldsAsSymbols) {
init = t.callExpression(t.identifier("Symbol"), [t.stringLiteral(name)]);
} else if (!isStatic) {
init = t.newExpression(
t.identifier(!isMethod || isAccessor ? "WeakMap" : "WeakSet"),
[],
);
annotateAsPure(init);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is like before, but while at it, does new WeakMap() / new WeakSet() really need a PURE annotation?

Copy link
Member

Choose a reason for hiding this comment

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

Oh actually, it looks like the PURE annotations is also needed for symbols:
Terser playground with minifying let x = /*#__PURE__*/ Symbol("x");let y = Symbol("y");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since this has the same effect as new Symbol("y"), I can see why.

}

if (init) {
annotateAsPure(init);
initNodes.push(template.statement.ast`var ${id} = ${init}`);
}
}
Expand Down
27 changes: 19 additions & 8 deletions packages/babel-helper-create-class-features-plugin/src/index.ts
@@ -1,5 +1,6 @@
import { types as t } from "@babel/core";
import type { PluginAPI, PluginObject } from "@babel/core";
import type { AssumptionName } from "@babel/core/src/config/validation/options";
import type { NodePath } from "@babel/traverse";
import nameFunction from "@babel/helper-function-name";
import splitExportDeclaration from "@babel/helper-split-export-declaration";
Expand Down Expand Up @@ -48,30 +49,38 @@ export function createClassFeaturePlugin({
inherits,
}: Options): PluginObject {
const setPublicClassFields = api.assumption("setPublicClassFields");
const privateFieldsAsSymbols = api.assumption("privateFieldsAsSymbols");
const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties");
const privateFieldsAsSymbolsOrProperties =
privateFieldsAsProperties || privateFieldsAsSymbols;
const constantSuper = api.assumption("constantSuper");
const noDocumentAll = api.assumption("noDocumentAll");

if (loose === true) {
const explicit = [];
const explicit: AssumptionName[] = [];

if (setPublicClassFields !== undefined) {
explicit.push(`"setPublicClassFields"`);
explicit.push("setPublicClassFields");
}
if (privateFieldsAsProperties !== undefined) {
explicit.push(`"privateFieldsAsProperties"`);
explicit.push("privateFieldsAsProperties");
}
if (privateFieldsAsSymbols !== undefined) {
explicit.push("privateFieldsAsSymbols");
}
if (explicit.length !== 0) {
console.warn(
`[${name}]: You are using the "loose: true" option and you are` +
` explicitly setting a value for the ${explicit.join(" and ")}` +
` explicitly setting a value for the ${explicit
.map(assumptionName => `"${assumptionName}"`)
.join(" and ")}` +
` assumption${explicit.length > 1 ? "s" : ""}. The "loose" option` +
` can cause incompatibilities with the other class features` +
` plugins, so it's recommended that you replace it with the` +
` following top-level option:\n` +
`\t"assumptions": {\n` +
`\t\t"setPublicClassFields": true,\n` +
`\t\t"privateFieldsAsProperties": true\n` +
`\t\t"privateFieldsAsSymbols": true\n` +
`\t}`,
);
}
Expand Down Expand Up @@ -191,6 +200,7 @@ export function createClassFeaturePlugin({
const privateNamesNodes = buildPrivateNamesNodes(
privateNamesMap,
privateFieldsAsProperties ?? loose,
privateFieldsAsSymbols ?? false,
file,
);

Expand All @@ -199,7 +209,8 @@ export function createClassFeaturePlugin({
path,
privateNamesMap,
{
privateFieldsAsProperties: privateFieldsAsProperties ?? loose,
privateFieldsAsProperties:
privateFieldsAsSymbolsOrProperties ?? loose,
noDocumentAll,
innerBinding,
},
Expand Down Expand Up @@ -231,7 +242,7 @@ export function createClassFeaturePlugin({
privateNamesMap,
file,
setPublicClassFields ?? loose,
privateFieldsAsProperties ?? loose,
privateFieldsAsSymbolsOrProperties ?? loose,
constantSuper ?? loose,
innerBinding,
));
Expand All @@ -246,7 +257,7 @@ export function createClassFeaturePlugin({
privateNamesMap,
file,
setPublicClassFields ?? loose,
privateFieldsAsProperties ?? loose,
privateFieldsAsSymbolsOrProperties ?? loose,
constantSuper ?? loose,
innerBinding,
));
Expand Down
@@ -1,5 +1,5 @@
[proposal-class-properties]: You are using the "loose: true" option and you are explicitly setting a value for the "setPublicClassFields" assumption. The "loose" option can cause incompatibilities with the other class features plugins, so it's recommended that you replace it with the following top-level option:
"assumptions": {
"setPublicClassFields": true,
"privateFieldsAsProperties": true
"privateFieldsAsSymbols": true
}
@@ -0,0 +1,5 @@
class X {
#privateMethod() {
return 42;
}
}
@@ -0,0 +1,7 @@
{
"plugins": [["proposal-private-methods", { "loose": false }]],
"assumptions": {
"setPublicClassFields": true,
"privateFieldsAsSymbols": true
}
}
@@ -0,0 +1,11 @@
var _privateMethod = Symbol("privateMethod");
class X {
constructor() {
Object.defineProperty(this, _privateMethod, {
value: _privateMethod2
});
}
}
function _privateMethod2() {
return 42;
}
@@ -1,5 +1,5 @@
[proposal-class-properties]: You are using the "loose: true" option and you are explicitly setting a value for the "setPublicClassFields" assumption. The "loose" option can cause incompatibilities with the other class features plugins, so it's recommended that you replace it with the following top-level option:
"assumptions": {
"setPublicClassFields": true,
"privateFieldsAsProperties": true
"privateFieldsAsSymbols": true
}