Skip to content

Commit 2c5627e

Browse files
committed
feat(generator): add comment to proptype blocks
1 parent 816fb5c commit 2c5627e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Diff for: src/generator.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ export interface GenerateOptions {
2525
* @param proptype The current PropType about to be converted to text
2626
*/
2727
shouldInclude?(proptype: t.PropTypeNode): boolean | undefined;
28+
29+
/**
30+
* A comment that will be added to the start of the PropTypes code block
31+
* @example
32+
* foo.propTypes = {
33+
* // Comment goes here
34+
* }
35+
*/
36+
comment?: string;
2837
}
2938

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

7584
if (t.isComponentNode(node)) {
76-
return `${node.name}.propTypes = {\n${generate(node.types, options)}\n}`;
85+
const comment =
86+
options.comment &&
87+
`// ${options.comment.split(/\r?\n/gm).reduce((prev, curr) => `${prev}\n// ${curr}`)}\n`;
88+
89+
return `${node.name}.propTypes = {\n${comment ? comment : ''}${generate(
90+
node.types,
91+
options,
92+
)}\n}`;
7793
}
7894

7995
if (t.isPropTypeNode(node)) {

Diff for: src/injector.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type InjectOptions = {
2222
* @default includeUnusedProps ? true : data.usedProps.includes(`${data.prop.name}`)
2323
*/
2424
shouldInclude?(data: { prop: t.PropTypeNode; usedProps: string[] }): boolean | undefined;
25-
} & Pick<GenerateOptions, 'sortProptypes' | 'includeJSDoc'>;
25+
} & Pick<GenerateOptions, 'sortProptypes' | 'includeJSDoc' | 'comment'>;
2626

2727
/**
2828
* Injects the PropTypes from `parse` into the provided JavaScript code

0 commit comments

Comments
 (0)