Support declare export statements (#5589)

* Add definition of declare export statements

* Add more codecoverage
This commit is contained in:
Daniel Tschinder 2017-06-08 23:15:54 +02:00 committed by GitHub
parent 9b05b0df5c
commit 36ab72f095
6 changed files with 148 additions and 9 deletions

View File

@ -1,3 +1,5 @@
import * as t from "babel-types";
export function AnyTypeAnnotation() {
this.word("any");
}
@ -20,17 +22,21 @@ export function NullLiteralTypeAnnotation() {
this.word("null");
}
export function DeclareClass(node: Object) {
this.word("declare");
this.space();
export function DeclareClass(node: Object, parent: Object) {
if (!t.isDeclareExportDeclaration(parent)) {
this.word("declare");
this.space();
}
this.word("class");
this.space();
this._interfaceish(node);
}
export function DeclareFunction(node: Object) {
this.word("declare");
this.space();
export function DeclareFunction(node: Object, parent: Object) {
if (!t.isDeclareExportDeclaration(parent)) {
this.word("declare");
this.space();
}
this.word("function");
this.space();
this.print(node.id, node);
@ -69,9 +75,11 @@ export function DeclareTypeAlias(node: Object) {
this.TypeAlias(node);
}
export function DeclareVariable(node: Object) {
this.word("declare");
this.space();
export function DeclareVariable(node: Object, parent: Object) {
if (!t.isDeclareExportDeclaration(parent)) {
this.word("declare");
this.space();
}
this.word("var");
this.space();
this.print(node.id, node);
@ -79,6 +87,57 @@ export function DeclareVariable(node: Object) {
this.semicolon();
}
export function DeclareExportDeclaration(node: Object) {
this.word("declare");
this.space();
this.word("export");
this.space();
if (node.default) {
this.word("default");
this.space();
}
FlowExportDeclaration.apply(this, arguments);
}
export function DeclareExportAllDeclaration(node: Object) {
this.word("declare");
this.space();
this.word("export");
this.space();
this.token("*");
this.space();
this.word("from");
this.space();
this.print(node.source, node);
this.semicolon();
}
function FlowExportDeclaration(node: Object) {
if (node.declaration) {
const declar = node.declaration;
this.print(declar, node);
if (!t.isStatement(declar)) this.semicolon();
} else {
this.token("{");
if (node.specifiers.length) {
this.space();
this.printList(node.specifiers, node);
this.space();
}
this.token("}");
if (node.source) {
this.space();
this.word("from");
this.space();
this.print(node.source, node);
}
this.semicolon();
}
}
export function ExistsTypeAnnotation() {
this.token("*");
}

View File

@ -0,0 +1,24 @@
declare export var foo
declare export var foo;
declare export function foo(): void
declare export function foo(): void;
declare export function foo<T>(): void;
declare export function foo(x: number, y: string): void;
declare export class A {}
declare export class A<T> extends B<T> { x: number }
declare export class A { static foo(): number; static x : string }
declare export class A { static [ indexer: number]: string }
declare export class A { static () : number }
declare export class A mixins B<T>, C {}
declare export default class A {}
declare export default function foo(): void;
declare export default string
declare export * from 'asd';
declare export { a, b };
declare export {};
declare export { c, d } from 'bar';
declare module B {
declare export type B = {};
declare export interface Moon {}
}

View File

@ -0,0 +1,24 @@
declare export var foo;
declare export var foo;
declare export function foo(): void;
declare export function foo(): void;
declare export function foo<T>(): void;
declare export function foo(x: number, y: string): void;
declare export class A {}
declare export class A<T> extends B<T> { x: number }
declare export class A { static foo: () => number, static x: string, }
declare export class A { static [indexer: number]: string }
declare export class A { static (): number }
declare export class A mixins B<T>, C {}
declare export default class A {}
declare export default function foo(): void;
declare export default string;
declare export * from 'asd';
declare export { a, b };
declare export {};
declare export { c, d } from 'bar';
declare module B {
declare export type B = {};
declare export interface Moon {}
}

View File

@ -0,0 +1,15 @@
declare export var foo
declare export var foo;
declare export function foo(): void
declare export function foo(): void;
declare export function foo<T>(): void;
declare export function foo(x: number, y: string): void;
declare export class A {}
declare export class A<T> extends B<T> { x: number }
declare export class A { static foo(): number; static x : string }
declare export class A { static [ indexer: number]: string }
declare export class A { static () : number }
declare export class A mixins B<T>, C {}
declare export default class A {}
declare export default function foo(): void;
declare export default string

View File

@ -14,3 +14,4 @@ declare type A = string
declare type T<U> = { [k:string]: U }
declare interface I { foo: string }
declare interface I<T> { foo: T }
declare module.exports: string;

View File

@ -111,6 +111,22 @@ defineType("DeclareVariable", {
},
});
defineType("DeclareExportDeclaration", {
visitor: ["declaration", "specifiers", "source"],
aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"],
fields: {
// todo
},
});
defineType("DeclareExportAllDeclaration", {
visitor: ["source"],
aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"],
fields: {
// todo
},
});
defineType("ExistsTypeAnnotation", {
aliases: ["Flow"],
});