@vue/compiler-vapor 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/compiler-vapor.cjs.js +3751 -0
- package/dist/compiler-vapor.d.ts +406 -0
- package/dist/compiler-vapor.esm-browser.js +24028 -0
- package/package.json +49 -0
|
@@ -0,0 +1,3751 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vue/compiler-vapor v3.5.13
|
|
3
|
+
* (c) 2018-present Yuxi (Evan) You and Vue contributors
|
|
4
|
+
* @license MIT
|
|
5
|
+
**/
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
9
|
+
|
|
10
|
+
var compilerDom = require('@vue/compiler-dom');
|
|
11
|
+
var shared = require('@vue/shared');
|
|
12
|
+
var sourceMapJs = require('source-map-js');
|
|
13
|
+
var estreeWalker = require('estree-walker');
|
|
14
|
+
var parser = require('@babel/parser');
|
|
15
|
+
|
|
16
|
+
const newDynamic = () => ({
|
|
17
|
+
flags: 1,
|
|
18
|
+
children: []
|
|
19
|
+
});
|
|
20
|
+
const newBlock = (node) => ({
|
|
21
|
+
type: 1,
|
|
22
|
+
node,
|
|
23
|
+
dynamic: newDynamic(),
|
|
24
|
+
effect: [],
|
|
25
|
+
operation: [],
|
|
26
|
+
returns: [],
|
|
27
|
+
expressions: []
|
|
28
|
+
});
|
|
29
|
+
function wrapTemplate(node, dirs) {
|
|
30
|
+
if (node.tagType === 3) {
|
|
31
|
+
return node;
|
|
32
|
+
}
|
|
33
|
+
const reserved = [];
|
|
34
|
+
const pass = [];
|
|
35
|
+
node.props.forEach((prop) => {
|
|
36
|
+
if (prop.type === 7 && dirs.includes(prop.name)) {
|
|
37
|
+
reserved.push(prop);
|
|
38
|
+
} else {
|
|
39
|
+
pass.push(prop);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return shared.extend({}, node, {
|
|
43
|
+
type: 1,
|
|
44
|
+
tag: "template",
|
|
45
|
+
props: reserved,
|
|
46
|
+
tagType: 3,
|
|
47
|
+
children: [shared.extend({}, node, { props: pass })]
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
const EMPTY_EXPRESSION = compilerDom.createSimpleExpression(
|
|
51
|
+
"",
|
|
52
|
+
true
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const findProp = compilerDom.findProp;
|
|
56
|
+
const findDir = compilerDom.findDir;
|
|
57
|
+
function propToExpression(prop) {
|
|
58
|
+
return prop.type === 6 ? prop.value ? compilerDom.createSimpleExpression(prop.value.content, true, prop.value.loc) : EMPTY_EXPRESSION : prop.exp;
|
|
59
|
+
}
|
|
60
|
+
function isConstantExpression(exp) {
|
|
61
|
+
return compilerDom.isLiteralWhitelisted(exp.content) || shared.isGloballyAllowed(exp.content) || getLiteralExpressionValue(exp) !== null;
|
|
62
|
+
}
|
|
63
|
+
function resolveExpression(exp) {
|
|
64
|
+
if (!exp.isStatic) {
|
|
65
|
+
const value = getLiteralExpressionValue(exp);
|
|
66
|
+
if (value !== null) {
|
|
67
|
+
return compilerDom.createSimpleExpression("" + value, true, exp.loc);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return exp;
|
|
71
|
+
}
|
|
72
|
+
function getLiteralExpressionValue(exp) {
|
|
73
|
+
if (exp.ast) {
|
|
74
|
+
if (exp.ast.type === "StringLiteral") {
|
|
75
|
+
return exp.ast.value;
|
|
76
|
+
} else if (exp.ast.type === "TemplateLiteral" && exp.ast.expressions.length === 0) {
|
|
77
|
+
return exp.ast.quasis[0].value.cooked;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return exp.isStatic ? exp.content : null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
class TransformContext {
|
|
84
|
+
constructor(ir, node, options = {}) {
|
|
85
|
+
this.ir = ir;
|
|
86
|
+
this.node = node;
|
|
87
|
+
this.parent = null;
|
|
88
|
+
this.index = 0;
|
|
89
|
+
this.block = this.ir.block;
|
|
90
|
+
this.template = "";
|
|
91
|
+
this.childrenTemplate = [];
|
|
92
|
+
this.dynamic = this.ir.block.dynamic;
|
|
93
|
+
this.inVOnce = false;
|
|
94
|
+
this.inVFor = 0;
|
|
95
|
+
this.comment = [];
|
|
96
|
+
this.component = this.ir.component;
|
|
97
|
+
this.directive = this.ir.directive;
|
|
98
|
+
this.slots = [];
|
|
99
|
+
this.globalId = 0;
|
|
100
|
+
this.increaseId = () => this.globalId++;
|
|
101
|
+
this.options = shared.extend({}, defaultOptions, options);
|
|
102
|
+
this.root = this;
|
|
103
|
+
}
|
|
104
|
+
enterBlock(ir, isVFor = false) {
|
|
105
|
+
const { block, template, dynamic, childrenTemplate, slots } = this;
|
|
106
|
+
this.block = ir;
|
|
107
|
+
this.dynamic = ir.dynamic;
|
|
108
|
+
this.template = "";
|
|
109
|
+
this.childrenTemplate = [];
|
|
110
|
+
this.slots = [];
|
|
111
|
+
isVFor && this.inVFor++;
|
|
112
|
+
return () => {
|
|
113
|
+
this.registerTemplate();
|
|
114
|
+
this.block = block;
|
|
115
|
+
this.template = template;
|
|
116
|
+
this.dynamic = dynamic;
|
|
117
|
+
this.childrenTemplate = childrenTemplate;
|
|
118
|
+
this.slots = slots;
|
|
119
|
+
isVFor && this.inVFor--;
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
reference() {
|
|
123
|
+
if (this.dynamic.id !== undefined) return this.dynamic.id;
|
|
124
|
+
this.dynamic.flags |= 1;
|
|
125
|
+
return this.dynamic.id = this.increaseId();
|
|
126
|
+
}
|
|
127
|
+
pushTemplate(content) {
|
|
128
|
+
const existing = this.ir.template.findIndex(
|
|
129
|
+
(template) => template === content
|
|
130
|
+
);
|
|
131
|
+
if (existing !== -1) return existing;
|
|
132
|
+
this.ir.template.push(content);
|
|
133
|
+
return this.ir.template.length - 1;
|
|
134
|
+
}
|
|
135
|
+
registerTemplate() {
|
|
136
|
+
if (!this.template) return -1;
|
|
137
|
+
const id = this.pushTemplate(this.template);
|
|
138
|
+
return this.dynamic.template = id;
|
|
139
|
+
}
|
|
140
|
+
registerEffect(expressions, ...operations) {
|
|
141
|
+
expressions = expressions.filter((exp) => !isConstantExpression(exp));
|
|
142
|
+
if (this.inVOnce || expressions.length === 0 || isStaticExpression(this.root, expressions)) {
|
|
143
|
+
return this.registerOperation(...operations);
|
|
144
|
+
}
|
|
145
|
+
this.block.expressions.push(...expressions);
|
|
146
|
+
const existing = this.block.effect.find(
|
|
147
|
+
(e) => isSameExpression(e.expressions, expressions)
|
|
148
|
+
);
|
|
149
|
+
if (existing) {
|
|
150
|
+
existing.operations.push(...operations);
|
|
151
|
+
} else {
|
|
152
|
+
this.block.effect.push({
|
|
153
|
+
expressions,
|
|
154
|
+
operations
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
function isSameExpression(a, b) {
|
|
158
|
+
if (a.length !== b.length) return false;
|
|
159
|
+
return a.every((exp, i) => exp.content === b[i].content);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
registerOperation(...node) {
|
|
163
|
+
this.block.operation.push(...node);
|
|
164
|
+
}
|
|
165
|
+
create(node, index) {
|
|
166
|
+
return Object.assign(Object.create(TransformContext.prototype), this, {
|
|
167
|
+
node,
|
|
168
|
+
parent: this,
|
|
169
|
+
index,
|
|
170
|
+
template: "",
|
|
171
|
+
childrenTemplate: [],
|
|
172
|
+
dynamic: newDynamic()
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const defaultOptions = {
|
|
177
|
+
filename: "",
|
|
178
|
+
prefixIdentifiers: true,
|
|
179
|
+
hoistStatic: false,
|
|
180
|
+
hmr: false,
|
|
181
|
+
cacheHandlers: false,
|
|
182
|
+
nodeTransforms: [],
|
|
183
|
+
directiveTransforms: {},
|
|
184
|
+
transformHoist: null,
|
|
185
|
+
isBuiltInComponent: shared.NOOP,
|
|
186
|
+
isCustomElement: shared.NOOP,
|
|
187
|
+
expressionPlugins: [],
|
|
188
|
+
scopeId: null,
|
|
189
|
+
slotted: true,
|
|
190
|
+
ssr: false,
|
|
191
|
+
inSSR: false,
|
|
192
|
+
ssrCssVars: ``,
|
|
193
|
+
bindingMetadata: shared.EMPTY_OBJ,
|
|
194
|
+
inline: false,
|
|
195
|
+
isTS: false,
|
|
196
|
+
onError: compilerDom.defaultOnError,
|
|
197
|
+
onWarn: compilerDom.defaultOnWarn
|
|
198
|
+
};
|
|
199
|
+
function transform(node, options = {}) {
|
|
200
|
+
const ir = {
|
|
201
|
+
type: 0,
|
|
202
|
+
node,
|
|
203
|
+
source: node.source,
|
|
204
|
+
template: [],
|
|
205
|
+
component: /* @__PURE__ */ new Set(),
|
|
206
|
+
directive: /* @__PURE__ */ new Set(),
|
|
207
|
+
block: newBlock(node),
|
|
208
|
+
hasTemplateRef: false
|
|
209
|
+
};
|
|
210
|
+
const context = new TransformContext(ir, node, options);
|
|
211
|
+
transformNode(context);
|
|
212
|
+
return ir;
|
|
213
|
+
}
|
|
214
|
+
function transformNode(context) {
|
|
215
|
+
let { node } = context;
|
|
216
|
+
const { nodeTransforms } = context.options;
|
|
217
|
+
const exitFns = [];
|
|
218
|
+
for (const nodeTransform of nodeTransforms) {
|
|
219
|
+
const onExit = nodeTransform(node, context);
|
|
220
|
+
if (onExit) {
|
|
221
|
+
if (shared.isArray(onExit)) {
|
|
222
|
+
exitFns.push(...onExit);
|
|
223
|
+
} else {
|
|
224
|
+
exitFns.push(onExit);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (!context.node) {
|
|
228
|
+
return;
|
|
229
|
+
} else {
|
|
230
|
+
node = context.node;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
context.node = node;
|
|
234
|
+
let i = exitFns.length;
|
|
235
|
+
while (i--) {
|
|
236
|
+
exitFns[i]();
|
|
237
|
+
}
|
|
238
|
+
if (context.node.type === 0) {
|
|
239
|
+
context.registerTemplate();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function createStructuralDirectiveTransform(name, fn) {
|
|
243
|
+
const matches = (n) => shared.isString(name) ? n === name : name.includes(n);
|
|
244
|
+
return (node, context) => {
|
|
245
|
+
if (node.type === 1) {
|
|
246
|
+
const { props } = node;
|
|
247
|
+
if (node.tagType === 3 && props.some(compilerDom.isVSlot)) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const exitFns = [];
|
|
251
|
+
for (const prop of props) {
|
|
252
|
+
if (prop.type === 7 && matches(prop.name)) {
|
|
253
|
+
const onExit = fn(
|
|
254
|
+
node,
|
|
255
|
+
prop,
|
|
256
|
+
context
|
|
257
|
+
);
|
|
258
|
+
if (onExit) exitFns.push(onExit);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return exitFns;
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
function isStaticExpression(context, expressions) {
|
|
266
|
+
const {
|
|
267
|
+
options: { bindingMetadata }
|
|
268
|
+
} = context;
|
|
269
|
+
const isLiteralConst = (name) => bindingMetadata[name] === "literal-const";
|
|
270
|
+
return expressions.every((node) => {
|
|
271
|
+
if (node.ast) {
|
|
272
|
+
return compilerDom.isConstantNode(node.ast, isLiteralConst);
|
|
273
|
+
} else if (node.ast === null) {
|
|
274
|
+
return isLiteralConst(node.content);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const NEWLINE = Symbol(`newline` );
|
|
280
|
+
const LF = Symbol(`line feed` );
|
|
281
|
+
const INDENT_START = Symbol(`indent start` );
|
|
282
|
+
const INDENT_END = Symbol(`indent end` );
|
|
283
|
+
function buildCodeFragment(...frag) {
|
|
284
|
+
const push = frag.push.bind(frag);
|
|
285
|
+
const unshift = frag.unshift.bind(frag);
|
|
286
|
+
return [frag, push, unshift];
|
|
287
|
+
}
|
|
288
|
+
function genMulti([left, right, seg, placeholder], ...frags) {
|
|
289
|
+
if (placeholder) {
|
|
290
|
+
while (frags.length > 0 && !frags[frags.length - 1]) {
|
|
291
|
+
frags.pop();
|
|
292
|
+
}
|
|
293
|
+
frags = frags.map((frag2) => frag2 || placeholder);
|
|
294
|
+
} else {
|
|
295
|
+
frags = frags.filter(Boolean);
|
|
296
|
+
}
|
|
297
|
+
const frag = [];
|
|
298
|
+
push(left);
|
|
299
|
+
for (let [i, fn] of frags.entries()) {
|
|
300
|
+
push(fn);
|
|
301
|
+
if (i < frags.length - 1) push(seg);
|
|
302
|
+
}
|
|
303
|
+
push(right);
|
|
304
|
+
return frag;
|
|
305
|
+
function push(fn) {
|
|
306
|
+
if (!shared.isArray(fn)) fn = [fn];
|
|
307
|
+
frag.push(...fn);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const DELIMITERS_ARRAY = ["[", "]", ", "];
|
|
311
|
+
const DELIMITERS_ARRAY_NEWLINE = [
|
|
312
|
+
["[", INDENT_START, NEWLINE],
|
|
313
|
+
[INDENT_END, NEWLINE, "]"],
|
|
314
|
+
[", ", NEWLINE]
|
|
315
|
+
];
|
|
316
|
+
const DELIMITERS_OBJECT = ["{ ", " }", ", "];
|
|
317
|
+
const DELIMITERS_OBJECT_NEWLINE = [
|
|
318
|
+
["{", INDENT_START, NEWLINE],
|
|
319
|
+
[INDENT_END, NEWLINE, "}"],
|
|
320
|
+
[", ", NEWLINE]
|
|
321
|
+
];
|
|
322
|
+
function genCall(name, ...frags) {
|
|
323
|
+
const hasPlaceholder = shared.isArray(name);
|
|
324
|
+
const fnName = hasPlaceholder ? name[0] : name;
|
|
325
|
+
const placeholder = hasPlaceholder ? name[1] : "null";
|
|
326
|
+
return [fnName, ...genMulti(["(", ")", ", ", placeholder], ...frags)];
|
|
327
|
+
}
|
|
328
|
+
function codeFragmentToString(code, context) {
|
|
329
|
+
const {
|
|
330
|
+
options: { filename, sourceMap }
|
|
331
|
+
} = context;
|
|
332
|
+
let map;
|
|
333
|
+
if (sourceMap) {
|
|
334
|
+
map = new sourceMapJs.SourceMapGenerator();
|
|
335
|
+
map.setSourceContent(filename, context.ir.source);
|
|
336
|
+
map._sources.add(filename);
|
|
337
|
+
}
|
|
338
|
+
let codegen = "";
|
|
339
|
+
const pos = { line: 1, column: 1, offset: 0 };
|
|
340
|
+
let indentLevel = 0;
|
|
341
|
+
for (let frag of code) {
|
|
342
|
+
if (!frag) continue;
|
|
343
|
+
if (frag === NEWLINE) {
|
|
344
|
+
frag = [`
|
|
345
|
+
${` `.repeat(indentLevel)}`, 0];
|
|
346
|
+
} else if (frag === INDENT_START) {
|
|
347
|
+
indentLevel++;
|
|
348
|
+
continue;
|
|
349
|
+
} else if (frag === INDENT_END) {
|
|
350
|
+
indentLevel--;
|
|
351
|
+
continue;
|
|
352
|
+
} else if (frag === LF) {
|
|
353
|
+
pos.line++;
|
|
354
|
+
pos.column = 0;
|
|
355
|
+
pos.offset++;
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
if (shared.isString(frag)) frag = [frag];
|
|
359
|
+
let [code2, newlineIndex = -2, loc, name] = frag;
|
|
360
|
+
codegen += code2;
|
|
361
|
+
if (map) {
|
|
362
|
+
if (loc) addMapping(loc.start, name);
|
|
363
|
+
if (newlineIndex === -3) {
|
|
364
|
+
compilerDom.advancePositionWithMutation(pos, code2);
|
|
365
|
+
} else {
|
|
366
|
+
pos.offset += code2.length;
|
|
367
|
+
if (newlineIndex === -2) {
|
|
368
|
+
pos.column += code2.length;
|
|
369
|
+
} else {
|
|
370
|
+
if (newlineIndex === -1) {
|
|
371
|
+
newlineIndex = code2.length - 1;
|
|
372
|
+
}
|
|
373
|
+
pos.line++;
|
|
374
|
+
pos.column = code2.length - newlineIndex;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (loc && loc !== compilerDom.locStub) {
|
|
378
|
+
addMapping(loc.end);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return [codegen, map];
|
|
383
|
+
function addMapping(loc, name = null) {
|
|
384
|
+
const { _names, _mappings } = map;
|
|
385
|
+
if (name !== null && !_names.has(name)) _names.add(name);
|
|
386
|
+
_mappings.add({
|
|
387
|
+
originalLine: loc.line,
|
|
388
|
+
originalColumn: loc.column - 1,
|
|
389
|
+
// source-map column is 0 based
|
|
390
|
+
generatedLine: pos.line,
|
|
391
|
+
generatedColumn: pos.column - 1,
|
|
392
|
+
source: filename,
|
|
393
|
+
name
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function genInsertNode({ parent, elements, anchor }, { helper }) {
|
|
399
|
+
let element = elements.map((el) => `n${el}`).join(", ");
|
|
400
|
+
if (elements.length > 1) element = `[${element}]`;
|
|
401
|
+
return [
|
|
402
|
+
NEWLINE,
|
|
403
|
+
...genCall(
|
|
404
|
+
helper("insert"),
|
|
405
|
+
element,
|
|
406
|
+
`n${parent}`,
|
|
407
|
+
anchor === undefined ? undefined : `n${anchor}`
|
|
408
|
+
)
|
|
409
|
+
];
|
|
410
|
+
}
|
|
411
|
+
function genPrependNode(oper, { helper }) {
|
|
412
|
+
return [
|
|
413
|
+
NEWLINE,
|
|
414
|
+
...genCall(
|
|
415
|
+
helper("prepend"),
|
|
416
|
+
`n${oper.parent}`,
|
|
417
|
+
...oper.elements.map((el) => `n${el}`)
|
|
418
|
+
)
|
|
419
|
+
];
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function genExpression(node, context, assignment) {
|
|
423
|
+
const { content, ast, isStatic, loc } = node;
|
|
424
|
+
if (isStatic) {
|
|
425
|
+
return [[JSON.stringify(content), -2, loc]];
|
|
426
|
+
}
|
|
427
|
+
if (!node.content.trim() || // there was a parsing error
|
|
428
|
+
ast === false || isConstantExpression(node)) {
|
|
429
|
+
return [[content, -2, loc], assignment && ` = ${assignment}`];
|
|
430
|
+
}
|
|
431
|
+
if (ast === null) {
|
|
432
|
+
return genIdentifier(content, context, loc, assignment);
|
|
433
|
+
}
|
|
434
|
+
const ids = [];
|
|
435
|
+
const parentStackMap = /* @__PURE__ */ new Map();
|
|
436
|
+
const parentStack = [];
|
|
437
|
+
compilerDom.walkIdentifiers(
|
|
438
|
+
ast,
|
|
439
|
+
(id) => {
|
|
440
|
+
ids.push(id);
|
|
441
|
+
parentStackMap.set(id, parentStack.slice());
|
|
442
|
+
},
|
|
443
|
+
false,
|
|
444
|
+
parentStack
|
|
445
|
+
);
|
|
446
|
+
let hasMemberExpression = false;
|
|
447
|
+
if (ids.length) {
|
|
448
|
+
const [frag, push] = buildCodeFragment();
|
|
449
|
+
ids.sort((a, b) => a.start - b.start).forEach((id, i) => {
|
|
450
|
+
const start = id.start - 1;
|
|
451
|
+
const end = id.end - 1;
|
|
452
|
+
const last = ids[i - 1];
|
|
453
|
+
const leadingText = content.slice(last ? last.end - 1 : 0, start);
|
|
454
|
+
if (leadingText.length) push([leadingText, -3]);
|
|
455
|
+
const source = content.slice(start, end);
|
|
456
|
+
const parentStack2 = parentStackMap.get(id);
|
|
457
|
+
const parent = parentStack2[parentStack2.length - 1];
|
|
458
|
+
hasMemberExpression || (hasMemberExpression = parent && (parent.type === "MemberExpression" || parent.type === "OptionalMemberExpression"));
|
|
459
|
+
push(
|
|
460
|
+
...genIdentifier(
|
|
461
|
+
source,
|
|
462
|
+
context,
|
|
463
|
+
{
|
|
464
|
+
start: compilerDom.advancePositionWithClone(node.loc.start, source, start),
|
|
465
|
+
end: compilerDom.advancePositionWithClone(node.loc.start, source, end),
|
|
466
|
+
source
|
|
467
|
+
},
|
|
468
|
+
hasMemberExpression ? undefined : assignment,
|
|
469
|
+
id,
|
|
470
|
+
parent,
|
|
471
|
+
parentStack2
|
|
472
|
+
)
|
|
473
|
+
);
|
|
474
|
+
if (i === ids.length - 1 && end < content.length) {
|
|
475
|
+
push([content.slice(end), -3]);
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
if (assignment && hasMemberExpression) {
|
|
479
|
+
push(` = ${assignment}`);
|
|
480
|
+
}
|
|
481
|
+
return frag;
|
|
482
|
+
} else {
|
|
483
|
+
return [[content, -3, loc]];
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
function genIdentifier(raw, context, loc, assignment, id, parent, parentStack) {
|
|
487
|
+
const { options, helper, identifiers } = context;
|
|
488
|
+
const { inline, bindingMetadata } = options;
|
|
489
|
+
let name = raw;
|
|
490
|
+
const idMap = identifiers[raw];
|
|
491
|
+
if (idMap && idMap.length) {
|
|
492
|
+
const replacement = idMap[0];
|
|
493
|
+
if (shared.isString(replacement)) {
|
|
494
|
+
return [[replacement, -2, loc]];
|
|
495
|
+
} else {
|
|
496
|
+
return genExpression(replacement, context, assignment);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
let prefix;
|
|
500
|
+
if (compilerDom.isStaticProperty(parent) && parent.shorthand) {
|
|
501
|
+
prefix = `${raw}: `;
|
|
502
|
+
}
|
|
503
|
+
const type = bindingMetadata && bindingMetadata[raw];
|
|
504
|
+
if (inline) {
|
|
505
|
+
switch (type) {
|
|
506
|
+
case "setup-let":
|
|
507
|
+
name = raw = assignment ? `_isRef(${raw}) ? (${raw}.value = ${assignment}) : (${raw} = ${assignment})` : unref();
|
|
508
|
+
break;
|
|
509
|
+
case "setup-ref":
|
|
510
|
+
name = raw = withAssignment(`${raw}.value`);
|
|
511
|
+
break;
|
|
512
|
+
case "setup-maybe-ref":
|
|
513
|
+
const isDestructureAssignment = parent && compilerDom.isInDestructureAssignment(parent, parentStack || []);
|
|
514
|
+
const isAssignmentLVal = parent && parent.type === "AssignmentExpression" && parent.left === id;
|
|
515
|
+
const isUpdateArg = parent && parent.type === "UpdateExpression" && parent.argument === id;
|
|
516
|
+
raw = isAssignmentLVal || isUpdateArg || isDestructureAssignment ? name = `${raw}.value` : assignment ? `${helper("isRef")}(${raw}) ? (${raw}.value = ${assignment}) : null` : unref();
|
|
517
|
+
break;
|
|
518
|
+
case "props":
|
|
519
|
+
raw = shared.genPropsAccessExp(raw);
|
|
520
|
+
break;
|
|
521
|
+
case "props-aliased":
|
|
522
|
+
raw = shared.genPropsAccessExp(bindingMetadata.__propsAliases[raw]);
|
|
523
|
+
break;
|
|
524
|
+
default:
|
|
525
|
+
raw = withAssignment(raw);
|
|
526
|
+
}
|
|
527
|
+
} else {
|
|
528
|
+
if (canPrefix(raw)) {
|
|
529
|
+
if (type === "props-aliased") {
|
|
530
|
+
raw = `$props['${bindingMetadata.__propsAliases[raw]}']`;
|
|
531
|
+
} else {
|
|
532
|
+
raw = `${type === "props" ? "$props" : "_ctx"}.${raw}`;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
raw = withAssignment(raw);
|
|
536
|
+
}
|
|
537
|
+
return [prefix, [raw, -2, loc, name]];
|
|
538
|
+
function withAssignment(s) {
|
|
539
|
+
return assignment ? `${s} = ${assignment}` : s;
|
|
540
|
+
}
|
|
541
|
+
function unref() {
|
|
542
|
+
return `${helper("unref")}(${raw})`;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
function canPrefix(name) {
|
|
546
|
+
if (shared.isGloballyAllowed(name)) {
|
|
547
|
+
return false;
|
|
548
|
+
}
|
|
549
|
+
if (
|
|
550
|
+
// special case for webpack compilation
|
|
551
|
+
name === "require" || name === "$props" || name === "$emit" || name === "$attrs" || name === "$slots"
|
|
552
|
+
)
|
|
553
|
+
return false;
|
|
554
|
+
return true;
|
|
555
|
+
}
|
|
556
|
+
function processExpressions(context, expressions) {
|
|
557
|
+
const { seenVariable, variableToExpMap, expToVariableMap, seenIdentifier } = analyzeExpressions(expressions);
|
|
558
|
+
const varDeclarations = processRepeatedVariables(
|
|
559
|
+
context,
|
|
560
|
+
seenVariable,
|
|
561
|
+
variableToExpMap,
|
|
562
|
+
expToVariableMap,
|
|
563
|
+
seenIdentifier
|
|
564
|
+
);
|
|
565
|
+
const expDeclarations = processRepeatedExpressions(
|
|
566
|
+
context,
|
|
567
|
+
expressions,
|
|
568
|
+
varDeclarations
|
|
569
|
+
);
|
|
570
|
+
return genDeclarations([...varDeclarations, ...expDeclarations], context);
|
|
571
|
+
}
|
|
572
|
+
function analyzeExpressions(expressions) {
|
|
573
|
+
const seenVariable = /* @__PURE__ */ Object.create(null);
|
|
574
|
+
const variableToExpMap = /* @__PURE__ */ new Map();
|
|
575
|
+
const expToVariableMap = /* @__PURE__ */ new Map();
|
|
576
|
+
const seenIdentifier = /* @__PURE__ */ new Set();
|
|
577
|
+
const registerVariable = (name, exp, isIdentifier) => {
|
|
578
|
+
if (isIdentifier) seenIdentifier.add(name);
|
|
579
|
+
seenVariable[name] = (seenVariable[name] || 0) + 1;
|
|
580
|
+
variableToExpMap.set(
|
|
581
|
+
name,
|
|
582
|
+
(variableToExpMap.get(name) || /* @__PURE__ */ new Set()).add(exp)
|
|
583
|
+
);
|
|
584
|
+
expToVariableMap.set(exp, (expToVariableMap.get(exp) || []).concat(name));
|
|
585
|
+
};
|
|
586
|
+
for (const exp of expressions) {
|
|
587
|
+
if (!exp.ast) {
|
|
588
|
+
exp.ast === null && registerVariable(exp.content, exp, true);
|
|
589
|
+
continue;
|
|
590
|
+
}
|
|
591
|
+
estreeWalker.walk(exp.ast, {
|
|
592
|
+
enter(currentNode) {
|
|
593
|
+
if (currentNode.type === "MemberExpression") {
|
|
594
|
+
const memberExp = extractMemberExpression(
|
|
595
|
+
currentNode,
|
|
596
|
+
(name) => {
|
|
597
|
+
registerVariable(name, exp, true);
|
|
598
|
+
}
|
|
599
|
+
);
|
|
600
|
+
registerVariable(memberExp, exp, false);
|
|
601
|
+
return this.skip();
|
|
602
|
+
}
|
|
603
|
+
if (currentNode.type === "Identifier") {
|
|
604
|
+
registerVariable(currentNode.name, exp, true);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
return { seenVariable, seenIdentifier, variableToExpMap, expToVariableMap };
|
|
610
|
+
}
|
|
611
|
+
function processRepeatedVariables(context, seenVariable, variableToExpMap, expToVariableMap, seenIdentifier) {
|
|
612
|
+
const declarations = [];
|
|
613
|
+
for (const [name, exps] of variableToExpMap) {
|
|
614
|
+
if (seenVariable[name] > 1 && exps.size > 0) {
|
|
615
|
+
const isIdentifier = seenIdentifier.has(name);
|
|
616
|
+
const varName = isIdentifier ? name : genVarName(name);
|
|
617
|
+
const replaceRE = new RegExp(escapeRegExp(name), "g");
|
|
618
|
+
exps.forEach((node) => {
|
|
619
|
+
if (node.ast) {
|
|
620
|
+
node.content = node.content.replace(replaceRE, varName);
|
|
621
|
+
node.ast = parseExp(context, node.content);
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
if (!declarations.some((d) => d.name === varName) && (!isIdentifier || shouldDeclareVariable(name, expToVariableMap, exps))) {
|
|
625
|
+
declarations.push({
|
|
626
|
+
name: varName,
|
|
627
|
+
isIdentifier,
|
|
628
|
+
value: shared.extend(
|
|
629
|
+
{ ast: isIdentifier ? null : parseExp(context, name) },
|
|
630
|
+
compilerDom.createSimpleExpression(name)
|
|
631
|
+
),
|
|
632
|
+
rawName: name,
|
|
633
|
+
exps,
|
|
634
|
+
seenCount: seenVariable[name]
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
return declarations;
|
|
640
|
+
}
|
|
641
|
+
function shouldDeclareVariable(name, expToVariableMap, exps) {
|
|
642
|
+
const vars = Array.from(exps, (exp) => expToVariableMap.get(exp));
|
|
643
|
+
if (vars.every((v) => v.length === 1)) {
|
|
644
|
+
return true;
|
|
645
|
+
}
|
|
646
|
+
if (vars.some((v) => v.filter((e) => e === name).length > 1)) {
|
|
647
|
+
return true;
|
|
648
|
+
}
|
|
649
|
+
const first = vars[0];
|
|
650
|
+
if (vars.some((v) => v.length !== first.length)) {
|
|
651
|
+
if (vars.some(
|
|
652
|
+
(v) => v.length > first.length && v.every((e) => first.includes(e))
|
|
653
|
+
) || vars.some((v) => first.length > v.length && first.every((e) => v.includes(e)))) {
|
|
654
|
+
return false;
|
|
655
|
+
}
|
|
656
|
+
return true;
|
|
657
|
+
}
|
|
658
|
+
if (vars.some((v) => v.some((e) => first.includes(e)))) {
|
|
659
|
+
return false;
|
|
660
|
+
}
|
|
661
|
+
return true;
|
|
662
|
+
}
|
|
663
|
+
function processRepeatedExpressions(context, expressions, varDeclarations) {
|
|
664
|
+
const declarations = [];
|
|
665
|
+
const seenExp = expressions.reduce(
|
|
666
|
+
(acc, exp) => {
|
|
667
|
+
if (exp.ast && exp.ast.type !== "Identifier") {
|
|
668
|
+
acc[exp.content] = (acc[exp.content] || 0) + 1;
|
|
669
|
+
}
|
|
670
|
+
return acc;
|
|
671
|
+
},
|
|
672
|
+
/* @__PURE__ */ Object.create(null)
|
|
673
|
+
);
|
|
674
|
+
Object.entries(seenExp).forEach(([content, count]) => {
|
|
675
|
+
if (count > 1) {
|
|
676
|
+
const varName = genVarName(content);
|
|
677
|
+
if (!declarations.some((d) => d.name === varName)) {
|
|
678
|
+
const delVars = {};
|
|
679
|
+
for (let i = varDeclarations.length - 1; i >= 0; i--) {
|
|
680
|
+
const item = varDeclarations[i];
|
|
681
|
+
if (!item.exps || !item.seenCount) continue;
|
|
682
|
+
const shouldRemove = [...item.exps].every(
|
|
683
|
+
(node) => node.content === content && item.seenCount === count
|
|
684
|
+
);
|
|
685
|
+
if (shouldRemove) {
|
|
686
|
+
delVars[item.name] = item.rawName;
|
|
687
|
+
varDeclarations.splice(i, 1);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
const value = shared.extend(
|
|
691
|
+
{},
|
|
692
|
+
expressions.find((exp) => exp.content === content)
|
|
693
|
+
);
|
|
694
|
+
Object.keys(delVars).forEach((name) => {
|
|
695
|
+
value.content = value.content.replace(name, delVars[name]);
|
|
696
|
+
if (value.ast) value.ast = parseExp(context, value.content);
|
|
697
|
+
});
|
|
698
|
+
declarations.push({
|
|
699
|
+
name: varName,
|
|
700
|
+
value
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
expressions.forEach((exp) => {
|
|
704
|
+
if (exp.content === content) {
|
|
705
|
+
exp.content = varName;
|
|
706
|
+
exp.ast = null;
|
|
707
|
+
} else if (exp.content.includes(content)) {
|
|
708
|
+
exp.content = exp.content.replace(
|
|
709
|
+
new RegExp(escapeRegExp(content), "g"),
|
|
710
|
+
varName
|
|
711
|
+
);
|
|
712
|
+
exp.ast = parseExp(context, exp.content);
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
return declarations;
|
|
718
|
+
}
|
|
719
|
+
function genDeclarations(declarations, context) {
|
|
720
|
+
const [frag, push] = buildCodeFragment();
|
|
721
|
+
const ids = /* @__PURE__ */ Object.create(null);
|
|
722
|
+
declarations.forEach(({ name, isIdentifier, value }) => {
|
|
723
|
+
if (isIdentifier) {
|
|
724
|
+
const varName = ids[name] = `_${name}`;
|
|
725
|
+
push(`const ${varName} = `, ...genExpression(value, context), NEWLINE);
|
|
726
|
+
}
|
|
727
|
+
});
|
|
728
|
+
declarations.forEach(({ name, isIdentifier, value }) => {
|
|
729
|
+
if (!isIdentifier) {
|
|
730
|
+
const varName = ids[name] = `_${name}`;
|
|
731
|
+
push(
|
|
732
|
+
`const ${varName} = `,
|
|
733
|
+
...context.withId(() => genExpression(value, context), ids),
|
|
734
|
+
NEWLINE
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
});
|
|
738
|
+
return { ids, frag };
|
|
739
|
+
}
|
|
740
|
+
function escapeRegExp(string) {
|
|
741
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
742
|
+
}
|
|
743
|
+
function parseExp(context, content) {
|
|
744
|
+
const plugins = context.options.expressionPlugins;
|
|
745
|
+
const options = {
|
|
746
|
+
plugins: plugins ? [...plugins, "typescript"] : ["typescript"]
|
|
747
|
+
};
|
|
748
|
+
return parser.parseExpression(`(${content})`, options);
|
|
749
|
+
}
|
|
750
|
+
function genVarName(exp) {
|
|
751
|
+
return `${exp.replace(/[^a-zA-Z0-9]/g, "_").replace(/_+/g, "_").replace(/_+$/, "")}`;
|
|
752
|
+
}
|
|
753
|
+
function extractMemberExpression(exp, onIdentifier) {
|
|
754
|
+
if (!exp) return "";
|
|
755
|
+
switch (exp.type) {
|
|
756
|
+
case "Identifier":
|
|
757
|
+
onIdentifier(exp.name);
|
|
758
|
+
return exp.name;
|
|
759
|
+
case "StringLiteral":
|
|
760
|
+
return exp.extra ? exp.extra.raw : exp.value;
|
|
761
|
+
case "NumericLiteral":
|
|
762
|
+
return exp.value.toString();
|
|
763
|
+
case "BinaryExpression":
|
|
764
|
+
return `${extractMemberExpression(exp.left, onIdentifier)} ${exp.operator} ${extractMemberExpression(exp.right, onIdentifier)}`;
|
|
765
|
+
case "CallExpression":
|
|
766
|
+
return `${extractMemberExpression(exp.callee, onIdentifier)}(${exp.arguments.map((arg) => extractMemberExpression(arg, onIdentifier)).join(", ")})`;
|
|
767
|
+
case "MemberExpression":
|
|
768
|
+
const object = extractMemberExpression(exp.object, onIdentifier);
|
|
769
|
+
const prop = exp.computed ? `[${extractMemberExpression(exp.property, onIdentifier)}]` : `.${extractMemberExpression(exp.property, shared.NOOP)}`;
|
|
770
|
+
return `${object}${prop}`;
|
|
771
|
+
default:
|
|
772
|
+
return "";
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
function genSetEvent(oper, context) {
|
|
777
|
+
const { helper } = context;
|
|
778
|
+
const { element, key, keyOverride, value, modifiers, delegate, effect } = oper;
|
|
779
|
+
const name = genName();
|
|
780
|
+
const handler = genEventHandler(context, value, modifiers);
|
|
781
|
+
const eventOptions = genEventOptions();
|
|
782
|
+
if (delegate) {
|
|
783
|
+
context.delegates.add(key.content);
|
|
784
|
+
}
|
|
785
|
+
return [
|
|
786
|
+
NEWLINE,
|
|
787
|
+
...genCall(
|
|
788
|
+
helper(delegate ? "delegate" : "on"),
|
|
789
|
+
`n${element}`,
|
|
790
|
+
name,
|
|
791
|
+
handler,
|
|
792
|
+
eventOptions
|
|
793
|
+
)
|
|
794
|
+
];
|
|
795
|
+
function genName() {
|
|
796
|
+
const expr = genExpression(key, context);
|
|
797
|
+
if (keyOverride) {
|
|
798
|
+
const find = JSON.stringify(keyOverride[0]);
|
|
799
|
+
const replacement = JSON.stringify(keyOverride[1]);
|
|
800
|
+
const wrapped = ["(", ...expr, ")"];
|
|
801
|
+
return [...wrapped, ` === ${find} ? ${replacement} : `, ...wrapped];
|
|
802
|
+
} else {
|
|
803
|
+
return genExpression(key, context);
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
function genEventOptions() {
|
|
807
|
+
let { options } = modifiers;
|
|
808
|
+
if (!options.length && !effect) return;
|
|
809
|
+
return genMulti(
|
|
810
|
+
DELIMITERS_OBJECT_NEWLINE,
|
|
811
|
+
effect && ["effect: true"],
|
|
812
|
+
...options.map((option) => [`${option}: true`])
|
|
813
|
+
);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
function genSetDynamicEvents(oper, context) {
|
|
817
|
+
const { helper } = context;
|
|
818
|
+
return [
|
|
819
|
+
NEWLINE,
|
|
820
|
+
...genCall(
|
|
821
|
+
helper("setDynamicEvents"),
|
|
822
|
+
`n${oper.element}`,
|
|
823
|
+
genExpression(oper.event, context)
|
|
824
|
+
)
|
|
825
|
+
];
|
|
826
|
+
}
|
|
827
|
+
function genEventHandler(context, value, modifiers = { nonKeys: [], keys: [] }, needWrap = true) {
|
|
828
|
+
let handlerExp = [`() => {}`];
|
|
829
|
+
if (value && value.content.trim()) {
|
|
830
|
+
const isMemberExp = compilerDom.isMemberExpression(value, context.options);
|
|
831
|
+
const isInlineStatement = !(isMemberExp || compilerDom.isFnExpression(value, context.options));
|
|
832
|
+
if (isInlineStatement) {
|
|
833
|
+
const expr = context.withId(() => genExpression(value, context), {
|
|
834
|
+
$event: null
|
|
835
|
+
});
|
|
836
|
+
const hasMultipleStatements = value.content.includes(`;`);
|
|
837
|
+
handlerExp = [
|
|
838
|
+
"$event => ",
|
|
839
|
+
hasMultipleStatements ? "{" : "(",
|
|
840
|
+
...expr,
|
|
841
|
+
hasMultipleStatements ? "}" : ")"
|
|
842
|
+
];
|
|
843
|
+
} else {
|
|
844
|
+
handlerExp = [...genExpression(value, context)];
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
const { keys, nonKeys } = modifiers;
|
|
848
|
+
if (nonKeys.length)
|
|
849
|
+
handlerExp = genWithModifiers(context, handlerExp, nonKeys);
|
|
850
|
+
if (keys.length) handlerExp = genWithKeys(context, handlerExp, keys);
|
|
851
|
+
if (needWrap) handlerExp.unshift(`() => `);
|
|
852
|
+
return handlerExp;
|
|
853
|
+
}
|
|
854
|
+
function genWithModifiers(context, handler, nonKeys) {
|
|
855
|
+
return genCall(
|
|
856
|
+
context.helper("withModifiers"),
|
|
857
|
+
handler,
|
|
858
|
+
JSON.stringify(nonKeys)
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
function genWithKeys(context, handler, keys) {
|
|
862
|
+
return genCall(context.helper("withKeys"), handler, JSON.stringify(keys));
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
function genFor(oper, context) {
|
|
866
|
+
const { helper } = context;
|
|
867
|
+
const { source, value, key, index, render, keyProp, once, id, component } = oper;
|
|
868
|
+
let rawValue = null;
|
|
869
|
+
const rawKey = key && key.content;
|
|
870
|
+
const rawIndex = index && index.content;
|
|
871
|
+
const sourceExpr = ["() => (", ...genExpression(source, context), ")"];
|
|
872
|
+
const idToPathMap = parseValueDestructure();
|
|
873
|
+
const [depth, exitScope] = context.enterScope();
|
|
874
|
+
const idMap = {};
|
|
875
|
+
const itemVar = `_for_item${depth}`;
|
|
876
|
+
idMap[itemVar] = null;
|
|
877
|
+
idToPathMap.forEach((pathInfo, id2) => {
|
|
878
|
+
let path = `${itemVar}.value${pathInfo ? pathInfo.path : ""}`;
|
|
879
|
+
if (pathInfo) {
|
|
880
|
+
if (pathInfo.helper) {
|
|
881
|
+
idMap[pathInfo.helper] = null;
|
|
882
|
+
path = `${pathInfo.helper}(${path}, ${pathInfo.helperArgs})`;
|
|
883
|
+
}
|
|
884
|
+
if (pathInfo.dynamic) {
|
|
885
|
+
const node = idMap[id2] = compilerDom.createSimpleExpression(path);
|
|
886
|
+
const plugins = context.options.expressionPlugins;
|
|
887
|
+
node.ast = parser.parseExpression(`(${path})`, {
|
|
888
|
+
plugins: plugins ? [...plugins, "typescript"] : ["typescript"]
|
|
889
|
+
});
|
|
890
|
+
} else {
|
|
891
|
+
idMap[id2] = path;
|
|
892
|
+
}
|
|
893
|
+
} else {
|
|
894
|
+
idMap[id2] = path;
|
|
895
|
+
}
|
|
896
|
+
});
|
|
897
|
+
const args = [itemVar];
|
|
898
|
+
if (rawKey) {
|
|
899
|
+
const keyVar = `_for_key${depth}`;
|
|
900
|
+
args.push(`, ${keyVar}`);
|
|
901
|
+
idMap[rawKey] = `${keyVar}.value`;
|
|
902
|
+
idMap[keyVar] = null;
|
|
903
|
+
}
|
|
904
|
+
if (rawIndex) {
|
|
905
|
+
const indexVar = `_for_index${depth}`;
|
|
906
|
+
args.push(`, ${indexVar}`);
|
|
907
|
+
idMap[rawIndex] = `${indexVar}.value`;
|
|
908
|
+
idMap[indexVar] = null;
|
|
909
|
+
}
|
|
910
|
+
const blockFn = context.withId(() => genBlock(render, context, args), idMap);
|
|
911
|
+
exitScope();
|
|
912
|
+
return [
|
|
913
|
+
NEWLINE,
|
|
914
|
+
`const n${id} = `,
|
|
915
|
+
...genCall(
|
|
916
|
+
helper("createFor"),
|
|
917
|
+
sourceExpr,
|
|
918
|
+
blockFn,
|
|
919
|
+
genCallback(keyProp),
|
|
920
|
+
component && "true",
|
|
921
|
+
once && "true"
|
|
922
|
+
// todo: hydrationNode
|
|
923
|
+
)
|
|
924
|
+
];
|
|
925
|
+
function parseValueDestructure() {
|
|
926
|
+
const map = /* @__PURE__ */ new Map();
|
|
927
|
+
if (value) {
|
|
928
|
+
rawValue = value && value.content;
|
|
929
|
+
if (value.ast) {
|
|
930
|
+
compilerDom.walkIdentifiers(
|
|
931
|
+
value.ast,
|
|
932
|
+
(id2, _, parentStack, ___, isLocal) => {
|
|
933
|
+
if (isLocal) {
|
|
934
|
+
let path = "";
|
|
935
|
+
let isDynamic = false;
|
|
936
|
+
let helper2;
|
|
937
|
+
let helperArgs;
|
|
938
|
+
for (let i = 0; i < parentStack.length; i++) {
|
|
939
|
+
const parent = parentStack[i];
|
|
940
|
+
const child = parentStack[i + 1] || id2;
|
|
941
|
+
if (parent.type === "ObjectProperty" && parent.value === child) {
|
|
942
|
+
if (parent.key.type === "StringLiteral") {
|
|
943
|
+
path += `[${JSON.stringify(parent.key.value)}]`;
|
|
944
|
+
} else if (parent.computed) {
|
|
945
|
+
isDynamic = true;
|
|
946
|
+
path += `[${value.content.slice(
|
|
947
|
+
parent.key.start - 1,
|
|
948
|
+
parent.key.end - 1
|
|
949
|
+
)}]`;
|
|
950
|
+
} else {
|
|
951
|
+
path += `.${parent.key.name}`;
|
|
952
|
+
}
|
|
953
|
+
} else if (parent.type === "ArrayPattern") {
|
|
954
|
+
const index2 = parent.elements.indexOf(child);
|
|
955
|
+
if (child.type === "RestElement") {
|
|
956
|
+
path += `.slice(${index2})`;
|
|
957
|
+
} else {
|
|
958
|
+
path += `[${index2}]`;
|
|
959
|
+
}
|
|
960
|
+
} else if (parent.type === "ObjectPattern" && child.type === "RestElement") {
|
|
961
|
+
helper2 = context.helper("getRestElement");
|
|
962
|
+
helperArgs = "[" + parent.properties.filter((p) => p.type === "ObjectProperty").map((p) => {
|
|
963
|
+
if (p.key.type === "StringLiteral") {
|
|
964
|
+
return JSON.stringify(p.key.value);
|
|
965
|
+
} else if (p.computed) {
|
|
966
|
+
isDynamic = true;
|
|
967
|
+
return value.content.slice(
|
|
968
|
+
p.key.start - 1,
|
|
969
|
+
p.key.end - 1
|
|
970
|
+
);
|
|
971
|
+
} else {
|
|
972
|
+
return JSON.stringify(p.key.name);
|
|
973
|
+
}
|
|
974
|
+
}).join(", ") + "]";
|
|
975
|
+
}
|
|
976
|
+
if (child.type === "AssignmentPattern" && (parent.type === "ObjectProperty" || parent.type === "ArrayPattern")) {
|
|
977
|
+
isDynamic = true;
|
|
978
|
+
helper2 = context.helper("getDefaultValue");
|
|
979
|
+
helperArgs = value.content.slice(
|
|
980
|
+
child.right.start - 1,
|
|
981
|
+
child.right.end - 1
|
|
982
|
+
);
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
map.set(id2.name, { path, dynamic: isDynamic, helper: helper2, helperArgs });
|
|
986
|
+
}
|
|
987
|
+
},
|
|
988
|
+
true
|
|
989
|
+
);
|
|
990
|
+
} else {
|
|
991
|
+
map.set(rawValue, null);
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
return map;
|
|
995
|
+
}
|
|
996
|
+
function genCallback(expr) {
|
|
997
|
+
if (!expr) return false;
|
|
998
|
+
const res = context.withId(
|
|
999
|
+
() => genExpression(expr, context),
|
|
1000
|
+
genSimpleIdMap()
|
|
1001
|
+
);
|
|
1002
|
+
return [
|
|
1003
|
+
...genMulti(
|
|
1004
|
+
["(", ")", ", "],
|
|
1005
|
+
rawValue ? rawValue : rawKey || rawIndex ? "_" : undefined,
|
|
1006
|
+
rawKey ? rawKey : rawIndex ? "__" : undefined,
|
|
1007
|
+
rawIndex
|
|
1008
|
+
),
|
|
1009
|
+
" => (",
|
|
1010
|
+
...res,
|
|
1011
|
+
")"
|
|
1012
|
+
];
|
|
1013
|
+
}
|
|
1014
|
+
function genSimpleIdMap() {
|
|
1015
|
+
const idMap2 = {};
|
|
1016
|
+
if (rawKey) idMap2[rawKey] = null;
|
|
1017
|
+
if (rawIndex) idMap2[rawIndex] = null;
|
|
1018
|
+
idToPathMap.forEach((_, id2) => idMap2[id2] = null);
|
|
1019
|
+
return idMap2;
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
function genSetHtml(oper, context) {
|
|
1024
|
+
const { helper } = context;
|
|
1025
|
+
const { value, element } = oper;
|
|
1026
|
+
return [
|
|
1027
|
+
NEWLINE,
|
|
1028
|
+
...genCall(helper("setHtml"), `n${element}`, genExpression(value, context))
|
|
1029
|
+
];
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
function genIf(oper, context, isNested = false) {
|
|
1033
|
+
const { helper } = context;
|
|
1034
|
+
const { condition, positive, negative, once } = oper;
|
|
1035
|
+
const [frag, push] = buildCodeFragment();
|
|
1036
|
+
const conditionExpr = [
|
|
1037
|
+
"() => (",
|
|
1038
|
+
...genExpression(condition, context),
|
|
1039
|
+
")"
|
|
1040
|
+
];
|
|
1041
|
+
let positiveArg = genBlock(positive, context);
|
|
1042
|
+
let negativeArg = false;
|
|
1043
|
+
if (negative) {
|
|
1044
|
+
if (negative.type === 1) {
|
|
1045
|
+
negativeArg = genBlock(negative, context);
|
|
1046
|
+
} else {
|
|
1047
|
+
negativeArg = ["() => ", ...genIf(negative, context, true)];
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
if (!isNested) push(NEWLINE, `const n${oper.id} = `);
|
|
1051
|
+
push(
|
|
1052
|
+
...genCall(
|
|
1053
|
+
helper("createIf"),
|
|
1054
|
+
conditionExpr,
|
|
1055
|
+
positiveArg,
|
|
1056
|
+
negativeArg,
|
|
1057
|
+
once && "true"
|
|
1058
|
+
)
|
|
1059
|
+
);
|
|
1060
|
+
return frag;
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
const locStub = {
|
|
1064
|
+
start: { line: 1, column: 1, offset: 0 },
|
|
1065
|
+
end: { line: 1, column: 1, offset: 0 },
|
|
1066
|
+
source: ""
|
|
1067
|
+
};
|
|
1068
|
+
function createSimpleExpression(content, isStatic = false, loc = locStub, constType = 0) {
|
|
1069
|
+
return {
|
|
1070
|
+
type: 4,
|
|
1071
|
+
loc,
|
|
1072
|
+
content,
|
|
1073
|
+
isStatic,
|
|
1074
|
+
constType: isStatic ? 3 : constType
|
|
1075
|
+
};
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
function createCompilerError(code, loc, messages, additionalMessage) {
|
|
1079
|
+
const msg = (errorMessages)[code] + (``) ;
|
|
1080
|
+
const error = new SyntaxError(String(msg));
|
|
1081
|
+
error.code = code;
|
|
1082
|
+
error.loc = loc;
|
|
1083
|
+
return error;
|
|
1084
|
+
}
|
|
1085
|
+
const errorMessages = {
|
|
1086
|
+
// parse errors
|
|
1087
|
+
[0]: "Illegal comment.",
|
|
1088
|
+
[1]: "CDATA section is allowed only in XML context.",
|
|
1089
|
+
[2]: "Duplicate attribute.",
|
|
1090
|
+
[3]: "End tag cannot have attributes.",
|
|
1091
|
+
[4]: "Illegal '/' in tags.",
|
|
1092
|
+
[5]: "Unexpected EOF in tag.",
|
|
1093
|
+
[6]: "Unexpected EOF in CDATA section.",
|
|
1094
|
+
[7]: "Unexpected EOF in comment.",
|
|
1095
|
+
[8]: "Unexpected EOF in script.",
|
|
1096
|
+
[9]: "Unexpected EOF in tag.",
|
|
1097
|
+
[10]: "Incorrectly closed comment.",
|
|
1098
|
+
[11]: "Incorrectly opened comment.",
|
|
1099
|
+
[12]: "Illegal tag name. Use '<' to print '<'.",
|
|
1100
|
+
[13]: "Attribute value was expected.",
|
|
1101
|
+
[14]: "End tag name was expected.",
|
|
1102
|
+
[15]: "Whitespace was expected.",
|
|
1103
|
+
[16]: "Unexpected '<!--' in comment.",
|
|
1104
|
+
[17]: `Attribute name cannot contain U+0022 ("), U+0027 ('), and U+003C (<).`,
|
|
1105
|
+
[18]: "Unquoted attribute value cannot contain U+0022 (\"), U+0027 ('), U+003C (<), U+003D (=), and U+0060 (`).",
|
|
1106
|
+
[19]: "Attribute name cannot start with '='.",
|
|
1107
|
+
[21]: "'<?' is allowed only in XML context.",
|
|
1108
|
+
[20]: `Unexpected null character.`,
|
|
1109
|
+
[22]: "Illegal '/' in tags.",
|
|
1110
|
+
// Vue-specific parse errors
|
|
1111
|
+
[23]: "Invalid end tag.",
|
|
1112
|
+
[24]: "Element is missing end tag.",
|
|
1113
|
+
[25]: "Interpolation end sign was not found.",
|
|
1114
|
+
[27]: "End bracket for dynamic directive argument was not found. Note that dynamic directive argument cannot contain spaces.",
|
|
1115
|
+
[26]: "Legal directive name was expected.",
|
|
1116
|
+
// transform errors
|
|
1117
|
+
[28]: `v-if/v-else-if is missing expression.`,
|
|
1118
|
+
[29]: `v-if/else branches must use unique keys.`,
|
|
1119
|
+
[30]: `v-else/v-else-if has no adjacent v-if or v-else-if.`,
|
|
1120
|
+
[31]: `v-for is missing expression.`,
|
|
1121
|
+
[32]: `v-for has invalid expression.`,
|
|
1122
|
+
[33]: `<template v-for> key should be placed on the <template> tag.`,
|
|
1123
|
+
[34]: `v-bind is missing expression.`,
|
|
1124
|
+
[52]: `v-bind with same-name shorthand only allows static argument.`,
|
|
1125
|
+
[35]: `v-on is missing expression.`,
|
|
1126
|
+
[36]: `Unexpected custom directive on <slot> outlet.`,
|
|
1127
|
+
[37]: `Mixed v-slot usage on both the component and nested <template>. When there are multiple named slots, all slots should use <template> syntax to avoid scope ambiguity.`,
|
|
1128
|
+
[38]: `Duplicate slot names found. `,
|
|
1129
|
+
[39]: `Extraneous children found when component already has explicitly named default slot. These children will be ignored.`,
|
|
1130
|
+
[40]: `v-slot can only be used on components or <template> tags.`,
|
|
1131
|
+
[41]: `v-model is missing expression.`,
|
|
1132
|
+
[42]: `v-model value must be a valid JavaScript member expression.`,
|
|
1133
|
+
[43]: `v-model cannot be used on v-for or v-slot scope variables because they are not writable.`,
|
|
1134
|
+
[44]: `v-model cannot be used on a prop, because local prop bindings are not writable.
|
|
1135
|
+
Use a v-bind binding combined with a v-on listener that emits update:x event instead.`,
|
|
1136
|
+
[45]: `Error parsing JavaScript expression: `,
|
|
1137
|
+
[46]: `<KeepAlive> expects exactly one child component.`,
|
|
1138
|
+
[51]: `@vnode-* hooks in templates are no longer supported. Use the vue: prefix instead. For example, @vnode-mounted should be changed to @vue:mounted. @vnode-* hooks support has been removed in 3.4.`,
|
|
1139
|
+
// generic errors
|
|
1140
|
+
[47]: `"prefixIdentifiers" option is not supported in this build of compiler.`,
|
|
1141
|
+
[48]: `ES module mode is not supported in this build of compiler.`,
|
|
1142
|
+
[49]: `"cacheHandlers" option is only supported when the "prefixIdentifiers" option is enabled.`,
|
|
1143
|
+
[50]: `"scopeId" option is only supported in module mode.`,
|
|
1144
|
+
// just to fulfill types
|
|
1145
|
+
[53]: ``
|
|
1146
|
+
};
|
|
1147
|
+
|
|
1148
|
+
function walkIdentifiers(root, onIdentifier, includeAll = false, parentStack = [], knownIds = /* @__PURE__ */ Object.create(null)) {
|
|
1149
|
+
const rootExp = root.type === "Program" ? root.body[0].type === "ExpressionStatement" && root.body[0].expression : root;
|
|
1150
|
+
estreeWalker.walk(root, {
|
|
1151
|
+
enter(node, parent) {
|
|
1152
|
+
parent && parentStack.push(parent);
|
|
1153
|
+
if (parent && parent.type.startsWith("TS") && !TS_NODE_TYPES.includes(parent.type)) {
|
|
1154
|
+
return this.skip();
|
|
1155
|
+
}
|
|
1156
|
+
if (node.type === "Identifier") {
|
|
1157
|
+
const isLocal = !!knownIds[node.name];
|
|
1158
|
+
const isRefed = isReferencedIdentifier(node, parent, parentStack);
|
|
1159
|
+
if (includeAll || isRefed && !isLocal) {
|
|
1160
|
+
onIdentifier(node, parent, parentStack, isRefed, isLocal);
|
|
1161
|
+
}
|
|
1162
|
+
} else if (node.type === "ObjectProperty" && // eslint-disable-next-line no-restricted-syntax
|
|
1163
|
+
(parent == null ? undefined : parent.type) === "ObjectPattern") {
|
|
1164
|
+
node.inPattern = true;
|
|
1165
|
+
} else if (isFunctionType(node)) {
|
|
1166
|
+
if (node.scopeIds) {
|
|
1167
|
+
node.scopeIds.forEach((id) => markKnownIds(id, knownIds));
|
|
1168
|
+
} else {
|
|
1169
|
+
walkFunctionParams(
|
|
1170
|
+
node,
|
|
1171
|
+
(id) => markScopeIdentifier(node, id, knownIds)
|
|
1172
|
+
);
|
|
1173
|
+
}
|
|
1174
|
+
} else if (node.type === "BlockStatement") {
|
|
1175
|
+
if (node.scopeIds) {
|
|
1176
|
+
node.scopeIds.forEach((id) => markKnownIds(id, knownIds));
|
|
1177
|
+
} else {
|
|
1178
|
+
walkBlockDeclarations(
|
|
1179
|
+
node,
|
|
1180
|
+
(id) => markScopeIdentifier(node, id, knownIds)
|
|
1181
|
+
);
|
|
1182
|
+
}
|
|
1183
|
+
} else if (node.type === "CatchClause" && node.param) {
|
|
1184
|
+
for (const id of extractIdentifiers(node.param)) {
|
|
1185
|
+
markScopeIdentifier(node, id, knownIds);
|
|
1186
|
+
}
|
|
1187
|
+
} else if (isForStatement(node)) {
|
|
1188
|
+
walkForStatement(
|
|
1189
|
+
node,
|
|
1190
|
+
false,
|
|
1191
|
+
(id) => markScopeIdentifier(node, id, knownIds)
|
|
1192
|
+
);
|
|
1193
|
+
}
|
|
1194
|
+
},
|
|
1195
|
+
leave(node, parent) {
|
|
1196
|
+
parent && parentStack.pop();
|
|
1197
|
+
if (node !== rootExp && node.scopeIds) {
|
|
1198
|
+
for (const id of node.scopeIds) {
|
|
1199
|
+
knownIds[id]--;
|
|
1200
|
+
if (knownIds[id] === 0) {
|
|
1201
|
+
delete knownIds[id];
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
});
|
|
1207
|
+
}
|
|
1208
|
+
function isReferencedIdentifier(id, parent, parentStack) {
|
|
1209
|
+
if (!parent) {
|
|
1210
|
+
return true;
|
|
1211
|
+
}
|
|
1212
|
+
if (id.name === "arguments") {
|
|
1213
|
+
return false;
|
|
1214
|
+
}
|
|
1215
|
+
if (isReferenced(id, parent)) {
|
|
1216
|
+
return true;
|
|
1217
|
+
}
|
|
1218
|
+
switch (parent.type) {
|
|
1219
|
+
case "AssignmentExpression":
|
|
1220
|
+
case "AssignmentPattern":
|
|
1221
|
+
return true;
|
|
1222
|
+
case "ObjectPattern":
|
|
1223
|
+
case "ArrayPattern":
|
|
1224
|
+
return isInDestructureAssignment(parent, parentStack);
|
|
1225
|
+
}
|
|
1226
|
+
return false;
|
|
1227
|
+
}
|
|
1228
|
+
function isInDestructureAssignment(parent, parentStack) {
|
|
1229
|
+
if (parent && (parent.type === "ObjectProperty" || parent.type === "ArrayPattern")) {
|
|
1230
|
+
let i = parentStack.length;
|
|
1231
|
+
while (i--) {
|
|
1232
|
+
const p = parentStack[i];
|
|
1233
|
+
if (p.type === "AssignmentExpression") {
|
|
1234
|
+
return true;
|
|
1235
|
+
} else if (p.type !== "ObjectProperty" && !p.type.endsWith("Pattern")) {
|
|
1236
|
+
break;
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
return false;
|
|
1241
|
+
}
|
|
1242
|
+
function walkFunctionParams(node, onIdent) {
|
|
1243
|
+
for (const p of node.params) {
|
|
1244
|
+
for (const id of extractIdentifiers(p)) {
|
|
1245
|
+
onIdent(id);
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
function walkBlockDeclarations(block, onIdent) {
|
|
1250
|
+
for (const stmt of block.body) {
|
|
1251
|
+
if (stmt.type === "VariableDeclaration") {
|
|
1252
|
+
if (stmt.declare) continue;
|
|
1253
|
+
for (const decl of stmt.declarations) {
|
|
1254
|
+
for (const id of extractIdentifiers(decl.id)) {
|
|
1255
|
+
onIdent(id);
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
} else if (stmt.type === "FunctionDeclaration" || stmt.type === "ClassDeclaration") {
|
|
1259
|
+
if (stmt.declare || !stmt.id) continue;
|
|
1260
|
+
onIdent(stmt.id);
|
|
1261
|
+
} else if (isForStatement(stmt)) {
|
|
1262
|
+
walkForStatement(stmt, true, onIdent);
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
function isForStatement(stmt) {
|
|
1267
|
+
return stmt.type === "ForOfStatement" || stmt.type === "ForInStatement" || stmt.type === "ForStatement";
|
|
1268
|
+
}
|
|
1269
|
+
function walkForStatement(stmt, isVar, onIdent) {
|
|
1270
|
+
const variable = stmt.type === "ForStatement" ? stmt.init : stmt.left;
|
|
1271
|
+
if (variable && variable.type === "VariableDeclaration" && (variable.kind === "var" ? isVar : !isVar)) {
|
|
1272
|
+
for (const decl of variable.declarations) {
|
|
1273
|
+
for (const id of extractIdentifiers(decl.id)) {
|
|
1274
|
+
onIdent(id);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
function extractIdentifiers(param, nodes = []) {
|
|
1280
|
+
switch (param.type) {
|
|
1281
|
+
case "Identifier":
|
|
1282
|
+
nodes.push(param);
|
|
1283
|
+
break;
|
|
1284
|
+
case "MemberExpression":
|
|
1285
|
+
let object = param;
|
|
1286
|
+
while (object.type === "MemberExpression") {
|
|
1287
|
+
object = object.object;
|
|
1288
|
+
}
|
|
1289
|
+
nodes.push(object);
|
|
1290
|
+
break;
|
|
1291
|
+
case "ObjectPattern":
|
|
1292
|
+
for (const prop of param.properties) {
|
|
1293
|
+
if (prop.type === "RestElement") {
|
|
1294
|
+
extractIdentifiers(prop.argument, nodes);
|
|
1295
|
+
} else {
|
|
1296
|
+
extractIdentifiers(prop.value, nodes);
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
break;
|
|
1300
|
+
case "ArrayPattern":
|
|
1301
|
+
param.elements.forEach((element) => {
|
|
1302
|
+
if (element) extractIdentifiers(element, nodes);
|
|
1303
|
+
});
|
|
1304
|
+
break;
|
|
1305
|
+
case "RestElement":
|
|
1306
|
+
extractIdentifiers(param.argument, nodes);
|
|
1307
|
+
break;
|
|
1308
|
+
case "AssignmentPattern":
|
|
1309
|
+
extractIdentifiers(param.left, nodes);
|
|
1310
|
+
break;
|
|
1311
|
+
}
|
|
1312
|
+
return nodes;
|
|
1313
|
+
}
|
|
1314
|
+
function markKnownIds(name, knownIds) {
|
|
1315
|
+
if (name in knownIds) {
|
|
1316
|
+
knownIds[name]++;
|
|
1317
|
+
} else {
|
|
1318
|
+
knownIds[name] = 1;
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
function markScopeIdentifier(node, child, knownIds) {
|
|
1322
|
+
const { name } = child;
|
|
1323
|
+
if (node.scopeIds && node.scopeIds.has(name)) {
|
|
1324
|
+
return;
|
|
1325
|
+
}
|
|
1326
|
+
markKnownIds(name, knownIds);
|
|
1327
|
+
(node.scopeIds || (node.scopeIds = /* @__PURE__ */ new Set())).add(name);
|
|
1328
|
+
}
|
|
1329
|
+
const isFunctionType = (node) => {
|
|
1330
|
+
return /Function(?:Expression|Declaration)$|Method$/.test(node.type);
|
|
1331
|
+
};
|
|
1332
|
+
function isReferenced(node, parent, grandparent) {
|
|
1333
|
+
switch (parent.type) {
|
|
1334
|
+
// yes: PARENT[NODE]
|
|
1335
|
+
// yes: NODE.child
|
|
1336
|
+
// no: parent.NODE
|
|
1337
|
+
case "MemberExpression":
|
|
1338
|
+
case "OptionalMemberExpression":
|
|
1339
|
+
if (parent.property === node) {
|
|
1340
|
+
return !!parent.computed;
|
|
1341
|
+
}
|
|
1342
|
+
return parent.object === node;
|
|
1343
|
+
case "JSXMemberExpression":
|
|
1344
|
+
return parent.object === node;
|
|
1345
|
+
// no: let NODE = init;
|
|
1346
|
+
// yes: let id = NODE;
|
|
1347
|
+
case "VariableDeclarator":
|
|
1348
|
+
return parent.init === node;
|
|
1349
|
+
// yes: () => NODE
|
|
1350
|
+
// no: (NODE) => {}
|
|
1351
|
+
case "ArrowFunctionExpression":
|
|
1352
|
+
return parent.body === node;
|
|
1353
|
+
// no: class { #NODE; }
|
|
1354
|
+
// no: class { get #NODE() {} }
|
|
1355
|
+
// no: class { #NODE() {} }
|
|
1356
|
+
// no: class { fn() { return this.#NODE; } }
|
|
1357
|
+
case "PrivateName":
|
|
1358
|
+
return false;
|
|
1359
|
+
// no: class { NODE() {} }
|
|
1360
|
+
// yes: class { [NODE]() {} }
|
|
1361
|
+
// no: class { foo(NODE) {} }
|
|
1362
|
+
case "ClassMethod":
|
|
1363
|
+
case "ClassPrivateMethod":
|
|
1364
|
+
case "ObjectMethod":
|
|
1365
|
+
if (parent.key === node) {
|
|
1366
|
+
return !!parent.computed;
|
|
1367
|
+
}
|
|
1368
|
+
return false;
|
|
1369
|
+
// yes: { [NODE]: "" }
|
|
1370
|
+
// no: { NODE: "" }
|
|
1371
|
+
// depends: { NODE }
|
|
1372
|
+
// depends: { key: NODE }
|
|
1373
|
+
case "ObjectProperty":
|
|
1374
|
+
if (parent.key === node) {
|
|
1375
|
+
return !!parent.computed;
|
|
1376
|
+
}
|
|
1377
|
+
return true;
|
|
1378
|
+
// no: class { NODE = value; }
|
|
1379
|
+
// yes: class { [NODE] = value; }
|
|
1380
|
+
// yes: class { key = NODE; }
|
|
1381
|
+
case "ClassProperty":
|
|
1382
|
+
if (parent.key === node) {
|
|
1383
|
+
return !!parent.computed;
|
|
1384
|
+
}
|
|
1385
|
+
return true;
|
|
1386
|
+
case "ClassPrivateProperty":
|
|
1387
|
+
return parent.key !== node;
|
|
1388
|
+
// no: class NODE {}
|
|
1389
|
+
// yes: class Foo extends NODE {}
|
|
1390
|
+
case "ClassDeclaration":
|
|
1391
|
+
case "ClassExpression":
|
|
1392
|
+
return parent.superClass === node;
|
|
1393
|
+
// yes: left = NODE;
|
|
1394
|
+
// no: NODE = right;
|
|
1395
|
+
case "AssignmentExpression":
|
|
1396
|
+
return parent.right === node;
|
|
1397
|
+
// no: [NODE = foo] = [];
|
|
1398
|
+
// yes: [foo = NODE] = [];
|
|
1399
|
+
case "AssignmentPattern":
|
|
1400
|
+
return parent.right === node;
|
|
1401
|
+
// no: NODE: for (;;) {}
|
|
1402
|
+
case "LabeledStatement":
|
|
1403
|
+
return false;
|
|
1404
|
+
// no: try {} catch (NODE) {}
|
|
1405
|
+
case "CatchClause":
|
|
1406
|
+
return false;
|
|
1407
|
+
// no: function foo(...NODE) {}
|
|
1408
|
+
case "RestElement":
|
|
1409
|
+
return false;
|
|
1410
|
+
case "BreakStatement":
|
|
1411
|
+
case "ContinueStatement":
|
|
1412
|
+
return false;
|
|
1413
|
+
// no: function NODE() {}
|
|
1414
|
+
// no: function foo(NODE) {}
|
|
1415
|
+
case "FunctionDeclaration":
|
|
1416
|
+
case "FunctionExpression":
|
|
1417
|
+
return false;
|
|
1418
|
+
// no: export NODE from "foo";
|
|
1419
|
+
// no: export * as NODE from "foo";
|
|
1420
|
+
case "ExportNamespaceSpecifier":
|
|
1421
|
+
case "ExportDefaultSpecifier":
|
|
1422
|
+
return false;
|
|
1423
|
+
// no: export { foo as NODE };
|
|
1424
|
+
// yes: export { NODE as foo };
|
|
1425
|
+
// no: export { NODE as foo } from "foo";
|
|
1426
|
+
case "ExportSpecifier":
|
|
1427
|
+
return parent.local === node;
|
|
1428
|
+
// no: import NODE from "foo";
|
|
1429
|
+
// no: import * as NODE from "foo";
|
|
1430
|
+
// no: import { NODE as foo } from "foo";
|
|
1431
|
+
// no: import { foo as NODE } from "foo";
|
|
1432
|
+
// no: import NODE from "bar";
|
|
1433
|
+
case "ImportDefaultSpecifier":
|
|
1434
|
+
case "ImportNamespaceSpecifier":
|
|
1435
|
+
case "ImportSpecifier":
|
|
1436
|
+
return false;
|
|
1437
|
+
// no: import "foo" assert { NODE: "json" }
|
|
1438
|
+
case "ImportAttribute":
|
|
1439
|
+
return false;
|
|
1440
|
+
// no: <div NODE="foo" />
|
|
1441
|
+
case "JSXAttribute":
|
|
1442
|
+
return false;
|
|
1443
|
+
// no: [NODE] = [];
|
|
1444
|
+
// no: ({ NODE }) = [];
|
|
1445
|
+
case "ObjectPattern":
|
|
1446
|
+
case "ArrayPattern":
|
|
1447
|
+
return false;
|
|
1448
|
+
// no: new.NODE
|
|
1449
|
+
// no: NODE.target
|
|
1450
|
+
case "MetaProperty":
|
|
1451
|
+
return false;
|
|
1452
|
+
// yes: type X = { someProperty: NODE }
|
|
1453
|
+
// no: type X = { NODE: OtherType }
|
|
1454
|
+
case "ObjectTypeProperty":
|
|
1455
|
+
return parent.key !== node;
|
|
1456
|
+
// yes: enum X { Foo = NODE }
|
|
1457
|
+
// no: enum X { NODE }
|
|
1458
|
+
case "TSEnumMember":
|
|
1459
|
+
return parent.id !== node;
|
|
1460
|
+
// yes: { [NODE]: value }
|
|
1461
|
+
// no: { NODE: value }
|
|
1462
|
+
case "TSPropertySignature":
|
|
1463
|
+
if (parent.key === node) {
|
|
1464
|
+
return !!parent.computed;
|
|
1465
|
+
}
|
|
1466
|
+
return true;
|
|
1467
|
+
}
|
|
1468
|
+
return true;
|
|
1469
|
+
}
|
|
1470
|
+
const TS_NODE_TYPES = [
|
|
1471
|
+
"TSAsExpression",
|
|
1472
|
+
// foo as number
|
|
1473
|
+
"TSTypeAssertion",
|
|
1474
|
+
// (<number>foo)
|
|
1475
|
+
"TSNonNullExpression",
|
|
1476
|
+
// foo!
|
|
1477
|
+
"TSInstantiationExpression",
|
|
1478
|
+
// foo<string>
|
|
1479
|
+
"TSSatisfiesExpression"
|
|
1480
|
+
// foo satisfies T
|
|
1481
|
+
];
|
|
1482
|
+
function unwrapTSNode(node) {
|
|
1483
|
+
if (TS_NODE_TYPES.includes(node.type)) {
|
|
1484
|
+
return unwrapTSNode(node.expression);
|
|
1485
|
+
} else {
|
|
1486
|
+
return node;
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
const isStaticExp = (p) => p.type === 4 && p.isStatic;
|
|
1491
|
+
const nonIdentifierRE = /^\d|[^\$\w\xA0-\uFFFF]/;
|
|
1492
|
+
const isSimpleIdentifier = (name) => !nonIdentifierRE.test(name);
|
|
1493
|
+
const getExpSource = (exp) => exp.type === 4 ? exp.content : exp.loc.source;
|
|
1494
|
+
const isMemberExpressionNode = (exp, context) => {
|
|
1495
|
+
try {
|
|
1496
|
+
let ret = exp.ast || parser.parseExpression(getExpSource(exp), {
|
|
1497
|
+
plugins: context.expressionPlugins ? [...context.expressionPlugins, "typescript"] : ["typescript"]
|
|
1498
|
+
});
|
|
1499
|
+
ret = unwrapTSNode(ret);
|
|
1500
|
+
return ret.type === "MemberExpression" || ret.type === "OptionalMemberExpression" || ret.type === "Identifier" && ret.name !== "undefined";
|
|
1501
|
+
} catch (e) {
|
|
1502
|
+
return false;
|
|
1503
|
+
}
|
|
1504
|
+
};
|
|
1505
|
+
const isMemberExpression = isMemberExpressionNode;
|
|
1506
|
+
function isStaticArgOf(arg, name) {
|
|
1507
|
+
return !!(arg && isStaticExp(arg) && arg.content === name);
|
|
1508
|
+
}
|
|
1509
|
+
function isVSlot(p) {
|
|
1510
|
+
return p.type === 7 && p.name === "slot";
|
|
1511
|
+
}
|
|
1512
|
+
function isTemplateNode(node) {
|
|
1513
|
+
return node.type === 1 && node.tagType === 3;
|
|
1514
|
+
}
|
|
1515
|
+
function toValidAssetId(name, type) {
|
|
1516
|
+
return `_${type}_${name.replace(/[^\w]/g, (searchValue, replaceValue) => {
|
|
1517
|
+
return searchValue === "-" ? "_" : name.charCodeAt(replaceValue).toString();
|
|
1518
|
+
})}`;
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
const helpers = {
|
|
1522
|
+
setText: { name: "setText" },
|
|
1523
|
+
setHtml: { name: "setHtml" },
|
|
1524
|
+
setClass: { name: "setClass" },
|
|
1525
|
+
setStyle: { name: "setStyle" },
|
|
1526
|
+
setValue: { name: "setValue" },
|
|
1527
|
+
setAttr: { name: "setAttr", needKey: true },
|
|
1528
|
+
setProp: { name: "setProp", needKey: true },
|
|
1529
|
+
setDOMProp: { name: "setDOMProp", needKey: true },
|
|
1530
|
+
setDynamicProps: { name: "setDynamicProps" }
|
|
1531
|
+
};
|
|
1532
|
+
function genSetProp(oper, context) {
|
|
1533
|
+
const { helper } = context;
|
|
1534
|
+
const {
|
|
1535
|
+
prop: { key, values, modifier },
|
|
1536
|
+
tag
|
|
1537
|
+
} = oper;
|
|
1538
|
+
const resolvedHelper = getRuntimeHelper(tag, key.content, modifier);
|
|
1539
|
+
const propValue = genPropValue(values, context);
|
|
1540
|
+
return [
|
|
1541
|
+
NEWLINE,
|
|
1542
|
+
...genCall(
|
|
1543
|
+
[helper(resolvedHelper.name), null],
|
|
1544
|
+
`n${oper.element}`,
|
|
1545
|
+
resolvedHelper.needKey ? genExpression(key, context) : false,
|
|
1546
|
+
propValue
|
|
1547
|
+
)
|
|
1548
|
+
];
|
|
1549
|
+
}
|
|
1550
|
+
function genDynamicProps$1(oper, context) {
|
|
1551
|
+
const { helper } = context;
|
|
1552
|
+
const values = oper.props.map(
|
|
1553
|
+
(props) => Array.isArray(props) ? genLiteralObjectProps(props, context) : props.kind === 1 ? genLiteralObjectProps([props], context) : genExpression(props.value, context)
|
|
1554
|
+
);
|
|
1555
|
+
return [
|
|
1556
|
+
NEWLINE,
|
|
1557
|
+
...genCall(
|
|
1558
|
+
helper("setDynamicProps"),
|
|
1559
|
+
`n${oper.element}`,
|
|
1560
|
+
genMulti(DELIMITERS_ARRAY, ...values),
|
|
1561
|
+
oper.root && "true"
|
|
1562
|
+
)
|
|
1563
|
+
];
|
|
1564
|
+
}
|
|
1565
|
+
function genLiteralObjectProps(props, context) {
|
|
1566
|
+
return genMulti(
|
|
1567
|
+
DELIMITERS_OBJECT,
|
|
1568
|
+
...props.map((prop) => [
|
|
1569
|
+
...genPropKey(prop, context),
|
|
1570
|
+
`: `,
|
|
1571
|
+
...genPropValue(prop.values, context)
|
|
1572
|
+
])
|
|
1573
|
+
);
|
|
1574
|
+
}
|
|
1575
|
+
function genPropKey({ key: node, modifier, runtimeCamelize, handler, handlerModifiers }, context) {
|
|
1576
|
+
const { helper } = context;
|
|
1577
|
+
const handlerModifierPostfix = handlerModifiers ? handlerModifiers.map(shared.capitalize).join("") : "";
|
|
1578
|
+
if (node.isStatic) {
|
|
1579
|
+
const keyName = (handler ? shared.toHandlerKey(node.content) : node.content) + handlerModifierPostfix;
|
|
1580
|
+
return [
|
|
1581
|
+
[
|
|
1582
|
+
isSimpleIdentifier(keyName) ? keyName : JSON.stringify(keyName),
|
|
1583
|
+
-2,
|
|
1584
|
+
node.loc
|
|
1585
|
+
]
|
|
1586
|
+
];
|
|
1587
|
+
}
|
|
1588
|
+
let key = genExpression(node, context);
|
|
1589
|
+
if (runtimeCamelize) {
|
|
1590
|
+
key = genCall(helper("camelize"), key);
|
|
1591
|
+
}
|
|
1592
|
+
if (handler) {
|
|
1593
|
+
key = genCall(helper("toHandlerKey"), key);
|
|
1594
|
+
}
|
|
1595
|
+
return [
|
|
1596
|
+
"[",
|
|
1597
|
+
modifier && `${JSON.stringify(modifier)} + `,
|
|
1598
|
+
...key,
|
|
1599
|
+
handlerModifierPostfix ? ` + ${JSON.stringify(handlerModifierPostfix)}` : undefined,
|
|
1600
|
+
"]"
|
|
1601
|
+
];
|
|
1602
|
+
}
|
|
1603
|
+
function genPropValue(values, context) {
|
|
1604
|
+
if (values.length === 1) {
|
|
1605
|
+
return genExpression(values[0], context);
|
|
1606
|
+
}
|
|
1607
|
+
return genMulti(
|
|
1608
|
+
DELIMITERS_ARRAY,
|
|
1609
|
+
...values.map((expr) => genExpression(expr, context))
|
|
1610
|
+
);
|
|
1611
|
+
}
|
|
1612
|
+
function getRuntimeHelper(tag, key, modifier) {
|
|
1613
|
+
const tagName = tag.toUpperCase();
|
|
1614
|
+
if (modifier) {
|
|
1615
|
+
if (modifier === ".") {
|
|
1616
|
+
return getSpecialHelper(key, tagName) || helpers.setDOMProp;
|
|
1617
|
+
} else {
|
|
1618
|
+
return helpers.setAttr;
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
const helper = getSpecialHelper(key, tagName);
|
|
1622
|
+
if (helper) {
|
|
1623
|
+
return helper;
|
|
1624
|
+
}
|
|
1625
|
+
if (/aria[A-Z]/.test(key)) {
|
|
1626
|
+
return helpers.setDOMProp;
|
|
1627
|
+
}
|
|
1628
|
+
if (shared.isSVGTag(tag)) {
|
|
1629
|
+
return helpers.setAttr;
|
|
1630
|
+
}
|
|
1631
|
+
if (shared.shouldSetAsAttr(tagName, key) || key.includes("-")) {
|
|
1632
|
+
return helpers.setAttr;
|
|
1633
|
+
}
|
|
1634
|
+
return helpers.setProp;
|
|
1635
|
+
}
|
|
1636
|
+
function getSpecialHelper(keyName, tagName) {
|
|
1637
|
+
if (keyName === "value" && shared.canSetValueDirectly(tagName)) {
|
|
1638
|
+
return helpers.setValue;
|
|
1639
|
+
} else if (keyName === "class") {
|
|
1640
|
+
return helpers.setClass;
|
|
1641
|
+
} else if (keyName === "style") {
|
|
1642
|
+
return helpers.setStyle;
|
|
1643
|
+
} else if (keyName === "innerHTML") {
|
|
1644
|
+
return helpers.setHtml;
|
|
1645
|
+
} else if (keyName === "textContent") {
|
|
1646
|
+
return helpers.setText;
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
const setTemplateRefIdent = `_setTemplateRef`;
|
|
1651
|
+
function genSetTemplateRef(oper, context) {
|
|
1652
|
+
return [
|
|
1653
|
+
NEWLINE,
|
|
1654
|
+
oper.effect && `r${oper.element} = `,
|
|
1655
|
+
...genCall(
|
|
1656
|
+
setTemplateRefIdent,
|
|
1657
|
+
// will be generated in root scope
|
|
1658
|
+
`n${oper.element}`,
|
|
1659
|
+
genExpression(oper.value, context),
|
|
1660
|
+
oper.effect ? `r${oper.element}` : oper.refFor ? "void 0" : undefined,
|
|
1661
|
+
oper.refFor && "true"
|
|
1662
|
+
)
|
|
1663
|
+
];
|
|
1664
|
+
}
|
|
1665
|
+
function genDeclareOldRef(oper) {
|
|
1666
|
+
return [NEWLINE, `let r${oper.id}`];
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
function genSetText(oper, context) {
|
|
1670
|
+
const { helper } = context;
|
|
1671
|
+
const { element, values } = oper;
|
|
1672
|
+
const texts = values.map((value) => genExpression(value, context));
|
|
1673
|
+
return [NEWLINE, ...genCall(helper("setText"), `n${element}`, ...texts)];
|
|
1674
|
+
}
|
|
1675
|
+
function genCreateTextNode(oper, context) {
|
|
1676
|
+
const { helper } = context;
|
|
1677
|
+
const { id, values, effect } = oper;
|
|
1678
|
+
return [
|
|
1679
|
+
NEWLINE,
|
|
1680
|
+
`const n${id} = `,
|
|
1681
|
+
...genCall(helper("createTextNode"), [
|
|
1682
|
+
effect && "() => ",
|
|
1683
|
+
...genMulti(
|
|
1684
|
+
DELIMITERS_ARRAY,
|
|
1685
|
+
...values.map((value) => genExpression(value, context))
|
|
1686
|
+
)
|
|
1687
|
+
])
|
|
1688
|
+
];
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
function genVShow(oper, context) {
|
|
1692
|
+
return [
|
|
1693
|
+
NEWLINE,
|
|
1694
|
+
...genCall(context.helper("applyVShow"), `n${oper.element}`, [
|
|
1695
|
+
`() => (`,
|
|
1696
|
+
...genExpression(oper.dir.exp, context),
|
|
1697
|
+
`)`
|
|
1698
|
+
])
|
|
1699
|
+
];
|
|
1700
|
+
}
|
|
1701
|
+
|
|
1702
|
+
const helperMap = {
|
|
1703
|
+
text: "applyTextModel",
|
|
1704
|
+
radio: "applyRadioModel",
|
|
1705
|
+
checkbox: "applyCheckboxModel",
|
|
1706
|
+
select: "applySelectModel",
|
|
1707
|
+
dynamic: "applyDynamicModel"
|
|
1708
|
+
};
|
|
1709
|
+
function genVModel(oper, context) {
|
|
1710
|
+
const {
|
|
1711
|
+
modelType,
|
|
1712
|
+
element,
|
|
1713
|
+
dir: { exp, modifiers }
|
|
1714
|
+
} = oper;
|
|
1715
|
+
return [
|
|
1716
|
+
NEWLINE,
|
|
1717
|
+
...genCall(
|
|
1718
|
+
context.helper(helperMap[modelType]),
|
|
1719
|
+
`n${element}`,
|
|
1720
|
+
// getter
|
|
1721
|
+
[`() => (`, ...genExpression(exp, context), `)`],
|
|
1722
|
+
// setter
|
|
1723
|
+
genModelHandler(exp, context),
|
|
1724
|
+
// modifiers
|
|
1725
|
+
modifiers.length ? `{ ${modifiers.map((e) => e.content + ": true").join(",")} }` : undefined
|
|
1726
|
+
)
|
|
1727
|
+
];
|
|
1728
|
+
}
|
|
1729
|
+
function genModelHandler(exp, context) {
|
|
1730
|
+
return [
|
|
1731
|
+
`${context.options.isTS ? `(_value: any)` : `_value`} => (`,
|
|
1732
|
+
...genExpression(exp, context, "_value"),
|
|
1733
|
+
")"
|
|
1734
|
+
];
|
|
1735
|
+
}
|
|
1736
|
+
|
|
1737
|
+
function genBuiltinDirective(oper, context) {
|
|
1738
|
+
switch (oper.name) {
|
|
1739
|
+
case "show":
|
|
1740
|
+
return genVShow(oper, context);
|
|
1741
|
+
case "model":
|
|
1742
|
+
return genVModel(oper, context);
|
|
1743
|
+
default:
|
|
1744
|
+
return [];
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
function genDirectivesForElement(id, context) {
|
|
1748
|
+
const dirs = filterCustomDirectives(id, context.block.operation);
|
|
1749
|
+
return dirs.length ? genCustomDirectives(dirs, context) : [];
|
|
1750
|
+
}
|
|
1751
|
+
function genCustomDirectives(opers, context) {
|
|
1752
|
+
const { helper } = context;
|
|
1753
|
+
const element = `n${opers[0].element}`;
|
|
1754
|
+
const directiveItems = opers.map(genDirectiveItem);
|
|
1755
|
+
const directives = genMulti(DELIMITERS_ARRAY, ...directiveItems);
|
|
1756
|
+
return [
|
|
1757
|
+
NEWLINE,
|
|
1758
|
+
// @ts-expect-error
|
|
1759
|
+
...genCall(helper("withVaporDirectives"), element, directives)
|
|
1760
|
+
];
|
|
1761
|
+
function genDirectiveItem({
|
|
1762
|
+
dir,
|
|
1763
|
+
name,
|
|
1764
|
+
asset
|
|
1765
|
+
}) {
|
|
1766
|
+
const directiveVar = asset ? compilerDom.toValidAssetId(name, "directive") : genExpression(
|
|
1767
|
+
shared.extend(compilerDom.createSimpleExpression(name, false), { ast: null }),
|
|
1768
|
+
context
|
|
1769
|
+
);
|
|
1770
|
+
const value = dir.exp && ["() => ", ...genExpression(dir.exp, context)];
|
|
1771
|
+
const argument = dir.arg && genExpression(dir.arg, context);
|
|
1772
|
+
const modifiers = !!dir.modifiers.length && [
|
|
1773
|
+
"{ ",
|
|
1774
|
+
genDirectiveModifiers(dir.modifiers.map((m) => m.content)),
|
|
1775
|
+
" }"
|
|
1776
|
+
];
|
|
1777
|
+
return genMulti(
|
|
1778
|
+
DELIMITERS_ARRAY.concat("void 0"),
|
|
1779
|
+
directiveVar,
|
|
1780
|
+
value,
|
|
1781
|
+
argument,
|
|
1782
|
+
modifiers
|
|
1783
|
+
);
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
function genDirectiveModifiers(modifiers) {
|
|
1787
|
+
return modifiers.map(
|
|
1788
|
+
(value) => `${compilerDom.isSimpleIdentifier(value) ? value : JSON.stringify(value)}: true`
|
|
1789
|
+
).join(", ");
|
|
1790
|
+
}
|
|
1791
|
+
function filterCustomDirectives(id, operations) {
|
|
1792
|
+
return operations.filter(
|
|
1793
|
+
(oper) => oper.type === 14 && oper.element === id && !oper.builtin
|
|
1794
|
+
);
|
|
1795
|
+
}
|
|
1796
|
+
|
|
1797
|
+
function genCreateComponent(operation, context) {
|
|
1798
|
+
const { helper } = context;
|
|
1799
|
+
const tag = genTag();
|
|
1800
|
+
const { root, props, slots, once } = operation;
|
|
1801
|
+
const rawSlots = genRawSlots(slots, context);
|
|
1802
|
+
const [ids, handlers] = processInlineHandlers(props, context);
|
|
1803
|
+
const rawProps = context.withId(() => genRawProps(props, context), ids);
|
|
1804
|
+
const inlineHandlers = handlers.reduce(
|
|
1805
|
+
(acc, { name, value }) => {
|
|
1806
|
+
const handler = genEventHandler(context, value, undefined, false);
|
|
1807
|
+
return [...acc, `const ${name} = `, ...handler, NEWLINE];
|
|
1808
|
+
},
|
|
1809
|
+
[]
|
|
1810
|
+
);
|
|
1811
|
+
return [
|
|
1812
|
+
NEWLINE,
|
|
1813
|
+
...inlineHandlers,
|
|
1814
|
+
`const n${operation.id} = `,
|
|
1815
|
+
...genCall(
|
|
1816
|
+
operation.dynamic && !operation.dynamic.isStatic ? helper("createDynamicComponent") : operation.asset ? helper("createComponentWithFallback") : helper("createComponent"),
|
|
1817
|
+
tag,
|
|
1818
|
+
rawProps,
|
|
1819
|
+
rawSlots,
|
|
1820
|
+
root ? "true" : false,
|
|
1821
|
+
once && "true"
|
|
1822
|
+
),
|
|
1823
|
+
...genDirectivesForElement(operation.id, context)
|
|
1824
|
+
];
|
|
1825
|
+
function genTag() {
|
|
1826
|
+
if (operation.dynamic) {
|
|
1827
|
+
if (operation.dynamic.isStatic) {
|
|
1828
|
+
return genCall(
|
|
1829
|
+
helper("resolveDynamicComponent"),
|
|
1830
|
+
genExpression(operation.dynamic, context)
|
|
1831
|
+
);
|
|
1832
|
+
} else {
|
|
1833
|
+
return ["() => (", ...genExpression(operation.dynamic, context), ")"];
|
|
1834
|
+
}
|
|
1835
|
+
} else if (operation.asset) {
|
|
1836
|
+
return toValidAssetId(operation.tag, "component");
|
|
1837
|
+
} else {
|
|
1838
|
+
return genExpression(
|
|
1839
|
+
shared.extend(createSimpleExpression(operation.tag, false), { ast: null }),
|
|
1840
|
+
context
|
|
1841
|
+
);
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
function getUniqueHandlerName(context, name) {
|
|
1846
|
+
const { seenInlineHandlerNames } = context;
|
|
1847
|
+
const count = seenInlineHandlerNames[name] || 0;
|
|
1848
|
+
seenInlineHandlerNames[name] = count + 1;
|
|
1849
|
+
return count === 0 ? name : `${name}${count}`;
|
|
1850
|
+
}
|
|
1851
|
+
function processInlineHandlers(props, context) {
|
|
1852
|
+
const ids = /* @__PURE__ */ Object.create(null);
|
|
1853
|
+
const handlers = [];
|
|
1854
|
+
const staticProps = props[0];
|
|
1855
|
+
if (shared.isArray(staticProps)) {
|
|
1856
|
+
for (let i = 0; i < staticProps.length; i++) {
|
|
1857
|
+
const prop = staticProps[i];
|
|
1858
|
+
if (!prop.handler) continue;
|
|
1859
|
+
prop.values.forEach((value, i2) => {
|
|
1860
|
+
const isMemberExp = isMemberExpression(value, context.options);
|
|
1861
|
+
if (!isMemberExp) {
|
|
1862
|
+
const name = getUniqueHandlerName(context, `_on_${prop.key.content}`);
|
|
1863
|
+
handlers.push({ name, value });
|
|
1864
|
+
ids[name] = null;
|
|
1865
|
+
prop.values[i2] = shared.extend({ ast: null }, createSimpleExpression(name));
|
|
1866
|
+
}
|
|
1867
|
+
});
|
|
1868
|
+
}
|
|
1869
|
+
}
|
|
1870
|
+
return [ids, handlers];
|
|
1871
|
+
}
|
|
1872
|
+
function genRawProps(props, context) {
|
|
1873
|
+
const staticProps = props[0];
|
|
1874
|
+
if (shared.isArray(staticProps)) {
|
|
1875
|
+
if (!staticProps.length && props.length === 1) {
|
|
1876
|
+
return;
|
|
1877
|
+
}
|
|
1878
|
+
return genStaticProps(
|
|
1879
|
+
staticProps,
|
|
1880
|
+
context,
|
|
1881
|
+
genDynamicProps(props.slice(1), context)
|
|
1882
|
+
);
|
|
1883
|
+
} else if (props.length) {
|
|
1884
|
+
return genStaticProps([], context, genDynamicProps(props, context));
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
function genStaticProps(props, context, dynamicProps) {
|
|
1888
|
+
const args = props.map((prop) => genProp(prop, context, true));
|
|
1889
|
+
if (dynamicProps) {
|
|
1890
|
+
args.push([`$: `, ...dynamicProps]);
|
|
1891
|
+
}
|
|
1892
|
+
return genMulti(
|
|
1893
|
+
args.length > 1 ? DELIMITERS_OBJECT_NEWLINE : DELIMITERS_OBJECT,
|
|
1894
|
+
...args
|
|
1895
|
+
);
|
|
1896
|
+
}
|
|
1897
|
+
function genDynamicProps(props, context) {
|
|
1898
|
+
const { helper } = context;
|
|
1899
|
+
const frags = [];
|
|
1900
|
+
for (const p of props) {
|
|
1901
|
+
let expr;
|
|
1902
|
+
if (shared.isArray(p)) {
|
|
1903
|
+
if (p.length) {
|
|
1904
|
+
frags.push(genStaticProps(p, context));
|
|
1905
|
+
}
|
|
1906
|
+
continue;
|
|
1907
|
+
} else {
|
|
1908
|
+
if (p.kind === 1)
|
|
1909
|
+
expr = genMulti(DELIMITERS_OBJECT, genProp(p, context));
|
|
1910
|
+
else {
|
|
1911
|
+
expr = genExpression(p.value, context);
|
|
1912
|
+
if (p.handler) expr = genCall(helper("toHandlers"), expr);
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1915
|
+
frags.push(["() => (", ...expr, ")"]);
|
|
1916
|
+
}
|
|
1917
|
+
if (frags.length) {
|
|
1918
|
+
return genMulti(DELIMITERS_ARRAY_NEWLINE, ...frags);
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
function genProp(prop, context, isStatic) {
|
|
1922
|
+
const values = genPropValue(prop.values, context);
|
|
1923
|
+
return [
|
|
1924
|
+
...genPropKey(prop, context),
|
|
1925
|
+
": ",
|
|
1926
|
+
...prop.handler ? genEventHandler(context, prop.values[0]) : isStatic ? ["() => (", ...values, ")"] : values,
|
|
1927
|
+
...prop.model ? [...genModelEvent(prop, context), ...genModelModifiers(prop, context)] : []
|
|
1928
|
+
];
|
|
1929
|
+
}
|
|
1930
|
+
function genModelEvent(prop, context) {
|
|
1931
|
+
const name = prop.key.isStatic ? [JSON.stringify(`onUpdate:${shared.camelize(prop.key.content)}`)] : ['["onUpdate:" + ', ...genExpression(prop.key, context), "]"];
|
|
1932
|
+
const handler = genModelHandler(prop.values[0], context);
|
|
1933
|
+
return [",", NEWLINE, ...name, ": () => ", ...handler];
|
|
1934
|
+
}
|
|
1935
|
+
function genModelModifiers(prop, context) {
|
|
1936
|
+
const { key, modelModifiers } = prop;
|
|
1937
|
+
if (!modelModifiers || !modelModifiers.length) return [];
|
|
1938
|
+
const modifiersKey = key.isStatic ? key.content === "modelValue" ? [`modelModifiers`] : [`${key.content}Modifiers`] : ["[", ...genExpression(key, context), ' + "Modifiers"]'];
|
|
1939
|
+
const modifiersVal = genDirectiveModifiers(modelModifiers);
|
|
1940
|
+
return [",", NEWLINE, ...modifiersKey, `: () => ({ ${modifiersVal} })`];
|
|
1941
|
+
}
|
|
1942
|
+
function genRawSlots(slots, context) {
|
|
1943
|
+
if (!slots.length) return;
|
|
1944
|
+
const staticSlots = slots[0];
|
|
1945
|
+
if (staticSlots.slotType === 0) {
|
|
1946
|
+
return genStaticSlots(
|
|
1947
|
+
staticSlots,
|
|
1948
|
+
context,
|
|
1949
|
+
slots.length > 1 ? slots.slice(1) : undefined
|
|
1950
|
+
);
|
|
1951
|
+
} else {
|
|
1952
|
+
return genStaticSlots(
|
|
1953
|
+
{ slotType: 0, slots: {} },
|
|
1954
|
+
context,
|
|
1955
|
+
slots
|
|
1956
|
+
);
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
function genStaticSlots({ slots }, context, dynamicSlots) {
|
|
1960
|
+
const args = Object.keys(slots).map((name) => [
|
|
1961
|
+
`${JSON.stringify(name)}: `,
|
|
1962
|
+
...genSlotBlockWithProps(slots[name], context)
|
|
1963
|
+
]);
|
|
1964
|
+
if (dynamicSlots) {
|
|
1965
|
+
args.push([`$: `, ...genDynamicSlots(dynamicSlots, context)]);
|
|
1966
|
+
}
|
|
1967
|
+
return genMulti(DELIMITERS_OBJECT_NEWLINE, ...args);
|
|
1968
|
+
}
|
|
1969
|
+
function genDynamicSlots(slots, context) {
|
|
1970
|
+
return genMulti(
|
|
1971
|
+
DELIMITERS_ARRAY_NEWLINE,
|
|
1972
|
+
...slots.map(
|
|
1973
|
+
(slot) => slot.slotType === 0 ? genStaticSlots(slot, context) : slot.slotType === 4 ? slot.slots.content : genDynamicSlot(slot, context, true)
|
|
1974
|
+
)
|
|
1975
|
+
);
|
|
1976
|
+
}
|
|
1977
|
+
function genDynamicSlot(slot, context, withFunction = false) {
|
|
1978
|
+
let frag;
|
|
1979
|
+
switch (slot.slotType) {
|
|
1980
|
+
case 1:
|
|
1981
|
+
frag = genBasicDynamicSlot(slot, context);
|
|
1982
|
+
break;
|
|
1983
|
+
case 2:
|
|
1984
|
+
frag = genLoopSlot(slot, context);
|
|
1985
|
+
break;
|
|
1986
|
+
case 3:
|
|
1987
|
+
frag = genConditionalSlot(slot, context);
|
|
1988
|
+
break;
|
|
1989
|
+
}
|
|
1990
|
+
return withFunction ? ["() => (", ...frag, ")"] : frag;
|
|
1991
|
+
}
|
|
1992
|
+
function genBasicDynamicSlot(slot, context) {
|
|
1993
|
+
const { name, fn } = slot;
|
|
1994
|
+
return genMulti(
|
|
1995
|
+
DELIMITERS_OBJECT_NEWLINE,
|
|
1996
|
+
["name: ", ...genExpression(name, context)],
|
|
1997
|
+
["fn: ", ...genSlotBlockWithProps(fn, context)]
|
|
1998
|
+
);
|
|
1999
|
+
}
|
|
2000
|
+
function genLoopSlot(slot, context) {
|
|
2001
|
+
const { name, fn, loop } = slot;
|
|
2002
|
+
const { value, key, index, source } = loop;
|
|
2003
|
+
const rawValue = value && value.content;
|
|
2004
|
+
const rawKey = key && key.content;
|
|
2005
|
+
const rawIndex = index && index.content;
|
|
2006
|
+
const idMap = {};
|
|
2007
|
+
if (rawValue) idMap[rawValue] = rawValue;
|
|
2008
|
+
if (rawKey) idMap[rawKey] = rawKey;
|
|
2009
|
+
if (rawIndex) idMap[rawIndex] = rawIndex;
|
|
2010
|
+
const slotExpr = genMulti(
|
|
2011
|
+
DELIMITERS_OBJECT_NEWLINE,
|
|
2012
|
+
["name: ", ...context.withId(() => genExpression(name, context), idMap)],
|
|
2013
|
+
[
|
|
2014
|
+
"fn: ",
|
|
2015
|
+
...context.withId(() => genSlotBlockWithProps(fn, context), idMap)
|
|
2016
|
+
]
|
|
2017
|
+
);
|
|
2018
|
+
return [
|
|
2019
|
+
...genCall(
|
|
2020
|
+
context.helper("createForSlots"),
|
|
2021
|
+
genExpression(source, context),
|
|
2022
|
+
[
|
|
2023
|
+
...genMulti(
|
|
2024
|
+
["(", ")", ", "],
|
|
2025
|
+
rawValue ? rawValue : rawKey || rawIndex ? "_" : undefined,
|
|
2026
|
+
rawKey ? rawKey : rawIndex ? "__" : undefined,
|
|
2027
|
+
rawIndex
|
|
2028
|
+
),
|
|
2029
|
+
" => (",
|
|
2030
|
+
...slotExpr,
|
|
2031
|
+
")"
|
|
2032
|
+
]
|
|
2033
|
+
)
|
|
2034
|
+
];
|
|
2035
|
+
}
|
|
2036
|
+
function genConditionalSlot(slot, context) {
|
|
2037
|
+
const { condition, positive, negative } = slot;
|
|
2038
|
+
return [
|
|
2039
|
+
...genExpression(condition, context),
|
|
2040
|
+
INDENT_START,
|
|
2041
|
+
NEWLINE,
|
|
2042
|
+
"? ",
|
|
2043
|
+
...genDynamicSlot(positive, context),
|
|
2044
|
+
NEWLINE,
|
|
2045
|
+
": ",
|
|
2046
|
+
...negative ? [...genDynamicSlot(negative, context)] : ["void 0"],
|
|
2047
|
+
INDENT_END
|
|
2048
|
+
];
|
|
2049
|
+
}
|
|
2050
|
+
function genSlotBlockWithProps(oper, context) {
|
|
2051
|
+
let isDestructureAssignment = false;
|
|
2052
|
+
let rawProps;
|
|
2053
|
+
let propsName;
|
|
2054
|
+
let exitScope;
|
|
2055
|
+
let depth;
|
|
2056
|
+
const { props } = oper;
|
|
2057
|
+
const idsOfProps = /* @__PURE__ */ new Set();
|
|
2058
|
+
if (props) {
|
|
2059
|
+
rawProps = props.content;
|
|
2060
|
+
if (isDestructureAssignment = !!props.ast) {
|
|
2061
|
+
[depth, exitScope] = context.enterScope();
|
|
2062
|
+
propsName = `_slotProps${depth}`;
|
|
2063
|
+
walkIdentifiers(
|
|
2064
|
+
props.ast,
|
|
2065
|
+
(id, _, __, ___, isLocal) => {
|
|
2066
|
+
if (isLocal) idsOfProps.add(id.name);
|
|
2067
|
+
},
|
|
2068
|
+
true
|
|
2069
|
+
);
|
|
2070
|
+
} else {
|
|
2071
|
+
idsOfProps.add(propsName = rawProps);
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
const idMap = {};
|
|
2075
|
+
idsOfProps.forEach(
|
|
2076
|
+
(id) => idMap[id] = isDestructureAssignment ? `${propsName}[${JSON.stringify(id)}]` : null
|
|
2077
|
+
);
|
|
2078
|
+
const blockFn = context.withId(
|
|
2079
|
+
() => genBlock(oper, context, [propsName]),
|
|
2080
|
+
idMap
|
|
2081
|
+
);
|
|
2082
|
+
exitScope && exitScope();
|
|
2083
|
+
return blockFn;
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
function genSlotOutlet(oper, context) {
|
|
2087
|
+
const { helper } = context;
|
|
2088
|
+
const { id, name, fallback } = oper;
|
|
2089
|
+
const [frag, push] = buildCodeFragment();
|
|
2090
|
+
const nameExpr = name.isStatic ? genExpression(name, context) : ["() => (", ...genExpression(name, context), ")"];
|
|
2091
|
+
let fallbackArg;
|
|
2092
|
+
if (fallback) {
|
|
2093
|
+
fallbackArg = genBlock(fallback, context);
|
|
2094
|
+
}
|
|
2095
|
+
push(
|
|
2096
|
+
NEWLINE,
|
|
2097
|
+
`const n${id} = `,
|
|
2098
|
+
...genCall(
|
|
2099
|
+
helper("createSlot"),
|
|
2100
|
+
nameExpr,
|
|
2101
|
+
genRawProps(oper.props, context) || "null",
|
|
2102
|
+
fallbackArg
|
|
2103
|
+
)
|
|
2104
|
+
);
|
|
2105
|
+
return frag;
|
|
2106
|
+
}
|
|
2107
|
+
|
|
2108
|
+
function genOperations(opers, context) {
|
|
2109
|
+
const [frag, push] = buildCodeFragment();
|
|
2110
|
+
for (const operation of opers) {
|
|
2111
|
+
push(...genOperation(operation, context));
|
|
2112
|
+
}
|
|
2113
|
+
return frag;
|
|
2114
|
+
}
|
|
2115
|
+
function genOperation(oper, context) {
|
|
2116
|
+
switch (oper.type) {
|
|
2117
|
+
case 2:
|
|
2118
|
+
return genSetProp(oper, context);
|
|
2119
|
+
case 3:
|
|
2120
|
+
return genDynamicProps$1(oper, context);
|
|
2121
|
+
case 4:
|
|
2122
|
+
return genSetText(oper, context);
|
|
2123
|
+
case 5:
|
|
2124
|
+
return genSetEvent(oper, context);
|
|
2125
|
+
case 6:
|
|
2126
|
+
return genSetDynamicEvents(oper, context);
|
|
2127
|
+
case 7:
|
|
2128
|
+
return genSetHtml(oper, context);
|
|
2129
|
+
case 8:
|
|
2130
|
+
return genSetTemplateRef(oper, context);
|
|
2131
|
+
case 11:
|
|
2132
|
+
return genCreateTextNode(oper, context);
|
|
2133
|
+
case 9:
|
|
2134
|
+
return genInsertNode(oper, context);
|
|
2135
|
+
case 10:
|
|
2136
|
+
return genPrependNode(oper, context);
|
|
2137
|
+
case 16:
|
|
2138
|
+
return genIf(oper, context);
|
|
2139
|
+
case 17:
|
|
2140
|
+
return genFor(oper, context);
|
|
2141
|
+
case 12:
|
|
2142
|
+
return genCreateComponent(oper, context);
|
|
2143
|
+
case 15:
|
|
2144
|
+
return genDeclareOldRef(oper);
|
|
2145
|
+
case 13:
|
|
2146
|
+
return genSlotOutlet(oper, context);
|
|
2147
|
+
case 14:
|
|
2148
|
+
return genBuiltinDirective(oper, context);
|
|
2149
|
+
default:
|
|
2150
|
+
const exhaustiveCheck = oper;
|
|
2151
|
+
throw new Error(
|
|
2152
|
+
`Unhandled operation type in genOperation: ${exhaustiveCheck}`
|
|
2153
|
+
);
|
|
2154
|
+
}
|
|
2155
|
+
}
|
|
2156
|
+
function genEffects(effects, context) {
|
|
2157
|
+
const {
|
|
2158
|
+
helper,
|
|
2159
|
+
block: { expressions }
|
|
2160
|
+
} = context;
|
|
2161
|
+
const [frag, push, unshift] = buildCodeFragment();
|
|
2162
|
+
let operationsCount = 0;
|
|
2163
|
+
const { ids, frag: declarationFrags } = processExpressions(
|
|
2164
|
+
context,
|
|
2165
|
+
expressions
|
|
2166
|
+
);
|
|
2167
|
+
push(...declarationFrags);
|
|
2168
|
+
for (let i = 0; i < effects.length; i++) {
|
|
2169
|
+
const effect = effects[i];
|
|
2170
|
+
operationsCount += effect.operations.length;
|
|
2171
|
+
const frags = context.withId(() => genEffect(effect, context), ids);
|
|
2172
|
+
i > 0 && push(NEWLINE);
|
|
2173
|
+
if (frag[frag.length - 1] === ")" && frags[0] === "(") {
|
|
2174
|
+
push(";");
|
|
2175
|
+
}
|
|
2176
|
+
push(...frags);
|
|
2177
|
+
}
|
|
2178
|
+
const newLineCount = frag.filter((frag2) => frag2 === NEWLINE).length;
|
|
2179
|
+
if (newLineCount > 1 || operationsCount > 1 || declarationFrags.length > 0) {
|
|
2180
|
+
unshift(`{`, INDENT_START, NEWLINE);
|
|
2181
|
+
push(INDENT_END, NEWLINE, "}");
|
|
2182
|
+
}
|
|
2183
|
+
if (effects.length) {
|
|
2184
|
+
unshift(NEWLINE, `${helper("renderEffect")}(() => `);
|
|
2185
|
+
push(`)`);
|
|
2186
|
+
}
|
|
2187
|
+
return frag;
|
|
2188
|
+
}
|
|
2189
|
+
function genEffect({ operations }, context) {
|
|
2190
|
+
const [frag, push] = buildCodeFragment();
|
|
2191
|
+
const operationsExps = genOperations(operations, context);
|
|
2192
|
+
const newlineCount = operationsExps.filter((frag2) => frag2 === NEWLINE).length;
|
|
2193
|
+
if (newlineCount > 1) {
|
|
2194
|
+
push(...operationsExps);
|
|
2195
|
+
} else {
|
|
2196
|
+
push(...operationsExps.filter((frag2) => frag2 !== NEWLINE));
|
|
2197
|
+
}
|
|
2198
|
+
return frag;
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
function genTemplates(templates, rootIndex, { helper }) {
|
|
2202
|
+
return templates.map(
|
|
2203
|
+
(template, i) => `const t${i} = ${helper("template")}(${JSON.stringify(
|
|
2204
|
+
template
|
|
2205
|
+
)}${i === rootIndex ? ", true" : ""})
|
|
2206
|
+
`
|
|
2207
|
+
).join("");
|
|
2208
|
+
}
|
|
2209
|
+
function genChildren(dynamic, context, from, paths = []) {
|
|
2210
|
+
const { helper } = context;
|
|
2211
|
+
const [frag, push] = buildCodeFragment();
|
|
2212
|
+
let offset = 0;
|
|
2213
|
+
const { children, id, template } = dynamic;
|
|
2214
|
+
if (id !== undefined && template !== undefined) {
|
|
2215
|
+
push(NEWLINE, `const n${id} = t${template}()`);
|
|
2216
|
+
push(...genDirectivesForElement(id, context));
|
|
2217
|
+
}
|
|
2218
|
+
let prev;
|
|
2219
|
+
for (const [index, child] of children.entries()) {
|
|
2220
|
+
if (child.flags & 2) {
|
|
2221
|
+
offset--;
|
|
2222
|
+
}
|
|
2223
|
+
const id2 = child.flags & 1 ? child.flags & 4 ? child.anchor : child.id : undefined;
|
|
2224
|
+
const elementIndex = Number(index) + offset;
|
|
2225
|
+
const newPaths = [...paths, elementIndex];
|
|
2226
|
+
if (id2 === undefined) {
|
|
2227
|
+
push(...genChildren(child, context, from, newPaths));
|
|
2228
|
+
continue;
|
|
2229
|
+
}
|
|
2230
|
+
push(NEWLINE, `const n${id2} = `);
|
|
2231
|
+
if (prev) {
|
|
2232
|
+
const offset2 = elementIndex - prev[1];
|
|
2233
|
+
if (offset2 === 1) {
|
|
2234
|
+
push(`n${prev[0]}.nextSibling`);
|
|
2235
|
+
} else {
|
|
2236
|
+
push(...genCall(helper("next"), `n${prev[0]}`, String(offset2)));
|
|
2237
|
+
}
|
|
2238
|
+
} else {
|
|
2239
|
+
if (newPaths.length === 1 && newPaths[0] === 0) {
|
|
2240
|
+
push(`n${from}.firstChild`);
|
|
2241
|
+
} else {
|
|
2242
|
+
push(
|
|
2243
|
+
...genCall(helper("children"), `n${from}`, ...newPaths.map(String))
|
|
2244
|
+
);
|
|
2245
|
+
}
|
|
2246
|
+
}
|
|
2247
|
+
push(...genDirectivesForElement(id2, context));
|
|
2248
|
+
prev = [id2, elementIndex];
|
|
2249
|
+
push(...genChildren(child, context, id2, []));
|
|
2250
|
+
}
|
|
2251
|
+
return frag;
|
|
2252
|
+
}
|
|
2253
|
+
|
|
2254
|
+
function genBlock(oper, context, args = [], root, customReturns) {
|
|
2255
|
+
return [
|
|
2256
|
+
"(",
|
|
2257
|
+
...args,
|
|
2258
|
+
") => {",
|
|
2259
|
+
INDENT_START,
|
|
2260
|
+
...genBlockContent(oper, context, root),
|
|
2261
|
+
INDENT_END,
|
|
2262
|
+
NEWLINE,
|
|
2263
|
+
"}"
|
|
2264
|
+
];
|
|
2265
|
+
}
|
|
2266
|
+
function genBlockContent(block, context, root, customReturns) {
|
|
2267
|
+
const [frag, push] = buildCodeFragment();
|
|
2268
|
+
const { dynamic, effect, operation, returns } = block;
|
|
2269
|
+
const resetBlock = context.enterBlock(block);
|
|
2270
|
+
if (root) {
|
|
2271
|
+
genResolveAssets("component", "resolveComponent");
|
|
2272
|
+
genResolveAssets("directive", "resolveDirective");
|
|
2273
|
+
}
|
|
2274
|
+
for (const child of dynamic.children) {
|
|
2275
|
+
push(...genChildren(child, context, child.id));
|
|
2276
|
+
}
|
|
2277
|
+
push(...genOperations(operation, context));
|
|
2278
|
+
push(...genEffects(effect, context));
|
|
2279
|
+
push(NEWLINE, `return `);
|
|
2280
|
+
const returnNodes = returns.map((n) => `n${n}`);
|
|
2281
|
+
const returnsCode = returnNodes.length > 1 ? genMulti(DELIMITERS_ARRAY, ...returnNodes) : [returnNodes[0] || "null"];
|
|
2282
|
+
push(...returnsCode);
|
|
2283
|
+
resetBlock();
|
|
2284
|
+
return frag;
|
|
2285
|
+
function genResolveAssets(kind, helper) {
|
|
2286
|
+
for (const name of context.ir[kind]) {
|
|
2287
|
+
push(
|
|
2288
|
+
NEWLINE,
|
|
2289
|
+
`const ${compilerDom.toValidAssetId(name, kind)} = `,
|
|
2290
|
+
...genCall(context.helper(helper), JSON.stringify(name))
|
|
2291
|
+
);
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
}
|
|
2295
|
+
|
|
2296
|
+
class CodegenContext {
|
|
2297
|
+
constructor(ir, options) {
|
|
2298
|
+
this.ir = ir;
|
|
2299
|
+
this.helpers = /* @__PURE__ */ new Set([]);
|
|
2300
|
+
this.helper = (name) => {
|
|
2301
|
+
this.helpers.add(name);
|
|
2302
|
+
return `_${name}`;
|
|
2303
|
+
};
|
|
2304
|
+
this.delegates = /* @__PURE__ */ new Set();
|
|
2305
|
+
this.identifiers = /* @__PURE__ */ Object.create(null);
|
|
2306
|
+
this.seenInlineHandlerNames = /* @__PURE__ */ Object.create(null);
|
|
2307
|
+
this.scopeLevel = 0;
|
|
2308
|
+
const defaultOptions = {
|
|
2309
|
+
mode: "module",
|
|
2310
|
+
prefixIdentifiers: true,
|
|
2311
|
+
sourceMap: false,
|
|
2312
|
+
filename: `template.vue.html`,
|
|
2313
|
+
scopeId: null,
|
|
2314
|
+
runtimeGlobalName: `Vue`,
|
|
2315
|
+
runtimeModuleName: `vue`,
|
|
2316
|
+
ssrRuntimeModuleName: "vue/server-renderer",
|
|
2317
|
+
ssr: false,
|
|
2318
|
+
isTS: false,
|
|
2319
|
+
inSSR: false,
|
|
2320
|
+
inline: false,
|
|
2321
|
+
bindingMetadata: {},
|
|
2322
|
+
expressionPlugins: []
|
|
2323
|
+
};
|
|
2324
|
+
this.options = shared.extend(defaultOptions, options);
|
|
2325
|
+
this.block = ir.block;
|
|
2326
|
+
}
|
|
2327
|
+
withId(fn, map) {
|
|
2328
|
+
const { identifiers } = this;
|
|
2329
|
+
const ids = Object.keys(map);
|
|
2330
|
+
for (const id of ids) {
|
|
2331
|
+
identifiers[id] || (identifiers[id] = []);
|
|
2332
|
+
identifiers[id].unshift(map[id] || id);
|
|
2333
|
+
}
|
|
2334
|
+
const ret = fn();
|
|
2335
|
+
ids.forEach((id) => shared.remove(identifiers[id], map[id] || id));
|
|
2336
|
+
return ret;
|
|
2337
|
+
}
|
|
2338
|
+
enterBlock(block) {
|
|
2339
|
+
const parent = this.block;
|
|
2340
|
+
this.block = block;
|
|
2341
|
+
return () => this.block = parent;
|
|
2342
|
+
}
|
|
2343
|
+
enterScope() {
|
|
2344
|
+
return [this.scopeLevel++, () => this.scopeLevel--];
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
function generate(ir, options = {}) {
|
|
2348
|
+
const [frag, push] = buildCodeFragment();
|
|
2349
|
+
const context = new CodegenContext(ir, options);
|
|
2350
|
+
const { helpers } = context;
|
|
2351
|
+
const { inline, bindingMetadata } = options;
|
|
2352
|
+
const functionName = "render";
|
|
2353
|
+
const args = ["_ctx"];
|
|
2354
|
+
if (bindingMetadata && !inline) {
|
|
2355
|
+
args.push("$props", "$emit", "$attrs", "$slots");
|
|
2356
|
+
}
|
|
2357
|
+
const signature = (options.isTS ? args.map((arg) => `${arg}: any`) : args).join(
|
|
2358
|
+
", "
|
|
2359
|
+
);
|
|
2360
|
+
if (!inline) {
|
|
2361
|
+
push(NEWLINE, `export function ${functionName}(${signature}) {`);
|
|
2362
|
+
}
|
|
2363
|
+
push(INDENT_START);
|
|
2364
|
+
if (ir.hasTemplateRef) {
|
|
2365
|
+
push(
|
|
2366
|
+
NEWLINE,
|
|
2367
|
+
`const ${setTemplateRefIdent} = ${context.helper("createTemplateRefSetter")}()`
|
|
2368
|
+
);
|
|
2369
|
+
}
|
|
2370
|
+
push(...genBlockContent(ir.block, context, true));
|
|
2371
|
+
push(INDENT_END, NEWLINE);
|
|
2372
|
+
if (!inline) {
|
|
2373
|
+
push("}");
|
|
2374
|
+
}
|
|
2375
|
+
const delegates = genDelegates(context);
|
|
2376
|
+
const templates = genTemplates(ir.template, ir.rootTemplateIndex, context);
|
|
2377
|
+
const imports = genHelperImports(context);
|
|
2378
|
+
const preamble = imports + templates + delegates;
|
|
2379
|
+
const newlineCount = [...preamble].filter((c) => c === "\n").length;
|
|
2380
|
+
if (newlineCount && !inline) {
|
|
2381
|
+
frag.unshift(...new Array(newlineCount).fill(LF));
|
|
2382
|
+
}
|
|
2383
|
+
let [code, map] = codeFragmentToString(frag, context);
|
|
2384
|
+
if (!inline) {
|
|
2385
|
+
code = preamble + code;
|
|
2386
|
+
}
|
|
2387
|
+
return {
|
|
2388
|
+
code,
|
|
2389
|
+
ast: ir,
|
|
2390
|
+
preamble,
|
|
2391
|
+
map: map && map.toJSON(),
|
|
2392
|
+
helpers
|
|
2393
|
+
};
|
|
2394
|
+
}
|
|
2395
|
+
function genDelegates({ delegates, helper }) {
|
|
2396
|
+
return delegates.size ? genCall(
|
|
2397
|
+
helper("delegateEvents"),
|
|
2398
|
+
...Array.from(delegates).map((v) => `"${v}"`)
|
|
2399
|
+
).join("") + "\n" : "";
|
|
2400
|
+
}
|
|
2401
|
+
function genHelperImports({ helpers, helper, options }) {
|
|
2402
|
+
let imports = "";
|
|
2403
|
+
if (helpers.size) {
|
|
2404
|
+
imports += `import { ${[...helpers].map((h) => `${h} as _${h}`).join(", ")} } from '${options.runtimeModuleName}';
|
|
2405
|
+
`;
|
|
2406
|
+
}
|
|
2407
|
+
return imports;
|
|
2408
|
+
}
|
|
2409
|
+
|
|
2410
|
+
const transformChildren = (node, context) => {
|
|
2411
|
+
const isFragment = node.type === 0 || node.type === 1 && (node.tagType === 3 || node.tagType === 1);
|
|
2412
|
+
if (!isFragment && node.type !== 1) return;
|
|
2413
|
+
for (const [i, child] of node.children.entries()) {
|
|
2414
|
+
const childContext = context.create(child, i);
|
|
2415
|
+
transformNode(childContext);
|
|
2416
|
+
if (isFragment) {
|
|
2417
|
+
childContext.reference();
|
|
2418
|
+
childContext.registerTemplate();
|
|
2419
|
+
if (!(childContext.dynamic.flags & 2) || childContext.dynamic.flags & 4) {
|
|
2420
|
+
context.block.returns.push(childContext.dynamic.id);
|
|
2421
|
+
}
|
|
2422
|
+
} else {
|
|
2423
|
+
context.childrenTemplate.push(childContext.template);
|
|
2424
|
+
}
|
|
2425
|
+
context.dynamic.children[i] = childContext.dynamic;
|
|
2426
|
+
}
|
|
2427
|
+
if (!isFragment) {
|
|
2428
|
+
processDynamicChildren(context);
|
|
2429
|
+
}
|
|
2430
|
+
};
|
|
2431
|
+
function processDynamicChildren(context) {
|
|
2432
|
+
let prevDynamics = [];
|
|
2433
|
+
let hasStaticTemplate = false;
|
|
2434
|
+
const children = context.dynamic.children;
|
|
2435
|
+
for (const [index, child] of children.entries()) {
|
|
2436
|
+
if (child.flags & 4) {
|
|
2437
|
+
prevDynamics.push(child);
|
|
2438
|
+
}
|
|
2439
|
+
if (!(child.flags & 2)) {
|
|
2440
|
+
if (prevDynamics.length) {
|
|
2441
|
+
if (hasStaticTemplate) {
|
|
2442
|
+
context.childrenTemplate[index - prevDynamics.length] = `<!>`;
|
|
2443
|
+
prevDynamics[0].flags -= 2;
|
|
2444
|
+
const anchor = prevDynamics[0].anchor = context.increaseId();
|
|
2445
|
+
context.registerOperation({
|
|
2446
|
+
type: 9,
|
|
2447
|
+
elements: prevDynamics.map((child2) => child2.id),
|
|
2448
|
+
parent: context.reference(),
|
|
2449
|
+
anchor
|
|
2450
|
+
});
|
|
2451
|
+
} else {
|
|
2452
|
+
context.registerOperation({
|
|
2453
|
+
type: 10,
|
|
2454
|
+
elements: prevDynamics.map((child2) => child2.id),
|
|
2455
|
+
parent: context.reference()
|
|
2456
|
+
});
|
|
2457
|
+
}
|
|
2458
|
+
prevDynamics = [];
|
|
2459
|
+
}
|
|
2460
|
+
hasStaticTemplate = true;
|
|
2461
|
+
}
|
|
2462
|
+
}
|
|
2463
|
+
if (prevDynamics.length) {
|
|
2464
|
+
context.registerOperation({
|
|
2465
|
+
type: 9,
|
|
2466
|
+
elements: prevDynamics.map((child) => child.id),
|
|
2467
|
+
parent: context.reference()
|
|
2468
|
+
});
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
|
|
2472
|
+
const transformVOnce = (node, context) => {
|
|
2473
|
+
if (
|
|
2474
|
+
// !context.inSSR &&
|
|
2475
|
+
node.type === 1 && compilerDom.findDir(node, "once", true)
|
|
2476
|
+
) {
|
|
2477
|
+
context.inVOnce = true;
|
|
2478
|
+
}
|
|
2479
|
+
};
|
|
2480
|
+
|
|
2481
|
+
const isReservedProp = /* @__PURE__ */ shared.makeMap(
|
|
2482
|
+
// the leading comma is intentional so empty string "" is also included
|
|
2483
|
+
",key,ref,ref_for,ref_key,"
|
|
2484
|
+
);
|
|
2485
|
+
const transformElement = (node, context) => {
|
|
2486
|
+
return function postTransformElement() {
|
|
2487
|
+
({ node } = context);
|
|
2488
|
+
if (!(node.type === 1 && (node.tagType === 0 || node.tagType === 1)))
|
|
2489
|
+
return;
|
|
2490
|
+
const isComponent = node.tagType === 1;
|
|
2491
|
+
const isDynamicComponent = isComponentTag(node.tag);
|
|
2492
|
+
const propsResult = buildProps(
|
|
2493
|
+
node,
|
|
2494
|
+
context,
|
|
2495
|
+
isComponent,
|
|
2496
|
+
isDynamicComponent
|
|
2497
|
+
);
|
|
2498
|
+
let { parent } = context;
|
|
2499
|
+
while (parent && parent.parent && parent.node.type === 1 && parent.node.tagType === 3) {
|
|
2500
|
+
parent = parent.parent;
|
|
2501
|
+
}
|
|
2502
|
+
const singleRoot = context.root === parent && parent.node.children.filter((child) => child.type !== 3).length === 1;
|
|
2503
|
+
(isComponent ? transformComponentElement : transformNativeElement)(
|
|
2504
|
+
node,
|
|
2505
|
+
propsResult,
|
|
2506
|
+
singleRoot,
|
|
2507
|
+
context,
|
|
2508
|
+
isDynamicComponent
|
|
2509
|
+
);
|
|
2510
|
+
};
|
|
2511
|
+
};
|
|
2512
|
+
function transformComponentElement(node, propsResult, singleRoot, context, isDynamicComponent) {
|
|
2513
|
+
const dynamicComponent = isDynamicComponent ? resolveDynamicComponent(node) : undefined;
|
|
2514
|
+
let { tag } = node;
|
|
2515
|
+
let asset = true;
|
|
2516
|
+
if (!dynamicComponent) {
|
|
2517
|
+
const fromSetup = resolveSetupReference(tag, context);
|
|
2518
|
+
if (fromSetup) {
|
|
2519
|
+
tag = fromSetup;
|
|
2520
|
+
asset = false;
|
|
2521
|
+
}
|
|
2522
|
+
const dotIndex = tag.indexOf(".");
|
|
2523
|
+
if (dotIndex > 0) {
|
|
2524
|
+
const ns = resolveSetupReference(tag.slice(0, dotIndex), context);
|
|
2525
|
+
if (ns) {
|
|
2526
|
+
tag = ns + tag.slice(dotIndex);
|
|
2527
|
+
asset = false;
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
if (asset) {
|
|
2531
|
+
context.component.add(tag);
|
|
2532
|
+
}
|
|
2533
|
+
}
|
|
2534
|
+
context.dynamic.flags |= 2 | 4;
|
|
2535
|
+
context.registerOperation({
|
|
2536
|
+
type: 12,
|
|
2537
|
+
id: context.reference(),
|
|
2538
|
+
tag,
|
|
2539
|
+
props: propsResult[0] ? propsResult[1] : [propsResult[1]],
|
|
2540
|
+
asset,
|
|
2541
|
+
root: singleRoot,
|
|
2542
|
+
slots: [...context.slots],
|
|
2543
|
+
once: context.inVOnce,
|
|
2544
|
+
dynamic: dynamicComponent
|
|
2545
|
+
});
|
|
2546
|
+
context.slots = [];
|
|
2547
|
+
}
|
|
2548
|
+
function resolveDynamicComponent(node) {
|
|
2549
|
+
const isProp = findProp(
|
|
2550
|
+
node,
|
|
2551
|
+
"is",
|
|
2552
|
+
false,
|
|
2553
|
+
true
|
|
2554
|
+
/* allow empty */
|
|
2555
|
+
);
|
|
2556
|
+
if (!isProp) return;
|
|
2557
|
+
if (isProp.type === 6) {
|
|
2558
|
+
return isProp.value && compilerDom.createSimpleExpression(isProp.value.content, true);
|
|
2559
|
+
} else {
|
|
2560
|
+
return isProp.exp || // #10469 handle :is shorthand
|
|
2561
|
+
shared.extend(compilerDom.createSimpleExpression(`is`, false, isProp.arg.loc), {
|
|
2562
|
+
ast: null
|
|
2563
|
+
});
|
|
2564
|
+
}
|
|
2565
|
+
}
|
|
2566
|
+
function resolveSetupReference(name, context) {
|
|
2567
|
+
const bindings = context.options.bindingMetadata;
|
|
2568
|
+
if (!bindings || bindings.__isScriptSetup === false) {
|
|
2569
|
+
return;
|
|
2570
|
+
}
|
|
2571
|
+
const camelName = shared.camelize(name);
|
|
2572
|
+
const PascalName = shared.capitalize(camelName);
|
|
2573
|
+
return bindings[name] ? name : bindings[camelName] ? camelName : bindings[PascalName] ? PascalName : undefined;
|
|
2574
|
+
}
|
|
2575
|
+
function transformNativeElement(node, propsResult, singleRoot, context) {
|
|
2576
|
+
const { tag } = node;
|
|
2577
|
+
const { scopeId } = context.options;
|
|
2578
|
+
let template = "";
|
|
2579
|
+
template += `<${tag}`;
|
|
2580
|
+
if (scopeId) template += ` ${scopeId}`;
|
|
2581
|
+
const dynamicProps = [];
|
|
2582
|
+
if (propsResult[0]) {
|
|
2583
|
+
const [, dynamicArgs, expressions] = propsResult;
|
|
2584
|
+
context.registerEffect(expressions, {
|
|
2585
|
+
type: 3,
|
|
2586
|
+
element: context.reference(),
|
|
2587
|
+
props: dynamicArgs,
|
|
2588
|
+
root: singleRoot
|
|
2589
|
+
});
|
|
2590
|
+
} else {
|
|
2591
|
+
for (const prop of propsResult[1]) {
|
|
2592
|
+
const { key, values } = prop;
|
|
2593
|
+
if (key.isStatic && values.length === 1 && values[0].isStatic) {
|
|
2594
|
+
template += ` ${key.content}`;
|
|
2595
|
+
if (values[0].content) template += `="${values[0].content}"`;
|
|
2596
|
+
} else {
|
|
2597
|
+
dynamicProps.push(key.content);
|
|
2598
|
+
context.registerEffect(values, {
|
|
2599
|
+
type: 2,
|
|
2600
|
+
element: context.reference(),
|
|
2601
|
+
prop,
|
|
2602
|
+
root: singleRoot,
|
|
2603
|
+
tag
|
|
2604
|
+
});
|
|
2605
|
+
}
|
|
2606
|
+
}
|
|
2607
|
+
}
|
|
2608
|
+
template += `>` + context.childrenTemplate.join("");
|
|
2609
|
+
if (!shared.isVoidTag(tag)) {
|
|
2610
|
+
template += `</${tag}>`;
|
|
2611
|
+
}
|
|
2612
|
+
if (singleRoot) {
|
|
2613
|
+
context.ir.rootTemplateIndex = context.ir.template.length;
|
|
2614
|
+
}
|
|
2615
|
+
if (context.parent && context.parent.node.type === 1 && !compilerDom.isValidHTMLNesting(context.parent.node.tag, tag)) {
|
|
2616
|
+
context.reference();
|
|
2617
|
+
context.dynamic.template = context.pushTemplate(template);
|
|
2618
|
+
context.dynamic.flags |= 4 | 2;
|
|
2619
|
+
} else {
|
|
2620
|
+
context.template += template;
|
|
2621
|
+
}
|
|
2622
|
+
}
|
|
2623
|
+
function buildProps(node, context, isComponent, isDynamicComponent) {
|
|
2624
|
+
const props = node.props;
|
|
2625
|
+
if (props.length === 0) return [false, []];
|
|
2626
|
+
const dynamicArgs = [];
|
|
2627
|
+
const dynamicExpr = [];
|
|
2628
|
+
let results = [];
|
|
2629
|
+
function pushMergeArg() {
|
|
2630
|
+
if (results.length) {
|
|
2631
|
+
dynamicArgs.push(dedupeProperties(results));
|
|
2632
|
+
results = [];
|
|
2633
|
+
}
|
|
2634
|
+
}
|
|
2635
|
+
for (const prop of props) {
|
|
2636
|
+
if (prop.type === 7 && !prop.arg) {
|
|
2637
|
+
if (prop.name === "bind") {
|
|
2638
|
+
if (prop.exp) {
|
|
2639
|
+
dynamicExpr.push(prop.exp);
|
|
2640
|
+
pushMergeArg();
|
|
2641
|
+
dynamicArgs.push({
|
|
2642
|
+
kind: 0,
|
|
2643
|
+
value: prop.exp
|
|
2644
|
+
});
|
|
2645
|
+
} else {
|
|
2646
|
+
context.options.onError(
|
|
2647
|
+
compilerDom.createCompilerError(34, prop.loc)
|
|
2648
|
+
);
|
|
2649
|
+
}
|
|
2650
|
+
continue;
|
|
2651
|
+
} else if (prop.name === "on") {
|
|
2652
|
+
if (prop.exp) {
|
|
2653
|
+
if (isComponent) {
|
|
2654
|
+
dynamicExpr.push(prop.exp);
|
|
2655
|
+
pushMergeArg();
|
|
2656
|
+
dynamicArgs.push({
|
|
2657
|
+
kind: 0,
|
|
2658
|
+
value: prop.exp,
|
|
2659
|
+
handler: true
|
|
2660
|
+
});
|
|
2661
|
+
} else {
|
|
2662
|
+
context.registerEffect(
|
|
2663
|
+
[prop.exp],
|
|
2664
|
+
{
|
|
2665
|
+
type: 6,
|
|
2666
|
+
element: context.reference(),
|
|
2667
|
+
event: prop.exp
|
|
2668
|
+
}
|
|
2669
|
+
);
|
|
2670
|
+
}
|
|
2671
|
+
} else {
|
|
2672
|
+
context.options.onError(
|
|
2673
|
+
compilerDom.createCompilerError(35, prop.loc)
|
|
2674
|
+
);
|
|
2675
|
+
}
|
|
2676
|
+
continue;
|
|
2677
|
+
}
|
|
2678
|
+
}
|
|
2679
|
+
if (isDynamicComponent && prop.type === 6 && prop.name === "is" || prop.type === 7 && prop.name === "bind" && compilerDom.isStaticArgOf(prop.arg, "is")) {
|
|
2680
|
+
continue;
|
|
2681
|
+
}
|
|
2682
|
+
const result = transformProp(prop, node, context);
|
|
2683
|
+
if (result) {
|
|
2684
|
+
dynamicExpr.push(result.key, result.value);
|
|
2685
|
+
if (isComponent && !result.key.isStatic) {
|
|
2686
|
+
pushMergeArg();
|
|
2687
|
+
dynamicArgs.push(
|
|
2688
|
+
shared.extend(resolveDirectiveResult(result), {
|
|
2689
|
+
kind: 1
|
|
2690
|
+
})
|
|
2691
|
+
);
|
|
2692
|
+
} else {
|
|
2693
|
+
results.push(result);
|
|
2694
|
+
}
|
|
2695
|
+
}
|
|
2696
|
+
}
|
|
2697
|
+
if (dynamicArgs.length || results.some(({ key }) => !key.isStatic)) {
|
|
2698
|
+
pushMergeArg();
|
|
2699
|
+
return [true, dynamicArgs, dynamicExpr];
|
|
2700
|
+
}
|
|
2701
|
+
const irProps = dedupeProperties(results);
|
|
2702
|
+
return [false, irProps];
|
|
2703
|
+
}
|
|
2704
|
+
function transformProp(prop, node, context) {
|
|
2705
|
+
let { name } = prop;
|
|
2706
|
+
if (prop.type === 6) {
|
|
2707
|
+
if (isReservedProp(name)) return;
|
|
2708
|
+
return {
|
|
2709
|
+
key: compilerDom.createSimpleExpression(prop.name, true, prop.nameLoc),
|
|
2710
|
+
value: prop.value ? compilerDom.createSimpleExpression(prop.value.content, true, prop.value.loc) : EMPTY_EXPRESSION
|
|
2711
|
+
};
|
|
2712
|
+
}
|
|
2713
|
+
const directiveTransform = context.options.directiveTransforms[name];
|
|
2714
|
+
if (directiveTransform) {
|
|
2715
|
+
return directiveTransform(prop, node, context);
|
|
2716
|
+
}
|
|
2717
|
+
if (!shared.isBuiltInDirective(name)) {
|
|
2718
|
+
const fromSetup = resolveSetupReference(`v-${name}`, context);
|
|
2719
|
+
if (fromSetup) {
|
|
2720
|
+
name = fromSetup;
|
|
2721
|
+
} else {
|
|
2722
|
+
context.directive.add(name);
|
|
2723
|
+
}
|
|
2724
|
+
context.registerOperation({
|
|
2725
|
+
type: 14,
|
|
2726
|
+
element: context.reference(),
|
|
2727
|
+
dir: prop,
|
|
2728
|
+
name,
|
|
2729
|
+
asset: !fromSetup
|
|
2730
|
+
});
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
function dedupeProperties(results) {
|
|
2734
|
+
const knownProps = /* @__PURE__ */ new Map();
|
|
2735
|
+
const deduped = [];
|
|
2736
|
+
for (const result of results) {
|
|
2737
|
+
const prop = resolveDirectiveResult(result);
|
|
2738
|
+
if (!prop.key.isStatic) {
|
|
2739
|
+
deduped.push(prop);
|
|
2740
|
+
continue;
|
|
2741
|
+
}
|
|
2742
|
+
const name = prop.key.content;
|
|
2743
|
+
const existing = knownProps.get(name);
|
|
2744
|
+
if (existing) {
|
|
2745
|
+
if (name === "style" || name === "class") {
|
|
2746
|
+
mergePropValues(existing, prop);
|
|
2747
|
+
}
|
|
2748
|
+
} else {
|
|
2749
|
+
knownProps.set(name, prop);
|
|
2750
|
+
deduped.push(prop);
|
|
2751
|
+
}
|
|
2752
|
+
}
|
|
2753
|
+
return deduped;
|
|
2754
|
+
}
|
|
2755
|
+
function resolveDirectiveResult(prop) {
|
|
2756
|
+
return shared.extend({}, prop, {
|
|
2757
|
+
value: undefined,
|
|
2758
|
+
values: [prop.value]
|
|
2759
|
+
});
|
|
2760
|
+
}
|
|
2761
|
+
function mergePropValues(existing, incoming) {
|
|
2762
|
+
const newValues = incoming.values;
|
|
2763
|
+
existing.values.push(...newValues);
|
|
2764
|
+
}
|
|
2765
|
+
function isComponentTag(tag) {
|
|
2766
|
+
return tag === "component" || tag === "Component";
|
|
2767
|
+
}
|
|
2768
|
+
|
|
2769
|
+
const transformVHtml = (dir, node, context) => {
|
|
2770
|
+
let { exp, loc } = dir;
|
|
2771
|
+
if (!exp) {
|
|
2772
|
+
context.options.onError(
|
|
2773
|
+
compilerDom.createDOMCompilerError(53, loc)
|
|
2774
|
+
);
|
|
2775
|
+
exp = EMPTY_EXPRESSION;
|
|
2776
|
+
}
|
|
2777
|
+
if (node.children.length) {
|
|
2778
|
+
context.options.onError(
|
|
2779
|
+
compilerDom.createDOMCompilerError(54, loc)
|
|
2780
|
+
);
|
|
2781
|
+
context.childrenTemplate.length = 0;
|
|
2782
|
+
}
|
|
2783
|
+
context.registerEffect([exp], {
|
|
2784
|
+
type: 7,
|
|
2785
|
+
element: context.reference(),
|
|
2786
|
+
value: exp
|
|
2787
|
+
});
|
|
2788
|
+
};
|
|
2789
|
+
|
|
2790
|
+
const transformVText = (dir, node, context) => {
|
|
2791
|
+
let { exp, loc } = dir;
|
|
2792
|
+
if (!exp) {
|
|
2793
|
+
context.options.onError(
|
|
2794
|
+
compilerDom.createDOMCompilerError(55, loc)
|
|
2795
|
+
);
|
|
2796
|
+
exp = EMPTY_EXPRESSION;
|
|
2797
|
+
}
|
|
2798
|
+
if (node.children.length) {
|
|
2799
|
+
context.options.onError(
|
|
2800
|
+
compilerDom.createDOMCompilerError(56, loc)
|
|
2801
|
+
);
|
|
2802
|
+
context.childrenTemplate.length = 0;
|
|
2803
|
+
}
|
|
2804
|
+
const literal = getLiteralExpressionValue(exp);
|
|
2805
|
+
if (literal != null) {
|
|
2806
|
+
context.childrenTemplate = [String(literal)];
|
|
2807
|
+
} else {
|
|
2808
|
+
context.registerEffect([exp], {
|
|
2809
|
+
type: 4,
|
|
2810
|
+
element: context.reference(),
|
|
2811
|
+
values: [exp]
|
|
2812
|
+
});
|
|
2813
|
+
}
|
|
2814
|
+
};
|
|
2815
|
+
|
|
2816
|
+
function normalizeBindShorthand(arg, context) {
|
|
2817
|
+
if (arg.type !== 4 || !arg.isStatic) {
|
|
2818
|
+
context.options.onError(
|
|
2819
|
+
compilerDom.createCompilerError(
|
|
2820
|
+
52,
|
|
2821
|
+
arg.loc
|
|
2822
|
+
)
|
|
2823
|
+
);
|
|
2824
|
+
return compilerDom.createSimpleExpression("", true, arg.loc);
|
|
2825
|
+
}
|
|
2826
|
+
const propName = shared.camelize(arg.content);
|
|
2827
|
+
const exp = compilerDom.createSimpleExpression(propName, false, arg.loc);
|
|
2828
|
+
exp.ast = null;
|
|
2829
|
+
return exp;
|
|
2830
|
+
}
|
|
2831
|
+
const transformVBind = (dir, node, context) => {
|
|
2832
|
+
const { loc, modifiers } = dir;
|
|
2833
|
+
let { exp } = dir;
|
|
2834
|
+
let arg = dir.arg;
|
|
2835
|
+
const modifiersString = modifiers.map((s) => s.content);
|
|
2836
|
+
if (!exp) exp = normalizeBindShorthand(arg, context);
|
|
2837
|
+
if (!exp.content.trim()) {
|
|
2838
|
+
context.options.onError(
|
|
2839
|
+
compilerDom.createCompilerError(34, loc)
|
|
2840
|
+
);
|
|
2841
|
+
exp = compilerDom.createSimpleExpression("", true, loc);
|
|
2842
|
+
}
|
|
2843
|
+
exp = resolveExpression(exp);
|
|
2844
|
+
arg = resolveExpression(arg);
|
|
2845
|
+
if (arg.isStatic && isReservedProp(arg.content)) return;
|
|
2846
|
+
let camel = false;
|
|
2847
|
+
if (modifiersString.includes("camel")) {
|
|
2848
|
+
if (arg.isStatic) {
|
|
2849
|
+
arg = shared.extend({}, arg, { content: shared.camelize(arg.content) });
|
|
2850
|
+
} else {
|
|
2851
|
+
camel = true;
|
|
2852
|
+
}
|
|
2853
|
+
}
|
|
2854
|
+
return {
|
|
2855
|
+
key: arg,
|
|
2856
|
+
value: exp,
|
|
2857
|
+
loc,
|
|
2858
|
+
runtimeCamelize: camel,
|
|
2859
|
+
modifier: modifiersString.includes("prop") ? "." : modifiersString.includes("attr") ? "^" : undefined
|
|
2860
|
+
};
|
|
2861
|
+
};
|
|
2862
|
+
|
|
2863
|
+
const delegatedEvents = /* @__PURE__ */ shared.makeMap(
|
|
2864
|
+
"beforeinput,click,dblclick,contextmenu,focusin,focusout,input,keydown,keyup,mousedown,mousemove,mouseout,mouseover,mouseup,pointerdown,pointermove,pointerout,pointerover,pointerup,touchend,touchmove,touchstart"
|
|
2865
|
+
);
|
|
2866
|
+
const transformVOn = (dir, node, context) => {
|
|
2867
|
+
let { arg, exp, loc, modifiers } = dir;
|
|
2868
|
+
const isComponent = node.tagType === 1;
|
|
2869
|
+
const isSlotOutlet = node.tag === "slot";
|
|
2870
|
+
if (!exp && !modifiers.length) {
|
|
2871
|
+
context.options.onError(
|
|
2872
|
+
compilerDom.createCompilerError(35, loc)
|
|
2873
|
+
);
|
|
2874
|
+
}
|
|
2875
|
+
arg = resolveExpression(arg);
|
|
2876
|
+
const { keyModifiers, nonKeyModifiers, eventOptionModifiers } = compilerDom.resolveModifiers(
|
|
2877
|
+
arg.isStatic ? `on${arg.content}` : arg,
|
|
2878
|
+
modifiers,
|
|
2879
|
+
null,
|
|
2880
|
+
loc
|
|
2881
|
+
);
|
|
2882
|
+
let keyOverride;
|
|
2883
|
+
const isStaticClick = arg.isStatic && arg.content.toLowerCase() === "click";
|
|
2884
|
+
const delegate = arg.isStatic && !eventOptionModifiers.length && delegatedEvents(arg.content);
|
|
2885
|
+
if (nonKeyModifiers.includes("middle")) {
|
|
2886
|
+
if (isStaticClick) {
|
|
2887
|
+
arg = shared.extend({}, arg, { content: "mouseup" });
|
|
2888
|
+
} else if (!arg.isStatic) {
|
|
2889
|
+
keyOverride = ["click", "mouseup"];
|
|
2890
|
+
}
|
|
2891
|
+
}
|
|
2892
|
+
if (nonKeyModifiers.includes("right")) {
|
|
2893
|
+
if (isStaticClick) {
|
|
2894
|
+
arg = shared.extend({}, arg, { content: "contextmenu" });
|
|
2895
|
+
} else if (!arg.isStatic) {
|
|
2896
|
+
keyOverride = ["click", "contextmenu"];
|
|
2897
|
+
}
|
|
2898
|
+
}
|
|
2899
|
+
if (isComponent || isSlotOutlet) {
|
|
2900
|
+
const handler = exp || EMPTY_EXPRESSION;
|
|
2901
|
+
return {
|
|
2902
|
+
key: arg,
|
|
2903
|
+
value: handler,
|
|
2904
|
+
handler: true,
|
|
2905
|
+
handlerModifiers: eventOptionModifiers
|
|
2906
|
+
};
|
|
2907
|
+
}
|
|
2908
|
+
const operation = {
|
|
2909
|
+
type: 5,
|
|
2910
|
+
element: context.reference(),
|
|
2911
|
+
key: arg,
|
|
2912
|
+
value: exp,
|
|
2913
|
+
modifiers: {
|
|
2914
|
+
keys: keyModifiers,
|
|
2915
|
+
nonKeys: nonKeyModifiers,
|
|
2916
|
+
options: eventOptionModifiers
|
|
2917
|
+
},
|
|
2918
|
+
keyOverride,
|
|
2919
|
+
delegate,
|
|
2920
|
+
effect: !arg.isStatic
|
|
2921
|
+
};
|
|
2922
|
+
context.registerEffect([arg], operation);
|
|
2923
|
+
};
|
|
2924
|
+
|
|
2925
|
+
const transformVShow = (dir, node, context) => {
|
|
2926
|
+
const { exp, loc } = dir;
|
|
2927
|
+
if (!exp) {
|
|
2928
|
+
context.options.onError(
|
|
2929
|
+
compilerDom.createDOMCompilerError(61, loc)
|
|
2930
|
+
);
|
|
2931
|
+
return;
|
|
2932
|
+
}
|
|
2933
|
+
if (node.tagType === 2) {
|
|
2934
|
+
context.options.onError(
|
|
2935
|
+
compilerDom.createCompilerError(
|
|
2936
|
+
36,
|
|
2937
|
+
loc
|
|
2938
|
+
)
|
|
2939
|
+
);
|
|
2940
|
+
return;
|
|
2941
|
+
}
|
|
2942
|
+
context.registerOperation({
|
|
2943
|
+
type: 14,
|
|
2944
|
+
element: context.reference(),
|
|
2945
|
+
dir,
|
|
2946
|
+
name: "show",
|
|
2947
|
+
builtin: true
|
|
2948
|
+
});
|
|
2949
|
+
};
|
|
2950
|
+
|
|
2951
|
+
const transformTemplateRef = (node, context) => {
|
|
2952
|
+
if (node.type !== 1) return;
|
|
2953
|
+
const dir = findProp(node, "ref", false, true);
|
|
2954
|
+
if (!dir) return;
|
|
2955
|
+
context.ir.hasTemplateRef = true;
|
|
2956
|
+
let value;
|
|
2957
|
+
if (dir.type === 7) {
|
|
2958
|
+
value = dir.exp || normalizeBindShorthand(dir.arg, context);
|
|
2959
|
+
} else {
|
|
2960
|
+
value = dir.value ? compilerDom.createSimpleExpression(dir.value.content, true, dir.value.loc) : EMPTY_EXPRESSION;
|
|
2961
|
+
}
|
|
2962
|
+
return () => {
|
|
2963
|
+
const id = context.reference();
|
|
2964
|
+
const effect = !isConstantExpression(value);
|
|
2965
|
+
effect && context.registerOperation({
|
|
2966
|
+
type: 15,
|
|
2967
|
+
id
|
|
2968
|
+
});
|
|
2969
|
+
context.registerEffect([value], {
|
|
2970
|
+
type: 8,
|
|
2971
|
+
element: id,
|
|
2972
|
+
value,
|
|
2973
|
+
refFor: !!context.inVFor,
|
|
2974
|
+
effect
|
|
2975
|
+
});
|
|
2976
|
+
};
|
|
2977
|
+
};
|
|
2978
|
+
|
|
2979
|
+
const seen = /* @__PURE__ */ new WeakMap();
|
|
2980
|
+
const transformText = (node, context) => {
|
|
2981
|
+
if (!seen.has(context.root)) seen.set(context.root, /* @__PURE__ */ new WeakSet());
|
|
2982
|
+
if (seen.get(context.root).has(node)) {
|
|
2983
|
+
context.dynamic.flags |= 2;
|
|
2984
|
+
return;
|
|
2985
|
+
}
|
|
2986
|
+
if (node.type === 1 && node.tagType === 0 && isAllTextLike(node.children)) {
|
|
2987
|
+
processTextLikeContainer(
|
|
2988
|
+
node.children,
|
|
2989
|
+
context
|
|
2990
|
+
);
|
|
2991
|
+
} else if (node.type === 5) {
|
|
2992
|
+
processTextLike(context);
|
|
2993
|
+
} else if (node.type === 2) {
|
|
2994
|
+
context.template += node.content;
|
|
2995
|
+
}
|
|
2996
|
+
};
|
|
2997
|
+
function processTextLike(context) {
|
|
2998
|
+
const nexts = context.parent.node.children.slice(context.index);
|
|
2999
|
+
const idx = nexts.findIndex((n) => !isTextLike(n));
|
|
3000
|
+
const nodes = idx > -1 ? nexts.slice(0, idx) : nexts;
|
|
3001
|
+
const id = context.reference();
|
|
3002
|
+
const values = nodes.map((node) => createTextLikeExpression(node, context));
|
|
3003
|
+
context.dynamic.flags |= 4 | 2;
|
|
3004
|
+
context.registerOperation({
|
|
3005
|
+
type: 11,
|
|
3006
|
+
id,
|
|
3007
|
+
values,
|
|
3008
|
+
effect: !values.every(isConstantExpression) && !context.inVOnce
|
|
3009
|
+
});
|
|
3010
|
+
}
|
|
3011
|
+
function processTextLikeContainer(children, context) {
|
|
3012
|
+
const values = children.map((child) => createTextLikeExpression(child, context));
|
|
3013
|
+
const literals = values.map(getLiteralExpressionValue);
|
|
3014
|
+
if (literals.every((l) => l != null)) {
|
|
3015
|
+
context.childrenTemplate = literals.map((l) => String(l));
|
|
3016
|
+
} else {
|
|
3017
|
+
context.registerEffect(values, {
|
|
3018
|
+
type: 4,
|
|
3019
|
+
element: context.reference(),
|
|
3020
|
+
values
|
|
3021
|
+
});
|
|
3022
|
+
}
|
|
3023
|
+
}
|
|
3024
|
+
function createTextLikeExpression(node, context) {
|
|
3025
|
+
seen.get(context.root).add(node);
|
|
3026
|
+
if (node.type === 2) {
|
|
3027
|
+
return compilerDom.createSimpleExpression(node.content, true, node.loc);
|
|
3028
|
+
} else {
|
|
3029
|
+
return node.content;
|
|
3030
|
+
}
|
|
3031
|
+
}
|
|
3032
|
+
function isAllTextLike(children) {
|
|
3033
|
+
return !!children.length && children.every(isTextLike) && // at least one an interpolation
|
|
3034
|
+
children.some((n) => n.type === 5);
|
|
3035
|
+
}
|
|
3036
|
+
function isTextLike(node) {
|
|
3037
|
+
return node.type === 5 || node.type === 2;
|
|
3038
|
+
}
|
|
3039
|
+
|
|
3040
|
+
const transformVModel = (dir, node, context) => {
|
|
3041
|
+
const { exp, arg } = dir;
|
|
3042
|
+
if (!exp) {
|
|
3043
|
+
context.options.onError(
|
|
3044
|
+
compilerDom.createCompilerError(41, dir.loc)
|
|
3045
|
+
);
|
|
3046
|
+
return;
|
|
3047
|
+
}
|
|
3048
|
+
const rawExp = exp.loc.source;
|
|
3049
|
+
const bindingType = context.options.bindingMetadata[rawExp];
|
|
3050
|
+
if (bindingType === "props" || bindingType === "props-aliased") {
|
|
3051
|
+
context.options.onError(
|
|
3052
|
+
compilerDom.createCompilerError(44, exp.loc)
|
|
3053
|
+
);
|
|
3054
|
+
return;
|
|
3055
|
+
}
|
|
3056
|
+
const expString = exp.content;
|
|
3057
|
+
const maybeRef = context.options.inline && (bindingType === "setup-let" || bindingType === "setup-ref" || bindingType === "setup-maybe-ref");
|
|
3058
|
+
if (!expString.trim() || !compilerDom.isMemberExpression(exp, context.options) && !maybeRef) {
|
|
3059
|
+
context.options.onError(
|
|
3060
|
+
compilerDom.createCompilerError(42, exp.loc)
|
|
3061
|
+
);
|
|
3062
|
+
return;
|
|
3063
|
+
}
|
|
3064
|
+
const isComponent = node.tagType === 1;
|
|
3065
|
+
if (isComponent) {
|
|
3066
|
+
return {
|
|
3067
|
+
key: arg ? arg : compilerDom.createSimpleExpression("modelValue", true),
|
|
3068
|
+
value: exp,
|
|
3069
|
+
model: true,
|
|
3070
|
+
modelModifiers: dir.modifiers.map((m) => m.content)
|
|
3071
|
+
};
|
|
3072
|
+
}
|
|
3073
|
+
if (dir.arg)
|
|
3074
|
+
context.options.onError(
|
|
3075
|
+
compilerDom.createDOMCompilerError(
|
|
3076
|
+
58,
|
|
3077
|
+
dir.arg.loc
|
|
3078
|
+
)
|
|
3079
|
+
);
|
|
3080
|
+
const { tag } = node;
|
|
3081
|
+
const isCustomElement = context.options.isCustomElement(tag);
|
|
3082
|
+
let modelType = "text";
|
|
3083
|
+
if (tag === "input" || tag === "textarea" || tag === "select" || isCustomElement) {
|
|
3084
|
+
if (tag === "input" || isCustomElement) {
|
|
3085
|
+
const type = compilerDom.findProp(node, "type");
|
|
3086
|
+
if (type) {
|
|
3087
|
+
if (type.type === 7) {
|
|
3088
|
+
modelType = "dynamic";
|
|
3089
|
+
} else if (type.value) {
|
|
3090
|
+
switch (type.value.content) {
|
|
3091
|
+
case "radio":
|
|
3092
|
+
modelType = "radio";
|
|
3093
|
+
break;
|
|
3094
|
+
case "checkbox":
|
|
3095
|
+
modelType = "checkbox";
|
|
3096
|
+
break;
|
|
3097
|
+
case "file":
|
|
3098
|
+
modelType = undefined;
|
|
3099
|
+
context.options.onError(
|
|
3100
|
+
compilerDom.createDOMCompilerError(
|
|
3101
|
+
59,
|
|
3102
|
+
dir.loc
|
|
3103
|
+
)
|
|
3104
|
+
);
|
|
3105
|
+
break;
|
|
3106
|
+
default:
|
|
3107
|
+
checkDuplicatedValue();
|
|
3108
|
+
break;
|
|
3109
|
+
}
|
|
3110
|
+
}
|
|
3111
|
+
} else if (compilerDom.hasDynamicKeyVBind(node)) {
|
|
3112
|
+
modelType = "dynamic";
|
|
3113
|
+
} else {
|
|
3114
|
+
checkDuplicatedValue();
|
|
3115
|
+
}
|
|
3116
|
+
} else if (tag === "select") {
|
|
3117
|
+
modelType = "select";
|
|
3118
|
+
} else {
|
|
3119
|
+
checkDuplicatedValue();
|
|
3120
|
+
}
|
|
3121
|
+
} else {
|
|
3122
|
+
context.options.onError(
|
|
3123
|
+
compilerDom.createDOMCompilerError(
|
|
3124
|
+
57,
|
|
3125
|
+
dir.loc
|
|
3126
|
+
)
|
|
3127
|
+
);
|
|
3128
|
+
}
|
|
3129
|
+
if (modelType)
|
|
3130
|
+
context.registerOperation({
|
|
3131
|
+
type: 14,
|
|
3132
|
+
element: context.reference(),
|
|
3133
|
+
dir,
|
|
3134
|
+
name: "model",
|
|
3135
|
+
modelType,
|
|
3136
|
+
builtin: true
|
|
3137
|
+
});
|
|
3138
|
+
function checkDuplicatedValue() {
|
|
3139
|
+
const value = compilerDom.findDir(node, "bind");
|
|
3140
|
+
if (value && compilerDom.isStaticArgOf(value.arg, "value")) {
|
|
3141
|
+
context.options.onError(
|
|
3142
|
+
compilerDom.createDOMCompilerError(
|
|
3143
|
+
60,
|
|
3144
|
+
value.loc
|
|
3145
|
+
)
|
|
3146
|
+
);
|
|
3147
|
+
}
|
|
3148
|
+
}
|
|
3149
|
+
};
|
|
3150
|
+
|
|
3151
|
+
const transformComment = (node, context) => {
|
|
3152
|
+
if (node.type !== 3) return;
|
|
3153
|
+
if (getSiblingIf(context)) {
|
|
3154
|
+
context.comment.push(node);
|
|
3155
|
+
context.dynamic.flags |= 2;
|
|
3156
|
+
} else {
|
|
3157
|
+
context.template += `<!--${node.content}-->`;
|
|
3158
|
+
}
|
|
3159
|
+
};
|
|
3160
|
+
function getSiblingIf(context, reverse) {
|
|
3161
|
+
const parent = context.parent;
|
|
3162
|
+
if (!parent) return;
|
|
3163
|
+
const siblings = parent.node.children;
|
|
3164
|
+
let sibling;
|
|
3165
|
+
let i = siblings.indexOf(context.node);
|
|
3166
|
+
while (reverse ? --i >= 0 : ++i < siblings.length) {
|
|
3167
|
+
sibling = siblings[i];
|
|
3168
|
+
if (!isCommentLike(sibling)) {
|
|
3169
|
+
break;
|
|
3170
|
+
}
|
|
3171
|
+
}
|
|
3172
|
+
if (sibling && sibling.type === 1 && sibling.props.some(
|
|
3173
|
+
({ type, name }) => type === 7 && ["else-if", reverse ? "if" : "else"].includes(name)
|
|
3174
|
+
)) {
|
|
3175
|
+
return sibling;
|
|
3176
|
+
}
|
|
3177
|
+
}
|
|
3178
|
+
function isCommentLike(node) {
|
|
3179
|
+
return node.type === 3 || node.type === 2 && !node.content.trim().length;
|
|
3180
|
+
}
|
|
3181
|
+
|
|
3182
|
+
const transformVIf = createStructuralDirectiveTransform(
|
|
3183
|
+
["if", "else", "else-if"],
|
|
3184
|
+
processIf
|
|
3185
|
+
);
|
|
3186
|
+
function processIf(node, dir, context) {
|
|
3187
|
+
if (dir.name !== "else" && (!dir.exp || !dir.exp.content.trim())) {
|
|
3188
|
+
const loc = dir.exp ? dir.exp.loc : node.loc;
|
|
3189
|
+
context.options.onError(
|
|
3190
|
+
compilerDom.createCompilerError(28, dir.loc)
|
|
3191
|
+
);
|
|
3192
|
+
dir.exp = compilerDom.createSimpleExpression(`true`, false, loc);
|
|
3193
|
+
}
|
|
3194
|
+
context.dynamic.flags |= 2;
|
|
3195
|
+
if (dir.name === "if") {
|
|
3196
|
+
const id = context.reference();
|
|
3197
|
+
context.dynamic.flags |= 4;
|
|
3198
|
+
const [branch, onExit] = createIfBranch(node, context);
|
|
3199
|
+
return () => {
|
|
3200
|
+
onExit();
|
|
3201
|
+
context.registerOperation({
|
|
3202
|
+
type: 16,
|
|
3203
|
+
id,
|
|
3204
|
+
condition: dir.exp,
|
|
3205
|
+
positive: branch,
|
|
3206
|
+
once: context.inVOnce
|
|
3207
|
+
});
|
|
3208
|
+
};
|
|
3209
|
+
} else {
|
|
3210
|
+
const siblingIf = getSiblingIf(context, true);
|
|
3211
|
+
const { operation } = context.block;
|
|
3212
|
+
let lastIfNode = operation[operation.length - 1];
|
|
3213
|
+
if (
|
|
3214
|
+
// check if v-if is the sibling node
|
|
3215
|
+
!siblingIf || // check if IfNode is the last operation and get the root IfNode
|
|
3216
|
+
!lastIfNode || lastIfNode.type !== 16
|
|
3217
|
+
) {
|
|
3218
|
+
context.options.onError(
|
|
3219
|
+
compilerDom.createCompilerError(30, node.loc)
|
|
3220
|
+
);
|
|
3221
|
+
return;
|
|
3222
|
+
}
|
|
3223
|
+
while (lastIfNode.negative && lastIfNode.negative.type === 16) {
|
|
3224
|
+
lastIfNode = lastIfNode.negative;
|
|
3225
|
+
}
|
|
3226
|
+
if (dir.name === "else-if" && lastIfNode.negative) {
|
|
3227
|
+
context.options.onError(
|
|
3228
|
+
compilerDom.createCompilerError(30, node.loc)
|
|
3229
|
+
);
|
|
3230
|
+
}
|
|
3231
|
+
if (context.root.comment.length) {
|
|
3232
|
+
node = wrapTemplate(node, ["else-if", "else"]);
|
|
3233
|
+
context.node = node = shared.extend({}, node, {
|
|
3234
|
+
children: [...context.comment, ...node.children]
|
|
3235
|
+
});
|
|
3236
|
+
}
|
|
3237
|
+
context.root.comment = [];
|
|
3238
|
+
const [branch, onExit] = createIfBranch(node, context);
|
|
3239
|
+
if (dir.name === "else") {
|
|
3240
|
+
lastIfNode.negative = branch;
|
|
3241
|
+
} else {
|
|
3242
|
+
lastIfNode.negative = {
|
|
3243
|
+
type: 16,
|
|
3244
|
+
id: -1,
|
|
3245
|
+
condition: dir.exp,
|
|
3246
|
+
positive: branch,
|
|
3247
|
+
once: context.inVOnce
|
|
3248
|
+
};
|
|
3249
|
+
}
|
|
3250
|
+
return () => onExit();
|
|
3251
|
+
}
|
|
3252
|
+
}
|
|
3253
|
+
function createIfBranch(node, context) {
|
|
3254
|
+
context.node = node = wrapTemplate(node, ["if", "else-if", "else"]);
|
|
3255
|
+
const branch = newBlock(node);
|
|
3256
|
+
const exitBlock = context.enterBlock(branch);
|
|
3257
|
+
context.reference();
|
|
3258
|
+
return [branch, exitBlock];
|
|
3259
|
+
}
|
|
3260
|
+
|
|
3261
|
+
const transformVFor = createStructuralDirectiveTransform(
|
|
3262
|
+
"for",
|
|
3263
|
+
processFor
|
|
3264
|
+
);
|
|
3265
|
+
function processFor(node, dir, context) {
|
|
3266
|
+
if (!dir.exp) {
|
|
3267
|
+
context.options.onError(
|
|
3268
|
+
compilerDom.createCompilerError(31, dir.loc)
|
|
3269
|
+
);
|
|
3270
|
+
return;
|
|
3271
|
+
}
|
|
3272
|
+
const parseResult = dir.forParseResult;
|
|
3273
|
+
if (!parseResult) {
|
|
3274
|
+
context.options.onError(
|
|
3275
|
+
compilerDom.createCompilerError(32, dir.loc)
|
|
3276
|
+
);
|
|
3277
|
+
return;
|
|
3278
|
+
}
|
|
3279
|
+
const { source, value, key, index } = parseResult;
|
|
3280
|
+
const keyProp = findProp(node, "key");
|
|
3281
|
+
const keyProperty = keyProp && propToExpression(keyProp);
|
|
3282
|
+
const isComponent = node.tagType === 1;
|
|
3283
|
+
context.node = node = wrapTemplate(node, ["for"]);
|
|
3284
|
+
context.dynamic.flags |= 2 | 4;
|
|
3285
|
+
const id = context.reference();
|
|
3286
|
+
const render = newBlock(node);
|
|
3287
|
+
const exitBlock = context.enterBlock(render, true);
|
|
3288
|
+
context.reference();
|
|
3289
|
+
return () => {
|
|
3290
|
+
exitBlock();
|
|
3291
|
+
context.registerOperation({
|
|
3292
|
+
type: 17,
|
|
3293
|
+
id,
|
|
3294
|
+
source,
|
|
3295
|
+
value,
|
|
3296
|
+
key,
|
|
3297
|
+
index,
|
|
3298
|
+
keyProp: keyProperty,
|
|
3299
|
+
render,
|
|
3300
|
+
once: context.inVOnce,
|
|
3301
|
+
component: isComponent
|
|
3302
|
+
});
|
|
3303
|
+
};
|
|
3304
|
+
}
|
|
3305
|
+
|
|
3306
|
+
const transformSlotOutlet = (node, context) => {
|
|
3307
|
+
if (node.type !== 1 || node.tag !== "slot") {
|
|
3308
|
+
return;
|
|
3309
|
+
}
|
|
3310
|
+
const id = context.reference();
|
|
3311
|
+
context.dynamic.flags |= 4 | 2;
|
|
3312
|
+
const [fallback, exitBlock] = createFallback(
|
|
3313
|
+
node,
|
|
3314
|
+
context
|
|
3315
|
+
);
|
|
3316
|
+
let slotName;
|
|
3317
|
+
const slotProps = [];
|
|
3318
|
+
for (const prop of node.props) {
|
|
3319
|
+
if (prop.type === 6) {
|
|
3320
|
+
if (prop.value) {
|
|
3321
|
+
if (prop.name === "name") {
|
|
3322
|
+
slotName = createSimpleExpression(prop.value.content, true, prop.loc);
|
|
3323
|
+
} else {
|
|
3324
|
+
slotProps.push(shared.extend({}, prop, { name: shared.camelize(prop.name) }));
|
|
3325
|
+
}
|
|
3326
|
+
}
|
|
3327
|
+
} else if (prop.name === "bind" && isStaticArgOf(prop.arg, "name")) {
|
|
3328
|
+
if (prop.exp) {
|
|
3329
|
+
slotName = prop.exp;
|
|
3330
|
+
} else {
|
|
3331
|
+
slotName = createSimpleExpression(
|
|
3332
|
+
shared.camelize(prop.arg.content),
|
|
3333
|
+
false,
|
|
3334
|
+
prop.arg.loc
|
|
3335
|
+
);
|
|
3336
|
+
slotName.ast = null;
|
|
3337
|
+
}
|
|
3338
|
+
} else {
|
|
3339
|
+
let slotProp = prop;
|
|
3340
|
+
if (slotProp.name === "bind" && slotProp.arg && isStaticExp(slotProp.arg)) {
|
|
3341
|
+
slotProp = shared.extend({}, prop, {
|
|
3342
|
+
arg: shared.extend({}, slotProp.arg, {
|
|
3343
|
+
content: shared.camelize(slotProp.arg.content)
|
|
3344
|
+
})
|
|
3345
|
+
});
|
|
3346
|
+
}
|
|
3347
|
+
slotProps.push(slotProp);
|
|
3348
|
+
}
|
|
3349
|
+
}
|
|
3350
|
+
slotName || (slotName = createSimpleExpression("default", true));
|
|
3351
|
+
let irProps = [];
|
|
3352
|
+
if (slotProps.length) {
|
|
3353
|
+
const [isDynamic, props] = buildProps(
|
|
3354
|
+
shared.extend({}, node, { props: slotProps }),
|
|
3355
|
+
context,
|
|
3356
|
+
true
|
|
3357
|
+
);
|
|
3358
|
+
irProps = isDynamic ? props : [props];
|
|
3359
|
+
const runtimeDirective = context.block.operation.find(
|
|
3360
|
+
(oper) => oper.type === 14 && oper.element === id
|
|
3361
|
+
);
|
|
3362
|
+
if (runtimeDirective) {
|
|
3363
|
+
context.options.onError(
|
|
3364
|
+
createCompilerError(
|
|
3365
|
+
36,
|
|
3366
|
+
runtimeDirective.dir.loc
|
|
3367
|
+
)
|
|
3368
|
+
);
|
|
3369
|
+
}
|
|
3370
|
+
}
|
|
3371
|
+
return () => {
|
|
3372
|
+
exitBlock && exitBlock();
|
|
3373
|
+
context.registerOperation({
|
|
3374
|
+
type: 13,
|
|
3375
|
+
id,
|
|
3376
|
+
name: slotName,
|
|
3377
|
+
props: irProps,
|
|
3378
|
+
fallback
|
|
3379
|
+
});
|
|
3380
|
+
};
|
|
3381
|
+
};
|
|
3382
|
+
function createFallback(node, context) {
|
|
3383
|
+
if (!node.children.length) {
|
|
3384
|
+
return [];
|
|
3385
|
+
}
|
|
3386
|
+
context.node = node = shared.extend({}, node, {
|
|
3387
|
+
type: 1,
|
|
3388
|
+
tag: "template",
|
|
3389
|
+
props: [],
|
|
3390
|
+
tagType: 3,
|
|
3391
|
+
children: node.children
|
|
3392
|
+
});
|
|
3393
|
+
const fallback = newBlock(node);
|
|
3394
|
+
const exitBlock = context.enterBlock(fallback);
|
|
3395
|
+
context.reference();
|
|
3396
|
+
return [fallback, exitBlock];
|
|
3397
|
+
}
|
|
3398
|
+
|
|
3399
|
+
const transformVSlot = (node, context) => {
|
|
3400
|
+
if (node.type !== 1) return;
|
|
3401
|
+
const dir = findDir(node, "slot", true);
|
|
3402
|
+
const { tagType, children } = node;
|
|
3403
|
+
const { parent } = context;
|
|
3404
|
+
const isComponent = tagType === 1;
|
|
3405
|
+
const isSlotTemplate = isTemplateNode(node) && parent && parent.node.type === 1 && parent.node.tagType === 1;
|
|
3406
|
+
if (isComponent && children.length) {
|
|
3407
|
+
return transformComponentSlot(
|
|
3408
|
+
node,
|
|
3409
|
+
dir,
|
|
3410
|
+
context
|
|
3411
|
+
);
|
|
3412
|
+
} else if (isSlotTemplate && dir) {
|
|
3413
|
+
return transformTemplateSlot(
|
|
3414
|
+
node,
|
|
3415
|
+
dir,
|
|
3416
|
+
context
|
|
3417
|
+
);
|
|
3418
|
+
} else if (!isComponent && dir) {
|
|
3419
|
+
context.options.onError(
|
|
3420
|
+
createCompilerError(40, dir.loc)
|
|
3421
|
+
);
|
|
3422
|
+
}
|
|
3423
|
+
};
|
|
3424
|
+
function transformComponentSlot(node, dir, context) {
|
|
3425
|
+
const { children } = node;
|
|
3426
|
+
const arg = dir && dir.arg;
|
|
3427
|
+
const nonSlotTemplateChildren = children.filter(
|
|
3428
|
+
(n) => isNonWhitespaceContent(node) && !(n.type === 1 && n.props.some(isVSlot))
|
|
3429
|
+
);
|
|
3430
|
+
const [block, onExit] = createSlotBlock(node, dir, context);
|
|
3431
|
+
const { slots } = context;
|
|
3432
|
+
return () => {
|
|
3433
|
+
onExit();
|
|
3434
|
+
const hasOtherSlots = !!slots.length;
|
|
3435
|
+
if (dir && hasOtherSlots) {
|
|
3436
|
+
context.options.onError(
|
|
3437
|
+
createCompilerError(37, dir.loc)
|
|
3438
|
+
);
|
|
3439
|
+
return;
|
|
3440
|
+
}
|
|
3441
|
+
if (nonSlotTemplateChildren.length) {
|
|
3442
|
+
if (hasStaticSlot(slots, "default")) {
|
|
3443
|
+
context.options.onError(
|
|
3444
|
+
createCompilerError(
|
|
3445
|
+
39,
|
|
3446
|
+
nonSlotTemplateChildren[0].loc
|
|
3447
|
+
)
|
|
3448
|
+
);
|
|
3449
|
+
} else {
|
|
3450
|
+
registerSlot(slots, arg, block);
|
|
3451
|
+
context.slots = slots;
|
|
3452
|
+
}
|
|
3453
|
+
} else if (hasOtherSlots) {
|
|
3454
|
+
context.slots = slots;
|
|
3455
|
+
}
|
|
3456
|
+
};
|
|
3457
|
+
}
|
|
3458
|
+
function transformTemplateSlot(node, dir, context) {
|
|
3459
|
+
context.dynamic.flags |= 2;
|
|
3460
|
+
const arg = dir.arg && resolveExpression(dir.arg);
|
|
3461
|
+
const vFor = findDir(node, "for");
|
|
3462
|
+
const vIf = findDir(node, "if");
|
|
3463
|
+
const vElse = findDir(
|
|
3464
|
+
node,
|
|
3465
|
+
/^else(-if)?$/,
|
|
3466
|
+
true
|
|
3467
|
+
/* allowEmpty */
|
|
3468
|
+
);
|
|
3469
|
+
const { slots } = context;
|
|
3470
|
+
const [block, onExit] = createSlotBlock(node, dir, context);
|
|
3471
|
+
if (!vFor && !vIf && !vElse) {
|
|
3472
|
+
const slotName = arg ? arg.isStatic && arg.content : "default";
|
|
3473
|
+
if (slotName && hasStaticSlot(slots, slotName)) {
|
|
3474
|
+
context.options.onError(
|
|
3475
|
+
createCompilerError(38, dir.loc)
|
|
3476
|
+
);
|
|
3477
|
+
} else {
|
|
3478
|
+
registerSlot(slots, arg, block);
|
|
3479
|
+
}
|
|
3480
|
+
} else if (vIf) {
|
|
3481
|
+
registerDynamicSlot(slots, {
|
|
3482
|
+
slotType: 3,
|
|
3483
|
+
condition: vIf.exp,
|
|
3484
|
+
positive: {
|
|
3485
|
+
slotType: 1,
|
|
3486
|
+
name: arg,
|
|
3487
|
+
fn: block
|
|
3488
|
+
}
|
|
3489
|
+
});
|
|
3490
|
+
} else if (vElse) {
|
|
3491
|
+
const vIfSlot = slots[slots.length - 1];
|
|
3492
|
+
if (vIfSlot.slotType === 3) {
|
|
3493
|
+
let ifNode = vIfSlot;
|
|
3494
|
+
while (ifNode.negative && ifNode.negative.slotType === 3)
|
|
3495
|
+
ifNode = ifNode.negative;
|
|
3496
|
+
const negative = vElse.exp ? {
|
|
3497
|
+
slotType: 3,
|
|
3498
|
+
condition: vElse.exp,
|
|
3499
|
+
positive: {
|
|
3500
|
+
slotType: 1,
|
|
3501
|
+
name: arg,
|
|
3502
|
+
fn: block
|
|
3503
|
+
}
|
|
3504
|
+
} : {
|
|
3505
|
+
slotType: 1,
|
|
3506
|
+
name: arg,
|
|
3507
|
+
fn: block
|
|
3508
|
+
};
|
|
3509
|
+
ifNode.negative = negative;
|
|
3510
|
+
} else {
|
|
3511
|
+
context.options.onError(
|
|
3512
|
+
createCompilerError(30, vElse.loc)
|
|
3513
|
+
);
|
|
3514
|
+
}
|
|
3515
|
+
} else if (vFor) {
|
|
3516
|
+
if (vFor.forParseResult) {
|
|
3517
|
+
registerDynamicSlot(slots, {
|
|
3518
|
+
slotType: 2,
|
|
3519
|
+
name: arg,
|
|
3520
|
+
fn: block,
|
|
3521
|
+
loop: vFor.forParseResult
|
|
3522
|
+
});
|
|
3523
|
+
} else {
|
|
3524
|
+
context.options.onError(
|
|
3525
|
+
createCompilerError(32, vFor.loc)
|
|
3526
|
+
);
|
|
3527
|
+
}
|
|
3528
|
+
}
|
|
3529
|
+
return onExit;
|
|
3530
|
+
}
|
|
3531
|
+
function ensureStaticSlots(slots) {
|
|
3532
|
+
let lastSlots = slots[slots.length - 1];
|
|
3533
|
+
if (!slots.length || lastSlots.slotType !== 0) {
|
|
3534
|
+
slots.push(
|
|
3535
|
+
lastSlots = {
|
|
3536
|
+
slotType: 0,
|
|
3537
|
+
slots: {}
|
|
3538
|
+
}
|
|
3539
|
+
);
|
|
3540
|
+
}
|
|
3541
|
+
return lastSlots.slots;
|
|
3542
|
+
}
|
|
3543
|
+
function registerSlot(slots, name, block) {
|
|
3544
|
+
const isStatic = !name || name.isStatic;
|
|
3545
|
+
if (isStatic) {
|
|
3546
|
+
const staticSlots = ensureStaticSlots(slots);
|
|
3547
|
+
staticSlots[name ? name.content : "default"] = block;
|
|
3548
|
+
} else {
|
|
3549
|
+
slots.push({
|
|
3550
|
+
slotType: 1,
|
|
3551
|
+
name,
|
|
3552
|
+
fn: block
|
|
3553
|
+
});
|
|
3554
|
+
}
|
|
3555
|
+
}
|
|
3556
|
+
function registerDynamicSlot(allSlots, dynamic) {
|
|
3557
|
+
allSlots.push(dynamic);
|
|
3558
|
+
}
|
|
3559
|
+
function hasStaticSlot(slots, name) {
|
|
3560
|
+
return slots.some((slot) => {
|
|
3561
|
+
if (slot.slotType === 0) return !!slot.slots[name];
|
|
3562
|
+
});
|
|
3563
|
+
}
|
|
3564
|
+
function createSlotBlock(slotNode, dir, context) {
|
|
3565
|
+
const block = newBlock(slotNode);
|
|
3566
|
+
block.props = dir && dir.exp;
|
|
3567
|
+
const exitBlock = context.enterBlock(block);
|
|
3568
|
+
return [block, exitBlock];
|
|
3569
|
+
}
|
|
3570
|
+
function isNonWhitespaceContent(node) {
|
|
3571
|
+
if (node.type !== 2) return true;
|
|
3572
|
+
return !!node.content.trim();
|
|
3573
|
+
}
|
|
3574
|
+
|
|
3575
|
+
function compile(source, options = {}) {
|
|
3576
|
+
const resolvedOptions = shared.extend({}, options);
|
|
3577
|
+
const ast = shared.isString(source) ? compilerDom.parse(source, resolvedOptions) : source;
|
|
3578
|
+
const [nodeTransforms, directiveTransforms] = getBaseTransformPreset();
|
|
3579
|
+
if (options.isTS) {
|
|
3580
|
+
const { expressionPlugins } = options;
|
|
3581
|
+
if (!expressionPlugins || !expressionPlugins.includes("typescript")) {
|
|
3582
|
+
resolvedOptions.expressionPlugins = [
|
|
3583
|
+
...expressionPlugins || [],
|
|
3584
|
+
"typescript"
|
|
3585
|
+
];
|
|
3586
|
+
}
|
|
3587
|
+
}
|
|
3588
|
+
const ir = transform(
|
|
3589
|
+
ast,
|
|
3590
|
+
shared.extend({}, resolvedOptions, {
|
|
3591
|
+
nodeTransforms: [
|
|
3592
|
+
...nodeTransforms,
|
|
3593
|
+
...options.nodeTransforms || []
|
|
3594
|
+
// user transforms
|
|
3595
|
+
],
|
|
3596
|
+
directiveTransforms: shared.extend(
|
|
3597
|
+
{},
|
|
3598
|
+
directiveTransforms,
|
|
3599
|
+
options.directiveTransforms || {}
|
|
3600
|
+
// user transforms
|
|
3601
|
+
)
|
|
3602
|
+
})
|
|
3603
|
+
);
|
|
3604
|
+
return generate(ir, resolvedOptions);
|
|
3605
|
+
}
|
|
3606
|
+
function getBaseTransformPreset() {
|
|
3607
|
+
return [
|
|
3608
|
+
[
|
|
3609
|
+
transformVOnce,
|
|
3610
|
+
transformVIf,
|
|
3611
|
+
transformVFor,
|
|
3612
|
+
transformSlotOutlet,
|
|
3613
|
+
transformTemplateRef,
|
|
3614
|
+
transformText,
|
|
3615
|
+
transformElement,
|
|
3616
|
+
transformVSlot,
|
|
3617
|
+
transformComment,
|
|
3618
|
+
transformChildren
|
|
3619
|
+
],
|
|
3620
|
+
{
|
|
3621
|
+
bind: transformVBind,
|
|
3622
|
+
on: transformVOn,
|
|
3623
|
+
html: transformVHtml,
|
|
3624
|
+
text: transformVText,
|
|
3625
|
+
show: transformVShow,
|
|
3626
|
+
model: transformVModel
|
|
3627
|
+
}
|
|
3628
|
+
];
|
|
3629
|
+
}
|
|
3630
|
+
|
|
3631
|
+
const IRDynamicPropsKind = {
|
|
3632
|
+
"EXPRESSION": 0,
|
|
3633
|
+
"0": "EXPRESSION",
|
|
3634
|
+
"ATTRIBUTE": 1,
|
|
3635
|
+
"1": "ATTRIBUTE"
|
|
3636
|
+
};
|
|
3637
|
+
const IRSlotType = {
|
|
3638
|
+
"STATIC": 0,
|
|
3639
|
+
"0": "STATIC",
|
|
3640
|
+
"DYNAMIC": 1,
|
|
3641
|
+
"1": "DYNAMIC",
|
|
3642
|
+
"LOOP": 2,
|
|
3643
|
+
"2": "LOOP",
|
|
3644
|
+
"CONDITIONAL": 3,
|
|
3645
|
+
"3": "CONDITIONAL",
|
|
3646
|
+
"EXPRESSION": 4,
|
|
3647
|
+
"4": "EXPRESSION"
|
|
3648
|
+
};
|
|
3649
|
+
|
|
3650
|
+
const IRNodeTypes = {
|
|
3651
|
+
"ROOT": 0,
|
|
3652
|
+
"0": "ROOT",
|
|
3653
|
+
"BLOCK": 1,
|
|
3654
|
+
"1": "BLOCK",
|
|
3655
|
+
"SET_PROP": 2,
|
|
3656
|
+
"2": "SET_PROP",
|
|
3657
|
+
"SET_DYNAMIC_PROPS": 3,
|
|
3658
|
+
"3": "SET_DYNAMIC_PROPS",
|
|
3659
|
+
"SET_TEXT": 4,
|
|
3660
|
+
"4": "SET_TEXT",
|
|
3661
|
+
"SET_EVENT": 5,
|
|
3662
|
+
"5": "SET_EVENT",
|
|
3663
|
+
"SET_DYNAMIC_EVENTS": 6,
|
|
3664
|
+
"6": "SET_DYNAMIC_EVENTS",
|
|
3665
|
+
"SET_HTML": 7,
|
|
3666
|
+
"7": "SET_HTML",
|
|
3667
|
+
"SET_TEMPLATE_REF": 8,
|
|
3668
|
+
"8": "SET_TEMPLATE_REF",
|
|
3669
|
+
"INSERT_NODE": 9,
|
|
3670
|
+
"9": "INSERT_NODE",
|
|
3671
|
+
"PREPEND_NODE": 10,
|
|
3672
|
+
"10": "PREPEND_NODE",
|
|
3673
|
+
"CREATE_TEXT_NODE": 11,
|
|
3674
|
+
"11": "CREATE_TEXT_NODE",
|
|
3675
|
+
"CREATE_COMPONENT_NODE": 12,
|
|
3676
|
+
"12": "CREATE_COMPONENT_NODE",
|
|
3677
|
+
"SLOT_OUTLET_NODE": 13,
|
|
3678
|
+
"13": "SLOT_OUTLET_NODE",
|
|
3679
|
+
"DIRECTIVE": 14,
|
|
3680
|
+
"14": "DIRECTIVE",
|
|
3681
|
+
"DECLARE_OLD_REF": 15,
|
|
3682
|
+
"15": "DECLARE_OLD_REF",
|
|
3683
|
+
"IF": 16,
|
|
3684
|
+
"16": "IF",
|
|
3685
|
+
"FOR": 17,
|
|
3686
|
+
"17": "FOR"
|
|
3687
|
+
};
|
|
3688
|
+
const DynamicFlag = {
|
|
3689
|
+
"NONE": 0,
|
|
3690
|
+
"0": "NONE",
|
|
3691
|
+
"REFERENCED": 1,
|
|
3692
|
+
"1": "REFERENCED",
|
|
3693
|
+
"NON_TEMPLATE": 2,
|
|
3694
|
+
"2": "NON_TEMPLATE",
|
|
3695
|
+
"INSERT": 4,
|
|
3696
|
+
"4": "INSERT"
|
|
3697
|
+
};
|
|
3698
|
+
|
|
3699
|
+
function createVaporCompilerError(code, loc) {
|
|
3700
|
+
return compilerDom.createCompilerError(
|
|
3701
|
+
code,
|
|
3702
|
+
loc,
|
|
3703
|
+
VaporErrorMessages
|
|
3704
|
+
);
|
|
3705
|
+
}
|
|
3706
|
+
const VaporErrorCodes = {
|
|
3707
|
+
"X_V_PLACEHOLDER": 100,
|
|
3708
|
+
"100": "X_V_PLACEHOLDER",
|
|
3709
|
+
"__EXTEND_POINT__": 101,
|
|
3710
|
+
"101": "__EXTEND_POINT__"
|
|
3711
|
+
};
|
|
3712
|
+
const VaporErrorMessages = {
|
|
3713
|
+
[100]: `[placeholder]`,
|
|
3714
|
+
// just to fulfill types
|
|
3715
|
+
[101]: ``
|
|
3716
|
+
};
|
|
3717
|
+
|
|
3718
|
+
exports.parse = compilerDom.parse;
|
|
3719
|
+
exports.CodegenContext = CodegenContext;
|
|
3720
|
+
exports.DynamicFlag = DynamicFlag;
|
|
3721
|
+
exports.IRDynamicPropsKind = IRDynamicPropsKind;
|
|
3722
|
+
exports.IRNodeTypes = IRNodeTypes;
|
|
3723
|
+
exports.IRSlotType = IRSlotType;
|
|
3724
|
+
exports.VaporErrorCodes = VaporErrorCodes;
|
|
3725
|
+
exports.VaporErrorMessages = VaporErrorMessages;
|
|
3726
|
+
exports.buildCodeFragment = buildCodeFragment;
|
|
3727
|
+
exports.codeFragmentToString = codeFragmentToString;
|
|
3728
|
+
exports.compile = compile;
|
|
3729
|
+
exports.createStructuralDirectiveTransform = createStructuralDirectiveTransform;
|
|
3730
|
+
exports.createVaporCompilerError = createVaporCompilerError;
|
|
3731
|
+
exports.genCall = genCall;
|
|
3732
|
+
exports.genMulti = genMulti;
|
|
3733
|
+
exports.generate = generate;
|
|
3734
|
+
exports.transform = transform;
|
|
3735
|
+
exports.transformChildren = transformChildren;
|
|
3736
|
+
exports.transformComment = transformComment;
|
|
3737
|
+
exports.transformElement = transformElement;
|
|
3738
|
+
exports.transformSlotOutlet = transformSlotOutlet;
|
|
3739
|
+
exports.transformTemplateRef = transformTemplateRef;
|
|
3740
|
+
exports.transformText = transformText;
|
|
3741
|
+
exports.transformVBind = transformVBind;
|
|
3742
|
+
exports.transformVFor = transformVFor;
|
|
3743
|
+
exports.transformVHtml = transformVHtml;
|
|
3744
|
+
exports.transformVIf = transformVIf;
|
|
3745
|
+
exports.transformVModel = transformVModel;
|
|
3746
|
+
exports.transformVOn = transformVOn;
|
|
3747
|
+
exports.transformVOnce = transformVOnce;
|
|
3748
|
+
exports.transformVShow = transformVShow;
|
|
3749
|
+
exports.transformVSlot = transformVSlot;
|
|
3750
|
+
exports.transformVText = transformVText;
|
|
3751
|
+
exports.wrapTemplate = wrapTemplate;
|