Skip to content

Commit

Permalink
Allow compiling #foo in obj without compiling private fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Apr 19, 2021
1 parent 838211b commit 4901045
Show file tree
Hide file tree
Showing 35 changed files with 490 additions and 2 deletions.
Expand Up @@ -18,7 +18,8 @@
],
"dependencies": {
"@babel/helper-create-class-features-plugin": "workspace:^7.13.0",
"@babel/helper-plugin-utils": "workspace:^7.13.0"
"@babel/helper-plugin-utils": "workspace:^7.13.0",
"@babel/plugin-syntax-private-property-in-object": "workspace:^7.13.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
Expand Down
Expand Up @@ -6,15 +6,21 @@ import {
FEATURES,
} from "@babel/helper-create-class-features-plugin";

import pluginPrivateIn from "./native-private-fields";

export default declare((api, options) => {
api.assertVersion(7);

const { loose, nativePrivateFields } = options;

if (nativePrivateFields) return pluginPrivateIn(api);

return createClassFeaturePlugin({
name: "proposal-class-properties",

api,
feature: FEATURES.privateIn,
loose: options.loose,
loose,

manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("privateIn");
Expand Down
@@ -0,0 +1,112 @@
import syntaxPlugin from "@babel/plugin-syntax-private-property-in-object";
import { injectInitialization as injectConstructorInit } from "@babel/helper-create-class-features-plugin";

export default function pluginPrivateIn({ types: t, template }) {
const ids = new WeakMap();

function unshadow(name, targetScope, scope) {
while (scope !== targetScope) {
if (scope.hasOwnBinding(name)) scope.rename(name);
scope = scope.parent;
}
}

function injectInitialization(classPath, init) {
let firstFieldPath;
let consturctorPath;

for (const el of classPath.get("body.body")) {
if (
(el.isClassProperty() || el.isClassPrivateProperty()) &&
!el.node.static
) {
firstFieldPath = el;
break;
}
if (!consturctorPath && el.isClassMethod({ kind: "constructor" })) {
consturctorPath = el;
}
}

if (firstFieldPath) {
if (firstFieldPath.node.value) {
firstFieldPath.set(
"value",
t.sequenceExpression([init, firstFieldPath.node.value]),
);
} else {
firstFieldPath.set("value", t.unaryExpression("void", init));
}
} else {
injectConstructorInit(classPath, consturctorPath, [
t.expressionStatement(init),
]);
}
}

return {
name: "proposal-private-property-in-object",
inherits: syntaxPlugin,
visitor: {
BinaryExpression(path) {
const { node } = path;
if (node.operator !== "in") return;
if (!t.isPrivateName(node.left)) return;

const { name } = node.left.id;

let isStatic;
const outerClass = path.findParent(path => {
if (!path.isClass()) return false;

const privateElement = path.node.body.body.find(
node => t.isPrivate(node) && node.key.id.name === name,
);
if (!privateElement) return false;

isStatic = privateElement.static;
return true;
});

if (outerClass.parentPath.scope.path.isPattern()) {
outerClass.replaceWith(template.ast`(() => ${outerClass.node})()`);
// The injected class will be queued and eventually transformed when visited
return;
}

if (isStatic) {
if (outerClass.node.id) {
unshadow(outerClass.node.id.name, outerClass.scope, path.scope);
} else {
outerClass.set("id", path.scope.generateUidIdentifier("class"));
}
path.replaceWith(
template.expression.ast`
${t.cloneNode(outerClass.node.id)} === ${path.node.right}
`,
);
return;
}

let id = ids.get(outerClass.node);
if (!id) {
id = outerClass.scope.generateUidIdentifier(
`${outerClass.node.id?.name || ""} brandCheck`,
);
ids.set(outerClass.node, id);

injectInitialization(
outerClass,
template.expression.ast`${t.cloneNode(id)}.add(this)`,
);

outerClass.insertBefore(template.ast`var ${id} = new WeakSet()`);
}

path.replaceWith(
template.expression.ast`${t.cloneNode(id)}.has(${path.node.right})`,
);
},
},
};
}
@@ -0,0 +1,7 @@
class Foo {
get #foo() {}

test(other) {
return #foo in other;
}
}
@@ -0,0 +1,14 @@
var _FooBrandCheck = new WeakSet();

class Foo {
constructor() {
_FooBrandCheck.add(this);
}

get #foo() {}

test(other) {
return _FooBrandCheck.has(other);
}

}
@@ -0,0 +1,4 @@
(x = class {
#foo;
test(other) { return #foo in other }
}) => {}
@@ -0,0 +1,12 @@
(x = (() => {
var _brandCheck;

return _brandCheck = new WeakSet(), class {
#foo = void _brandCheck.add(this);

test(other) {
return _brandCheck.has(other);
}

};
})()) => {};
@@ -0,0 +1,9 @@
function fn() {
return new class {
#priv;

method(obj) {
return #priv in obj;
}
}
}
@@ -0,0 +1,12 @@
function fn() {
var _brandCheck;

return new (_brandCheck = new WeakSet(), class {
#priv = void _brandCheck.add(this);

method(obj) {
return _brandCheck.has(obj);
}

})();
}
@@ -0,0 +1,9 @@
function fn() {
return new class {
static #priv;

method(obj) {
return #priv in obj;
}
}
}
@@ -0,0 +1,10 @@
function fn() {
return new class _class {
static #priv;

method(obj) {
return _class === obj;
}

}();
}
@@ -0,0 +1,7 @@
class Foo {
#foo = 1;

test(other) {
return #foo in other;
}
}
@@ -0,0 +1,10 @@
var _FooBrandCheck = new WeakSet();

class Foo {
#foo = (_FooBrandCheck.add(this), 1);

test(other) {
return _FooBrandCheck.has(other);
}

}
@@ -0,0 +1,7 @@
class Foo {
#foo() {}

test(other) {
return #foo in other;
}
}
@@ -0,0 +1,14 @@
var _FooBrandCheck = new WeakSet();

class Foo {
constructor() {
_FooBrandCheck.add(this);
}

#foo() {}

test(other) {
return _FooBrandCheck.has(other);
}

}
@@ -0,0 +1,18 @@
class Foo {
#foo = 1;
#bar = 1;

test() {
class Nested {
#bar = 2;

test() {
#foo in this;
#bar in this;
}
}

#foo in this;
#bar in this;
}
}
@@ -0,0 +1,26 @@
var _FooBrandCheck = new WeakSet();

class Foo {
#foo = (_FooBrandCheck.add(this), 1);
#bar = 1;

test() {
var _NestedBrandCheck = new WeakSet();

class Nested {
#bar = (_NestedBrandCheck.add(this), 2);

test() {
_FooBrandCheck.has(this);

_NestedBrandCheck.has(this);
}

}

_FooBrandCheck.has(this);

_FooBrandCheck.has(this);
}

}
@@ -0,0 +1,15 @@
class Foo {
#foo = 1;

test() {
class Nested {
#foo = 2;

test() {
#foo in this;
}
}

#foo in this;
}
}
@@ -0,0 +1,21 @@
var _FooBrandCheck = new WeakSet();

class Foo {
#foo = (_FooBrandCheck.add(this), 1);

test() {
var _NestedBrandCheck = new WeakSet();

class Nested {
#foo = (_NestedBrandCheck.add(this), 2);

test() {
_NestedBrandCheck.has(this);
}

}

_FooBrandCheck.has(this);
}

}
@@ -0,0 +1,13 @@
class Foo {
#foo = 1;

test() {
class Nested {
test() {
#foo in this;
}
}

#foo in this;
}
}
@@ -0,0 +1,17 @@
var _FooBrandCheck = new WeakSet();

class Foo {
#foo = (_FooBrandCheck.add(this), 1);

test() {
class Nested {
test() {
_FooBrandCheck.has(this);
}

}

_FooBrandCheck.has(this);
}

}
@@ -0,0 +1,6 @@
{
"plugins": [
["proposal-private-property-in-object", { "nativePrivateFields": true }],
"syntax-class-properties"
]
}

0 comments on commit 4901045

Please sign in to comment.