Merge branch 'Raathigesh-master'

This commit is contained in:
Ritesh Kumar committed 2017-08-31 18:41:12 +05:30
commit 2dd4586544
6 files changed
+135 -4

No files matched your search

+9
View File
@@ -52,6 +52,15 @@
"ideas",
"code"
]
},
{
"login": "Raathigesh",
"name": "Raathigeshan Kugarajan",
"avatar_url": "https://avatars0.githubusercontent.com/u/3108160?v=4",
"profile": "http://raathigesh.com/",
"contributions": [
"code"
]
}
]
}
+1 -1
View File
@@ -7,7 +7,7 @@
"start": "next start",
"build": "next build",
"dev": "next dev",
"add": "all-contributors add",
"addContributor": "all-contributors add",
"analyze": "cross-env ANALYZE=1 next build",
"format": "prettier --write **/*.js",
"test": "echo \"Error: no test specified\" && exit 1"
+10 -1
View File
@@ -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"
/>
</Layout>
);
+2 -2
View File
@@ -27,8 +27,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://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") | [<img src="https://avatars3.githubusercontent.com/u/12202757?v=4" width="100px;"/><br /><sub>Mohammad Rajabifard</sub>](https://tarino.ir)<br />[🤔](#ideas-morajabi "Ideas, Planning, & Feedback") [💻](https://github.com/ritz078/transform-www/commits?author=morajabi "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") | [<img src="https://avatars3.githubusercontent.com/u/12202757?v=4" width="100px;"/><br /><sub>Mohammad Rajabifard</sub>](https://tarino.ir)<br />[🤔](#ideas-morajabi "Ideas, Planning, & Feedback") [💻](https://github.com/ritz078/transform-www/commits?author=morajabi "Code") | [<img src="https://avatars0.githubusercontent.com/u/3108160?v=4" width="100px;"/><br /><sub>Raathigeshan Kugarajan</sub>](http://raathigesh.com/)<br />[💻](https://github.com/ritz078/transform-www/commits?author=Raathigesh "Code") |
| :---: | :---: | :---: | :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
@@ -0,0 +1,106 @@
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"));
},
VariableDeclaration(path) {
path.insertBefore(t.identifier(`import ImmutablePropTypes from 'react-immutable-proptypes'
import PropTypes from 'prop-types
`))
},
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
)
);
}
}
};
}
+7
View File
@@ -76,6 +76,13 @@ export default function({ types: t }) {
});
},
VariableDeclaration(path) {
path.insertBefore(t.identifier(
`import PropTypes from 'prop-types'
`))
},
ObjectExpression(path) {
if (
t.isVariableDeclarator(path.parent) ||