add json-to-mobx-state-tree

This commit is contained in:
Mattia Manzati committed 2017-08-19 10:45:13 +02:00
1 parent 2cf0f6eaa3
commit fd11359596
3 files changed
+144

No files matched your search

+5
View File
@@ -192,6 +192,11 @@ export default function({ children, pathname }) {
<a>JSON to Typescript Interface</a>
</Link>
</li>
<li className={getClass("/json-to-mobx-state-tree")}>
<Link prefetch href="/json-to-mobx-state-tree">
<a>JSON to MobX-State-Tree Model</a>
</Link>
</li>
<li className={getClass("/css-to-js")}>
<Link prefetch href="/css-to-js">
<a>CSS to JS Objects</a>
+34
View File
@@ -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 (
<Layout pathname={this.props.url.pathname}>
<ConversionPanel
leftTitle="JSON"
rightTitle="MobX-State-Tree Model"
url={this.props.url}
leftMode="json"
getTransformedValue={this.getTransformedValue}
name={"mobx_state_tree_model"}
defaultText={defaultText}
/>
</Layout>
);
}
}
+105
View File
@@ -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
)
);
}
}
};
}