Skip to content

Commit

Permalink
feat: add support for @DefaultValue TSDoc tag
Browse files Browse the repository at this point in the history
  • Loading branch information
jrolfs committed Mar 6, 2023
1 parent 287e701 commit f258f7c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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<StatelessWithDefaultValueAndDescriptionJsDocProps> = props => (
<div>My Property = {props.myProp}</div>
);
12 changes: 12 additions & 0 deletions src/__tests__/data/StatelessWithDefaultValueOnlyJsDoc.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as React from 'react';

export interface StatelessWithDefaultValueOnlyJsDocProps {
/**
* @defaultValue hello
*/
myProp: string;
}
/** StatelessWithDefaultValueOnlyJsDoc description */
export const StatelessWithDefaultValueOnlyJsDoc: React.StatelessComponent<StatelessWithDefaultValueOnlyJsDocProps> = props => (
<div>My Property = {props.myProp}</div>
);
20 changes: 20 additions & 0 deletions src/__tests__/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
10 changes: 8 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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));
}
});
Expand Down

0 comments on commit f258f7c

Please sign in to comment.