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

Add support for @defaultValue TSDoc tag #473

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