Skip to content

Commit a7fda83

Browse files
committed
fix: replace existing propTypes when removeExistingPropTypes
1 parent 92a6b19 commit a7fda83

File tree

7 files changed

+63
-7
lines changed

7 files changed

+63
-7
lines changed

src/injector.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ function plugin(
119119
let needImport = false;
120120
let alreadyImported = false;
121121
let previousPropTypesSource = new Map<string, string>();
122+
let originalPropTypesPath: null | babel.NodePath = null;
122123

123124
return {
124125
visitor: {
@@ -159,9 +160,7 @@ function plugin(
159160
});
160161
}
161162

162-
if (removeExistingPropTypes) {
163-
nodePath.remove();
164-
}
163+
originalPropTypesPath = nodePath;
165164
}
166165
});
167166
},
@@ -312,8 +311,9 @@ function plugin(
312311

313312
mapOfPropTypes.set(placeholder, source);
314313

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)) {
317317
path.insertAfter(babel.template.ast(`export { ${nodeName} };`));
318318
path.insertAfter(babel.template.ast(placeholder));
319319
path.parentPath.replaceWith(path.node);

test/code-order/input.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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;

test/code-order/input.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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;

test/code-order/options.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { TestOptions } from '../types';
2+
3+
const options: TestOptions = {
4+
injector: {
5+
removeExistingPropTypes: true,
6+
},
7+
};
8+
9+
export default options;

test/code-order/output.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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;

test/code-order/output.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}

test/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ for (const testCase of testCases) {
5858
}
5959

6060
let result = '';
61-
// For d.ts-only files we just generate the AST
61+
// For d.ts files we just generate the AST
6262
if (!inputSource) {
63-
result = ttp.generate(ast);
63+
result = ttp.generate(ast, options.generator);
6464
}
6565
// For .tsx? files we transpile them and inject the proptypes
6666
else {

0 commit comments

Comments
 (0)