From e5b751723670a36a6c7903c2009460576a7af6a5 Mon Sep 17 00:00:00 2001 From: Christine Abernathy Date: Wed, 22 Jun 2016 07:00:29 -0700 Subject: [PATCH] Handle optional param types when parsing jsdoc format --- src/utils/__tests__/parseJsDoc-test.js | 16 ++++++++++++++++ src/utils/parseJsDoc.js | 11 ++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/utils/__tests__/parseJsDoc-test.js b/src/utils/__tests__/parseJsDoc-test.js index 36d02a7dbaf..44afcfc0ffe 100644 --- a/src/utils/__tests__/parseJsDoc-test.js +++ b/src/utils/__tests__/parseJsDoc-test.js @@ -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', () => { diff --git a/src/utils/parseJsDoc.js b/src/utils/parseJsDoc.js index 9b7ebbae731..5e825e0bccb 100644 --- a/src/utils/parseJsDoc.js +++ b/src/utils/parseJsDoc.js @@ -17,6 +17,7 @@ type JsDoc = { name: string; description: ?string; type: ?{name: string}; + optional?: boolean; }]; returns: ?{ description: ?string; @@ -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. @@ -57,6 +65,7 @@ function getParamsJsDoc(jsDoc) { name: tag.name, description: tag.description, type: getType(tag), + optional: getOptional(tag), }; }); }