Skip to content

Commit 125697b

Browse files
feat(API): support options with the plugings API (#118)
1 parent 142aeaa commit 125697b

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"prebuild": "rm -rf lib/",
1212
"build": "babel src --out-dir lib",
1313
"flow": "flow",
14-
"prepublish": "npm run build && pkgfiles"
14+
"source": "npm run build && pkgfiles"
1515
},
1616
"repository": {
1717
"type": "git",

src/index.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @flow weak
22
/* eslint-disable global-require, import/no-dynamic-require */
33

4-
import { visitors } from 'babel-traverse';
4+
import traverse, { visitors } from 'babel-traverse';
55
// import generate from 'babel-generator';
66
// console.log(generate(node).code);
77
import isAnnotatedForRemoval from './isAnnotatedForRemoval';
@@ -79,12 +79,29 @@ export default function ({ template, types }) {
7979
};
8080

8181
if (state.opts.plugins) {
82-
const pluginsVisitors = state.opts.plugins.map((pluginName) => {
82+
const pluginsState = state;
83+
const pluginsVisitors = state.opts.plugins.map((pluginOpts) => {
84+
const pluginName =
85+
typeof pluginOpts === 'string' ? pluginOpts : pluginOpts[0];
86+
87+
if (typeof pluginOpts !== 'string') {
88+
pluginsState.opts = {
89+
...pluginsState.opts,
90+
...pluginOpts[1],
91+
};
92+
}
93+
8394
const plugin = require(pluginName);
8495
return plugin({ template, types }).visitor;
8596
});
8697

87-
programPath.traverse(visitors.merge(pluginsVisitors));
98+
traverse(
99+
programPath.parent,
100+
visitors.merge(pluginsVisitors),
101+
programPath.scope,
102+
pluginsState,
103+
programPath.parentPath,
104+
);
88105
}
89106

90107
// On program start, do an explicit traversal up front for this plugin.

test/fixtures/flow/actual.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Foo1 extends Component {
55
render() {}
66
}
77

8-
type Props2 = {
8+
export type Props2 = {
99
bar2?: string
1010
}
1111

test/fixtures/flow/expected-wrap-es5.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ var Foo1 = function (_Component) {
2828
Foo1.propTypes = process.env.NODE_ENV !== "production" ? {
2929
bar1: require("prop-types").string
3030
} : {};
31+
var babelPluginFlowReactPropTypes_proptype_Props2 = {
32+
bar2: require("prop-types").string
33+
};
3134

3235
var Foo2 = function (_Component2) {
3336
_inherits(Foo2, _Component2);

test/fixtures/flow/options.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"plugins": [
3-
"babel-plugin-flow-react-proptypes",
3+
["babel-plugin-flow-react-proptypes", {
4+
"omitRuntimeTypeExport": true
5+
}],
46
"babel-plugin-transform-flow-strip-types"
57
]
68
}

0 commit comments

Comments
 (0)