From f258f7cfc8c4ed0b641d489b6afa0f4575f9bf8a Mon Sep 17 00:00:00 2001 From: Jamie Rolfs Date: Mon, 6 Mar 2023 11:56:40 -0800 Subject: [PATCH] feat: add support for @defaultValue TSDoc tag --- ...essWithDefaultValueAndDescriptionJsDoc.tsx | 14 +++++++++++++ .../StatelessWithDefaultValueOnlyJsDoc.tsx | 12 +++++++++++ src/__tests__/parser.ts | 20 +++++++++++++++++++ src/parser.ts | 10 ++++++++-- 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/__tests__/data/StatelessWithDefaultValueAndDescriptionJsDoc.tsx create mode 100644 src/__tests__/data/StatelessWithDefaultValueOnlyJsDoc.tsx diff --git a/src/__tests__/data/StatelessWithDefaultValueAndDescriptionJsDoc.tsx b/src/__tests__/data/StatelessWithDefaultValueAndDescriptionJsDoc.tsx new file mode 100644 index 00000000..829ce31c --- /dev/null +++ b/src/__tests__/data/StatelessWithDefaultValueAndDescriptionJsDoc.tsx @@ -0,0 +1,14 @@ +import * as React from 'react'; + +export interface StatelessWithDefaultValueAndDescriptionJsDocProps { + /** + * The content + * + * @defaultValue hello + */ + myProp: string; +} +/** StatelessWithDefaultValueAndDescriptionJsDoc description */ +export const StatelessWithDefaultValueAndDescriptionJsDoc: React.StatelessComponent = props => ( +
My Property = {props.myProp}
+); diff --git a/src/__tests__/data/StatelessWithDefaultValueOnlyJsDoc.tsx b/src/__tests__/data/StatelessWithDefaultValueOnlyJsDoc.tsx new file mode 100644 index 00000000..d8d533f3 --- /dev/null +++ b/src/__tests__/data/StatelessWithDefaultValueOnlyJsDoc.tsx @@ -0,0 +1,12 @@ +import * as React from 'react'; + +export interface StatelessWithDefaultValueOnlyJsDocProps { + /** + * @defaultValue hello + */ + myProp: string; +} +/** StatelessWithDefaultValueOnlyJsDoc description */ +export const StatelessWithDefaultValueOnlyJsDoc: React.StatelessComponent = props => ( +
My Property = {props.myProp}
+); diff --git a/src/__tests__/parser.ts b/src/__tests__/parser.ts index fab7b73e..259fa8e5 100644 --- a/src/__tests__/parser.ts +++ b/src/__tests__/parser.ts @@ -747,6 +747,26 @@ describe('parser', () => { }); }); + it('should parse jsdocs with the @defaultValue tag and no description', () => { + check('StatelessWithDefaultValueOnlyJsDoc', { + StatelessWithDefaultValueOnlyJsDoc: { + myProp: { defaultValue: 'hello', description: '', type: 'string' } + } + }); + }); + + it('should parse jsdocs with the @defaultValue tag and description', () => { + check('StatelessWithDefaultValueAndDescriptionJsDoc', { + StatelessWithDefaultValueAndDescriptionJsDoc: { + myProp: { + defaultValue: 'hello', + description: 'The content', + type: 'string' + } + } + }); + }); + it('should parse functional component component defined as function', () => { check('FunctionDeclaration', { Jumbotron: { diff --git a/src/parser.ts b/src/parser.ts index 47a86e5d..0d933e41 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -704,6 +704,8 @@ export class Parser { if (hasCodeBasedDefault) { defaultValue = { value: defaultProps[propName] }; + } else if (jsDocComment.tags.defaultValue) { + defaultValue = { value: jsDocComment.tags.defaultValue }; } else if (jsDocComment.tags.default) { defaultValue = { value: jsDocComment.tags.default }; } @@ -751,7 +753,11 @@ export class Parser { public findDocComment(symbol: ts.Symbol): JSDoc { const comment = this.getFullJsDocComment(symbol); - if (comment.fullComment || comment.tags.default) { + if ( + comment.fullComment || + comment.tags.default || + comment.tags.defaultValue + ) { return comment; } @@ -799,7 +805,7 @@ export class Parser { ? currentValue + '\n' + trimmedText : trimmedText; - if (['default', 'type'].indexOf(tag.name) < 0) { + if (['default', 'type', 'defaultValue'].indexOf(tag.name) < 0) { tagComments.push(formatTag(tag)); } });