mirror of
https://github.com/ritz078/transform.git
synced 2026-11-22 17:11:32 +03:00
add babel-plugin-json-to-proptypes
This commit is contained in:
1 parent
fd049587ab
commit
668cbc0299
4 files changed
+15
-115
No files matched your search
@@ -19,6 +19,7 @@
|
||||
"dependencies": {
|
||||
"@tsuyoshiwada/htmltojsx": "^0.0.4",
|
||||
"apollo-codegen": "0.17.0-alpha.1",
|
||||
"babel-plugin-json-to-proptypes": "^0.1.0",
|
||||
"babel-standalone": "^6.26.0",
|
||||
"classnames": "^2.2.5",
|
||||
"codemirror": "^5.29.0",
|
||||
|
||||
+8
-6
@@ -1,10 +1,9 @@
|
||||
import React, { PureComponent } from "react";
|
||||
import propsPlugin from "../utils/babel-plugin-js-to-prop-types";
|
||||
import jsonToProptypes from "babel-plugin-json-to-proptypes";
|
||||
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";
|
||||
import { toValidJSON } from "../utils/json-to-ts-flow";
|
||||
import merge from "lodash/merge"
|
||||
|
||||
import ConversionPanel from "../components/ConversionPanel";
|
||||
@@ -15,17 +14,20 @@ export default class Main extends PureComponent {
|
||||
};
|
||||
|
||||
getTransformedValue = newValue => {
|
||||
let x = JSON.parse(toValidJSON(newValue))
|
||||
let x = JSON.parse(newValue)
|
||||
if (typeof x !== "object" || Array.isArray(x)) {
|
||||
x = merge({}, ...x)
|
||||
}
|
||||
x = "const propTypes = " + JSON.stringify(x);
|
||||
const plugin = this.state.isImmutable ? immutablePropsPlugin : propsPlugin;
|
||||
const plugin = this.state.isImmutable ? immutablePropsPlugin : jsonToProptypes;
|
||||
const { code } = transform(x, {
|
||||
presets: ["es2015"],
|
||||
plugins: [plugin]
|
||||
});
|
||||
return code;
|
||||
|
||||
|
||||
return `import PropTypes from "prop-types";
|
||||
|
||||
${code}`;
|
||||
};
|
||||
|
||||
render() {
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
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("PropTypes"),
|
||||
t.identifier("shape")
|
||||
),
|
||||
uniqueElements
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
if (
|
||||
(!isEmpty(uniqueElements) && uniqueElements.length === 1) ||
|
||||
t.isCallExpression(uniqueElements)
|
||||
) {
|
||||
path.replaceWith(
|
||||
t.callExpression(
|
||||
t.memberExpression(
|
||||
t.identifier("PropTypes"),
|
||||
t.identifier("arrayOf")
|
||||
),
|
||||
uniqueElements
|
||||
)
|
||||
);
|
||||
} else {
|
||||
path.replaceWith(t.identifier("PropTypes.array"));
|
||||
}
|
||||
},
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
VariableDeclaration(path) {
|
||||
path.insertBefore(
|
||||
t.identifier(
|
||||
`import PropTypes from 'prop-types'
|
||||
`
|
||||
)
|
||||
);
|
||||
},
|
||||
|
||||
ObjectExpression(path) {
|
||||
if (
|
||||
t.isVariableDeclarator(path.parent) ||
|
||||
t.isCallExpression(path.parent)
|
||||
)
|
||||
return;
|
||||
|
||||
const elements = [path.node];
|
||||
|
||||
path.replaceWith(
|
||||
t.callExpression(
|
||||
t.memberExpression(
|
||||
t.identifier("PropTypes"),
|
||||
t.identifier("shape")
|
||||
),
|
||||
elements
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -459,6 +459,12 @@ babel-plugin-check-es2015-constants@^6.22.0:
|
||||
dependencies:
|
||||
babel-runtime "^6.22.0"
|
||||
|
||||
babel-plugin-json-to-proptypes@^0.1.0:
|
||||
version "0.1.0"
|
||||
resolved "https://registry.yarnpkg.com/babel-plugin-json-to-proptypes/-/babel-plugin-json-to-proptypes-0.1.0.tgz#a087bdfad142a2c886413fc63754547f2e39abce"
|
||||
dependencies:
|
||||
lodash "^4.17.4"
|
||||
|
||||
babel-plugin-minify-builtins@^0.1.3:
|
||||
version "0.1.3"
|
||||
resolved "https://registry.yarnpkg.com/babel-plugin-minify-builtins/-/babel-plugin-minify-builtins-0.1.3.tgz#4f21a7dcb51f91a04ea71d47ff0e8e3b05fec021"
|
||||
|
||||
Reference in new issue
Block a user