Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle optional param types when parsing jsdoc format #89

Merged
merged 1 commit into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I think this should be return false;, but it's too late now anyway since I already published a new release :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I intentionally return undefined (a little hacky) because I didn't want to always add the property and so as not to break existing tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

option: getOptional(tag) will add the property anyway. I mean, the property will disappear when converted to JSON, but it will still be on the JS object.

}

// 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