-
Notifications
You must be signed in to change notification settings - Fork 0
/
call-expression.ts
214 lines (195 loc) · 5.49 KB
/
call-expression.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { NodePath, types as t } from '@babel/core';
import {
CallExpression,
Expression,
SpreadElement,
JSXNamespacedName,
ArgumentPlaceholder,
} from '@babel/types';
import { PluginTransformState } from './types';
function getJSXIdentifier(node: any, tag = false) {
if (t.isIdentifier(node) && (!tag || node.name.match(/^[A-Z]/))) {
return t.jsxIdentifier(node.name);
}
if (t.isStringLiteral(node)) {
return t.jsxIdentifier(node.value);
}
return null;
}
function getJSXAttributeValue(node: any) {
if (t.isStringLiteral(node)) {
return node;
}
if (t.isJSXElement(node)) {
return node;
}
if (t.isExpression(node)) {
return t.jsxExpressionContainer(node);
}
return null;
}
/**
* Tests if a node is `null` or `undefined`
*/
const isNullLikeNode = (
node:
| t.Expression
| t.SpreadElement
| t.JSXNamespacedName
| t.ArgumentPlaceholder
) => t.isNullLiteral(node) || t.isIdentifier(node, { name: 'undefined' });
/**
* Tests if a node is an object expression with noncomputed, nonmethod attrs
*/
const isPlainObjectExpression = (
node:
| t.Expression
| t.SpreadElement
| t.JSXNamespacedName
| t.ArgumentPlaceholder
) =>
t.isObjectExpression(node) &&
node.properties.every(
property =>
t.isSpreadElement(property) ||
(t.isObjectProperty(property, { computed: false }) &&
getJSXIdentifier(property.key) != null &&
getJSXAttributeValue(property.value) != null)
);
/**
* Tests if a node is a CallExpression with callee `React.createElement`
*/
const isReactCreateElement = (
node:
| t.Expression
| t.SpreadElement
| t.JSXNamespacedName
| t.ArgumentPlaceholder
) =>
t.isCallExpression(node) &&
t.isMemberExpression(node.callee) &&
t.isIdentifier(node.callee.object, { name: 'React' }) &&
t.isIdentifier(node.callee.property, { name: 'createElement' }) &&
!node.callee.computed;
/**
* Tests if a node is a MemberExpression with object `Object.assign`
*/
const isObjectAssignMemberExpression = (
node:
| t.Expression
| t.SpreadElement
| t.JSXNamespacedName
| t.ArgumentPlaceholder
) =>
t.isCallExpression(node) &&
t.isMemberExpression(node.callee) &&
t.isIdentifier(node.callee.object, { name: 'Object' }) &&
t.isIdentifier(node.callee.property, { name: 'assign' });
/**
* Get a array of JSX(Spread)Attribute from a props ObjectExpression.
* Handles the _extends Expression babel creates from SpreadElement nodes.
* Returns null if a validation error occurs.
*/
function getJSXProps(node: any): any[] | null {
if (node == null || isNullLikeNode(node)) {
return [];
}
if (
t.isCallExpression(node) &&
t.isIdentifier(node.callee, { name: '_extends' })
) {
const props: any[] = node.arguments.map(getJSXProps);
// if calling this recursively works, flatten.
if (props.every(prop => prop != null)) {
return [].concat(...props);
}
}
if (!t.isObjectExpression(node) && t.isExpression(node))
return [t.jsxSpreadAttribute(node)];
if (!isPlainObjectExpression(node)) {
return null;
}
return node.properties.map((prop: any) =>
t.isObjectProperty(prop)
? t.jsxAttribute(
getJSXIdentifier(prop.key)!,
getJSXAttributeValue(prop.value)
)
: t.jsxSpreadAttribute(prop.argument)
);
}
/**
* Apply className to properties of an ObjectExpression
*/
function traverseApplyClassName(
nodeArgs: (
| Expression
| SpreadElement
| JSXNamespacedName
| ArgumentPlaceholder
)[],
className: string
) {
nodeArgs &&
nodeArgs.forEach(arg => {
if (t.isObjectExpression(arg)) {
if (arg.properties) {
const classNameAttrIndex = arg.properties.findIndex(
attr =>
'key' in attr &&
'name' in attr.key &&
attr.key.name === 'className'
);
// if className is already set, add the className to the existing className
if (classNameAttrIndex !== -1) {
const classNameAttr = arg.properties[classNameAttrIndex] as any;
if (
classNameAttr.value &&
classNameAttr.value.value &&
!classNameAttr.value.value.split(' ').includes(className)
) {
classNameAttr.value.value = `${classNameAttr.value.value} ${className}`;
}
} else {
// push object property
arg.properties.push(
t.objectProperty(
t.identifier('className'),
t.stringLiteral(className)
)
);
}
}
return;
}
if (t.isCallExpression(arg)) {
if (isReactCreateElement(arg)) {
traverseApplyClassName(arg.arguments, className);
}
if (isObjectAssignMemberExpression(arg)) {
// only parse the first ObjectExpression argument
const parseIdx = arg.arguments.findIndex(a =>
t.isObjectExpression(a)
);
traverseApplyClassName([arg.arguments[parseIdx]], className);
}
}
});
}
export default function CallExpression(
path: NodePath<CallExpression>,
{ opts }: PluginTransformState
) {
const customClassName = Array.isArray(opts.className)
? opts.className.join(' ')
: opts.className;
const node = path.node;
if (!isReactCreateElement(node)) {
return;
}
const [, propsNode] = node.arguments;
const props = getJSXProps(propsNode);
if (props) {
traverseApplyClassName(node.arguments, customClassName);
}
}