Unify property handling

master
Paul Loyd 2017-11-03 10:41:36 +03:00
parent 29348f75e0
commit 84dd683ac4
2 changed files with 35 additions and 40 deletions

View File

@ -47,22 +47,7 @@ const extractors = {
return null;
}
let type = node.leadingComments && (yield* extractLastPragma(node.leadingComments));
if (!type) {
type = yield node.typeAnnotation;
}
if (type.type === 'record') {
type.namespace = yield namespace();
yield provide(type);
type = type.name;
}
return {
name: yield node.key,
type,
};
return yield* extractProperty(node, node.typeAnnotation);
},
* ClassMethod(node) {
@ -92,30 +77,7 @@ const extractors = {
},
* ObjectTypeProperty(node) {
let type = null;
if (node.leadingComments) {
type = yield* extractLastPragma(node.leadingComments);
}
if (!type) {
type = yield node.value;
}
if (!type) {
return null;
}
if (type.type === 'record') {
type.namespace = yield namespace();
yield provide(type);
type = type.name;
}
return {
name: yield node.key,
type,
};
return yield* extractProperty(node, node.value);
},
* ObjectTypeIndexer(node) {
@ -248,6 +210,33 @@ function* extractLastPragma(comments) {
return pragmas.length > 0 ? pragmas[pragmas.length - 1] : null;
}
function* extractProperty(prop, value) {
let type = null;
if (prop.leadingComments) {
type = yield* extractLastPragma(prop.leadingComments);
}
if (!type) {
type = yield value;
}
if (!type) {
return null;
}
if (type.type === 'record') {
type.namespace = yield namespace();
yield provide(type);
type = type.name;
}
return {
name: yield prop.key,
type,
};
}
function parsePragma(pragma) {
let [type, arg] = pragma.split(/\s+/);

View File

@ -4,6 +4,8 @@ type Type = {
foo(): void,
b: number,
bar: () => void,
};
interface Interface {
@ -12,6 +14,8 @@ interface Interface {
foo(): void;
b: number;
bar: () => void;
}
class Class {
@ -22,6 +26,8 @@ class Class {
set bar(a) {}
b: number;
baz: () => void;
}
// ###