Skip to content

Commit

Permalink
Added 'replace' mode, to convert "View.propTypes = ..." to "View.prop…
Browse files Browse the repository at this point in the history
…Types = process.env.NODE_ENV === 'production' ? {} : <...>"
  • Loading branch information
ndbroadbent committed Apr 24, 2017
1 parent 5b91d1f commit 18edb32
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
34 changes: 33 additions & 1 deletion src/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 18edb32

Please sign in to comment.