diff --git a/components/Layout.js b/components/Layout.js index ea8e6fe..6f93ae3 100644 --- a/components/Layout.js +++ b/components/Layout.js @@ -192,6 +192,11 @@ export default function({ children, pathname }) { JSON to Typescript Interface +
  • + + JSON to MobX-State-Tree Model + +
  • CSS to JS Objects diff --git a/pages/json-to-mobx-state-tree.js b/pages/json-to-mobx-state-tree.js new file mode 100644 index 0000000..9044640 --- /dev/null +++ b/pages/json-to-mobx-state-tree.js @@ -0,0 +1,34 @@ +import React, { PureComponent } from "react"; +import plugin from "../utils/babel-plugin-js-to-mobx-state-tree"; +import Layout from "../components/Layout"; +import { transform } from "babel-standalone"; +import defaultText from "../utils/dummy-json"; + +import ConversionPanel from "../components/ConversionPanel"; + +export default class Main extends PureComponent { + getTransformedValue = newValue => { + newValue = "const MyModel = " + newValue; + const { code } = transform(newValue, { + presets: ["es2015"], + plugins: [plugin] + }); + return code; + }; + + render() { + return ( + + + + ); + } +} diff --git a/utils/babel-plugin-js-to-mobx-state-tree.js b/utils/babel-plugin-js-to-mobx-state-tree.js new file mode 100644 index 0000000..564d71c --- /dev/null +++ b/utils/babel-plugin-js-to-mobx-state-tree.js @@ -0,0 +1,105 @@ +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("t"), + t.identifier("model") + ), + uniqueElements + ) + ]; + } + + if ( + (!isEmpty(uniqueElements) && uniqueElements.length === 1) || + t.isCallExpression(uniqueElements) + ) { + path.replaceWith( + t.callExpression( + t.memberExpression( + t.identifier("t"), + t.identifier("array") + ), + uniqueElements + ) + ); + } else { + path.replaceWith(t.identifier("t.array")); + } + }, + + NullLiteral(path) { + path.replaceWith(t.identifier("t.null")); + }, + + StringLiteral(path) { + if (path.parentKey === "key") return; + path.replaceWith(t.identifier(`t.string`)); + }, + + NumericLiteral(path) { + path.replaceWith(t.identifier("t.number")); + }, + + ArrowFunctionExpression(path) { + path.replaceWith(t.identifier("t.frozen")); + }, + + FunctionExpression(path) { + path.replaceWith(t.identifier("t.frozen")); + }, + + BooleanLiteral(path) { + path.replaceWith(t.identifier("t.boolean")); + }, + + Program(path) { + path.traverse({ + enter(path) { + t.removeComments(path.node); + } + }); + }, + + ObjectProperty(path){ + if(t.isStringLiteral(path.node.key)){ + path.node.key = t.identifier(path.node.key.value) + } + }, + + ObjectExpression(path) { + if ( + t.isCallExpression(path.parent) + ) + return; + + const elements = [path.node]; + + path.replaceWith( + t.callExpression( + t.memberExpression( + t.identifier("t"), + t.identifier("model") + ), + elements + ) + ); + } + } + }; +}