mirror of
https://github.com/documentationjs/documentation.git
synced 2026-01-25 14:26:29 +00:00
This infers the type from Flow type annotations for variable declarations and class properties. This also moves the logic for setting the `type` for typedefs to the same type inferrer. Also infer the type for statements like: ```js const x = 42; ``` same as: ```js const x: number = 42; ```
48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
var test = require('tap').test,
|
|
parse = require('../../../lib/parsers/javascript'),
|
|
inferParams = require('../../../lib/infer/params')();
|
|
|
|
function toComment(fn, file) {
|
|
return parse({
|
|
file: file,
|
|
source: fn instanceof Function ? '(' + fn.toString() + ')' : fn
|
|
})[0];
|
|
}
|
|
|
|
function evaluate(fn, file) {
|
|
return inferParams(toComment(fn, file));
|
|
}
|
|
|
|
test('inferParams', function (t) {
|
|
t.deepEqual(evaluate(function () {
|
|
/** Test */
|
|
function f(x) {}
|
|
}).params, [{lineNumber: 3, name: 'x', title: 'param'}]);
|
|
|
|
t.deepEqual(evaluate(function () {
|
|
/** Test */
|
|
var f = function (x) {};
|
|
}).params, [{lineNumber: 3, name: 'x', title: 'param'}]);
|
|
|
|
t.deepEqual(evaluate('/** Test */ var f = (x) => {}').params,
|
|
[{lineNumber: 1, name: 'x', title: 'param'}]);
|
|
|
|
t.deepEqual(evaluate(function () {
|
|
var x = 1,
|
|
g = function (y) {},
|
|
/** Test */
|
|
f = function (x) {};
|
|
}).params, [{lineNumber: 5, name: 'x', title: 'param'}]);
|
|
|
|
|
|
t.deepEqual(evaluate('/** Test */ export function f(x) {}').params,
|
|
[{lineNumber: 1, name: 'x', title: 'param'}]);
|
|
|
|
t.deepEqual(evaluate('/** Test */ export default function f(x) {}').params,
|
|
[{lineNumber: 1, name: 'x', title: 'param'}]);
|
|
|
|
t.end();
|
|
});
|