mirror of
https://github.com/ritz078/transform.git
synced 2026-11-21 16:43:56 +03:00
Support for react-immutable-proptypes (#14)
This commit is contained in:
1 parent
64ae142e1d
commit
7bbf1fbc00
2 files changed
+110
-1
No files matched your search
+10
-1
@@ -1,5 +1,6 @@
|
||||
import React, { PureComponent } from "react";
|
||||
import plugin from "../utils/babel-plugin-js-to-prop-types";
|
||||
import propsPlugin from "../utils/babel-plugin-js-to-prop-types";
|
||||
import immutablePropsPlugin from "../utils/babel-plugin-js-to-immutable-prop-types";
|
||||
import Layout from "../components/Layout";
|
||||
import { transform } from "babel-standalone";
|
||||
import defaultText from "../utils/dummy-json";
|
||||
@@ -7,8 +8,13 @@ import defaultText from "../utils/dummy-json";
|
||||
import ConversionPanel from "../components/ConversionPanel";
|
||||
|
||||
export default class Main extends PureComponent {
|
||||
state = {
|
||||
isImmutable: false
|
||||
};
|
||||
|
||||
getTransformedValue = newValue => {
|
||||
newValue = "const propTypes = " + newValue;
|
||||
const plugin = this.state.isImmutable ? immutablePropsPlugin : propsPlugin;
|
||||
const { code } = transform(newValue, {
|
||||
presets: ["es2015"],
|
||||
plugins: [plugin]
|
||||
@@ -27,6 +33,9 @@ export default class Main extends PureComponent {
|
||||
getTransformedValue={this.getTransformedValue}
|
||||
name={"prop_types"}
|
||||
defaultText={defaultText}
|
||||
onCheckboxChange={(checked, cb) =>
|
||||
this.setState({ isImmutable: checked }, cb)}
|
||||
checkboxText="Immutable Proptypes"
|
||||
/>
|
||||
</Layout>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
import uniqBy from "lodash/uniqBy";
|
||||
import isEmpty from "lodash/isEmpty";
|
||||
|
||||
/**
|
||||
* This is a babel plugin that converts an object to ImmutablePropTypes
|
||||
* @param t
|
||||
* @returns {{visitor: {ArrayExpression: (function(*)), NullLiteral: (function(*)), StringLiteral: (function(*)), NumericLiteral: (function(*)), ArrowFunctionExpression: (function(*)), FunctionExpression: (function(*)), BooleanLiteral: (function(*)), Program: (function(*, *)), ObjectExpression: (function(*))}}}
|
||||
*/
|
||||
export default function({ types: t }) {
|
||||
return {
|
||||
visitor: {
|
||||
ArrayExpression(path) {
|
||||
if (t.isCallExpression(path.parent)) return;
|
||||
let uniqueElements = uniqBy(path.node.elements, "type");
|
||||
|
||||
if (t.isObjectExpression(uniqueElements[0])) {
|
||||
uniqueElements = [
|
||||
t.callExpression(
|
||||
t.memberExpression(
|
||||
t.identifier("ImmutablePropTypes"),
|
||||
t.identifier("mapContains")
|
||||
),
|
||||
uniqueElements
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
if (
|
||||
(!isEmpty(uniqueElements) && uniqueElements.length === 1) ||
|
||||
t.isCallExpression(uniqueElements)
|
||||
) {
|
||||
path.replaceWith(
|
||||
t.callExpression(
|
||||
t.memberExpression(
|
||||
t.identifier("ImmutablePropTypes"),
|
||||
t.identifier("listOf")
|
||||
),
|
||||
uniqueElements
|
||||
)
|
||||
);
|
||||
} else {
|
||||
path.replaceWith(t.identifier("ImmutablePropTypes.list"));
|
||||
}
|
||||
},
|
||||
|
||||
NullLiteral(path) {
|
||||
path.replaceWith(t.identifier("PropTypes.any"));
|
||||
},
|
||||
|
||||
StringLiteral(path) {
|
||||
if (path.parentKey === "key") return;
|
||||
path.replaceWith(t.identifier(`PropTypes.string`));
|
||||
},
|
||||
|
||||
NumericLiteral(path) {
|
||||
path.replaceWith(t.identifier("PropTypes.number"));
|
||||
},
|
||||
|
||||
ArrowFunctionExpression(path) {
|
||||
path.replaceWith(t.identifier("PropTypes.func"));
|
||||
},
|
||||
|
||||
FunctionExpression(path) {
|
||||
path.replaceWith(t.identifier("PropTypes.func"));
|
||||
},
|
||||
|
||||
BooleanLiteral(path) {
|
||||
path.replaceWith(t.identifier("PropTypes.bool"));
|
||||
},
|
||||
|
||||
Program(path) {
|
||||
path.traverse({
|
||||
enter(path) {
|
||||
t.removeComments(path.node);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
ObjectExpression(path) {
|
||||
if (
|
||||
t.isVariableDeclarator(path.parent) ||
|
||||
t.isCallExpression(path.parent)
|
||||
)
|
||||
return;
|
||||
|
||||
const elements = [path.node];
|
||||
|
||||
path.replaceWith(
|
||||
t.callExpression(
|
||||
t.memberExpression(
|
||||
t.identifier("ImmutablePropTypes"),
|
||||
t.identifier("mapContains")
|
||||
),
|
||||
elements
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user