diff --git a/pages/index.js b/pages/index.js index 44cac2f..3c6b70f 100644 --- a/pages/index.js +++ b/pages/index.js @@ -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" /> ); diff --git a/utils/babel-plugin-js-to-immutable-prop-types.js b/utils/babel-plugin-js-to-immutable-prop-types.js new file mode 100644 index 0000000..1e6f9a2 --- /dev/null +++ b/utils/babel-plugin-js-to-immutable-prop-types.js @@ -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 + ) + ); + } + } + }; +}