8000 Mark `WeakMap`s of private fields as pure (#13194) · babel/babel@f30c99a · GitHub
[go: up one dir, main page]

Skip to content

Commit f30c99a

Browse files
Mark WeakMaps of private fields as pure (#13194)
1 parent b4c21c7 commit f30c99a

File tree

242 files changed

+378
-368
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

242 files changed

+378
-368
lines changed

packages/babel-helper-create-class-features-plugin/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"babel-plugin"
1919
],
2020
"dependencies": {
21+
"@babel/helper-annotate-as-pure": "workspace:^7.12.13",
2122
"@babel/helper-function-name": "workspace:^7.12.13",
2223
"@babel/helper-member-expression-to-functions": "workspace:^7.13.0",
2324
"@babel/helper-optimise-call-expression": "workspace:^7.12.13",

packages/babel-helper-create-class-features-plugin/src/fields.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ReplaceSupers, {
44
} from "@babel/helper-replace-supers";
55
import memberExpressionToFunctions from "@babel/helper-member-expression-to-functions";
66
import optimiseCall from "@babel/helper-optimise-call-expression";
7+
import annotateAsPure from "@babel/helper-annotate-as-pure";
78

89
import * as ts from "./typescript";
910

@@ -53,20 +54,23 @@ export function buildPrivateNamesNodes(
5354
const { static: isStatic, method: isMethod, getId, setId } = value;
5455
const isAccessor = getId || setId;
5556
const id = t.cloneNode(value.id);
57+
58+
let init;
59+
5660
if (privateFieldsAsProperties) {
57-
initNodes.push(
58-
template.statement.ast`
59-
var ${id} = ${state.addHelper("classPrivateFieldLooseKey")}("${name}")
60-
`,
61-
);
62-
} else if (isMethod && !isStatic) {
63-
if (isAccessor) {
64-
initNodes.push(template.statement.ast`var ${id} = new WeakMap();`);
65-
} else {
66-
initNodes.push(template.statement.ast`var ${id} = new WeakSet();`);
67-
}
61+
init = t.callExpression(state.addHelper("classPrivateFieldLooseKey"), [
62+
t.stringLiteral(name),
63+
]);
6864
} else if (!isStatic) {
69-
initNodes.push(template.statement.ast`var ${id} = new WeakMap();`);
65+
init = t.newExpression(
66+
t.identifier(!isMethod || isAccessor ? "WeakMap" : "WeakSet"),
67+
[],
68+
);
69+
}
70+
71+
if (init) {
72+
annotateAsPure(init);
73+
initNodes.push(template.statement.ast`var ${id} = ${init}`);
7074
}
7175
}
7276

packages/babel-helper-create-class-features-plugin/test/fixtures/plugin-proposal-private-methods/loose-false/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _privateMethod = new WeakSet();
1+
var _privateMethod = /*#__PURE__*/new WeakSet();
22

33
class X {
44
constructor() {

packages/babel-helper-create-class-features-plugin/test/fixtures/plugin-proposal-private-methods/loose-true/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _privateMethod = babelHelpers.classPrivateFieldLooseKey("privateMethod");
1+
var _privateMethod = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("privateMethod");
22

33
class X {
44
constructor() {

packages/babel-helper-create-class-features-plugin/test/fixtures/replace-supers/method/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _foo = new WeakSet();
1+
var _foo = /*#__PURE__*/new WeakSet();
22

33
class A extends B {
44
constructor(...args) {

packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/test/fixtures/basic/class-private-integration-optional-chaining/output.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _m = new WeakMap();
1+
var _m = /*#__PURE__*/new WeakMap();
22

33
class C {
44
constructor() {

packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/test/fixtures/basic/class-private-integration/output.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _m = new WeakMap();
1+
var _m = /*#__PURE__*/new WeakMap();
22

33
class C {
44
constructor() {

packages/babel-plugin-proposal-async-generator-functions/test/fixtures/async-generators/class-private-method/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _g = new WeakSet();
1+
var _g = /*#__PURE__*/new WeakSet();
22

33
class C {
44
constructor() {

packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/assignment/output.js

Lines changed: 1 addition & 1 deletion
8CF9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _foo = babelHelpers.classPrivateFieldLooseKey("foo");
1+
var _foo = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("foo");
22

33
var Foo = /*#__PURE__*/function () {
44
"use strict";

packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/call/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _foo = babelHelpers.classPrivateFieldLooseKey("foo");
1+
var _foo = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("foo");
22

33
var Foo = /*#__PURE__*/function () {
44
"use strict";

0 commit comments

Comments
 (0)
0