@@ -15,6 +15,13 @@ export type InjectOptions = {
15
15
* to have them removed before injecting the PropTypes
16
16
*/
17
17
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 ;
18
25
} & Pick < GenerateOptions , 'sortProptypes' | 'includeJSDoc' > ;
19
26
20
27
/**
@@ -71,6 +78,17 @@ function plugin(
71
78
removeExistingPropTypes = false ,
72
79
} = options ;
73
80
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
+
74
92
let importName = '' ;
75
93
let needImport = false ;
76
94
let alreadyImported = false ;
@@ -257,7 +275,7 @@ function plugin(
257
275
258
276
const source = generate ( props , {
259
277
importedName : importName ,
260
- shouldInclude : includeUnusedProps ? undefined : prop => usedProps . includes ( ` ${ prop . name } ` ) ,
278
+ shouldInclude : prop => shouldInclude ! ( { prop, usedProps } ) ,
261
279
sortProptypes,
262
280
includeJSDoc,
263
281
} ) ;
0 commit comments