Skip to content

Commit d2a978c

Browse files
committed
feat(injector): remove existing proptypes
1 parent d576597 commit d2a978c

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

Diff for: src/injector.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ export type InjectOptions = {
1010
* Set this to true to include them instead.
1111
*/
1212
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;
1318
} & Pick<GenerateOptions, 'sortProptypes' | 'includeJSDoc'>;
1419

1520
/**
@@ -59,7 +64,12 @@ function plugin(
5964
options: InjectOptions = {},
6065
mapOfPropTypes: Map<string, string>,
6166
): 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;
6373

6474
let importName = '';
6575
let needImport = false;
@@ -84,6 +94,20 @@ function plugin(
8494
) {
8595
importName = 'PropTypes';
8696
}
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+
}
87111
},
88112
exit(path) {
89113
if (alreadyImported || !needImport) return;

0 commit comments

Comments
 (0)