Merge branch 'mattiamanzati-json-to-mobx-state-tree'

This commit is contained in:
Ritesh Kumar committed 2017-08-19 14:52:37 +05:30
commit 139a4ded8f
5 files changed
+155 -2

No files matched your search

+9
View File
@@ -33,6 +33,15 @@
"contributions": [
"code"
]
},
{
"login": "mattiamanzati",
"name": "Mattia Manzati",
"avatar_url": "https://avatars1.githubusercontent.com/u/13787614?v=4",
"profile": "https://github.com/mattiamanzati",
"contributions": [
"code"
]
}
]
}
+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>
);
}
}
+2 -2
View File
@@ -19,8 +19,8 @@ MIT @ Ritesh Kumar
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars2.githubusercontent.com/u/47542?v=4" width="100px;"/><br /><sub>Florian Gilcher</sub>](http://asquera.de)<br />[💻](https://github.com/ritz078/transform-www/commits?author=skade "Code") | [<img src="https://avatars1.githubusercontent.com/u/20063?v=4" width="100px;"/><br /><sub>Pascal Hertleif</sub>](http://pascalhertleif.de/)<br />[💻](https://github.com/ritz078/transform-www/commits?author=killercup "Code") | [<img src="https://avatars3.githubusercontent.com/u/534610?v=4" width="100px;"/><br /><sub>Lado Lomidze</sub>](http://landish.github.io)<br />[💻](https://github.com/ritz078/transform-www/commits?author=Landish "Code") |
| :---: | :---: | :---: |
| [<img src="https://avatars2.githubusercontent.com/u/47542?v=4" width="100px;"/><br /><sub>Florian Gilcher</sub>](http://asquera.de)<br />[💻](https://github.com/ritz078/transform-www/commits?author=skade "Code") | [<img src="https://avatars1.githubusercontent.com/u/20063?v=4" width="100px;"/><br /><sub>Pascal Hertleif</sub>](http://pascalhertleif.de/)<br />[💻](https://github.com/ritz078/transform-www/commits?author=killercup "Code") | [<img src="https://avatars3.githubusercontent.com/u/534610?v=4" width="100px;"/><br /><sub>Lado Lomidze</sub>](http://landish.github.io)<br />[💻](https://github.com/ritz078/transform-www/commits?author=Landish "Code") | [<img src="https://avatars1.githubusercontent.com/u/13787614?v=4" width="100px;"/><br /><sub>Mattia Manzati</sub>](https://github.com/mattiamanzati)<br />[💻](https://github.com/ritz078/transform-www/commits?author=mattiamanzati "Code") |
| :---: | :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
+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
)
);
}
}
};
}