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 0c58c2e commit 3d06565
Show file tree
Hide file tree
Showing 31 changed files with 438 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,67 @@
import syntaxPlugin from "@babel/plugin-syntax-private-property-in-object";
import { injectInitialization } from "@babel/helper-create-class-features-plugin";

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

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 (isStatic) {
if (!outerClass.node.id) {
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);

const constructor = outerClass
.get("body.body")
.find(el => el.isClassMethod({ kind: "constructor" }));

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

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

path.replaceWith(
template.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,9 @@
function fn() {
return new class {
#priv;

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

return new (_brandCheck = new WeakSet(), class {
constructor() {
_brandCheck.add(this);
}

#priv;

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,14 @@
var _FooBrandCheck = new WeakSet();

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

#foo = 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,34 @@
var _FooBrandCheck = new WeakSet();

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

#foo = 1;
#bar = 1;

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

class Nested {
constructor() {
_NestedBrandCheck.add(this);
}

#bar = 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,29 @@
var _FooBrandCheck = new WeakSet();

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

#foo = 1;

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

class Nested {
constructor() {
_NestedBrandCheck.add(this);
}

#foo = 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,21 @@
var _FooBrandCheck = new WeakSet();

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

#foo = 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,0 +1,7 @@
class Foo {
static get #foo() {}

test(other) {
return #foo in other;
}
}
@@ -0,0 +1,8 @@
class Foo {
static get #foo() {}

test(other) {
return Foo === other;
}

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

test(other) {
return #foo in other;
}
}
@@ -0,0 +1,8 @@
class Foo {
static #foo = 1;

test(other) {
return Foo === other;
}

}

0 comments on commit 3d06565

Please sign in to comment.