Skip to content

Commit 83d140b

Browse files
jjowlencioni
authored andcommitted
Fix removing declarator type for unsafe-wrap mode. Fixes #175 (#180)
1 parent 30e6ab8 commit 83d140b

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

src/remove.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,15 @@ export default function remove(path, globalOptions, options) {
105105
break
106106

107107
case 'declarator':
108-
if (mode === 'unsafe-wrap') {
109-
path.replaceWith(
110-
unsafeWrapTemplate({
111-
NODE: path.node,
112-
})
108+
path.replaceWith(
109+
wrapTemplate(
110+
{
111+
LEFT: path.node.id,
112+
RIGHT: path.node.init,
113+
},
114+
{ as: 'variableDeclarator' }
113115
)
114-
} else {
115-
path.replaceWith(
116-
wrapTemplate(
117-
{
118-
LEFT: path.node.id,
119-
RIGHT: path.node.init,
120-
},
121-
{ as: 'variableDeclarator' }
122-
)
123-
)
124-
}
116+
)
125117
path.node[visitedKey] = true
126118
break
127119

test/fixtures.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import babelPluginProposalObjectRestSpread from '@babel/plugin-proposal-object-r
1313
import babelPluginTransformReactRemovePropTypes from '../src/index'
1414
import { trim } from './utils'
1515

16-
const modes = ['options', 'remove-es5', 'wrap-es5', 'remove-es6', 'wrap-es6']
16+
const modes = ['options', 'remove-es5', 'wrap-es5', 'remove-es6', 'wrap-es6', 'unsafe-wrap-es6']
1717

1818
describe('fixtures', () => {
1919
const fixturesDir = path.join(__dirname, 'fixtures')
@@ -123,6 +123,25 @@ describe('fixtures', () => {
123123
}
124124
break
125125

126+
case 'unsafe-wrap-es6':
127+
babelConfig = {
128+
babelrc: false,
129+
plugins: [
130+
babelPluginExternalHelpers,
131+
babelPluginSyntaxJsx,
132+
babelPluginTransformClassProperties,
133+
babelPluginProposalObjectRestSpread,
134+
[
135+
babelPluginTransformReactRemovePropTypes,
136+
{
137+
...options,
138+
mode: 'unsafe-wrap',
139+
},
140+
],
141+
],
142+
}
143+
break
144+
126145
default:
127146
babelConfig = {
128147
plugins: [

test/fixtures/bugfix-175/actual.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
const sharedPropType = PropTypes.number;
5+
6+
export default class Foo extends React.Component {
7+
static propTypes = {
8+
bar: sharedPropType,
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
const sharedPropType = process.env.NODE_ENV !== "production" ? PropTypes.number : {};
4+
export default class Foo extends React.Component {}
5+
process.env.NODE_ENV !== "production" ? Foo.propTypes = {
6+
bar: sharedPropType
7+
} : void 0;

0 commit comments

Comments
 (0)