Skip to content

Commit

Permalink
Add support for TS declare types to types and generator (#10544)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 5, 2019
1 parent 16e5870 commit d31cab5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 37 deletions.
39 changes: 2 additions & 37 deletions packages/babel-generator/src/generators/classes.js
Expand Up @@ -71,26 +71,8 @@ export function ClassBody(node: Object) {

export function ClassProperty(node: Object) {
this.printJoin(node.decorators, node);
this.tsPrintClassMemberModifiers(node, /* isField */ true);

if (node.accessibility) {
// TS
this.word(node.accessibility);
this.space();
}
if (node.static) {
this.word("static");
this.space();
}
if (node.abstract) {
// TS
this.word("abstract");
this.space();
}
if (node.readonly) {
// TS
this.word("readonly");
this.space();
}
if (node.computed) {
this.token("[");
this.print(node.key, node);
Expand Down Expand Up @@ -148,23 +130,6 @@ export function ClassPrivateMethod(node: Object) {

export function _classMethodHead(node) {
this.printJoin(node.decorators, node);

if (node.accessibility) {
// TS
this.word(node.accessibility);
this.space();
}

if (node.abstract) {
// TS
this.word("abstract");
this.space();
}

if (node.static) {
this.word("static");
this.space();
}

this.tsPrintClassMemberModifiers(node, /* isField */ false);
this._methodHead(node);
}
23 changes: 23 additions & 0 deletions packages/babel-generator/src/generators/typescript.js
Expand Up @@ -550,3 +550,26 @@ export function tsPrintSignatureDeclarationBase(node) {
this.token(")");
this.print(node.typeAnnotation, node);
}

export function tsPrintClassMemberModifiers(node, isField) {
if (isField && node.declare) {
this.word("declare");
this.space();
}
if (node.accessibility) {
this.word(node.accessibility);
this.space();
}
if (node.static) {
this.word("static");
this.space();
}
if (node.abstract) {
this.word("abstract");
this.space();
}
if (isField && node.readonly) {
this.word("readonly");
this.space();
}
}
@@ -0,0 +1,5 @@
class A {
declare foo;
declare bar: string;
declare readonly bax: number;
}
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["typescript", "classProperties"]
}
@@ -0,0 +1,5 @@
class A {
declare foo;
declare bar: string;
declare readonly bax: number;
}
4 changes: 4 additions & 0 deletions packages/babel-types/src/definitions/experimental.js
Expand Up @@ -67,6 +67,10 @@ defineType("ClassProperty", {
validate: assertValueType("boolean"),
optional: true,
},
declare: {
validate: assertValueType("boolean"),
optional: true,
},
},
});

Expand Down

0 comments on commit d31cab5

Please sign in to comment.