Include leadingComments as $comment

master
Vitaliy Filippov 2019-10-21 20:00:31 +03:00
parent 1d0a098c39
commit e5afdbcb58
4 changed files with 30 additions and 2 deletions

View File

@ -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);
}

View File

@ -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,
@ -37,6 +37,7 @@ function processTypeAlias(ctx: Context, node: TypeAlias | DeclareTypeAlias) {
ctx.define(name, t.createAny());
const type = makeType(ctx, node.right);
addComment(node, type);
// TODO: support function aliases.
invariant(type);
@ -45,6 +46,15 @@ function processTypeAlias(ctx: Context, node: TypeAlias | DeclareTypeAlias) {
}
}
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.
function processInterfaceDeclaration(
ctx: Context,
@ -52,6 +62,7 @@ function processInterfaceDeclaration(
) {
const {name} = node.id;
const type = makeType(ctx, node.body);
addComment(node, type);
invariant(type);
@ -215,6 +226,7 @@ function makeField(ctx: Context, node: ObjectTypeProperty | ClassProperty): ?Fie
invariant(value);
type = makeType(ctx, value);
addComment(node, type);
}
if (!type) {
@ -238,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;
}

View File

@ -12,6 +12,7 @@ export type Schema = boolean | {
id?: string,
$ref?: string,
$schema?: string,
$comment?: string,
title?: string,
description?: string,
default?: mixed,
@ -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',

View File

@ -21,6 +21,7 @@ export type TypeId = string[];
export type BaseType = {
id?: TypeId,
comment?: string,
};
export type RecordType = BaseType & {