Compare commits
3 Commits
v0.3.2-pre
...
master
Author | SHA1 | Date |
---|---|---|
Vitaliy Filippov | 6fe28de2dc | |
Vitaliy Filippov | e5afdbcb58 | |
Vitaliy Filippov | 1d0a098c39 |
|
@ -166,8 +166,8 @@ function extractCommonjsNamedExternals<+T: Node>(nodes: T[], path: string): Exte
|
||||||
|
|
||||||
function processExportNamedDeclaration(ctx: Context, node: ExportNamedDeclaration) {
|
function processExportNamedDeclaration(ctx: Context, node: ExportNamedDeclaration) {
|
||||||
if (isDeclaration(node.declaration)) {
|
if (isDeclaration(node.declaration)) {
|
||||||
|
node.declaration.leadingComments = node.leadingComments;
|
||||||
const reference = processDeclaration(ctx, node.declaration);
|
const reference = processDeclaration(ctx, node.declaration);
|
||||||
|
|
||||||
ctx.provide(reference, reference);
|
ctx.provide(reference, reference);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import wu from 'wu';
|
||||||
|
|
||||||
// @see flow#5376.
|
// @see flow#5376.
|
||||||
import type {
|
import type {
|
||||||
ArrayTypeAnnotation, ClassDeclaration, ClassProperty, Comment, FlowTypeAnnotation,
|
Node, ArrayTypeAnnotation, ClassDeclaration, ClassProperty, Comment, FlowTypeAnnotation,
|
||||||
GenericTypeAnnotation, InterfaceDeclaration, IntersectionTypeAnnotation, TypeAlias,
|
GenericTypeAnnotation, InterfaceDeclaration, IntersectionTypeAnnotation, TypeAlias,
|
||||||
UnionTypeAnnotation, NullableTypeAnnotation, ObjectTypeIndexer, ObjectTypeProperty,
|
UnionTypeAnnotation, NullableTypeAnnotation, ObjectTypeIndexer, ObjectTypeProperty,
|
||||||
StringLiteralTypeAnnotation, ObjectTypeAnnotation, AnyTypeAnnotation, MixedTypeAnnotation,
|
StringLiteralTypeAnnotation, ObjectTypeAnnotation, AnyTypeAnnotation, MixedTypeAnnotation,
|
||||||
|
@ -32,15 +32,27 @@ import {invariant} from '../utils';
|
||||||
function processTypeAlias(ctx: Context, node: TypeAlias | DeclareTypeAlias) {
|
function processTypeAlias(ctx: Context, node: TypeAlias | DeclareTypeAlias) {
|
||||||
const {name} = node.id;
|
const {name} = node.id;
|
||||||
|
|
||||||
|
if (name != 'integer') {
|
||||||
// Forward declaration for the recursive types
|
// Forward declaration for the recursive types
|
||||||
ctx.define(name, t.createAny());
|
ctx.define(name, t.createAny());
|
||||||
|
|
||||||
const type = makeType(ctx, node.right);
|
const type = makeType(ctx, node.right);
|
||||||
|
addComment(node, type);
|
||||||
|
|
||||||
// TODO: support function aliases.
|
// TODO: support function aliases.
|
||||||
invariant(type);
|
invariant(type);
|
||||||
|
|
||||||
ctx.define(name, type);
|
ctx.define(name, type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function addComment(node: Node, type: Type) {
|
||||||
|
if (node.leadingComments) {
|
||||||
|
const cmt = node.leadingComments.map(c => c.value).join('\n').trim();
|
||||||
|
if (cmt) {
|
||||||
|
type.comment = cmt;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: type params.
|
// TODO: type params.
|
||||||
|
@ -50,6 +62,7 @@ function processInterfaceDeclaration(
|
||||||
) {
|
) {
|
||||||
const {name} = node.id;
|
const {name} = node.id;
|
||||||
const type = makeType(ctx, node.body);
|
const type = makeType(ctx, node.body);
|
||||||
|
addComment(node, type);
|
||||||
|
|
||||||
invariant(type);
|
invariant(type);
|
||||||
|
|
||||||
|
@ -213,6 +226,7 @@ function makeField(ctx: Context, node: ObjectTypeProperty | ClassProperty): ?Fie
|
||||||
invariant(value);
|
invariant(value);
|
||||||
|
|
||||||
type = makeType(ctx, value);
|
type = makeType(ctx, value);
|
||||||
|
addComment(node, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!type) {
|
if (!type) {
|
||||||
|
@ -236,12 +250,14 @@ function makeField(ctx: Context, node: ObjectTypeProperty | ClassProperty): ?Fie
|
||||||
function makeMap(ctx: Context, node: ObjectTypeIndexer): ?MapType {
|
function makeMap(ctx: Context, node: ObjectTypeIndexer): ?MapType {
|
||||||
const keys = makeType(ctx, node.key);
|
const keys = makeType(ctx, node.key);
|
||||||
const values = makeType(ctx, node.value);
|
const values = makeType(ctx, node.value);
|
||||||
|
addComment(node, values);
|
||||||
|
|
||||||
return keys && values ? t.createMap(keys, values) : null;
|
return keys && values ? t.createMap(keys, values) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeArray(ctx: Context, node: ArrayTypeAnnotation): ?ArrayType {
|
function makeArray(ctx: Context, node: ArrayTypeAnnotation): ?ArrayType {
|
||||||
const items = makeType(ctx, node.elementType);
|
const items = makeType(ctx, node.elementType);
|
||||||
|
addComment(node, items);
|
||||||
|
|
||||||
return items != null ? t.createArray(items) : null;
|
return items != null ? t.createArray(items) : null;
|
||||||
}
|
}
|
||||||
|
@ -282,6 +298,9 @@ function makeIntersection(ctx: Context, node: IntersectionTypeAnnotation): ?Type
|
||||||
|
|
||||||
function makeReference(ctx: Context, node: GenericTypeAnnotation): ?Type {
|
function makeReference(ctx: Context, node: GenericTypeAnnotation): ?Type {
|
||||||
const {name} = node.id;
|
const {name} = node.id;
|
||||||
|
if (name == 'integer') {
|
||||||
|
return t.createNumber('i64');
|
||||||
|
}
|
||||||
const params = node.typeParameters
|
const params = node.typeParameters
|
||||||
&& wu(node.typeParameters.params).map(n => makeType(ctx, n)).toArray();
|
&& wu(node.typeParameters.params).map(n => makeType(ctx, n)).toArray();
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ export type Schema = boolean | {
|
||||||
id?: string,
|
id?: string,
|
||||||
$ref?: string,
|
$ref?: string,
|
||||||
$schema?: string,
|
$schema?: string,
|
||||||
|
$comment?: string,
|
||||||
title?: string,
|
title?: string,
|
||||||
description?: string,
|
description?: string,
|
||||||
default?: mixed,
|
default?: mixed,
|
||||||
|
@ -45,6 +46,18 @@ export type Schema = boolean | {
|
||||||
};
|
};
|
||||||
|
|
||||||
function convert(fund: Fund, type: ?Type): Schema {
|
function convert(fund: Fund, type: ?Type): Schema {
|
||||||
|
let schema = convertType(fund, type);
|
||||||
|
if (type && type.comment) {
|
||||||
|
if (schema === true) {
|
||||||
|
schema = { $comment: type.comment };
|
||||||
|
} else {
|
||||||
|
schema.$comment = type.comment;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return schema;
|
||||||
|
}
|
||||||
|
|
||||||
|
function convertType(fund: Fund, type: ?Type): Schema {
|
||||||
if (!type) {
|
if (!type) {
|
||||||
return {
|
return {
|
||||||
type: 'null',
|
type: 'null',
|
||||||
|
@ -90,13 +103,13 @@ function convert(fund: Fund, type: ?Type): Schema {
|
||||||
};
|
};
|
||||||
case 'union':
|
case 'union':
|
||||||
const enumerate = wu(type.variants)
|
const enumerate = wu(type.variants)
|
||||||
.filter(variant => variant.kind === 'literal')
|
.filter(variant => variant.kind === 'literal' && variant.value !== null)
|
||||||
.map(literal => (literal: $FlowFixMe).value)
|
.map(literal => (literal: $FlowFixMe).value)
|
||||||
.tap(value => invariant(value !== undefined))
|
.tap(value => invariant(value !== undefined))
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
const schemas = wu(type.variants)
|
const schemas = wu(type.variants)
|
||||||
.filter(variant => variant.kind !== 'literal')
|
.filter(variant => variant.kind !== 'literal' || variant.value === null)
|
||||||
.map(variant => convert(fund, variant))
|
.map(variant => convert(fund, variant))
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ export type TypeId = string[];
|
||||||
|
|
||||||
export type BaseType = {
|
export type BaseType = {
|
||||||
id?: TypeId,
|
id?: TypeId,
|
||||||
|
comment?: string,
|
||||||
};
|
};
|
||||||
|
|
||||||
export type RecordType = BaseType & {
|
export type RecordType = BaseType & {
|
||||||
|
|
Loading…
Reference in New Issue