Skip to content

Commit 618a898

Browse files
Christine Abernathyfkling
authored andcommitted
Handle optional param types when parsing jsdoc format (#89)
1 parent 3d576d4 commit 618a898

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/utils/__tests__/parseJsDoc-test.js

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

67+
it('extracts jsdoc optional', () => {
68+
const docblock = `
69+
@param {string=} bar
70+
`;
71+
expect(parseJsDoc(docblock)).toEqual({
72+
description: null,
73+
returns: null,
74+
params: [{
75+
name: 'bar',
76+
type: {name: 'string'},
77+
description: null,
78+
optional: true,
79+
}],
80+
});
81+
});
82+
6783
describe('returns', () => {
6884

6985
it('returns null if return is not documented', () => {

src/utils/parseJsDoc.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type JsDoc = {
1717
name: string;
1818
description: ?string;
1919
type: ?{name: string};
20+
optional?: boolean;
2021
}];
2122
returns: ?{
2223
description: ?string;
@@ -28,7 +29,14 @@ function getType(tag) {
2829
if (!tag.type) {
2930
return null;
3031
}
31-
return {name: tag.type.name};
32+
return {name: tag.type.name ? tag.type.name : tag.type.expression.name};
33+
}
34+
35+
function getOptional(tag) {
36+
if (tag.type && tag.type.type && tag.type.type === 'OptionalType') {
37+
return true;
38+
}
39+
return;
3240
}
3341

3442
// Add jsdoc @return description.
@@ -57,6 +65,7 @@ function getParamsJsDoc(jsDoc) {
5765
name: tag.name,
5866
description: tag.description,
5967
type: getType(tag),
68+
optional: getOptional(tag),
6069
};
6170
});
6271
}

0 commit comments

Comments
 (0)