mirror of
https://github.com/ritz078/transform.git
synced 2026-11-25 10:14:22 +03:00
first commit
This commit is contained in:
commit
8265bae04d
6 files changed
+3850
No files matched your search
@@ -0,0 +1,5 @@
|
||||
.next
|
||||
|
||||
node_modules
|
||||
|
||||
.idea
|
||||
@@ -0,0 +1,7 @@
|
||||
module.exports = {
|
||||
webpack: (config, { dev }) => {
|
||||
config.plugins = config.plugins.filter(plugin => plugin.constructor.name !== 'UglifyJsPlugin');
|
||||
|
||||
return config;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "transform",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "next start",
|
||||
"build": "next build",
|
||||
"dev": "next dev",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"babel-standalone": "^6.25.0",
|
||||
"copy-text-to-clipboard": "^1.0.1",
|
||||
"is-in-browser": "^1.1.3",
|
||||
"js-beautify": "^1.6.14",
|
||||
"lodash": "^4.17.4",
|
||||
"next": "^2.4.3",
|
||||
"react": "^15.6.0",
|
||||
"react-ace": "^5.0.1",
|
||||
"react-dom": "^15.6.0",
|
||||
"store2": "^2.5.1"
|
||||
}
|
||||
}
|
||||
+219
@@ -0,0 +1,219 @@
|
||||
import React, { PureComponent } from 'react'
|
||||
import isBrowser from 'is-in-browser'
|
||||
import { transform } from 'babel-standalone'
|
||||
import store from 'store2'
|
||||
import {js_beautify} from 'js-beautify'
|
||||
import copy from 'copy-text-to-clipboard'
|
||||
import plugin from '../utils/plugin'
|
||||
|
||||
let brace
|
||||
let AceEditor
|
||||
if (isBrowser) {
|
||||
brace = require('brace').default
|
||||
AceEditor = require('react-ace').default
|
||||
require('brace/mode/javascript')
|
||||
require('brace/theme/tomorrow')
|
||||
}
|
||||
|
||||
const defaultText = `
|
||||
// Enter the object you want to convert in the following form
|
||||
// const x = your_object
|
||||
// You need to assign your object to a variable (for now.)
|
||||
|
||||
const x = {
|
||||
foo: 'bar',
|
||||
ids: [1, 2, 3]
|
||||
}
|
||||
`
|
||||
|
||||
export default class Main extends PureComponent {
|
||||
constructor (props) {
|
||||
super(props)
|
||||
|
||||
this.state = {
|
||||
resultValue: '',
|
||||
value: '',
|
||||
error: ''
|
||||
}
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.setState({
|
||||
value: store('code') || defaultText,
|
||||
resultValue: store('result') || this.getTransformedValue(defaultText).code
|
||||
})
|
||||
}
|
||||
|
||||
getTransformedValue = (newValue) => {
|
||||
return transform(newValue, {
|
||||
presets: ['es2015'],
|
||||
plugins: [plugin]
|
||||
})
|
||||
}
|
||||
|
||||
onChange = (newValue) => {
|
||||
try {
|
||||
const {code, ast} = this.getTransformedValue(newValue)
|
||||
|
||||
this.setState({
|
||||
resultValue: code,
|
||||
error: ''
|
||||
})
|
||||
|
||||
store('code', newValue)
|
||||
store('result', code)
|
||||
} catch (e) {
|
||||
this.setState({
|
||||
error: e.message
|
||||
})
|
||||
}
|
||||
|
||||
this.setState({
|
||||
value: newValue
|
||||
})
|
||||
}
|
||||
|
||||
prettifyCode = () => {
|
||||
this.setState({
|
||||
value: js_beautify(this.state.value)
|
||||
})
|
||||
}
|
||||
|
||||
copyCode = () => {
|
||||
copy(this.state.resultValue)
|
||||
}
|
||||
|
||||
render () {
|
||||
return (
|
||||
<div className="wrapper">
|
||||
<div className="header">
|
||||
Object to PropTypes
|
||||
</div>
|
||||
<style jsx>{`
|
||||
@import url('https://fonts.googleapis.com/css?family=Lato');
|
||||
.wrapper {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
height: 100vh;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.header, .footer{
|
||||
height: 50px;
|
||||
width: 100%;
|
||||
font-family: 'Lato';
|
||||
font-weight: bold;
|
||||
background-color: white;
|
||||
box-shadow: 0 0 1px 1px rgba(0,0,0,0.2);
|
||||
z-index: 999;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
text-align: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.header {
|
||||
color: #666;
|
||||
letter-spacing: 1.2px;
|
||||
}
|
||||
|
||||
.content-wrapper {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.section {
|
||||
flex: 1;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.right {
|
||||
border-left: 1px solid #eee;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: red;
|
||||
font: 14px/normal 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', monospace;
|
||||
}
|
||||
|
||||
.btn {
|
||||
position: absolute;
|
||||
top: 20px;
|
||||
right: 20px;
|
||||
background-color: #fff;
|
||||
font-size: 14px;
|
||||
padding: 8px 14px;
|
||||
z-index: 9;
|
||||
border: 0;
|
||||
border-radius: 2px;
|
||||
cursor: pointer;
|
||||
outline: none;
|
||||
border: 1px solid #eee;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
background-color: #f6f6f6;
|
||||
}
|
||||
|
||||
`}</style>
|
||||
|
||||
<style global jsx>{`
|
||||
*{
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
#code{
|
||||
width: 100% !important;
|
||||
height: 100% !important;
|
||||
}
|
||||
`}</style>
|
||||
|
||||
<div className="content-wrapper">
|
||||
<div className="section left">
|
||||
{isBrowser &&
|
||||
<AceEditor
|
||||
mode="javascript"
|
||||
theme="tomorrow"
|
||||
onChange={this.onChange}
|
||||
name="code"
|
||||
value={this.state.value}
|
||||
editorProps={{$blockScrolling: true}}
|
||||
highlightActiveLine={false}
|
||||
scrollMargin={[20]}
|
||||
focus
|
||||
fontSize={14}
|
||||
wrapEnabled
|
||||
/>
|
||||
}
|
||||
|
||||
<button className="btn" onClick={this.prettifyCode}>Prettify</button>
|
||||
</div>
|
||||
<div className="section right">
|
||||
{isBrowser &&
|
||||
<AceEditor
|
||||
mode="javascript"
|
||||
theme="tomorrow"
|
||||
name="code"
|
||||
readOnly
|
||||
value={js_beautify(this.state.resultValue)}
|
||||
editorProps={{$blockScrolling: true}}
|
||||
scrollMargin={[20]}
|
||||
fontSize={14}
|
||||
showGutter={false}
|
||||
highlightActiveLine={false}
|
||||
wrapEnabled
|
||||
/>
|
||||
}
|
||||
<button className="btn" onClick={this.copyCode}>Copy</button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="footer">
|
||||
{this.state.error ? <span className="error">{this.state.error}</span> : <span>Created by <a href="https://twitter.com/ritz078">Ritesh Kumar</a></span>}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import {uniqBy, isEmpty} from 'lodash'
|
||||
|
||||
export default function ({types: t}) {
|
||||
return {
|
||||
visitor: {
|
||||
ArrayExpression(path) {
|
||||
if (t.isCallExpression(path.parent)) return;
|
||||
const uniqueElements = uniqBy(path.node.elements, 'type');
|
||||
|
||||
const elements = [t.arrayExpression([...uniqueElements])];
|
||||
|
||||
if (!isEmpty(uniqueElements)) {
|
||||
path.replaceWith(
|
||||
t.callExpression(
|
||||
t.memberExpression(t.identifier("PropTypes"), t.identifier("arrayOf")),
|
||||
elements
|
||||
)
|
||||
)
|
||||
} 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, state) {
|
||||
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("PropTypes"), t.identifier("shape")),
|
||||
elements
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user