Add support for "declare (type|interface|class)"
parent
57603cf0ff
commit
b4244fc196
|
@ -5,13 +5,14 @@ import type {
|
||||||
Block, ClassDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration, Identifier,
|
Block, ClassDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration, Identifier,
|
||||||
ImportDeclaration, ImportDefaultSpecifier, ImportSpecifier, InterfaceDeclaration,
|
ImportDeclaration, ImportDefaultSpecifier, ImportSpecifier, InterfaceDeclaration,
|
||||||
Node, TypeAlias, TypeParameterDeclaration, VariableDeclaration, VariableDeclarator,
|
Node, TypeAlias, TypeParameterDeclaration, VariableDeclaration, VariableDeclarator,
|
||||||
|
DeclareTypeAlias, DeclareInterface, DeclareClass,
|
||||||
} from '@babel/types';
|
} from '@babel/types';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
isCallExpression, isClassDeclaration, isClassMethod, isExportDefaultDeclaration,
|
isCallExpression, isClassDeclaration, isClassMethod, isExportDefaultDeclaration, isProgram,
|
||||||
isExportNamedDeclaration, isIdentifier, isImportDeclaration, isImportNamespaceSpecifier,
|
isExportNamedDeclaration, isIdentifier, isImportDeclaration, isImportNamespaceSpecifier,
|
||||||
isImportSpecifier, isInterfaceDeclaration, isObjectPattern, isObjectProperty, isProgram,
|
isImportSpecifier, isInterfaceDeclaration, isObjectPattern, isObjectProperty, isDeclareClass,
|
||||||
isStringLiteral, isTypeAlias, isVariableDeclaration,
|
isStringLiteral, isTypeAlias, isVariableDeclaration, isDeclareTypeAlias, isDeclareInterface,
|
||||||
} from '@babel/types';
|
} from '@babel/types';
|
||||||
|
|
||||||
import {invariant} from '../utils';
|
import {invariant} from '../utils';
|
||||||
|
@ -194,10 +195,12 @@ function processExportDefaultDeclaration(ctx: Context, node: ExportDefaultDeclar
|
||||||
* TODO: support "declare ..." form.
|
* TODO: support "declare ..." form.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type Declaration = TypeAlias | InterfaceDeclaration | ClassDeclaration;
|
type Declaration = TypeAlias | InterfaceDeclaration | ClassDeclaration
|
||||||
|
| DeclareTypeAlias | DeclareInterface | DeclareClass;
|
||||||
|
|
||||||
function isDeclaration(node: mixed): boolean %checks {
|
function isDeclaration(node: mixed): boolean %checks {
|
||||||
return isTypeAlias(node) || isInterfaceDeclaration(node) || isClassDeclaration(node);
|
return isTypeAlias(node) || isInterfaceDeclaration(node) || isClassDeclaration(node)
|
||||||
|
|| isDeclareTypeAlias(node) || isDeclareInterface(node) || isDeclareClass(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
function processDeclaration(ctx: Context, node: Declaration) {
|
function processDeclaration(ctx: Context, node: Declaration) {
|
||||||
|
|
|
@ -6,7 +6,7 @@ import type {
|
||||||
GenericTypeAnnotation, InterfaceDeclaration, IntersectionTypeAnnotation, TypeAlias,
|
GenericTypeAnnotation, InterfaceDeclaration, IntersectionTypeAnnotation, TypeAlias,
|
||||||
UnionTypeAnnotation, NullableTypeAnnotation, ObjectTypeIndexer, ObjectTypeProperty,
|
UnionTypeAnnotation, NullableTypeAnnotation, ObjectTypeIndexer, ObjectTypeProperty,
|
||||||
StringLiteralTypeAnnotation, ObjectTypeAnnotation, AnyTypeAnnotation, MixedTypeAnnotation,
|
StringLiteralTypeAnnotation, ObjectTypeAnnotation, AnyTypeAnnotation, MixedTypeAnnotation,
|
||||||
TupleTypeAnnotation,
|
TupleTypeAnnotation, DeclareTypeAlias, DeclareInterface, DeclareClass,
|
||||||
} from '@babel/types';
|
} from '@babel/types';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
@ -26,7 +26,7 @@ import {extractPragmas} from './pragmas';
|
||||||
|
|
||||||
import {invariant} from '../utils';
|
import {invariant} from '../utils';
|
||||||
|
|
||||||
function processTypeAlias(ctx: Context, node: TypeAlias) {
|
function processTypeAlias(ctx: Context, node: TypeAlias | DeclareTypeAlias) {
|
||||||
const {name} = node.id;
|
const {name} = node.id;
|
||||||
const type = makeType(ctx, node.right);
|
const type = makeType(ctx, node.right);
|
||||||
|
|
||||||
|
@ -37,7 +37,10 @@ function processTypeAlias(ctx: Context, node: TypeAlias) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: type params.
|
// TODO: type params.
|
||||||
function processInterfaceDeclaration(ctx: Context, node: InterfaceDeclaration) {
|
function processInterfaceDeclaration(
|
||||||
|
ctx: Context,
|
||||||
|
node: InterfaceDeclaration | DeclareInterface | DeclareClass
|
||||||
|
) {
|
||||||
const {name} = node.id;
|
const {name} = node.id;
|
||||||
const type = makeType(ctx, node.body);
|
const type = makeType(ctx, node.body);
|
||||||
|
|
||||||
|
@ -282,6 +285,9 @@ function makeReference(ctx: Context, node: GenericTypeAnnotation): ?Type {
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
TypeAlias: processTypeAlias,
|
TypeAlias: processTypeAlias,
|
||||||
|
DeclareTypeAlias: processTypeAlias,
|
||||||
InterfaceDeclaration: processInterfaceDeclaration,
|
InterfaceDeclaration: processInterfaceDeclaration,
|
||||||
|
DeclareInterface: processInterfaceDeclaration,
|
||||||
ClassDeclaration: processClassDeclaration,
|
ClassDeclaration: processClassDeclaration,
|
||||||
|
DeclareClass: processInterfaceDeclaration,
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
declare type Type = {
|
||||||
|
a: string,
|
||||||
|
};
|
||||||
|
|
||||||
|
declare interface Interface {
|
||||||
|
a: Type;
|
||||||
|
};
|
||||||
|
|
||||||
|
declare class Class {
|
||||||
|
a: Interface;
|
||||||
|
}
|
||||||
|
|
||||||
|
export {Type, Interface, Class};
|
|
@ -0,0 +1,22 @@
|
||||||
|
- kind: record
|
||||||
|
fields:
|
||||||
|
- name: a
|
||||||
|
value: {kind: string}
|
||||||
|
required: true
|
||||||
|
id: [declare, Type]
|
||||||
|
- kind: record
|
||||||
|
fields:
|
||||||
|
- name: a
|
||||||
|
value:
|
||||||
|
kind: reference
|
||||||
|
to: [declare, Type]
|
||||||
|
required: true
|
||||||
|
id: [declare, Interface]
|
||||||
|
- kind: record
|
||||||
|
fields:
|
||||||
|
- name: a
|
||||||
|
value:
|
||||||
|
kind: reference
|
||||||
|
to: [declare, Interface]
|
||||||
|
required: true
|
||||||
|
id: [declare, Class]
|
Loading…
Reference in New Issue