Skip to content

Commit 0edacd7

Browse files
弘树fkling
authored andcommitted
fix jsdoc @param union type support & @returns * all type support (#95)
Fix jsdoc @param union type support & @returns
1 parent bfa26d4 commit 0edacd7

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/utils/__tests__/parseJsDoc-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ describe('parseJsDoc', () => {
6464
});
6565
});
6666

67+
it('extracts jsdoc union type param', () => {
68+
const docblock = `
69+
@param {string|Object} bar
70+
`;
71+
expect(parseJsDoc(docblock)).toEqual({
72+
description: null,
73+
returns: null,
74+
params: [{
75+
name: 'bar',
76+
type: {name: 'union', value: ['string', 'Object']},
77+
description: null,
78+
}],
79+
});
80+
});
81+
6782
it('extracts jsdoc optional', () => {
6883
const docblock = `
6984
@param {string=} bar
@@ -107,6 +122,20 @@ describe('parseJsDoc', () => {
107122
});
108123
});
109124

125+
it('extracts jsdoc mixed types', () => {
126+
const docblock = `
127+
@returns {*}
128+
`;
129+
expect(parseJsDoc(docblock)).toEqual({
130+
description: null,
131+
returns: {
132+
type: {name: 'mixed'},
133+
description: null,
134+
},
135+
params: [],
136+
});
137+
});
138+
110139
it('extracts description from jsdoc', () => {
111140
const docblock = `
112141
@returns The number

src/utils/parseJsDoc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ type JsDoc = {
2828
function getType(tag) {
2929
if (!tag.type) {
3030
return null;
31+
} else if (tag.type.type === 'UnionType') {
32+
// union type
33+
return {name: 'union', value: tag.type.elements.map(function (element) {
34+
return element.name;
35+
})};
36+
} else if (tag.type.type === 'AllLiteral') {
37+
// return {*}
38+
return {name: 'mixed'};
3139
}
3240
return {name: tag.type.name ? tag.type.name : tag.type.expression.name};
3341
}

0 commit comments

Comments
 (0)