Internal slot properties (#7947)

* Support internalSlots to babel-types and babel-generator

* Parsing support for internal slot properties

* Print internal slots in babel-generator

* Add whitespace before first internal slot property
This commit is contained in:
Sam Goldman
2018-05-17 04:48:12 +01:00
committed by Brian Ng
parent 8dcfabd0d7
commit b396cdcbe5
111 changed files with 1232 additions and 10 deletions

View File

@@ -384,6 +384,7 @@ export function ObjectTypeAnnotation(node: Object) {
const props = node.properties.concat(
node.callProperties || [],
node.indexers || [],
node.internalSlots || [],
);
if (props.length) {
@@ -413,6 +414,23 @@ export function ObjectTypeAnnotation(node: Object) {
}
}
export function ObjectTypeInternalSlot(node: Object) {
if (node.static) {
this.word("static");
this.space();
}
this.token("[");
this.token("[");
this.print(node.id, node);
this.token("]");
this.token("]");
if (!node.method) {
this.token(":");
this.space();
}
this.print(node.value, node);
}
export function ObjectTypeCallProperty(node: Object) {
if (node.static) {
this.word("static");

View File

@@ -209,6 +209,22 @@ nodes.ObjectTypeIndexer = function(node: Object, parent): ?WhitespaceObject {
}
};
nodes.ObjectTypeInternalSlot = function(
node: Object,
parent,
): ?WhitespaceObject {
if (
parent.internalSlots[0] === node &&
(!parent.properties || !parent.properties.length) &&
(!parent.callProperties || !parent.callProperties.length) &&
(!parent.indexers || !parent.indexers.length)
) {
return {
before: true,
};
}
};
/**
* Returns lists from node types that need whitespace.
*/

View File

@@ -0,0 +1,6 @@
declare class C { static [[foo]]: T }
declare class C { [[foo]]: T }
interface T { [[foo]]: X }
interface T { [[foo]](): X }
type T = { [[foo]]: X }
type T = { [[foo]](): X }

View File

@@ -0,0 +1,18 @@
declare class C {
static [[foo]]: T
}
declare class C {
[[foo]]: T
}
interface T {
[[foo]]: X
}
interface T {
[[foo]]() => X
}
type T = {
[[foo]]: X
};
type T = {
[[foo]]() => X
};

View File

@@ -304,6 +304,7 @@ describe("programmatic generation", function() {
[t.objectTypeProperty(t.identifier("bar"), t.stringTypeAnnotation())],
null,
null,
null,
);
const output = generate(objectStatement).code;
@@ -317,6 +318,7 @@ describe("programmatic generation", function() {
[t.objectTypeProperty(t.identifier("bar"), t.stringTypeAnnotation())],
null,
null,
null,
true,
);
@@ -336,6 +338,7 @@ describe("programmatic generation", function() {
t.numberTypeAnnotation(),
),
],
null,
);
const output = generate(objectStatement).code;