@@ -31,7 +31,7 @@ export type InjectOptions = {
31
31
* Options passed to babel.transformSync
32
32
*/
33
33
babelOptions ?: babel . TransformOptions ;
34
- } & Pick < GenerateOptions , 'sortProptypes' | 'includeJSDoc' | 'comment' > ;
34
+ } & Pick < GenerateOptions , 'sortProptypes' | 'includeJSDoc' | 'comment' | 'reconcilePropTypes' > ;
35
35
36
36
/**
37
37
* Injects the PropTypes from `parse` into the provided JavaScript code
@@ -84,8 +84,16 @@ function plugin(
84
84
options : InjectOptions = { } ,
85
85
mapOfPropTypes : Map < string , string >
86
86
) : babel . PluginObj {
87
- const { includeUnusedProps = false , removeExistingPropTypes = false , ...otherOptions } = options ;
88
-
87
+ const {
88
+ includeUnusedProps = false ,
89
+ reconcilePropTypes = (
90
+ _prop : t . PropTypeNode ,
91
+ _previous : string | undefined ,
92
+ generated : string
93
+ ) => generated ,
94
+ removeExistingPropTypes = false ,
95
+ ...otherOptions
96
+ } = options ;
89
97
const shouldInclude : Exclude < InjectOptions [ 'shouldInclude' ] , undefined > = ( data ) => {
90
98
if ( options . shouldInclude ) {
91
99
const result = options . shouldInclude ( data ) ;
@@ -101,11 +109,12 @@ function plugin(
101
109
let needImport = false ;
102
110
let alreadyImported = false ;
103
111
let originalPropTypesPath : null | babel . NodePath = null ;
112
+ let previousPropTypesSource = new Map < string , string > ( ) ;
104
113
105
114
return {
106
115
visitor : {
107
116
Program : {
108
- enter ( path ) {
117
+ enter ( path , state : any ) {
109
118
if (
110
119
! path . node . body . some ( ( n ) => {
111
120
if (
@@ -131,6 +140,17 @@ function plugin(
131
140
babelTypes . isIdentifier ( node . expression . left . property , { name : 'propTypes' } )
132
141
) {
133
142
originalPropTypesPath = nodePath ;
143
+
144
+ if ( babelTypes . isObjectExpression ( node . expression . right ) ) {
145
+ const { code } = state . file ;
146
+
147
+ node . expression . right . properties . forEach ( ( property ) => {
148
+ if ( babelTypes . isObjectProperty ( property ) ) {
149
+ const validatorSource = code . slice ( property . value . start , property . value . end ) ;
150
+ previousPropTypesSource . set ( property . key . name , validatorSource ) ;
151
+ }
152
+ } ) ;
153
+ }
134
154
}
135
155
} ) ;
136
156
} ,
@@ -266,6 +286,8 @@ function plugin(
266
286
const source = generate ( props , {
267
287
...otherOptions ,
268
288
importedName : importName ,
289
+ previousPropTypesSource,
290
+ reconcilePropTypes,
269
291
shouldInclude : ( prop ) => shouldInclude ( { component : props , prop, usedProps } ) ,
270
292
} ) ;
271
293
0 commit comments