@@ -10,6 +10,11 @@ export type InjectOptions = {
10
10
* Set this to true to include them instead.
11
11
*/
12
12
includeUnusedProps ?: boolean ;
13
+ /**
14
+ * By default existing PropTypes are left alone, set this to true
15
+ * to have them removed before injecting the PropTypes
16
+ */
17
+ removeExistingPropTypes ?: boolean ;
13
18
} & Pick < GenerateOptions , 'sortProptypes' | 'includeJSDoc' > ;
14
19
15
20
/**
@@ -59,7 +64,12 @@ function plugin(
59
64
options : InjectOptions = { } ,
60
65
mapOfPropTypes : Map < string , string > ,
61
66
) : babel . PluginObj {
62
- const { includeUnusedProps = false , includeJSDoc = true , sortProptypes } = options ;
67
+ const {
68
+ includeUnusedProps = false ,
69
+ includeJSDoc = true ,
70
+ sortProptypes,
71
+ removeExistingPropTypes = false ,
72
+ } = options ;
63
73
64
74
let importName = '' ;
65
75
let needImport = false ;
@@ -84,6 +94,20 @@ function plugin(
84
94
) {
85
95
importName = 'PropTypes' ;
86
96
}
97
+
98
+ if ( removeExistingPropTypes ) {
99
+ path . get ( 'body' ) . forEach ( nodePath => {
100
+ const { node } = nodePath ;
101
+ if (
102
+ babelTypes . isExpressionStatement ( node ) &&
103
+ babelTypes . isAssignmentExpression ( node . expression , { operator : '=' } ) &&
104
+ babelTypes . isMemberExpression ( node . expression . left ) &&
105
+ babelTypes . isIdentifier ( node . expression . left . property , { name : 'propTypes' } )
106
+ ) {
107
+ nodePath . remove ( ) ;
108
+ }
109
+ } ) ;
110
+ }
87
111
} ,
88
112
exit ( path ) {
89
113
if ( alreadyImported || ! needImport ) return ;
0 commit comments