Skip to content

Commit 98e2d54

Browse files
Merge pull request #105 from oliviertassinari/flow-fix
fix(flow): use case
2 parents 271b61c + 463d987 commit 98e2d54

File tree

6 files changed

+117
-5
lines changed

6 files changed

+117
-5
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@
2929
"bugs": {
3030
"url": "https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types/issues"
3131
},
32-
"dependencies": {},
32+
"dependencies": {
33+
"babel-traverse": "^6.24.1"
34+
},
3335
"devDependencies": {
3436
"babel-cli": "^6.22.2",
3537
"babel-core": "^6.22.1",
3638
"babel-eslint": "^7.1.1",
39+
"babel-plugin-flow-react-proptypes": "^2.1.3",
40+
"babel-plugin-transform-flow-strip-types": "^6.22.0",
3741
"babel-preset-es2015": "^6.22.0",
3842
"babel-preset-react": "^6.22.0",
3943
"babel-preset-stage-0": "^6.22.0",

src/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// @flow weak
2+
/* eslint-disable global-require, import/no-dynamic-require */
23

4+
import { visitors } from 'babel-traverse';
35
import isAnnotatedForRemoval from './isAnnotatedForRemoval';
46
import isStatelessComponent from './isStatelessComponent';
57
import remove from './remove';
@@ -74,6 +76,15 @@ export default function ({ template, types }) {
7476
libraries: (state.opts.additionalLibraries || []).concat('prop-types'),
7577
};
7678

79+
if (state.opts.plugins) {
80+
const pluginsVisitors = state.opts.plugins.map((pluginName) => {
81+
const plugin = require(pluginName);
82+
return plugin({ template, types }).visitor;
83+
});
84+
85+
programPath.traverse(visitors.merge(pluginsVisitors));
86+
}
87+
7788
// On program start, do an explicit traversal up front for this plugin.
7889
programPath.traverse({
7990
ObjectProperty: {

test/fixtures/flow/actual.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Foo1 extends Component {
2+
props: {
3+
bar1?: string,
4+
};
5+
render() {}
6+
}
7+
8+
type Props2 = {
9+
bar2?: string
10+
}
11+
12+
class Foo2 extends Component {
13+
props: Props2;
14+
render() {}
15+
}
16+
17+
type Props3 = {
18+
bar3?: string,
19+
}
20+
21+
function Foo3(props: Props3) {
22+
return <div />;
23+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"use strict";
2+
3+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4+
5+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6+
7+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
8+
9+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
10+
11+
var Foo1 = function (_Component) {
12+
_inherits(Foo1, _Component);
13+
14+
function Foo1() {
15+
_classCallCheck(this, Foo1);
16+
17+
return _possibleConstructorReturn(this, (Foo1.__proto__ || Object.getPrototypeOf(Foo1)).apply(this, arguments));
18+
}
19+
20+
_createClass(Foo1, [{
21+
key: "render",
22+
value: function render() {}
23+
}]);
24+
25+
return Foo1;
26+
}(Component);
27+
28+
Foo1.propTypes = process.env.NODE_ENV !== "production" ? {
29+
bar1: require("prop-types").string
30+
} : {};
31+
32+
var Foo2 = function (_Component2) {
33+
_inherits(Foo2, _Component2);
34+
35+
function Foo2() {
36+
_classCallCheck(this, Foo2);
37+
38+
return _possibleConstructorReturn(this, (Foo2.__proto__ || Object.getPrototypeOf(Foo2)).apply(this, arguments));
39+
}
40+
41+
_createClass(Foo2, [{
42+
key: "render",
43+
value: function render() {}
44+
}]);
45+
46+
return Foo2;
47+
}(Component);
48+
49+
Foo2.propTypes = process.env.NODE_ENV !== "production" ? {
50+
bar2: require("prop-types").string
51+
} : {};
52+
53+
54+
function Foo3(props) {
55+
return React.createElement("div", null);
56+
}
57+
Foo3.propTypes = process.env.NODE_ENV !== "production" ? {
58+
bar3: require("prop-types").string
59+
} : {};

test/fixtures/flow/options.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"plugins": [
3+
"babel-plugin-flow-react-proptypes",
4+
"babel-plugin-transform-flow-strip-types"
5+
]
6+
}

yarn.lock

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ babel-code-frame@^6.16.0, babel-code-frame@^6.22.0:
163163
esutils "^2.0.2"
164164
js-tokens "^3.0.0"
165165

166-
babel-core@^6.22.1, babel-core@^6.24.1:
166+
babel-core@^6.18.0, babel-core@^6.22.1, babel-core@^6.24.1:
167167
version "6.24.1"
168168
resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.24.1.tgz#8c428564dce1e1f41fb337ec34f4c3b022b5ad83"
169169
dependencies:
@@ -347,6 +347,15 @@ babel-plugin-check-es2015-constants@^6.22.0:
347347
dependencies:
348348
babel-runtime "^6.22.0"
349349

350+
babel-plugin-flow-react-proptypes@^2.1.3:
351+
version "2.1.3"
352+
resolved "https://registry.yarnpkg.com/babel-plugin-flow-react-proptypes/-/babel-plugin-flow-react-proptypes-2.1.3.tgz#6db20e985baadf409ef3698b7f4645bb96761250"
353+
dependencies:
354+
babel-core "^6.18.0"
355+
babel-template "^6.16.0"
356+
babel-traverse "^6.18.0"
357+
babel-types "^6.18.0"
358+
350359
babel-plugin-syntax-async-functions@^6.8.0:
351360
version "6.13.0"
352361
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95"
@@ -806,7 +815,7 @@ babel-runtime@^6.18.0, babel-runtime@^6.22.0:
806815
core-js "^2.4.0"
807816
regenerator-runtime "^0.10.0"
808817

809-
babel-template@^6.24.1:
818+
babel-template@^6.16.0, babel-template@^6.24.1:
810819
version "6.24.1"
811820
resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.24.1.tgz#04ae514f1f93b3a2537f2a0f60a5a45fb8308333"
812821
dependencies:
@@ -816,7 +825,7 @@ babel-template@^6.24.1:
816825
babylon "^6.11.0"
817826
lodash "^4.2.0"
818827

819-
babel-traverse@^6.23.1, babel-traverse@^6.24.1:
828+
babel-traverse@^6.18.0, babel-traverse@^6.23.1, babel-traverse@^6.24.1:
820829
version "6.24.1"
821830
resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.24.1.tgz#ab36673fd356f9a0948659e7b338d5feadb31695"
822831
dependencies:
@@ -830,7 +839,7 @@ babel-traverse@^6.23.1, babel-traverse@^6.24.1:
830839
invariant "^2.2.0"
831840
lodash "^4.2.0"
832841

833-
babel-types@^6.19.0, babel-types@^6.23.0, babel-types@^6.24.1:
842+
babel-types@^6.18.0, babel-types@^6.19.0, babel-types@^6.23.0, babel-types@^6.24.1:
834843
version "6.24.1"
835844
resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.24.1.tgz#a136879dc15b3606bda0d90c1fc74304c2ff0975"
836845
dependencies:

0 commit comments

Comments
 (0)