Skip to content

Commit 6b981e0

Browse files
arvtmcw
authored andcommitted
Add the type property to typedef comments (#517)
* Add the type property to typedef comments When using Flow type aliases we didn't add the `type` property to the JSDoc comment for the inferred `typedef`. Fixes #516 * Rename files and update test expectations
1 parent b469bb7 commit 6b981e0

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var fs = require('fs'),
2020
inferMembership = require('./lib/infer/membership'),
2121
inferReturn = require('./lib/infer/return'),
2222
inferAccess = require('./lib/infer/access'),
23+
inferTypedefType = require('./lib/infer/typedef_type'),
2324
formatLint = require('./lib/lint').formatLint,
2425
garbageCollect = require('./lib/garbage_collect'),
2526
lintComments = require('./lib/lint').lintComments,
@@ -169,6 +170,7 @@ function buildSync(indexes, options) {
169170
inferProperties(),
170171
inferReturn(),
171172
inferMembership(),
173+
inferTypedefType(),
172174
nest,
173175
options.github && github,
174176
garbageCollect);
@@ -238,6 +240,7 @@ function lint(indexes, options, callback) {
238240
inferProperties(),
239241
inferReturn(),
240242
inferMembership(),
243+
inferTypedefType(),
241244
nest);
242245

243246
return expandInputs(indexes, options, function (error, inputs) {

lib/infer/typedef_type.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
var shouldSkipInference = require('./should_skip_inference'),
4+
flowDoctrine = require('../flow_doctrine'),
5+
finders = require('./finders');
6+
7+
/**
8+
* Infers the type of typedefs defined by Flow type aliases
9+
*
10+
* @name inferTypedefType
11+
* @param {Object} comment parsed comment
12+
* @returns {Object} comment with type tag inferred
13+
*/
14+
module.exports = function () {
15+
return shouldSkipInference(function inferTypedefType(comment) {
16+
if (comment.kind !== 'typedef') {
17+
return comment;
18+
}
19+
20+
var flowAlias = finders.findTarget(comment.context.ast);
21+
if (flowAlias && flowAlias.type === 'TypeAlias') {
22+
comment.type = flowDoctrine(flowAlias.right);
23+
}
24+
25+
return comment;
26+
});
27+
};

test/fixture/sync/flow-types.output.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@
378378
],
379379
"name": "Point",
380380
"kind": "typedef",
381+
"type": {
382+
"type": "NameExpression",
383+
"name": "Object"
384+
},
381385
"members": {
382386
"instance": [],
383387
"static": []
@@ -501,6 +505,10 @@
501505
}
502506
}
503507
],
508+
"type": {
509+
"type": "NameExpression",
510+
"name": "Object"
511+
},
504512
"members": {
505513
"instance": [],
506514
"static": []
@@ -592,6 +600,19 @@
592600
"errors": [],
593601
"name": "T",
594602
"kind": "typedef",
603+
"type": {
604+
"type": "TypeApplication",
605+
"expression": {
606+
"type": "NameExpression",
607+
"name": "Array"
608+
},
609+
"applications": [
610+
{
611+
"type": "NameExpression",
612+
"name": "string"
613+
}
614+
]
615+
},
595616
"members": {
596617
"instance": [],
597618
"static": []

test/lib/infer/typedef_type.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
var test = require('tap').test,
4+
parse = require('../../../lib/parsers/javascript'),
5+
inferKind = require('../../../lib/infer/kind')(),
6+
inferTypedefType = require('../../../lib/infer/typedef_type')();
7+
8+
function toComment(code) {
9+
return parse({
10+
source: code
11+
})[0];
12+
}
13+
14+
function evaluate(code) {
15+
return inferTypedefType(inferKind(toComment(code)));
16+
}
17+
18+
test('inferTypedefType', function (t) {
19+
t.deepEqual(evaluate(
20+
'/** @typedef {T} V */'
21+
).type, {
22+
name: 'T',
23+
type: 'NameExpression'
24+
});
25+
26+
t.deepEqual(evaluate(
27+
'/** */' +
28+
'type V = T'
29+
).type, {
30+
name: 'T',
31+
type: 'NameExpression'
32+
});
33+
34+
t.deepEqual(evaluate(
35+
'/** @typedef {Array<T>} V */'
36+
).type, {
37+
applications: [
38+
{
39+
name: 'T',
40+
type: 'NameExpression'
41+
}
42+
],
43+
expression: {
44+
name: 'Array',
45+
type: 'NameExpression'
46+
},
47+
type: 'TypeApplication'
48+
});
49+
50+
t.deepEqual(evaluate(
51+
'/** */' +
52+
'type V = Array<T>'
53+
).type, {
54+
applications: [
55+
{
56+
name: 'T',
57+
type: 'NameExpression'
58+
}
59+
],
60+
expression: {
61+
name: 'Array',
62+
type: 'NameExpression'
63+
},
64+
type: 'TypeApplication'
65+
});
66+
67+
68+
t.end();
69+
});

0 commit comments

Comments
 (0)