From 7bbf1fbc008133adf32c199f57190bae62c5825d Mon Sep 17 00:00:00 2001 From: Raathigeshan Kugarajan Date: Thu, 31 Aug 2017 22:25:07 +1000 Subject: [PATCH 1/4] Support for react-immutable-proptypes (#14) --- pages/index.js | 11 +- ...babel-plugin-js-to-immutable-prop-types.js | 100 ++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 utils/babel-plugin-js-to-immutable-prop-types.js 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/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..1e6f9a2 --- /dev/null +++ b/utils/babel-plugin-js-to-immutable-prop-types.js @@ -0,0 +1,100 @@ +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")); + }, + + 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 + ) + ); + } + } + }; +} From 6a8290bbfc628b07e04ad85315e3717d6ea64eb3 Mon Sep 17 00:00:00 2001 From: Ritesh Kumar Date: Thu, 31 Aug 2017 18:39:39 +0530 Subject: [PATCH 2/4] added immutable propTypes --- utils/babel-plugin-js-to-immutable-prop-types.js | 6 ++++++ utils/babel-plugin-js-to-prop-types.js | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/utils/babel-plugin-js-to-immutable-prop-types.js b/utils/babel-plugin-js-to-immutable-prop-types.js index 1e6f9a2..557b907 100644 --- a/utils/babel-plugin-js-to-immutable-prop-types.js +++ b/utils/babel-plugin-js-to-immutable-prop-types.js @@ -68,6 +68,12 @@ export default function({ types: t }) { 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) { 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) || From 82dad5796aab4aedbfa16cef549217125e20ee21 Mon Sep 17 00:00:00 2001 From: Ritesh Kumar Date: Thu, 31 Aug 2017 18:40:21 +0530 Subject: [PATCH 3/4] Add @Raathigesh as a contributor --- .all-contributorsrc | 9 +++++++++ 1 file changed, 9 insertions(+) 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" + ] } ] } From b47f0102c7ca90b38ef1723517425993b07f5aed Mon Sep 17 00:00:00 2001 From: Ritesh Kumar Date: Thu, 31 Aug 2017 18:40:39 +0530 Subject: [PATCH 4/4] add contributor --- package.json | 2 +- readme.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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/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!