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

Class private properties #6120

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"babel-preset-flow": "7.0.0-alpha.18",
"babel-preset-stage-0": "7.0.0-alpha.18",
"babel-register": "7.0.0-alpha.18",
"babylon": "7.0.0-beta.22",
"babylon": "7.0.0-beta.24",
"browserify": "^13.1.1",
"bundle-collapser": "^1.2.1",
"chai": "^4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"babel-template": "7.0.0-beta.1",
"babel-traverse": "7.0.0-beta.1",
"babel-types": "7.0.0-beta.1",
"babylon": "7.0.0-beta.22",
"babylon": "7.0.0-beta.24",
"convert-source-map": "^1.1.0",
"debug": "^3.0.1",
"json5": "^0.5.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
},
"devDependencies": {
"babel-helper-fixtures": "7.0.0-beta.1",
"babylon": "^7.0.0-beta.22"
"babylon": "^7.0.0-beta.24"
}
}
15 changes: 15 additions & 0 deletions packages/babel-generator/src/generators/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ export function ClassProperty(node: Object) {
this.semicolon();
}

export function ClassPrivateProperty(node: Object) {
if (node.static) {
this.word("static");
this.space();
}
this.print(node.key, node);
if (node.value) {
this.space();
this.token("=");
this.space();
this.print(node.value, node);
}
this.semicolon();
}

export function ClassMethod(node: Object) {
this._classMethodHead(node);
this.space();
Expand Down
5 changes: 5 additions & 0 deletions packages/babel-generator/src/generators/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,8 @@ export function MetaProperty(node: Object) {
this.token(".");
this.print(node.property, node);
}

export function PrivateName(node: Object) {
this.token("#");
this.print(node.id, node);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class Foo {
async;
foo; bar;
foo = 0; bar = 1;

#foo;
#foo = 1;
static #foo;
static #foo = Foo.#foo;
}

class A1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class Foo {
bar;
foo = 0;
bar = 1;
#foo;
#foo = 1;
static #foo;
static #foo = Foo.#foo;
}

class A1 {
Expand Down Expand Up @@ -65,4 +69,4 @@ class A6 {
class A7 {
static get static() {}

}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "plugins": ["classProperties"] }
{ "plugins": ["classProperties", "classPrivateProperties"] }
11 changes: 4 additions & 7 deletions packages/babel-helper-define-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function push(
key = t.toComputedKey(node, node.key);
}

if (t.isObjectProperty(node) || t.isClassProperty(node)) {
if (t.isProperty(node)) {
value = node.value;
} else if (t.isObjectMethod(node) || t.isClassMethod(node)) {
value = t.functionExpression(
Expand Down Expand Up @@ -131,15 +131,12 @@ export function toClassObject(mutatorMap: Object): Object {
const propNode = t.objectProperty(map._key, mapNode, map._computed);

Object.keys(map).forEach(function(key) {
let node = map[key];
const node = map[key];
if (key[0] === "_") return;

const inheritNode = node;
if (t.isClassMethod(node) || t.isClassProperty(node)) node = node.value;

const prop = t.objectProperty(t.identifier(key), node);
t.inheritsComments(prop, inheritNode);
t.removeComments(inheritNode);
t.inheritsComments(prop, node);
t.removeComments(node);

mapNode.properties.push(prop);
});
Expand Down
36 changes: 36 additions & 0 deletions packages/babel-helpers/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,42 @@ helpers.possibleConstructorReturn = defineHelper(`
}
`);

helpers.classPrivateFieldKey = defineHelper(`
var id = 0;
export default function _classPrivateFieldKey(name) {
// Can we use a middle finger emoji?

Choose a reason for hiding this comment

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

😄

return "__private_" + (id++) + "_" + name;
}
`);

helpers.classPrivateFieldBase = defineHelper(`
export default function _classPrivateFieldBase(receiver, privateKey) {
if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {

Choose a reason for hiding this comment

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

In loose mode, do you even want to bother with these hasOwnProperty checks, using defining non-configurable properties rather than using = when setting it up, etc? Seems like public class fields use =.

throw new TypeError("attempted to use private field on non-instance");
}
return receiver;
}
`);

Choose a reason for hiding this comment

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

Looks like you use classPrivateFieldBase/classPrivateFieldKey in the loose mode transform, and classPrivateFieldGet/classPrivateFieldSet in the normal mode. Is that right? Consider adding comments or putting "loose" in the names of the first two to make this more locally apparent.


helpers.classPrivateFieldGet = defineHelper(`
export default function _classPrivateFieldGet(receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
}
`);

helpers.classPrivateFieldPut = defineHelper(`
export default function _classPrivateFieldPut(receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
}
`);

helpers.set = defineHelper(`
export default function _set(object, property, value, receiver) {
var desc = Object.getOwnPropertyDescriptor(object, property);
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-syntax-class-properties/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default function() {
return {
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("classProperties");
parserOpts.plugins.push("classProperties", "classPrivateProperties");
},
};
}
Loading