diff --git a/package.json b/package.json index 416edaa..6adcb39 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "dependencies": { "@tsuyoshiwada/htmltojsx": "^0.0.4", "apollo-codegen": "0.17.0-alpha.1", + "babel-plugin-json-to-proptypes": "^0.1.0", "babel-standalone": "^6.26.0", "classnames": "^2.2.5", "codemirror": "^5.29.0", diff --git a/pages/index.js b/pages/index.js index 9565965..03dcc80 100644 --- a/pages/index.js +++ b/pages/index.js @@ -1,10 +1,9 @@ import React, { PureComponent } from "react"; -import propsPlugin from "../utils/babel-plugin-js-to-prop-types"; +import jsonToProptypes from "babel-plugin-json-to-proptypes"; 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"; -import { toValidJSON } from "../utils/json-to-ts-flow"; import merge from "lodash/merge" import ConversionPanel from "../components/ConversionPanel"; @@ -15,17 +14,20 @@ export default class Main extends PureComponent { }; getTransformedValue = newValue => { - let x = JSON.parse(toValidJSON(newValue)) + let x = JSON.parse(newValue) if (typeof x !== "object" || Array.isArray(x)) { x = merge({}, ...x) } x = "const propTypes = " + JSON.stringify(x); - const plugin = this.state.isImmutable ? immutablePropsPlugin : propsPlugin; + const plugin = this.state.isImmutable ? immutablePropsPlugin : jsonToProptypes; const { code } = transform(x, { - presets: ["es2015"], plugins: [plugin] }); - return code; + + + return `import PropTypes from "prop-types"; + + ${code}`; }; render() { diff --git a/utils/babel-plugin-js-to-prop-types.js b/utils/babel-plugin-js-to-prop-types.js deleted file mode 100644 index d17c86a..0000000 --- a/utils/babel-plugin-js-to-prop-types.js +++ /dev/null @@ -1,109 +0,0 @@ -import uniqBy from "lodash/uniqBy"; -import isEmpty from "lodash/isEmpty"; - -/** - * This is a babel plugin that converts an object to PropTypes - * @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("PropTypes"), - t.identifier("shape") - ), - uniqueElements - ) - ]; - } - - if ( - (!isEmpty(uniqueElements) && uniqueElements.length === 1) || - t.isCallExpression(uniqueElements) - ) { - path.replaceWith( - t.callExpression( - t.memberExpression( - t.identifier("PropTypes"), - t.identifier("arrayOf") - ), - uniqueElements - ) - ); - } else { - path.replaceWith(t.identifier("PropTypes.array")); - } - }, - - 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); - } - }); - }, - - VariableDeclaration(path) { - path.insertBefore( - t.identifier( - `import PropTypes from 'prop-types' - ` - ) - ); - }, - - ObjectExpression(path) { - if ( - t.isVariableDeclarator(path.parent) || - t.isCallExpression(path.parent) - ) - return; - - const elements = [path.node]; - - path.replaceWith( - t.callExpression( - t.memberExpression( - t.identifier("PropTypes"), - t.identifier("shape") - ), - elements - ) - ); - } - } - }; -} diff --git a/yarn.lock b/yarn.lock index db10f27..d4cde6b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -459,6 +459,12 @@ babel-plugin-check-es2015-constants@^6.22.0: dependencies: babel-runtime "^6.22.0" +babel-plugin-json-to-proptypes@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/babel-plugin-json-to-proptypes/-/babel-plugin-json-to-proptypes-0.1.0.tgz#a087bdfad142a2c886413fc63754547f2e39abce" + dependencies: + lodash "^4.17.4" + babel-plugin-minify-builtins@^0.1.3: version "0.1.3" resolved "https://registry.yarnpkg.com/babel-plugin-minify-builtins/-/babel-plugin-minify-builtins-0.1.3.tgz#4f21a7dcb51f91a04ea71d47ff0e8e3b05fec021"