Skip to content

Commit 9ce8ff2

Browse files
Merge pull request #108 from oliviertassinari/issue-106
fix: more aggressive remove-proptypes exploration
2 parents 9de2a21 + e740c04 commit 9ce8ff2

File tree

6 files changed

+71
-13
lines changed

6 files changed

+71
-13
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"babel-cli": "^6.22.2",
3737
"babel-core": "^6.22.1",
3838
"babel-eslint": "^7.1.1",
39+
"babel-generator": "^6.24.1",
3940
"babel-plugin-flow-react-proptypes": "^2.1.3",
4041
"babel-plugin-transform-flow-strip-types": "^6.22.0",
4142
"babel-preset-es2015": "^6.22.0",

src/index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/* eslint-disable global-require, import/no-dynamic-require */
33

44
import { visitors } from 'babel-traverse';
5+
// import generate from 'babel-generator';
6+
// console.log(generate(node).code);
57
import isAnnotatedForRemoval from './isAnnotatedForRemoval';
68
import isStatelessComponent from './isStatelessComponent';
79
import remove from './remove';
@@ -138,27 +140,27 @@ export default function ({ template, types }) {
138140
return;
139141
}
140142

143+
const forceRemoval = isAnnotatedForRemoval(path.node.left);
144+
145+
if (forceRemoval) {
146+
remove(path, globalOptions, { type: 'assign' });
147+
}
148+
141149
const className = node.left.object.name;
142150
const binding = scope.getBinding(className);
143151

144152
if (!binding) {
145153
return;
146154
}
147155

148-
const forceRemoval = isAnnotatedForRemoval(path.node.left);
149-
150156
if (binding.path.isClassDeclaration()) {
151157
const superClass = binding.path.get('superClass');
152158

153-
if (isReactClass(superClass, scope) || forceRemoval) {
154-
remove(path, globalOptions, {
155-
type: 'class assign',
156-
});
159+
if (isReactClass(superClass, scope)) {
160+
remove(path, globalOptions, { type: 'assign' });
157161
}
158-
} else if (isStatelessComponent(binding.path) || forceRemoval) {
159-
remove(path, globalOptions, {
160-
type: 'stateless',
161-
});
162+
} else if (isStatelessComponent(binding.path)) {
163+
remove(path, globalOptions, { type: 'assign' });
162164
}
163165
},
164166
});

src/remove.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ export default function remove(path, globalOptions, options) {
7878
break;
7979
}
8080

81-
case 'class assign':
82-
case 'stateless':
81+
case 'assign':
8382
if (mode === 'unsafe-wrap') {
8483
path.replaceWith(unsafeWrapTemplate(
8584
{

test/fixtures.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('fixtures', () => {
2121
const fixtureDir = path.join(fixturesDir, caseName);
2222

2323
// Only run a specific test
24-
// if (caseName !== 'create-class') {
24+
// if (caseName !== 'issue-106') {
2525
// return;
2626
// }
2727

test/fixtures/issue-106/actual.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { Component, createElement } from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
class RouteNode extends Component {
5+
render() {
6+
const { previousRoute, route } = this.state;
7+
return createElement(RouteSegment, Object.assign({}, this.props, route, previousRoute));
8+
}
9+
}
10+
11+
const storeName = 'storeName';
12+
13+
RouteNode.wrappedComponent.propTypes /* remove-proptypes */ = {
14+
[storeName]: PropTypes.object.isRequired,
15+
};
16+
17+
class BaseLink extends Component {
18+
render() {
19+
return React.createElement('a', { href, className, onClick }, this.props.children);
20+
}
21+
}
22+
23+
BaseLink.propTypes = {
24+
routeOptions: PropTypes.object,
25+
[storeName]: PropTypes.object,
26+
route: PropTypes.object,
27+
['previousRoute']: PropTypes.object,
28+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { Component, createElement } from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
class RouteNode extends Component {
5+
render() {
6+
const { previousRoute, route } = this.state;
7+
return createElement(RouteSegment, Object.assign({}, this.props, route, previousRoute));
8+
}
9+
}
10+
11+
const storeName = 'storeName';
12+
13+
RouteNode.wrappedComponent.propTypes /* remove-proptypes */ = process.env.NODE_ENV !== "production" ? {
14+
[storeName]: PropTypes.object.isRequired
15+
} : {};
16+
17+
class BaseLink extends Component {
18+
render() {
19+
return React.createElement('a', { href, className, onClick }, this.props.children);
20+
}
21+
}
22+
23+
BaseLink.propTypes = process.env.NODE_ENV !== "production" ? {
24+
routeOptions: PropTypes.object,
25+
[storeName]: PropTypes.object,
26+
route: PropTypes.object,
27+
['previousRoute']: PropTypes.object
28+
} : {};

0 commit comments

Comments
 (0)