diff --git a/.all-contributorsrc b/.all-contributorsrc
index f56c7a5..5d20f61 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -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"
+ ]
}
]
}
diff --git a/package.json b/package.json
index e467c96..cf2d17f 100644
--- a/package.json
+++ b/package.json
@@ -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"
diff --git a/pages/index.js b/pages/index.js
index 44cac2f..3c6b70f 100644
--- a/pages/index.js
+++ b/pages/index.js
@@ -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"
/>
);
diff --git a/readme.md b/readme.md
index f200bc0..9a5446c 100644
--- a/readme.md
+++ b/readme.md
@@ -27,8 +27,8 @@ MIT @ Ritesh Kumar
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
-| [
Florian Gilcher](http://asquera.de)
[💻](https://github.com/ritz078/transform-www/commits?author=skade "Code") | [
Pascal Hertleif](http://pascalhertleif.de/)
[💻](https://github.com/ritz078/transform-www/commits?author=killercup "Code") | [
Lado Lomidze](http://landish.github.io)
[💻](https://github.com/ritz078/transform-www/commits?author=Landish "Code") | [
Mattia Manzati](https://github.com/mattiamanzati)
[💻](https://github.com/ritz078/transform-www/commits?author=mattiamanzati "Code") | [
Mohammad Rajabifard](https://tarino.ir)
[🤔](#ideas-morajabi "Ideas, Planning, & Feedback") [💻](https://github.com/ritz078/transform-www/commits?author=morajabi "Code") |
-| :---: | :---: | :---: | :---: | :---: |
+| [
Florian Gilcher](http://asquera.de)
[💻](https://github.com/ritz078/transform-www/commits?author=skade "Code") | [
Pascal Hertleif](http://pascalhertleif.de/)
[💻](https://github.com/ritz078/transform-www/commits?author=killercup "Code") | [
Lado Lomidze](http://landish.github.io)
[💻](https://github.com/ritz078/transform-www/commits?author=Landish "Code") | [
Mattia Manzati](https://github.com/mattiamanzati)
[💻](https://github.com/ritz078/transform-www/commits?author=mattiamanzati "Code") | [
Mohammad Rajabifard](https://tarino.ir)
[🤔](#ideas-morajabi "Ideas, Planning, & Feedback") [💻](https://github.com/ritz078/transform-www/commits?author=morajabi "Code") | [
Raathigeshan Kugarajan](http://raathigesh.com/)
[💻](https://github.com/ritz078/transform-www/commits?author=Raathigesh "Code") |
+| :---: | :---: | :---: | :---: | :---: | :---: |
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
diff --git a/utils/babel-plugin-js-to-immutable-prop-types.js b/utils/babel-plugin-js-to-immutable-prop-types.js
new file mode 100644
index 0000000..557b907
--- /dev/null
+++ b/utils/babel-plugin-js-to-immutable-prop-types.js
@@ -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
+ )
+ );
+ }
+ }
+ };
+}
diff --git a/utils/babel-plugin-js-to-prop-types.js b/utils/babel-plugin-js-to-prop-types.js
index e330448..2454840 100644
--- a/utils/babel-plugin-js-to-prop-types.js
+++ b/utils/babel-plugin-js-to-prop-types.js
@@ -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) ||