-
Notifications
You must be signed in to change notification settings - Fork 142
/
utils.ts
309 lines (272 loc) · 8.97 KB
/
utils.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import * as t from '@babel/types';
import htmlTags from 'html-tags';
import svgTags from 'svg-tags';
import { NodePath } from '@babel/traverse';
import type { State } from './interface';
import SlotFlags from './slotFlags';
export const JSX_HELPER_KEY = 'JSX_HELPER_KEY';
export const FRAGMENT = 'Fragment';
export const KEEP_ALIVE = 'KeepAlive';
/**
* create Identifier
* @param path NodePath
* @param state
* @param name string
* @returns MemberExpression
*/
export const createIdentifier = (
state: State, name: string,
): t.Identifier | t.MemberExpression => state.get(name)();
/**
* Checks if string is describing a directive
* @param src string
*/
export const isDirective = (src: string): boolean => src.startsWith('v-')
|| (src.startsWith('v') && src.length >= 2 && src[1] >= 'A' && src[1] <= 'Z');
/**
* Should transformed to slots
* @param tag string
* @returns boolean
*/
// if _Fragment is already imported, it will end with number
export const shouldTransformedToSlots = (tag: string) => !(tag.match(RegExp(`^_?${FRAGMENT}\\d*$`)) || tag === KEEP_ALIVE);
/**
* Check if a Node is a component
*
* @param t
* @param path JSXOpeningElement
* @returns boolean
*/
export const checkIsComponent = (path: NodePath<t.JSXOpeningElement>, state: State): boolean => {
const namePath = path.get('name');
if (namePath.isJSXMemberExpression()) {
return shouldTransformedToSlots(namePath.node.property.name); // For withCtx
}
const tag = (namePath as NodePath<t.JSXIdentifier>).node.name;
return !state.opts.isCustomElement?.(tag) && shouldTransformedToSlots(tag) && !htmlTags.includes(tag as htmlTags.htmlTags) && !svgTags.includes(tag);
};
/**
* Transform JSXMemberExpression to MemberExpression
* @param path JSXMemberExpression
* @returns MemberExpression
*/
export const transformJSXMemberExpression = (
path: NodePath<t.JSXMemberExpression>,
): t.MemberExpression => {
const objectPath = path.node.object;
const propertyPath = path.node.property;
const transformedObject = t.isJSXMemberExpression(objectPath)
? transformJSXMemberExpression(path.get('object') as NodePath<t.JSXMemberExpression>)
: t.isJSXIdentifier(objectPath)
? t.identifier(objectPath.name)
: t.nullLiteral();
const transformedProperty = t.identifier(propertyPath.name);
return t.memberExpression(transformedObject, transformedProperty);
};
/**
* Get tag (first attribute for h) from JSXOpeningElement
* @param path JSXElement
* @param state State
* @returns Identifier | StringLiteral | MemberExpression | CallExpression
*/
export const getTag = (
path: NodePath<t.JSXElement>,
state: State,
): t.Identifier | t.CallExpression | t.StringLiteral | t.MemberExpression => {
const namePath = path.get('openingElement').get('name');
if (namePath.isJSXIdentifier()) {
const { name } = namePath.node;
if (!htmlTags.includes(name as htmlTags.htmlTags) && !svgTags.includes(name)) {
return (name === FRAGMENT
? createIdentifier(state, FRAGMENT)
: path.scope.hasBinding(name)
? t.identifier(name)
: state.opts.isCustomElement?.(name)
? t.stringLiteral(name)
: t.callExpression(createIdentifier(state, 'resolveComponent'), [t.stringLiteral(name)]));
}
return t.stringLiteral(name);
}
if (namePath.isJSXMemberExpression()) {
return transformJSXMemberExpression(namePath);
}
throw new Error(`getTag: ${namePath.type} is not supported`);
};
export const getJSXAttributeName = (path: NodePath<t.JSXAttribute>): string => {
const nameNode = path.node.name;
if (t.isJSXIdentifier(nameNode)) {
return nameNode.name;
}
return `${nameNode.namespace.name}:${nameNode.name.name}`;
};
/**
* Transform JSXText to StringLiteral
* @param path JSXText
* @returns StringLiteral | null
*/
export const transformJSXText = (path: NodePath<t.JSXText>): t.StringLiteral | null => {
const { node } = path;
const lines = node.value.split(/\r\n|\n|\r/);
let lastNonEmptyLine = 0;
for (let i = 0; i < lines.length; i++) {
if (lines[i].match(/[^ \t]/)) {
lastNonEmptyLine = i;
}
}
let str = '';
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const isFirstLine = i === 0;
const isLastLine = i === lines.length - 1;
const isLastNonEmptyLine = i === lastNonEmptyLine;
// replace rendered whitespace tabs with spaces
let trimmedLine = line.replace(/\t/g, ' ');
// trim whitespace touching a newline
if (!isFirstLine) {
trimmedLine = trimmedLine.replace(/^[ ]+/, '');
}
// trim whitespace touching an endline
if (!isLastLine) {
trimmedLine = trimmedLine.replace(/[ ]+$/, '');
}
if (trimmedLine) {
if (!isLastNonEmptyLine) {
trimmedLine += ' ';
}
str += trimmedLine;
}
}
return str !== '' ? t.stringLiteral(str) : null;
};
/**
* Transform JSXExpressionContainer to Expression
* @param path JSXExpressionContainer
* @returns Expression
*/
export const transformJSXExpressionContainer = (
path: NodePath<t.JSXExpressionContainer>,
): (
t.Expression
) => path.get('expression').node as t.Expression;
/**
* Transform JSXSpreadChild
* @param path JSXSpreadChild
* @returns SpreadElement
*/
export const transformJSXSpreadChild = (
path: NodePath<t.JSXSpreadChild>,
): t.SpreadElement => t.spreadElement(path.get('expression').node);
export const walksScope = (path: NodePath, name: string, slotFlag: SlotFlags): void => {
if (path.scope.hasBinding(name) && path.parentPath) {
if (t.isJSXElement(path.parentPath.node)) {
path.parentPath.setData('slotFlag', slotFlag);
}
walksScope(path.parentPath, name, slotFlag);
}
};
export const buildIIFE = (path: NodePath<t.JSXElement>, children: t.Expression[]) => {
const { parentPath } = path;
if (t.isAssignmentExpression(parentPath)) {
const { left } = parentPath.node as t.AssignmentExpression;
if (t.isIdentifier(left)) {
return children.map((child) => {
if (t.isIdentifier(child) && child.name === left.name) {
const insertName = path.scope.generateUidIdentifier(child.name);
parentPath.insertBefore(
t.variableDeclaration('const', [
t.variableDeclarator(
insertName,
t.callExpression(
t.functionExpression(null, [], t.blockStatement([t.returnStatement(child)])),
[],
),
),
]),
);
return insertName;
}
return child;
});
}
}
return children;
};
const onRE = /^on[^a-z]/;
export const isOn = (key: string) => onRE.test(key);
const mergeAsArray = (existing: t.ObjectProperty, incoming: t.ObjectProperty) => {
if (t.isArrayExpression(existing.value)) {
existing.value.elements.push(incoming.value as t.Expression);
} else {
existing.value = t.arrayExpression([
existing.value as t.Expression,
incoming.value as t.Expression,
]);
}
};
export const dedupeProperties = (properties: t.ObjectProperty[] = [], mergeProps?: boolean) => {
if (!mergeProps) {
return properties;
}
const knownProps = new Map<string, t.ObjectProperty>();
const deduped: t.ObjectProperty[] = [];
properties.forEach((prop) => {
if (t.isStringLiteral(prop.key)) {
const { value: name } = prop.key;
const existing = knownProps.get(name);
if (existing) {
if (name === 'style' || name === 'class' || name.startsWith('on')) {
mergeAsArray(existing, prop);
}
} else {
knownProps.set(name, prop);
deduped.push(prop);
}
} else {
// v-model target with variable
deduped.push(prop);
}
});
return deduped;
};
/**
* Check if an attribute value is constant
* @param node
* @returns boolean
*/
export const isConstant = (
node: t.Expression | t.Identifier | t.Literal | t.SpreadElement | null,
): boolean => {
if (t.isIdentifier(node)) {
return node.name === 'undefined';
}
if (t.isArrayExpression(node)) {
const { elements } = node;
return elements.every((element) => element && isConstant(element));
}
if (t.isObjectExpression(node)) {
return node.properties.every((property) => isConstant((property as any).value));
}
if (t.isLiteral(node)) {
return true;
}
return false;
};
export const transformJSXSpreadAttribute = (
nodePath: NodePath,
path: NodePath<t.JSXSpreadAttribute>,
mergeProps: boolean,
args: (t.ObjectProperty | t.Expression | t.SpreadElement)[],
) => {
const argument = path.get('argument') as NodePath<t.ObjectExpression | t.Identifier>;
const properties = t.isObjectExpression(argument.node) ? argument.node.properties : undefined;
if (!properties) {
if (argument.isIdentifier()) {
walksScope(nodePath, (argument.node as t.Identifier).name, SlotFlags.DYNAMIC);
}
args.push(mergeProps ? argument.node : t.spreadElement(argument.node));
} else if (mergeProps) {
args.push(t.objectExpression(properties));
} else {
args.push(...(properties as t.ObjectProperty[]));
}
};