Skip to content

Commit 4f8eaa1

Browse files
committed
feat(injector): control included props
1 parent d2a978c commit 4f8eaa1

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Diff for: src/generator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface GenerateOptions {
2424
* Control which PropTypes are included in the final result
2525
* @param proptype The current PropType about to be converted to text
2626
*/
27-
shouldInclude?(proptype: t.PropTypeNode): boolean;
27+
shouldInclude?(proptype: t.PropTypeNode): boolean | undefined;
2828
}
2929

3030
/**

Diff for: src/injector.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ export type InjectOptions = {
1515
* to have them removed before injecting the PropTypes
1616
*/
1717
removeExistingPropTypes?: boolean;
18+
/**
19+
* Used to control which props are includes in the result
20+
* @return true to include the prop, false to skip it, or undefined to
21+
* use the default behaviour
22+
* @default includeUnusedProps ? true : data.usedProps.includes(`${data.prop.name}`)
23+
*/
24+
shouldInclude?(data: { prop: t.PropTypeNode; usedProps: string[] }): boolean | undefined;
1825
} & Pick<GenerateOptions, 'sortProptypes' | 'includeJSDoc'>;
1926

2027
/**
@@ -71,6 +78,17 @@ function plugin(
7178
removeExistingPropTypes = false,
7279
} = options;
7380

81+
const shouldInclude: InjectOptions['shouldInclude'] = data => {
82+
if (options.shouldInclude) {
83+
const result = options.shouldInclude(data);
84+
if (result !== undefined) {
85+
return result;
86+
}
87+
}
88+
89+
return includeUnusedProps ? true : data.usedProps.includes(`${data.prop.name}`);
90+
};
91+
7492
let importName = '';
7593
let needImport = false;
7694
let alreadyImported = false;
@@ -257,7 +275,7 @@ function plugin(
257275

258276
const source = generate(props, {
259277
importedName: importName,
260-
shouldInclude: includeUnusedProps ? undefined : prop => usedProps.includes(`${prop.name}`),
278+
shouldInclude: prop => shouldInclude!({ prop, usedProps }),
261279
sortProptypes,
262280
includeJSDoc,
263281
});

0 commit comments

Comments
 (0)