File tree Expand file tree Collapse file tree 7 files changed +63
-7
lines changed Expand file tree Collapse file tree 7 files changed +63
-7
lines changed Original file line number Diff line number Diff line change @@ -119,6 +119,7 @@ function plugin(
119
119
let needImport = false ;
120
120
let alreadyImported = false ;
121
121
let previousPropTypesSource = new Map < string , string > ( ) ;
122
+ let originalPropTypesPath : null | babel . NodePath = null ;
122
123
123
124
return {
124
125
visitor : {
@@ -159,9 +160,7 @@ function plugin(
159
160
} ) ;
160
161
}
161
162
162
- if ( removeExistingPropTypes ) {
163
- nodePath . remove ( ) ;
164
- }
163
+ originalPropTypesPath = nodePath ;
165
164
}
166
165
} ) ;
167
166
} ,
@@ -312,8 +311,9 @@ function plugin(
312
311
313
312
mapOfPropTypes . set ( placeholder , source ) ;
314
313
315
- // Insert prop types
316
- if ( babelTypes . isExportNamedDeclaration ( path . parent ) ) {
314
+ if ( removeExistingPropTypes && originalPropTypesPath !== null ) {
315
+ originalPropTypesPath . replaceWith ( babel . template . ast ( placeholder ) as babelTypes . Statement ) ;
316
+ } else if ( babelTypes . isExportNamedDeclaration ( path . parent ) ) {
317
317
path . insertAfter ( babel . template . ast ( `export { ${ nodeName } };` ) ) ;
318
318
path . insertAfter ( babel . template . ast ( placeholder ) ) ;
319
319
path . parentPath . replaceWith ( path . node ) ;
Original file line number Diff line number Diff line change
1
+ import * as React from 'react' ;
2
+
3
+ export interface Props {
4
+ value : unknown ;
5
+ }
6
+
7
+ export default function Component ( props : Props ) : JSX . Element ;
Original file line number Diff line number Diff line change
1
+ import React from 'react' ;
2
+ import PropTypes from 'prop-types' ;
3
+
4
+ function Component ( props ) {
5
+ const { value } = props ;
6
+ return < div > { value } </ div > ;
7
+ }
8
+
9
+ const someValidator = ( ) => new Error ( ) ;
10
+
11
+ Component . propTypes = {
12
+ value : PropTypes . any ,
13
+ } ;
14
+
15
+ export default Component ;
Original file line number Diff line number Diff line change
1
+ import { TestOptions } from '../types' ;
2
+
3
+ const options : TestOptions = {
4
+ injector : {
5
+ removeExistingPropTypes : true ,
6
+ } ,
7
+ } ;
8
+
9
+ export default options ;
Original file line number Diff line number Diff line change
1
+ import React from 'react' ;
2
+ import PropTypes from 'prop-types' ;
3
+
4
+ function Component ( props ) {
5
+ const { value } = props ;
6
+ return < div > { value } </ div > ;
7
+ }
8
+
9
+ const someValidator = ( ) => new Error ( ) ;
10
+
11
+ Component . propTypes = {
12
+ value : PropTypes . any . isRequired ,
13
+ } ;
14
+
15
+ export default Component ;
Original file line number Diff line number Diff line change
1
+ {
2
+ "type" : " ProgramNode" ,
3
+ "body" : [
4
+ {
5
+ "type" : " ComponentNode" ,
6
+ "name" : " Component" ,
7
+ "types" : [{ "type" : " PropTypeNode" , "name" : " value" , "propType" : { "type" : " AnyNode" } }]
8
+ }
9
+ ]
10
+ }
Original file line number Diff line number Diff line change @@ -58,9 +58,9 @@ for (const testCase of testCases) {
58
58
}
59
59
60
60
let result = '' ;
61
- // For d.ts-only files we just generate the AST
61
+ // For d.ts files we just generate the AST
62
62
if ( ! inputSource ) {
63
- result = ttp . generate ( ast ) ;
63
+ result = ttp . generate ( ast , options . generator ) ;
64
64
}
65
65
// For .tsx? files we transpile them and inject the proptypes
66
66
else {
You can’t perform that action at this time.
0 commit comments