Skip to content

Commit

Permalink
feat(generator): add comment to proptype blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Sep 3, 2019
1 parent 816fb5c commit 2c5627e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ export interface GenerateOptions {
* @param proptype The current PropType about to be converted to text
*/
shouldInclude?(proptype: t.PropTypeNode): boolean | undefined;

/**
* A comment that will be added to the start of the PropTypes code block
* @example
* foo.propTypes = {
* // Comment goes here
* }
*/
comment?: string;
}

/**
Expand Down Expand Up @@ -73,7 +82,14 @@ export function generate(node: t.Node | t.PropTypeNode[], options: GenerateOptio
}

if (t.isComponentNode(node)) {
return `${node.name}.propTypes = {\n${generate(node.types, options)}\n}`;
const comment =
options.comment &&
`// ${options.comment.split(/\r?\n/gm).reduce((prev, curr) => `${prev}\n// ${curr}`)}\n`;

return `${node.name}.propTypes = {\n${comment ? comment : ''}${generate(
node.types,
options,
)}\n}`;
}

if (t.isPropTypeNode(node)) {
Expand Down
2 changes: 1 addition & 1 deletion src/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type InjectOptions = {
* @default includeUnusedProps ? true : data.usedProps.includes(`${data.prop.name}`)
*/
shouldInclude?(data: { prop: t.PropTypeNode; usedProps: string[] }): boolean | undefined;
} & Pick<GenerateOptions, 'sortProptypes' | 'includeJSDoc'>;
} & Pick<GenerateOptions, 'sortProptypes' | 'includeJSDoc' | 'comment'>;

/**
* Injects the PropTypes from `parse` into the provided JavaScript code
Expand Down

0 comments on commit 2c5627e

Please sign in to comment.