Permalink
Browse files

Added 'replace' mode, to convert "View.propTypes = ..." to "View.prop…

…Types = process.env.NODE_ENV === 'production' ? {} : <...>"
  • Loading branch information...
ndbroadbent committed Apr 24, 2017
1 parent 5b91d1f commit 18edb329aa4fe7ebfd91af6d4b325cf9204e2ae1
Showing with 40 additions and 1 deletion.
  1. +7 −0 README.md
  2. +33 −1 src/remove.js
@@ -118,10 +118,17 @@ if (process.env.NODE_ENV !== "production") {
// ...
}
```
- `replace`:
the `propTypes` will be replaced with an empty object, using the following code:
```js
XYZ.propTypes = process.env.NODE_ENV === "production" ? {} : <...>
```
The `wrap` mode is targeting react libraries like [material-ui](https://github.com/callemall/material-ui).
It's not intended to be used in userland.
`replace` mode fixes some problems when using React Native libraries with [react-native-web](https://github.com/necolas/react-native-web).
### `removeImport`
- `true`: the import statements are removed as well. This option only works if `mode` is set to `remove`:
@@ -35,7 +35,7 @@ export default function remove(path, globalOptions, options) {
} else {
path.remove();
}
} else if (mode === 'wrap') {
} else if (mode === 'wrap' || mode === 'replace') {
// Prevent infinity loop.
if (path.node[visitedKey]) {
return;
@@ -76,6 +76,38 @@ export default function remove(path, globalOptions, options) {
case 'class assign':
case 'stateless':
if (mode === 'replace') {
// Only know how to handle assignments
if (path.node.type !== 'AssignmentExpression') break
// Need this check to avoid an infinite loop
if (path.node.right.type === 'ConditionalExpression' &&
path.node.right.test.type === 'BinaryExpression' &&
path.node.right.test.left.type === 'MemberExpression' &&
path.node.right.test.left.object.type === 'MemberExpression' &&
path.node.right.test.left.object.object.type === 'Identifier' &&
path.node.right.test.left.object.object.name === 'process' &&
path.node.right.test.left.object.property.type === 'Identifier' &&
path.node.right.test.left.object.property.name === 'env') break;
path.replaceWith(
types.assignmentExpression('=',
path.node.left,
types.conditionalExpression(
types.BinaryExpression(
'===',
types.memberExpression(
types.memberExpression(
types.identifier('process'),
types.identifier('env')
),
types.identifier('NODE_ENV')),
types.stringLiteral('production')),
types.objectExpression([]),
path.node.right)))
break;
}
path.replaceWith(wrapperIfTemplate(
{
NODE: path.node,

0 comments on commit 18edb32

Please sign in to comment.