-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbabelPlugin.js
142 lines (140 loc) · 3.81 KB
/
babelPlugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const babel = require('@babel/core')
const t = require('@babel/types')
const code = `
function renderRoot(editor) {
return <div id='editor-root'>{formater.render([editor.$marks])}</div>
}
const aa=function(){
return (<div style='font-size:0;width:228px;'>
<Palette ref={this.paletteRef} hue={this.hueRef}></Palette>
<Hue ref={this.hueRef} color={this.props.color} paletteRef={this.paletteRef}></Hue>
</div>)
}
export default class ColorPicker extends Component {
constructor(props) {
super(props)
this.paletteRef = createRef()
this.hueRef = createRef()
const aa = <span>111</span>
}
render() {
return (
<div style='font-size:0;width:228px;'>
<Palette ref={this.paletteRef} hue={this.hueRef}></Palette>
<Hue ref={this.hueRef} color={this.props.color} paletteRef={this.paletteRef}></Hue>
</div>
)
}
onMounted() {
console.log('ColorPicker')
}
}
`
const visitor = {
JSXElement(path) {
path.replaceWith(converJSX(path))
},
'ClassMethod|FunctionDeclaration'(path) {
const jsxChecker = {
hasJsx: false,
}
path.traverse(
{
JSXElement(path) {
this.hasJsx = true
path.stop()
},
},
jsxChecker
)
if (!jsxChecker.hasJsx) {
return
}
if (
!path.node.params.length ||
(path.node.params.length &&
path.node.params[path.node.params.length - 1].name !== 'h' &&
path.node.key?.name !== 'constructor')
) {
path
.get('body')
.unshiftContainer(
'body',
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('h'),
t.memberExpression(
t.identifier('arguments'),
t.binaryExpression(
'-',
t.memberExpression(t.identifier('arguments'), t.identifier('length'), false),
t.numericLiteral(1)
),
true
)
),
])
)
}
},
}
function convertAttrName(node) {
if (t.isJSXNamespacedName(node.name)) {
return t.stringLiteral(node.name.namespace.name + ':' + node.name.name.name)
} else {
return t.stringLiteral(node.name.name)
}
}
function convertAttrValue(node) {
return t.isJSXExpressionContainer(node.value)
? node.value.expression
: node.value
? t.stringLiteral(node.value.value)
: t.booleanLiteral(true)
}
function convertAttribute(attrs) {
return t.ObjectExpression(
attrs.map((i) => {
if (t.isJSXAttribute(i)) {
const name = convertAttrName(i)
const value = convertAttrValue(i)
return t.ObjectProperty(name, value)
} else if (t.isJSXSpreadAttribute(i)) {
return t.spreadElement(i.argument)
}
})
)
}
function converJSX(path) {
if (path.isJSXElement()) {
const tagName = path.node.openingElement.name.name
return t.callExpression(t.identifier('h'), [
tagName.charCodeAt(0) < 96 ? t.identifier(tagName) : t.stringLiteral(tagName),
convertAttribute(path.node.openingElement.attributes),
t.ArrayExpression(
path
.get('children')
.map((ele) => converJSX(ele))
.filter((ele) => ele)
),
])
} else if (path.isJSXText()) {
return path.node.value.replace(/\n\s+/g, '')
? t.stringLiteral(path.node.value.replace(/\n\s+/g, ''))
: null
} else if (path.isJSXExpressionContainer()) {
return path.node.expression
}
}
const result = babel.transform(code, {
plugins: [
'@babel/plugin-syntax-jsx',
[
{
visitor: visitor,
},
{ nameSpace: false },
],
],
})
console.log(result.code)