Skip to content

Commit

Permalink
Handle optional param types when parsing jsdoc format (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
Christine Abernathy authored and fkling committed Jun 22, 2016
1 parent 3d576d4 commit 618a898
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/utils/__tests__/parseJsDoc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ describe('parseJsDoc', () => {
});
});

it('extracts jsdoc optional', () => {
const docblock = `
@param {string=} bar
`;
expect(parseJsDoc(docblock)).toEqual({
description: null,
returns: null,
params: [{
name: 'bar',
type: {name: 'string'},
description: null,
optional: true,
}],
});
});

describe('returns', () => {

it('returns null if return is not documented', () => {
Expand Down
11 changes: 10 additions & 1 deletion src/utils/parseJsDoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type JsDoc = {
name: string;
description: ?string;
type: ?{name: string};
optional?: boolean;
}];
returns: ?{
description: ?string;
Expand All @@ -28,7 +29,14 @@ function getType(tag) {
if (!tag.type) {
return null;
}
return {name: tag.type.name};
return {name: tag.type.name ? tag.type.name : tag.type.expression.name};
}

function getOptional(tag) {
if (tag.type && tag.type.type && tag.type.type === 'OptionalType') {
return true;
}
return;
}

// Add jsdoc @return description.
Expand Down Expand Up @@ -57,6 +65,7 @@ function getParamsJsDoc(jsDoc) {
name: tag.name,
description: tag.description,
type: getType(tag),
optional: getOptional(tag),
};
});
}
Expand Down

0 comments on commit 618a898

Please sign in to comment.