Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions declarations/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ declare module '@babel/types' {

declare class BooleanLiteralTypeAnnotation extends Node {
type: 'BooleanLiteralTypeAnnotation';
value: boolean;
}

declare class NullLiteralTypeAnnotation extends Node {
Expand Down
2 changes: 1 addition & 1 deletion src/collector/declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ function extractCommonjsNamedExternals<+T: Node>(nodes: T[], path: string): Exte

function processExportNamedDeclaration(ctx: Context, node: ExportNamedDeclaration) {
if (isDeclaration(node.declaration)) {
node.declaration.leadingComments = node.leadingComments;
const reference = processDeclaration(ctx, node.declaration);

ctx.provide(reference, reference);
}

Expand Down
37 changes: 31 additions & 6 deletions src/collector/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import wu from 'wu';

// @see flow#5376.
import type {
ArrayTypeAnnotation, ClassDeclaration, ClassProperty, Comment, FlowTypeAnnotation,
Node, ArrayTypeAnnotation, ClassDeclaration, ClassProperty, Comment, FlowTypeAnnotation,
GenericTypeAnnotation, InterfaceDeclaration, IntersectionTypeAnnotation, TypeAlias,
UnionTypeAnnotation, NullableTypeAnnotation, ObjectTypeIndexer, ObjectTypeProperty,
StringLiteralTypeAnnotation, ObjectTypeAnnotation, AnyTypeAnnotation, MixedTypeAnnotation,
Expand All @@ -31,12 +31,28 @@ import {invariant} from '../utils';

function processTypeAlias(ctx: Context, node: TypeAlias | DeclareTypeAlias) {
const {name} = node.id;
const type = makeType(ctx, node.right);

// TODO: support function aliases.
invariant(type);
if (name != 'integer') {
// Forward declaration for the recursive types
ctx.define(name, t.createAny());

const type = makeType(ctx, node.right);
addComment(node, type);

ctx.define(name, type);
// TODO: support function aliases.
invariant(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.
Expand All @@ -46,6 +62,7 @@ function processInterfaceDeclaration(
) {
const {name} = node.id;
const type = makeType(ctx, node.body);
addComment(node, type);

invariant(type);

Expand Down Expand Up @@ -108,6 +125,8 @@ function makeType(ctx: Context, node: FlowTypeAnnotation): ?Type {
return t.createLiteral(null);
case 'BooleanTypeAnnotation':
return t.createBoolean();
case 'BooleanLiteralTypeAnnotation':
return t.createLiteral(node.value);
case 'NumberTypeAnnotation':
return t.createNumber('f64');
case 'StringTypeAnnotation':
Expand Down Expand Up @@ -135,7 +154,7 @@ function makeType(ctx: Context, node: FlowTypeAnnotation): ?Type {
case 'MixedTypeAnnotation':
return t.createMixed();
case 'FunctionTypeAnnotation':
return null;
return t.createAny();
default:
invariant(false, `Unknown node: ${node.type}`);
}
Expand Down Expand Up @@ -207,6 +226,7 @@ function makeField(ctx: Context, node: ObjectTypeProperty | ClassProperty): ?Fie
invariant(value);

type = makeType(ctx, value);
addComment(node, type);
}

if (!type) {
Expand All @@ -230,12 +250,14 @@ function makeField(ctx: Context, node: ObjectTypeProperty | ClassProperty): ?Fie
function makeMap(ctx: Context, node: ObjectTypeIndexer): ?MapType {
const keys = makeType(ctx, node.key);
const values = makeType(ctx, node.value);
addComment(node, values);

return keys && values ? t.createMap(keys, values) : null;
}

function makeArray(ctx: Context, node: ArrayTypeAnnotation): ?ArrayType {
const items = makeType(ctx, node.elementType);
addComment(node, items);

return items != null ? t.createArray(items) : null;
}
Expand Down Expand Up @@ -276,6 +298,9 @@ function makeIntersection(ctx: Context, node: IntersectionTypeAnnotation): ?Type

function makeReference(ctx: Context, node: GenericTypeAnnotation): ?Type {
const {name} = node.id;
if (name == 'integer') {
return t.createNumber('i64');
}
const params = node.typeParameters
&& wu(node.typeParameters.params).map(n => makeType(ctx, n)).toArray();

Expand Down
2 changes: 1 addition & 1 deletion src/collector/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default class Scope {

if (declared) {
invariant(decl);
invariant(decl.kind === 'declaration');
invariant(decl.kind === 'declaration' || decl.kind === 'definition');
} else {
invariant(!decl);
}
Expand Down
17 changes: 15 additions & 2 deletions src/generators/jsonSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type Schema = boolean | {
id?: string,
$ref?: string,
$schema?: string,
$comment?: string,
title?: string,
description?: string,
default?: mixed,
Expand Down Expand Up @@ -45,6 +46,18 @@ export type Schema = boolean | {
};

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) {
return {
type: 'null',
Expand Down Expand Up @@ -90,13 +103,13 @@ function convert(fund: Fund, type: ?Type): Schema {
};
case 'union':
const enumerate = wu(type.variants)
.filter(variant => variant.kind === 'literal')
.filter(variant => variant.kind === 'literal' && variant.value !== null)
.map(literal => (literal: $FlowFixMe).value)
.tap(value => invariant(value !== undefined))
.toArray();

const schemas = wu(type.variants)
.filter(variant => variant.kind !== 'literal')
.filter(variant => variant.kind !== 'literal' || variant.value === null)
.map(variant => convert(fund, variant))
.toArray();

Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type TypeId = string[];

export type BaseType = {
id?: TypeId,
comment?: string,
};

export type RecordType = BaseType & {
Expand Down