documentation/test/lib/flow_doctrine.js
Tom MacWright 7730af7603 Implement flow type inference
cc @thejameskyle

`lib/flow_doctrine.js` is where we convert Babel-parsed
Flow annotations into doctrine-style objects so they
can be formatted with all of the existing helpers.
2015-10-06 10:31:54 -04:00

118 lines
3.0 KiB
JavaScript

'use strict';
var flowDoctrine = require('../../lib/flow_doctrine.js'),
parse = require('../../lib/parsers/javascript'),
test = require('tap').test;
function toComment(fn, filename) {
return parse({
file: filename,
source: fn instanceof Function ? '(' + fn.toString() + ')' : fn
})[0];
}
/* eslint-disable */
test('flowDoctrine', function (t) {
t.deepEqual(flowDoctrine(toComment(
"/** add */function add(a: number) { }"
).context.ast.value.params[0].typeAnnotation.typeAnnotation),
{
type: 'NameExpression',
name: 'number'
}, 'number');
t.deepEqual(flowDoctrine(toComment(
"/** add */function add(a: string) { }"
).context.ast.value.params[0].typeAnnotation.typeAnnotation),
{
type: 'NameExpression',
name: 'string'
}, 'string');
t.deepEqual(flowDoctrine(toComment(
"/** add */function add(a: any) { }"
).context.ast.value.params[0].typeAnnotation.typeAnnotation),
{
type: 'AllLiteral'
}, 'all');
t.deepEqual(flowDoctrine(toComment(
"/** add */function add(a: ?number) { }"
).context.ast.value.params[0].typeAnnotation.typeAnnotation),
{
type: 'OptionalType',
expression: {
type: 'NameExpression',
name: 'number'
}
}, 'optional');
t.deepEqual(flowDoctrine(toComment(
"/** add */function add(a: number | string) { }"
).context.ast.value.params[0].typeAnnotation.typeAnnotation),
{
type: 'UnionType',
elements: [
{
type: 'NameExpression',
name: 'number'
},
{
type: 'NameExpression',
name: 'string'
}
]
}, 'union');
t.deepEqual(flowDoctrine(toComment(
"/** add */function add(a: Object) { }"
).context.ast.value.params[0].typeAnnotation.typeAnnotation),
{
type: 'NameExpression',
name: 'Object'
}, 'object');
t.deepEqual(flowDoctrine(toComment(
"/** add */function add(a: Array) { }"
).context.ast.value.params[0].typeAnnotation.typeAnnotation),
{
type: 'NameExpression',
name: 'Array'
}, 'array');
t.deepEqual(flowDoctrine(toComment(
"/** add */function add(a: Array<number>) { }"
).context.ast.value.params[0].typeAnnotation.typeAnnotation),
{
type: 'TypeApplication',
expression: {
type: 'NameExpression',
name: 'Array'
},
applications: [{
type: 'NameExpression',
name: 'number'
}]
}, 'Array<number>');
t.deepEqual(flowDoctrine(toComment(
"/** add */function add(a: boolean) { }"
).context.ast.value.params[0].typeAnnotation.typeAnnotation),
{
type: 'NameExpression',
name: 'boolean'
}, 'boolean');
t.deepEqual(flowDoctrine(toComment(
"/** add */function add(a: undefined) { }"
).context.ast.value.params[0].typeAnnotation.typeAnnotation),
{
type: 'NameExpression',
name: 'undefined'
}, 'undefined');
t.end();
});
/* eslint-enable */