Add some tests

master
Paul Loyd 2017-11-28 17:22:49 +03:00
parent 2ad3f925eb
commit f56da6f9a8
8 changed files with 95 additions and 5 deletions

View File

@ -251,7 +251,7 @@ function makeArrayType(ctx: Context, node: ArrayTypeAnnotation): ?ArrayType {
};
}
function makeUnionType(ctx: Context, node: UnionTypeAnnotation): ?UnionType {
function makeUnionType(ctx: Context, node: UnionTypeAnnotation): ?Type {
const variants = wu(node.types)
.map(node => makeType(ctx, node))
.filter()
@ -261,6 +261,10 @@ function makeUnionType(ctx: Context, node: UnionTypeAnnotation): ?UnionType {
return null;
}
if (variants.length === 1) {
return variants[0];
}
return {
kind: 'union',
variants,

View File

@ -3,13 +3,16 @@ import type {File} from '@babel/types';
export default class Parser {
parse(code: string): File {
// TODO: customization.
// This parse configuration is intended to be as permissive as possible.
return babylon.parse(code, {
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
sourceType: 'module',
plugins: [ '*', 'jsx', 'flow' ],
// TODO: review other plugins.
plugins: ['*', 'jsx', 'flow', 'classProperties'],
});
}
}

View File

@ -6,4 +6,15 @@ interface Interface {
[string]: boolean;
}
export {Type, Interface};
type Couple = {
[string]: boolean,
[number]: string,
};
type Mix = {
[string]: boolean,
foo: string,
}
export {Type, Interface, Couple, Mix};

View File

@ -11,6 +11,41 @@
"kind": "map",
"keys": {"kind": "string"},
"values": {"kind": "boolean"}
},
{
"id": ["maps", "Couple"],
"kind": "intersection",
"parts": [
{
"kind": "map",
"keys": {"kind": "string"},
"values": {"kind": "boolean"}
},
{
"kind": "map",
"keys": {"kind": "number", "repr": "f64"},
"values": {"kind": "string"}
}
]
},
{
"id": ["maps", "Mix"],
"kind": "intersection",
"parts": [
{
"kind": "record",
"fields": [{
"name": "foo",
"value": {"kind": "string"},
"required": true
}]
},
{
"kind": "map",
"keys": {"kind": "string"},
"values": {"kind": "boolean"}
}
]
}
]
}

View File

@ -5,7 +5,12 @@ type Type = {
b: boolean,
baz: (() => void)[],
bar: () => void,
[string]: () => void,
c: string & () => void;
};
interface Interface {
@ -15,7 +20,14 @@ interface Interface {
b: boolean;
baz: ?() => void;
bar: () => void;
[() => void]: string;
c: string | () => void;
kek: (() => string) & (() => void);
}
class Class {
@ -27,7 +39,11 @@ class Class {
b: boolean;
static baz() {}
baz: () => void;
kek: (() => string) | (() => void);
}
export {Type, Interface, Class};

View File

@ -5,7 +5,8 @@
"kind": "record",
"fields": [
{"name": "a", "value": {"kind": "string"}, "required": true},
{"name": "b", "value": {"kind": "boolean"}, "required": true}
{"name": "b", "value": {"kind": "boolean"}, "required": true},
{"name": "c", "value": {"kind": "string"}, "required": true}
]
},
{
@ -13,7 +14,8 @@
"kind": "record",
"fields": [
{"name": "a", "value": {"kind": "string"}, "required": true},
{"name": "b", "value": {"kind": "boolean"}, "required": true}
{"name": "b", "value": {"kind": "boolean"}, "required": true},
{"name": "c", "value": {"kind": "string"}, "required": true}
]
},
{

View File

@ -0,0 +1,8 @@
class X {
static x: number = 20;
static xx = 20;
xxx: string;
}
export {X};

View File

@ -0,0 +1,11 @@
{
"types": [{
"id": ["skipStatics", "X"],
"kind": "record",
"fields": [{
"name": "xxx",
"value": {"kind": "string"},
"required": true
}]
}]
}