vue-jsx 0.2.8 → 3.3.0-beta.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 +91 -92
- package/dist/api.d.ts +7 -0
- package/dist/api.js +2 -0
- package/dist/astro.d.ts +11 -0
- package/dist/astro.js +11 -0
- package/dist/bun.d.ts +6 -0
- package/dist/bun.js +5 -0
- package/dist/core-D6A0aY-g.js +11 -0
- package/dist/esbuild.d.ts +7 -0
- package/dist/esbuild.js +5 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/nuxt.d.ts +8 -0
- package/dist/nuxt.js +17 -0
- package/dist/options-DHzozgGn.d.ts +25 -0
- package/dist/raw.d.ts +7 -0
- package/dist/raw.js +64 -0
- package/dist/rolldown.d.ts +7 -0
- package/dist/rolldown.js +6 -0
- package/dist/rollup.d.ts +7 -0
- package/dist/rollup.js +5 -0
- package/dist/rsbuild.d.ts +9 -0
- package/dist/rsbuild.js +12 -0
- package/dist/rspack.d.ts +6 -0
- package/dist/rspack.js +5 -0
- package/dist/unplugin.d.ts +9 -0
- package/dist/unplugin.js +9 -0
- package/dist/vapor.d.ts +3 -0
- package/dist/vapor.js +3 -0
- package/dist/vite.d.ts +7 -0
- package/dist/vite.js +5 -0
- package/dist/volar.d.ts +7 -0
- package/dist/volar.js +770 -0
- package/dist/webpack.d.ts +7 -0
- package/dist/webpack.js +5 -0
- package/jsx-runtime/index.d.ts +10 -0
- package/jsx-runtime/index.js +11 -0
- package/package.json +107 -25
- package/.browserslistrc +0 -3
- package/.eslintrc.js +0 -17
- package/babel.config.js +0 -5
- package/dist/demo.html +0 -8
- package/dist/vue-jsx.common.js +0 -1645
- package/dist/vue-jsx.common.js.map +0 -1
- package/dist/vue-jsx.umd.js +0 -1655
- package/dist/vue-jsx.umd.js.map +0 -1
- package/dist/vue-jsx.umd.min.js +0 -2
- package/dist/vue-jsx.umd.min.js.map +0 -1
- package/postcss.config.js +0 -5
- package/public/favicon.ico +0 -0
- package/public/index.html +0 -17
- package/src/App.vue +0 -122
- package/src/assets/logo.png +0 -0
- package/src/components/HelloWorld.vue +0 -58
- package/src/index.js +0 -210
- package/src/main.js +0 -8
- package/vue.config.js +0 -11
package/dist/volar.js
ADDED
|
@@ -0,0 +1,770 @@
|
|
|
1
|
+
import jsxMacros from "@vue-jsx/macros/volar";
|
|
2
|
+
import { allCodeFeatures, createPlugin } from "ts-macro";
|
|
3
|
+
import { isHTMLTag, isSVGTag } from "@vue/shared";
|
|
4
|
+
//#region ../../node_modules/.pnpm/@vue-macros+volar@3.1.4_patch_hash=79e6492c51b3afca57b92023dd1aae9ddd57140efa1cf2cdf74d_acc5ae0987bad0943555f2f9fe5e1571/node_modules/@vue-macros/volar/dist/jsx-directive-w2gUTWdx.js
|
|
5
|
+
function getDirectiveArgs(attribute, options) {
|
|
6
|
+
const { ts, ast } = options;
|
|
7
|
+
const attributeName = attribute.name.getText(ast);
|
|
8
|
+
let isDynamic = false;
|
|
9
|
+
let argument = "";
|
|
10
|
+
let [name, ...modifiers] = attributeName.split(/\s/)[0].replace(/\$([^$]+)\$/, (_, $1) => {
|
|
11
|
+
isDynamic = true;
|
|
12
|
+
return $1.replaceAll("_", ".");
|
|
13
|
+
}).split("_");
|
|
14
|
+
if (name.includes(":")) [name, argument] = name.split(":");
|
|
15
|
+
const initializer = attribute.initializer;
|
|
16
|
+
let modifiersCode;
|
|
17
|
+
let valueCode;
|
|
18
|
+
let argumentCode;
|
|
19
|
+
if (initializer && initializer.kind === ts.SyntaxKind.JsxExpression && initializer.expression && ts.isArrayLiteralExpression(initializer.expression)) {
|
|
20
|
+
const elements = initializer.expression.elements;
|
|
21
|
+
if (elements[0]) valueCode = [elements[0].getText(ast), elements[0].getStart(ast)];
|
|
22
|
+
if (elements[1] && ts.isArrayLiteralExpression(elements[1])) modifiersCode = [elements[1].getText(ast), elements[1].getStart(ast)];
|
|
23
|
+
else {
|
|
24
|
+
if (!argument && elements[1]) {
|
|
25
|
+
isDynamic ||= !ts.isStringLiteral(elements[1]);
|
|
26
|
+
argumentCode = [elements[1].getText(ast), elements[1].getStart(ast)];
|
|
27
|
+
}
|
|
28
|
+
if (elements[2] && ts.isArrayLiteralExpression(elements[2])) modifiersCode = [elements[2].getText(ast), elements[2].getStart(ast)];
|
|
29
|
+
}
|
|
30
|
+
} else if (initializer) valueCode = [ts.isStringLiteral(attribute.initializer) ? attribute.initializer.getText(ast) : attribute.initializer.getText(ast).slice(1, -1), attribute.initializer.getStart(ast) + (ts.isStringLiteral(attribute.initializer) ? 0 : 1)];
|
|
31
|
+
return {
|
|
32
|
+
name,
|
|
33
|
+
valueCode,
|
|
34
|
+
argumentCode,
|
|
35
|
+
modifiersCode,
|
|
36
|
+
isDynamic,
|
|
37
|
+
argument,
|
|
38
|
+
modifiers
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
const getModifierPropName = (name) => {
|
|
42
|
+
return `${name === "modelValue" || name === "model-value" ? "model" : name}Modifiers${name === "model" ? "$" : ""}`;
|
|
43
|
+
};
|
|
44
|
+
function resolveCtxMap(ctxNodeMap, options) {
|
|
45
|
+
if (ctxNodeMap.size) options.codes.push(`
|
|
46
|
+
type __VLS_IsAny<T> = 0 extends 1 & T ? true : false;
|
|
47
|
+
type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A;
|
|
48
|
+
type __VLS_PrettifyGlobal<T> = { [K in keyof T as K]: T[K] } & {};
|
|
49
|
+
declare function __VLS_asFunctionalComponent<
|
|
50
|
+
T,
|
|
51
|
+
K = T extends new (...args: any) => any ? InstanceType<T> : unknown,
|
|
52
|
+
>(
|
|
53
|
+
t: T,
|
|
54
|
+
): T extends new (...args: any) => any
|
|
55
|
+
? (
|
|
56
|
+
props: (K extends { $props: infer Props }
|
|
57
|
+
? Props
|
|
58
|
+
: K extends { props: infer Props }
|
|
59
|
+
? Props
|
|
60
|
+
: any),
|
|
61
|
+
ctx?: any,
|
|
62
|
+
) => JSX.Element & {
|
|
63
|
+
__ctx: {
|
|
64
|
+
attrs: Record<string, any>,
|
|
65
|
+
props: (K extends { $props: infer Props }
|
|
66
|
+
? Props
|
|
67
|
+
: K extends { props: infer Props }
|
|
68
|
+
? Props
|
|
69
|
+
: any),
|
|
70
|
+
slots: K extends { $slots: infer Slots }
|
|
71
|
+
? Slots
|
|
72
|
+
: K extends { slots: infer Slots }
|
|
73
|
+
? Slots
|
|
74
|
+
: any
|
|
75
|
+
emit: K extends { $emit: infer Emit }
|
|
76
|
+
? Emit
|
|
77
|
+
: K extends { emit: infer Emit }
|
|
78
|
+
? Emit
|
|
79
|
+
: any
|
|
80
|
+
expose: (
|
|
81
|
+
exposed: K extends { exposeProxy: infer Exposed }
|
|
82
|
+
? string extends keyof NonNullable<Exposed>
|
|
83
|
+
? K
|
|
84
|
+
: Exposed
|
|
85
|
+
: K,
|
|
86
|
+
) => void
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
: T extends () => any
|
|
90
|
+
? (props: {}, ctx?: any) => ReturnType<T>
|
|
91
|
+
: T extends (...args: any) => any
|
|
92
|
+
? T
|
|
93
|
+
: (_: {}, ctx?: any) => {
|
|
94
|
+
__ctx: {
|
|
95
|
+
attrs?: any
|
|
96
|
+
expose?: any
|
|
97
|
+
slots?: any
|
|
98
|
+
emit?: any
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const __VLS_nativeElements = {
|
|
102
|
+
...{} as SVGElementTagNameMap,
|
|
103
|
+
...{} as HTMLElementTagNameMap,
|
|
104
|
+
};
|
|
105
|
+
declare function __VLS_getFunctionalComponentCtx<T, K, const S>(
|
|
106
|
+
comp: T,
|
|
107
|
+
compInstance: K,
|
|
108
|
+
s: S,
|
|
109
|
+
): S extends keyof typeof __VLS_nativeElements
|
|
110
|
+
? { expose: (exposed: (typeof __VLS_nativeElements)[S]) => any }
|
|
111
|
+
: '__ctx' extends keyof __VLS_PickNotAny<K, {}>
|
|
112
|
+
? K extends { __ctx?: infer Ctx } ? Ctx : never
|
|
113
|
+
: T extends (props: infer P, ctx: { expose: (exposed: infer Exposed) => void } & infer Ctx) => any
|
|
114
|
+
? Ctx & {
|
|
115
|
+
props: P,
|
|
116
|
+
expose: (
|
|
117
|
+
exposed: __VLS_PrettifyGlobal<
|
|
118
|
+
import('vue').ShallowUnwrapRef<Exposed>
|
|
119
|
+
>,
|
|
120
|
+
) => void,
|
|
121
|
+
}
|
|
122
|
+
: {};\n`);
|
|
123
|
+
return new Map(Array.from(ctxNodeMap).map(([node, root], index) => [node, transformCtx(node, root, index, options)]));
|
|
124
|
+
}
|
|
125
|
+
function transformCtx(node, root, index, options) {
|
|
126
|
+
const { ts, ast, codes, prefix } = options;
|
|
127
|
+
const openingElement = getOpeningElement(node, options);
|
|
128
|
+
if (!openingElement) return "";
|
|
129
|
+
let props = "";
|
|
130
|
+
let refValue;
|
|
131
|
+
for (const prop of openingElement.attributes.properties) {
|
|
132
|
+
if (!ts.isJsxAttribute(prop)) continue;
|
|
133
|
+
let name = prop.name.getText(ast);
|
|
134
|
+
if (name === "ref" && prop.initializer && ts.isJsxExpression(prop.initializer) && prop.initializer.expression) {
|
|
135
|
+
refValue = getRefValue(prop.initializer.expression, options);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const prefixModel = `${prefix}model`;
|
|
139
|
+
if (name.startsWith(prefixModel)) {
|
|
140
|
+
const dir = getDirectiveArgs(prop, options);
|
|
141
|
+
if (dir.valueCode && !dir.isDynamic) props += `'${dir.argument || dir.argumentCode?.[0].slice(1, -1) || "modelValue"}': ${dir.valueCode[0]},`;
|
|
142
|
+
continue;
|
|
143
|
+
} else if (name.includes("_")) name = name.split("_")[0];
|
|
144
|
+
else if (prefix && name.startsWith(prefix)) continue;
|
|
145
|
+
if (!name) continue;
|
|
146
|
+
const value = prop.initializer ? ts.isJsxExpression(prop.initializer) && prop.initializer.expression ? prop.initializer.expression.getText(ast) : prop.initializer.getText(ast) : "true";
|
|
147
|
+
props += `'${name}': ${value},`;
|
|
148
|
+
}
|
|
149
|
+
const ctxName = `__VLS_ctx_${refValue || index}`;
|
|
150
|
+
let tagName = "";
|
|
151
|
+
const originTagName = tagName = getTagName(node, options);
|
|
152
|
+
if (isHTMLTag(tagName) || isSVGTag(tagName) || tagName.includes("-")) tagName = `{}`;
|
|
153
|
+
else {
|
|
154
|
+
let types = "";
|
|
155
|
+
if (openingElement.typeArguments?.length) {
|
|
156
|
+
types = `<${openingElement.typeArguments.map((argument) => argument.getText(ast)).join(", ")}>`;
|
|
157
|
+
tagName += types;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const result = `\nconst ${ctxName} = __VLS_getFunctionalComponentCtx(${tagName}, __VLS_asFunctionalComponent(${tagName})({${props}}), '${originTagName}');\n`;
|
|
161
|
+
if (root) codes.replaceRange(root.end - 1, root.end - 1, result);
|
|
162
|
+
else addCode(codes, result);
|
|
163
|
+
return ctxName;
|
|
164
|
+
}
|
|
165
|
+
function addCode(codes, ...args) {
|
|
166
|
+
const index = codes.findIndex((code) => code.includes("__VLS_setup = (async () => {"));
|
|
167
|
+
codes.splice(index === -1 ? codes.length : index + 1, 0, ...args);
|
|
168
|
+
}
|
|
169
|
+
function getRefValue(expression, options) {
|
|
170
|
+
const { ts, ast } = options;
|
|
171
|
+
if (ts.isIdentifier(expression)) return expression.getText(ast);
|
|
172
|
+
else if (ts.isFunctionLike(expression)) {
|
|
173
|
+
let left;
|
|
174
|
+
if (ts.isBinaryExpression(expression.body)) left = expression.body.left;
|
|
175
|
+
ts.forEachChild(expression.body, (node) => {
|
|
176
|
+
if (ts.isBinaryExpression(node)) left = node.left;
|
|
177
|
+
else if (ts.isExpressionStatement(node) && ts.isBinaryExpression(node.expression)) left = node.expression.left;
|
|
178
|
+
});
|
|
179
|
+
return left && (ts.isPropertyAccessExpression(left) || ts.isElementAccessExpression(left) ? left.expression : left).getText(ast);
|
|
180
|
+
} else if (ts.isCallExpression(expression) && expression.arguments[0] && ts.isIdentifier(expression.arguments[0])) return expression.arguments[0].getText(ast);
|
|
181
|
+
}
|
|
182
|
+
function transformCustomDirective(attributes, options) {
|
|
183
|
+
if (!attributes.length) return;
|
|
184
|
+
attributes.forEach((attribute) => transform$1(attribute, options));
|
|
185
|
+
options.codes.push(`
|
|
186
|
+
type __VLS_ResolveDirective<T> = T extends import('vue').Directive<
|
|
187
|
+
any,
|
|
188
|
+
infer Value,
|
|
189
|
+
infer Modifiers extends string,
|
|
190
|
+
infer Argument extends string
|
|
191
|
+
>
|
|
192
|
+
? [Value, Argument, Array<Modifiers>]
|
|
193
|
+
: any;
|
|
194
|
+
`);
|
|
195
|
+
}
|
|
196
|
+
function transform$1(attribute, options) {
|
|
197
|
+
const { codes, ast } = options;
|
|
198
|
+
const { name, modifiersCode, argument, modifiers, argumentCode, valueCode, isDynamic } = getDirectiveArgs(attribute, options);
|
|
199
|
+
const start = attribute.getStart(ast);
|
|
200
|
+
codes.replaceRange(start, attribute.end, [
|
|
201
|
+
ast.text.slice(start, start + name.length),
|
|
202
|
+
start,
|
|
203
|
+
{ verification: true }
|
|
204
|
+
], `={[`, valueCode || "{} as any", ",", ...argument ? [
|
|
205
|
+
...isDynamic && !argument.includes("-") ? [] : [[
|
|
206
|
+
`'`,
|
|
207
|
+
start + name.length + 1,
|
|
208
|
+
{ verification: true }
|
|
209
|
+
]],
|
|
210
|
+
[argument, start + name.length + 1 + (isDynamic ? 1 : 0)],
|
|
211
|
+
...isDynamic && !argument.includes("-") ? [] : [[
|
|
212
|
+
`'`,
|
|
213
|
+
start + name.length + argument.length,
|
|
214
|
+
{ verification: true }
|
|
215
|
+
]]
|
|
216
|
+
] : argumentCode ? [argumentCode] : ["{} as any"], ",", ...modifiersCode ? [modifiersCode] : modifiers.length ? [
|
|
217
|
+
"[",
|
|
218
|
+
...modifiers.flatMap((modify, index) => {
|
|
219
|
+
const offset = start + name.length + (argument ? argument.length + 1 : 0) + (argument && isDynamic ? 2 : 0) + 1 + (index ? modifiers.slice(0, index).join("").length + index : 0);
|
|
220
|
+
return [
|
|
221
|
+
[`'`, offset],
|
|
222
|
+
[modify, offset],
|
|
223
|
+
[`'`, offset + modify.length - 1],
|
|
224
|
+
","
|
|
225
|
+
];
|
|
226
|
+
}),
|
|
227
|
+
"]"
|
|
228
|
+
] : ["{} as any"], `] satisfies __VLS_ResolveDirective<typeof `, [
|
|
229
|
+
`v`,
|
|
230
|
+
start,
|
|
231
|
+
{
|
|
232
|
+
...allCodeFeatures,
|
|
233
|
+
verification: false
|
|
234
|
+
}
|
|
235
|
+
], [
|
|
236
|
+
name[2].toUpperCase() + name.slice(3),
|
|
237
|
+
start + 2,
|
|
238
|
+
{
|
|
239
|
+
...allCodeFeatures,
|
|
240
|
+
verification: false
|
|
241
|
+
}
|
|
242
|
+
], `>}`);
|
|
243
|
+
}
|
|
244
|
+
function transformRef$1(nodes, ctxMap, options) {
|
|
245
|
+
const { codes, ts, ast } = options;
|
|
246
|
+
for (const { node, attribute } of nodes) if (attribute.initializer && ts.isJsxExpression(attribute.initializer) && attribute.initializer.expression && (ts.isFunctionExpression(attribute.initializer.expression) || ts.isArrowFunction(attribute.initializer.expression))) codes.replaceRange(attribute.getStart(ast), attribute.end, "{...({ ", ["ref", attribute.name.getStart(ast)], ": ", [attribute.initializer.expression.getText(ast), attribute.initializer.expression.getStart(ast)], `} satisfies { ref: (e: Parameters<typeof ${ctxMap.get(node)}.expose>[0]) => any }) as {}}`);
|
|
247
|
+
}
|
|
248
|
+
function transformVBind(nodes, options) {
|
|
249
|
+
if (nodes.length === 0) return;
|
|
250
|
+
const { codes, ast } = options;
|
|
251
|
+
for (const { attribute } of nodes) {
|
|
252
|
+
const attributeName = attribute.name.getText(ast);
|
|
253
|
+
const start = attribute.name.getStart(ast);
|
|
254
|
+
const end = attribute.name.end;
|
|
255
|
+
if (attributeName.includes("_")) codes.replaceRange(start + attributeName.indexOf("_"), end);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function resolveVFor(attribute, options) {
|
|
259
|
+
const { ts, ast } = options;
|
|
260
|
+
const result = [];
|
|
261
|
+
if (attribute.initializer && ts.isJsxExpression(attribute.initializer) && attribute.initializer.expression && ts.isBinaryExpression(attribute.initializer.expression)) {
|
|
262
|
+
let index;
|
|
263
|
+
let objectIndex;
|
|
264
|
+
let item = attribute.initializer.expression.left;
|
|
265
|
+
const list = attribute.initializer.expression.right;
|
|
266
|
+
if (ts.isParenthesizedExpression(item)) if (ts.isBinaryExpression(item.expression)) if (ts.isBinaryExpression(item.expression.left)) {
|
|
267
|
+
index = item.expression.left.right;
|
|
268
|
+
objectIndex = item.expression.right;
|
|
269
|
+
item = item.expression.left.left;
|
|
270
|
+
} else {
|
|
271
|
+
index = item.expression.right;
|
|
272
|
+
item = item.expression.left;
|
|
273
|
+
}
|
|
274
|
+
else item = item.expression;
|
|
275
|
+
if (item && list) result.push("__VLS_getVForSourceType(", [ast.text.slice(list.getStart(ast), list.end), list.getStart(ast)], ").map(([", [String(ast.text.slice(item.getStart(ast), item.end)), item.getStart(ast)], ", ", index ? [String(ast.text.slice(index.getStart(ast), index.end)), index.getStart(ast)] : objectIndex ? "undefined" : "", ...objectIndex ? [", ", [String(ast?.text.slice(objectIndex.getStart(ast), objectIndex.end)), objectIndex.getStart(ast)]] : "", "]) => ");
|
|
276
|
+
}
|
|
277
|
+
return result;
|
|
278
|
+
}
|
|
279
|
+
function transformVFor(nodes, options, hasVForAttribute) {
|
|
280
|
+
if (!nodes.length && !hasVForAttribute) return;
|
|
281
|
+
const { codes, ast } = options;
|
|
282
|
+
nodes.forEach(({ attribute, node, parent }) => {
|
|
283
|
+
const result = resolveVFor(attribute, options);
|
|
284
|
+
if (parent) result.unshift("{");
|
|
285
|
+
codes.replaceRange(node.getStart(ast), node.getStart(ast), ...result);
|
|
286
|
+
codes.replaceRange(node.end - 1, node.end, `>)${parent ? "}" : ""}`);
|
|
287
|
+
codes.replaceRange(attribute.getStart(ast), attribute.end);
|
|
288
|
+
});
|
|
289
|
+
codes.push(`
|
|
290
|
+
// @ts-ignore
|
|
291
|
+
function __VLS_getVForSourceType<T extends number | string | any[] | Iterable<any>>(source: T): [
|
|
292
|
+
item: T extends number ? number
|
|
293
|
+
: T extends string ? string
|
|
294
|
+
: T extends any[] ? T[number]
|
|
295
|
+
: T extends Iterable<infer T1> ? T1
|
|
296
|
+
: any,
|
|
297
|
+
index: number,
|
|
298
|
+
][];
|
|
299
|
+
// @ts-ignore
|
|
300
|
+
function __VLS_getVForSourceType<T>(source: T): [
|
|
301
|
+
item: T[keyof T],
|
|
302
|
+
key: keyof T,
|
|
303
|
+
index: number,
|
|
304
|
+
][];
|
|
305
|
+
`);
|
|
306
|
+
}
|
|
307
|
+
function transformVIf(nodes, options) {
|
|
308
|
+
const { codes, ts, prefix, ast } = options;
|
|
309
|
+
nodes.forEach(({ node, attribute, parent }, index) => {
|
|
310
|
+
if (!ts.isIdentifier(attribute.name)) return;
|
|
311
|
+
if ([`${prefix}if`, `${prefix}else-if`].includes(attribute.name.getText(ast)) && attribute.initializer && ts.isJsxExpression(attribute.initializer) && attribute.initializer.expression) {
|
|
312
|
+
const hasScope = parent && attribute.name.text === `${prefix}if`;
|
|
313
|
+
codes.replaceRange(node.getStart(ast), node.getStart(ast), `${hasScope ? "{" : ""}(`, [attribute.initializer.expression.getText(ast), attribute.initializer.expression.getStart(ast)], ") ? ");
|
|
314
|
+
const nextAttribute = nodes[index + 1]?.attribute;
|
|
315
|
+
const nextNodeHasElse = nextAttribute && ts.isIdentifier(nextAttribute.name) ? String(nextAttribute.name.text).startsWith(`${prefix}else`) : false;
|
|
316
|
+
codes.replaceRange(node.end, node.end, nextNodeHasElse ? " : " : ` : null${parent ? "}" : ""}`);
|
|
317
|
+
} else if (attribute.name.text === `${prefix}else`) codes.replaceRange(node.end, node.end, parent ? "}" : "");
|
|
318
|
+
codes.replaceRange(attribute.getStart(ast), attribute.end);
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
const isNativeFormElement = (tag) => {
|
|
322
|
+
return [
|
|
323
|
+
"input",
|
|
324
|
+
"select",
|
|
325
|
+
"textarea"
|
|
326
|
+
].includes(tag);
|
|
327
|
+
};
|
|
328
|
+
function transformVModel(nodeMap, ctxMap, options) {
|
|
329
|
+
if (!nodeMap.size) return;
|
|
330
|
+
for (const [, nodes] of nodeMap) transform(nodes, ctxMap, options);
|
|
331
|
+
getModelsType(options.codes);
|
|
332
|
+
}
|
|
333
|
+
function transform(nodes, ctxMap, options) {
|
|
334
|
+
const { codes, ast } = options;
|
|
335
|
+
let firstNamespacedNode;
|
|
336
|
+
const result = [];
|
|
337
|
+
const emitsResult = [];
|
|
338
|
+
const modifiersResult = [];
|
|
339
|
+
for (const { attribute, node } of nodes) {
|
|
340
|
+
const isNative = isNativeFormElement(getTagName(node, options));
|
|
341
|
+
const modelValue = isNative ? "value" : "modelValue";
|
|
342
|
+
const { name, argument, argumentCode, isDynamic, modifiers, modifiersCode, valueCode } = getDirectiveArgs(attribute, options);
|
|
343
|
+
if (!valueCode) continue;
|
|
344
|
+
const start = attribute.name.getStart(ast);
|
|
345
|
+
const resolvedArgument = argument || argumentCode?.[0].slice(1, -1) || modelValue;
|
|
346
|
+
const modifierProps = isDynamic ? "Record<string, true>" : `__VLS_GetModifierProps<typeof ${ctxMap.get(node)}.props, '${getModifierPropName(resolvedArgument)}'>`;
|
|
347
|
+
if (!argument && !argumentCode) {
|
|
348
|
+
const result$1 = [];
|
|
349
|
+
result$1.push(...isNative ? isRadioOrCheckbox(node, options) ? "v-model" : [[modelValue, start + 2]] : [[modelValue.slice(0, 3), start], [modelValue.slice(3), start]]);
|
|
350
|
+
if (modifiers.length || modifiersCode) result$1.unshift(`{...`, ...modifiersCode ? [modifiersCode] : modifiers.length ? [
|
|
351
|
+
"{",
|
|
352
|
+
...modifiers.flatMap((modify, index) => [
|
|
353
|
+
modify ? modify.includes("-") ? `'` : "" : `'`,
|
|
354
|
+
[modify, start + name.length + 1 + (index ? modifiers.slice(0, index).join("").length + index : 0)],
|
|
355
|
+
modify ? `${modify.includes("-") ? `'` : ""}: true, ` : `'`
|
|
356
|
+
]),
|
|
357
|
+
"}"
|
|
358
|
+
] : [], ` satisfies `, isNative ? modifiersCode ? `('trim' | 'number' | 'lazy')[]` : `{ trim?: true, number?: true, lazy?: true }` : modifiersCode ? `(keyof ${modifierProps})[]` : modifierProps, "} ");
|
|
359
|
+
if (!isNative) result$1.unshift(`{...{'onUpdate:${modelValue}': () => {} }} `);
|
|
360
|
+
codes.replaceRange(start, attribute.end, ...result$1, "={", valueCode, "}");
|
|
361
|
+
} else {
|
|
362
|
+
if (firstNamespacedNode) {
|
|
363
|
+
codes.replaceRange(attribute.getStart(ast), attribute.end);
|
|
364
|
+
result.push(",");
|
|
365
|
+
} else firstNamespacedNode = {
|
|
366
|
+
attribute,
|
|
367
|
+
node
|
|
368
|
+
};
|
|
369
|
+
result.push(isDynamic ? "[" : "", ...argument ? [
|
|
370
|
+
argument.includes("-") ? [
|
|
371
|
+
`'`,
|
|
372
|
+
start + name.length + 1,
|
|
373
|
+
{ verification: true }
|
|
374
|
+
] : "",
|
|
375
|
+
[argument, start + name.length + 1 + (isDynamic ? 1 : 0)],
|
|
376
|
+
argument.includes("-") ? [
|
|
377
|
+
`'`,
|
|
378
|
+
start + name.length + argument.length,
|
|
379
|
+
{ verification: true }
|
|
380
|
+
] : ""
|
|
381
|
+
] : argumentCode ? [argumentCode] : [modelValue], isDynamic ? "]" : "", ":", valueCode);
|
|
382
|
+
if (modifiersCode) modifiersResult.push(` {...`, modifiersCode, ` satisfies (keyof ${modifierProps})[]}`);
|
|
383
|
+
else if (modifiers.length) modifiersResult.push(` {...{`, ...modifiers.flatMap((modify, index) => [
|
|
384
|
+
modify ? modify.includes("-") ? `'` : "" : `'`,
|
|
385
|
+
[modify, start + name.length + (argument ? argument.length : -1) + (argument && isDynamic ? 4 : 2) + (index ? modifiers.slice(0, index).join("").length + index : 0)],
|
|
386
|
+
modify ? `${modify.includes("-") ? `'` : ""}: true, ` : `'`
|
|
387
|
+
]), "} satisfies ", modifierProps, "}");
|
|
388
|
+
if (!isDynamic) emitsResult.push(`'onUpdate:${resolvedArgument}': () => {}, `);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (firstNamespacedNode) {
|
|
392
|
+
const { attribute, node } = firstNamespacedNode;
|
|
393
|
+
codes.replaceRange(attribute.getStart(ast), attribute.end, `{...{`, ...result, `} satisfies __VLS_GetModels<__VLS_NormalizeProps<typeof ${ctxMap.get(node)}.props>>}`, ...modifiersResult, ` {...{`, ...emitsResult, `}}`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function isRadioOrCheckbox(node, options) {
|
|
397
|
+
const { ts, ast } = options;
|
|
398
|
+
const openingElement = getOpeningElement(node, options);
|
|
399
|
+
if (!openingElement) return false;
|
|
400
|
+
return openingElement.tagName.getText(ast) === "input" && openingElement.attributes.properties.some((attr) => {
|
|
401
|
+
return ts.isJsxAttribute(attr) && attr.name.getText(ast) === "type" && attr.initializer && ts.isStringLiteral(attr.initializer) && (attr.initializer.text === "radio" || attr.initializer.text === "checkbox");
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
function getModelsType(codes) {
|
|
405
|
+
codes.push(`
|
|
406
|
+
type __VLS_NormalizeProps<T> = T extends object
|
|
407
|
+
? {
|
|
408
|
+
[K in keyof T as {} extends Record<K, 1>
|
|
409
|
+
? never
|
|
410
|
+
: K extends keyof import('vue').VNodeProps | 'class' | 'style'
|
|
411
|
+
? never
|
|
412
|
+
: K]: T[K]
|
|
413
|
+
}
|
|
414
|
+
: never;
|
|
415
|
+
type __VLS_CamelCase<S extends string> = S extends \`\${infer F}-\${infer RF}\${infer R}\`
|
|
416
|
+
? \`\${F}\${Uppercase<RF>}\${__VLS_CamelCase<R>}\`
|
|
417
|
+
: S;
|
|
418
|
+
type __VLS_PropsToEmits<T> = T extends object
|
|
419
|
+
? {
|
|
420
|
+
[K in keyof T as K extends \`onUpdate:\${infer R}\`
|
|
421
|
+
? R extends 'modelValue'
|
|
422
|
+
? never
|
|
423
|
+
: __VLS_CamelCase<R>
|
|
424
|
+
: never]: T[K]
|
|
425
|
+
}
|
|
426
|
+
: never
|
|
427
|
+
type __VLS_GetModels<P, E = __VLS_PropsToEmits<P>> = P extends object
|
|
428
|
+
? {
|
|
429
|
+
[K in keyof P as K extends keyof E
|
|
430
|
+
? K
|
|
431
|
+
: never]: K extends keyof P ? P[K] : never
|
|
432
|
+
}
|
|
433
|
+
: {};
|
|
434
|
+
type __VLS_GetModifierProps<T, K extends string> = K extends keyof T
|
|
435
|
+
? NonNullable<T[K]>
|
|
436
|
+
: Record<symbol, never>;
|
|
437
|
+
`);
|
|
438
|
+
}
|
|
439
|
+
function transformVOn(nodes, ctxMap, options) {
|
|
440
|
+
if (nodes.length === 0) return;
|
|
441
|
+
const { codes, ast } = options;
|
|
442
|
+
for (const { node, attribute } of nodes) {
|
|
443
|
+
codes.replaceRange(attribute.getStart(ast), attribute.name.end + 2, "{...");
|
|
444
|
+
codes.replaceRange(attribute.end - 1, attribute.end - 1, ` satisfies __VLS_NormalizeEmits<typeof ${ctxMap.get(node)}.emit>`);
|
|
445
|
+
}
|
|
446
|
+
codes.push(`
|
|
447
|
+
type __VLS_UnionToIntersection<U> = (U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never;
|
|
448
|
+
type __VLS_OverloadUnionInner<T, U = unknown> = U & T extends (...args: infer A) => infer R
|
|
449
|
+
? U extends T
|
|
450
|
+
? never
|
|
451
|
+
: __VLS_OverloadUnionInner<T, Pick<T, keyof T> & U & ((...args: A) => R)> | ((...args: A) => R)
|
|
452
|
+
: never;
|
|
453
|
+
type __VLS_OverloadUnion<T> = Exclude<
|
|
454
|
+
__VLS_OverloadUnionInner<(() => never) & T>,
|
|
455
|
+
T extends () => never ? never : () => never
|
|
456
|
+
>;
|
|
457
|
+
type __VLS_ConstructorOverloads<T> = __VLS_OverloadUnion<T> extends infer F
|
|
458
|
+
? F extends (event: infer E, ...args: infer A) => any
|
|
459
|
+
? { [K in E & string]: (...args: A) => void; }
|
|
460
|
+
: never
|
|
461
|
+
: never;
|
|
462
|
+
type __VLS_NormalizeEmits<T> = __VLS_PrettifyGlobal<
|
|
463
|
+
__VLS_UnionToIntersection<
|
|
464
|
+
__VLS_ConstructorOverloads<T> & {
|
|
465
|
+
[K in keyof T]: T[K] extends any[] ? { (...args: T[K]): void } : never
|
|
466
|
+
}
|
|
467
|
+
>
|
|
468
|
+
>;\n`);
|
|
469
|
+
}
|
|
470
|
+
function transformOnWithModifiers(nodes, options) {
|
|
471
|
+
const { codes, ast, ts } = options;
|
|
472
|
+
for (const { attribute } of nodes) {
|
|
473
|
+
const attributeName = attribute.name.getText(ast).split("_")[0];
|
|
474
|
+
const start = attribute.name.getStart(ast);
|
|
475
|
+
const end = attribute.name.end;
|
|
476
|
+
codes.replaceRange(start, end, "{...{", [attributeName, start]);
|
|
477
|
+
if (!attribute.initializer) codes.replaceRange(end, end, ": () => {}}}");
|
|
478
|
+
else if (ts.isJsxExpression(attribute.initializer) && attribute.initializer.expression) {
|
|
479
|
+
codes.replaceRange(end, attribute.initializer.expression.getStart(ast), ": ");
|
|
480
|
+
codes.replaceRange(attribute.end, attribute.end, "}");
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
function transformVSlot(nodeMap, ctxMap, options) {
|
|
485
|
+
if (nodeMap.size === 0) return;
|
|
486
|
+
const { codes, ts, ast, prefix } = options;
|
|
487
|
+
nodeMap.forEach(({ attributeMap, vSlotAttribute }, node) => {
|
|
488
|
+
const result = [" v-slots={{"];
|
|
489
|
+
const attributes = Array.from(attributeMap);
|
|
490
|
+
attributes.forEach(([attribute, { children, vIfAttribute, vForAttribute }], index) => {
|
|
491
|
+
if (!attribute) return;
|
|
492
|
+
let vIfAttributeName;
|
|
493
|
+
if (vIfAttribute && (vIfAttributeName = vIfAttribute.name.getText(ast))) {
|
|
494
|
+
if (`${prefix}if` === vIfAttributeName) result.push("...");
|
|
495
|
+
if ([`${prefix}if`, `${prefix}else-if`].includes(vIfAttributeName) && vIfAttribute.initializer && ts.isJsxExpression(vIfAttribute.initializer) && vIfAttribute.initializer.expression) result.push("(", [vIfAttribute.initializer.expression.getText(ast), vIfAttribute.initializer.expression.getStart(ast)], ") ? {");
|
|
496
|
+
else if (`${prefix}else` === vIfAttributeName) result.push("{");
|
|
497
|
+
}
|
|
498
|
+
if (vForAttribute) result.push("...", ...resolveVFor(vForAttribute, options), "({");
|
|
499
|
+
const { name, argument, argumentCode, valueCode, isDynamic } = getDirectiveArgs(attribute, options);
|
|
500
|
+
const offset = attribute.getStart(ast) + name.length;
|
|
501
|
+
result.push(isDynamic ? "[" : "", ...argument ? [
|
|
502
|
+
argument.includes("-") ? [
|
|
503
|
+
`'`,
|
|
504
|
+
offset + 1,
|
|
505
|
+
{ verification: true }
|
|
506
|
+
] : "",
|
|
507
|
+
[argument, offset + 1 + (isDynamic ? 1 : 0)],
|
|
508
|
+
argument.includes("-") ? [
|
|
509
|
+
`'`,
|
|
510
|
+
offset + argument.length,
|
|
511
|
+
{ verification: true }
|
|
512
|
+
] : ""
|
|
513
|
+
] : [argumentCode || "default"], isDynamic ? "]" : "", `: (`, ...valueCode ? [valueCode, isDynamic ? ": any" : ""] : [], ") => <>", ...children.map((child) => {
|
|
514
|
+
codes.replaceRange(child.pos, child.end);
|
|
515
|
+
const isSlotTemplate = getTagName(child, options) === "template" && !vSlotAttribute;
|
|
516
|
+
const node$1 = isSlotTemplate && ts.isJsxElement(child) ? child.children : child;
|
|
517
|
+
return isSlotTemplate && ts.isJsxSelfClosingElement(child) ? "" : [ast.text.slice(node$1.pos, node$1.end), node$1.pos];
|
|
518
|
+
}), "</>,");
|
|
519
|
+
if (vForAttribute) result.push("})) as any,");
|
|
520
|
+
if (vIfAttribute && vIfAttributeName) {
|
|
521
|
+
if ([`${prefix}if`, `${prefix}else-if`].includes(vIfAttributeName)) {
|
|
522
|
+
const nextAttribute = attributes[index + (attributes[index + 1]?.[0] ? 1 : 2)]?.[1].vIfAttribute;
|
|
523
|
+
result.push("}", nextAttribute && nextAttribute.name.getText(ast).startsWith(`${prefix}else`) ? " : " : " : null,");
|
|
524
|
+
} else if (`${prefix}else` === vIfAttributeName) result.push("},");
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
const slotType = `} satisfies typeof ${ctxMap.get(node)}.slots as Record<string, any>}`;
|
|
528
|
+
if (attributeMap.has(null)) result.push("default: () => <>");
|
|
529
|
+
else result.push(slotType);
|
|
530
|
+
if (vSlotAttribute) codes.replaceRange(vSlotAttribute.getStart(ast), vSlotAttribute.end, ...result);
|
|
531
|
+
else if (ts.isJsxElement(node)) {
|
|
532
|
+
codes.replaceRange(node.openingElement.end - 1, node.openingElement.end, ...result);
|
|
533
|
+
codes.replaceRange(node.closingElement.pos, node.closingElement.pos, attributeMap.has(null) ? `</>${slotType}>` : ">");
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
function transformVSlots(nodes, ctxMap, options) {
|
|
538
|
+
const { codes, ts } = options;
|
|
539
|
+
for (const { node, attribute: { initializer } } of nodes) if (initializer && ts.isJsxExpression(initializer) && initializer.expression) {
|
|
540
|
+
if (ts.isObjectLiteralExpression(initializer.expression)) codes.replaceRange(initializer.expression.end, initializer.expression.end, ` satisfies typeof ${ctxMap.get(node)}.slots`);
|
|
541
|
+
else if (ts.isFunctionExpression(initializer.expression) || ts.isArrowFunction(initializer.expression)) {
|
|
542
|
+
codes.replaceRange(initializer.expression.pos, initializer.expression.pos, "(");
|
|
543
|
+
codes.replaceRange(initializer.expression.end, initializer.expression.end, `) satisfies typeof ${ctxMap.get(node)}.slots.default`);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
function transformJsxDirective(options) {
|
|
548
|
+
const { ast, ts, prefix, codes } = options;
|
|
549
|
+
const resolvedPrefix = prefix.replaceAll("$", String.raw`\$`);
|
|
550
|
+
const slotRegex = /* @__PURE__ */ new RegExp(`^${resolvedPrefix}slot(?=:|$)`);
|
|
551
|
+
const modelRegex = /* @__PURE__ */ new RegExp(`^${resolvedPrefix}model(?=[:_]|$)`);
|
|
552
|
+
const bindRegex = /* @__PURE__ */ new RegExp(`^(?!${resolvedPrefix}|on[A-Z])\\S+_\\S+`);
|
|
553
|
+
const onWithModifiersRegex = /^on[A-Z]\S*_\S+/;
|
|
554
|
+
const vIfMap = /* @__PURE__ */ new Map();
|
|
555
|
+
const vForNodes = [];
|
|
556
|
+
const vSlotMap = /* @__PURE__ */ new Map();
|
|
557
|
+
const vModelMap = /* @__PURE__ */ new Map();
|
|
558
|
+
const vOnNodes = [];
|
|
559
|
+
const onWithModifiers = [];
|
|
560
|
+
const vBindNodes = [];
|
|
561
|
+
const refNodes = [];
|
|
562
|
+
const vSlots = [];
|
|
563
|
+
const customDirectives = [];
|
|
564
|
+
const ctxNodeMap = /* @__PURE__ */ new Map();
|
|
565
|
+
let hasVForAttribute = false;
|
|
566
|
+
function walkJsxDirective(node, parent, parents = []) {
|
|
567
|
+
const tagName = getTagName(node, options);
|
|
568
|
+
const properties = getOpeningElement(node, options);
|
|
569
|
+
let ctxNode;
|
|
570
|
+
let vIfAttribute;
|
|
571
|
+
let vForAttribute;
|
|
572
|
+
let vSlotAttribute;
|
|
573
|
+
for (const attribute of properties?.attributes.properties || []) {
|
|
574
|
+
if (!ts.isJsxAttribute(attribute)) continue;
|
|
575
|
+
const attributeName = attribute.name.getText(ast);
|
|
576
|
+
if ([
|
|
577
|
+
`${prefix}if`,
|
|
578
|
+
`${prefix}else-if`,
|
|
579
|
+
`${prefix}else`
|
|
580
|
+
].includes(attributeName)) vIfAttribute = attribute;
|
|
581
|
+
else if (attributeName === `${prefix}for`) {
|
|
582
|
+
vForAttribute = attribute;
|
|
583
|
+
hasVForAttribute = true;
|
|
584
|
+
} else if (slotRegex.test(attributeName)) vSlotAttribute = attribute;
|
|
585
|
+
else if (modelRegex.test(attributeName)) {
|
|
586
|
+
vModelMap.has(node) || vModelMap.set(node, []);
|
|
587
|
+
vModelMap.get(node).push({
|
|
588
|
+
node,
|
|
589
|
+
attribute
|
|
590
|
+
});
|
|
591
|
+
if (!isNativeFormElement(tagName)) ctxNode = node;
|
|
592
|
+
} else if (attributeName === `${prefix}on`) {
|
|
593
|
+
vOnNodes.push({
|
|
594
|
+
node,
|
|
595
|
+
attribute
|
|
596
|
+
});
|
|
597
|
+
ctxNode = node;
|
|
598
|
+
} else if (onWithModifiersRegex.test(attributeName)) onWithModifiers.push({
|
|
599
|
+
node,
|
|
600
|
+
attribute
|
|
601
|
+
});
|
|
602
|
+
else if (bindRegex.test(attributeName)) vBindNodes.push({
|
|
603
|
+
node,
|
|
604
|
+
attribute
|
|
605
|
+
});
|
|
606
|
+
else if (attributeName === "ref") {
|
|
607
|
+
refNodes.push({
|
|
608
|
+
node,
|
|
609
|
+
attribute
|
|
610
|
+
});
|
|
611
|
+
ctxNode = node;
|
|
612
|
+
} else if (attributeName === `${prefix}slots`) {
|
|
613
|
+
ctxNode = node;
|
|
614
|
+
vSlots.push({
|
|
615
|
+
node,
|
|
616
|
+
attribute
|
|
617
|
+
});
|
|
618
|
+
} else if ([
|
|
619
|
+
`${prefix}html`,
|
|
620
|
+
`${prefix}memo`,
|
|
621
|
+
`${prefix}once`
|
|
622
|
+
].includes(attributeName)) codes.replaceRange(attribute.pos, attribute.end);
|
|
623
|
+
else if (attributeName.startsWith("v-")) customDirectives.push(attribute);
|
|
624
|
+
}
|
|
625
|
+
if (ts.isJsxExpression(node) && node.expression && (ts.isObjectLiteralExpression(node.expression) || ts.isArrowFunction(node.expression) || ts.isFunctionExpression(node.expression)) && parent && ts.isJsxElement(parent) && !parent.children.every((child) => child !== node && (ts.isJsxText(child) ? child.getText(ast).trim() : true))) {
|
|
626
|
+
ctxNode = parent;
|
|
627
|
+
vSlots.push({
|
|
628
|
+
node: parent,
|
|
629
|
+
attribute: { initializer: {
|
|
630
|
+
kind: ts.SyntaxKind.JsxExpression,
|
|
631
|
+
expression: node.expression
|
|
632
|
+
} }
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
if (!vSlotAttribute || tagName !== "template") {
|
|
636
|
+
if (vIfAttribute) {
|
|
637
|
+
vIfMap.has(parent) || vIfMap.set(parent, []);
|
|
638
|
+
vIfMap.get(parent).push({
|
|
639
|
+
node,
|
|
640
|
+
attribute: vIfAttribute,
|
|
641
|
+
parent
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
if (vForAttribute) vForNodes.push({
|
|
645
|
+
node,
|
|
646
|
+
attribute: vForAttribute,
|
|
647
|
+
parent: vIfAttribute ? void 0 : parent
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
if (vSlotAttribute) {
|
|
651
|
+
const slotNode = tagName === "template" ? parent : node;
|
|
652
|
+
if (!slotNode) return;
|
|
653
|
+
ctxNode = slotNode;
|
|
654
|
+
const attributeMap = vSlotMap.get(slotNode)?.attributeMap || vSlotMap.set(slotNode, {
|
|
655
|
+
vSlotAttribute: tagName === "template" ? void 0 : vSlotAttribute,
|
|
656
|
+
attributeMap: /* @__PURE__ */ new Map()
|
|
657
|
+
}).get(slotNode).attributeMap;
|
|
658
|
+
const children = attributeMap.get(vSlotAttribute)?.children || attributeMap.set(vSlotAttribute, {
|
|
659
|
+
children: [],
|
|
660
|
+
...tagName === "template" ? {
|
|
661
|
+
vIfAttribute,
|
|
662
|
+
vForAttribute
|
|
663
|
+
} : {}
|
|
664
|
+
}).get(vSlotAttribute).children;
|
|
665
|
+
if (slotNode === parent && ts.isJsxElement(parent)) {
|
|
666
|
+
children.push(node);
|
|
667
|
+
if (attributeMap.get(null)) return;
|
|
668
|
+
for (const child of parent.children) {
|
|
669
|
+
if (getTagName(child, options) === "template" || ts.isJsxText(child) && !child.getText(ast).trim()) continue;
|
|
670
|
+
(attributeMap.get(null)?.children || attributeMap.set(null, { children: [] }).get(null).children).push(child);
|
|
671
|
+
}
|
|
672
|
+
} else if (ts.isJsxElement(node)) children.push(...node.children);
|
|
673
|
+
}
|
|
674
|
+
if (ctxNode) ctxNodeMap.set(ctxNode, parents.find(ts.isBlock));
|
|
675
|
+
node.forEachChild((child) => {
|
|
676
|
+
parents.unshift(node);
|
|
677
|
+
walkJsxDirective(child, ts.isJsxElement(node) || ts.isJsxFragment(node) ? node : void 0, parents);
|
|
678
|
+
parents.shift();
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
ast.forEachChild(walkJsxDirective);
|
|
682
|
+
const ctxMap = resolveCtxMap(ctxNodeMap, options);
|
|
683
|
+
transformVSlot(vSlotMap, ctxMap, options);
|
|
684
|
+
transformVFor(vForNodes, options, hasVForAttribute);
|
|
685
|
+
vIfMap.forEach((nodes) => transformVIf(nodes, options));
|
|
686
|
+
transformVModel(vModelMap, ctxMap, options);
|
|
687
|
+
transformVOn(vOnNodes, ctxMap, options);
|
|
688
|
+
transformOnWithModifiers(onWithModifiers, options);
|
|
689
|
+
transformVBind(vBindNodes, options);
|
|
690
|
+
transformRef$1(refNodes, ctxMap, options);
|
|
691
|
+
transformVSlots(vSlots, ctxMap, options);
|
|
692
|
+
transformCustomDirective(customDirectives, options);
|
|
693
|
+
}
|
|
694
|
+
function getOpeningElement(node, options) {
|
|
695
|
+
const { ts } = options;
|
|
696
|
+
return ts.isJsxSelfClosingElement(node) ? node : ts.isJsxElement(node) ? node.openingElement : void 0;
|
|
697
|
+
}
|
|
698
|
+
function getTagName(node, options) {
|
|
699
|
+
const openingElement = getOpeningElement(node, options);
|
|
700
|
+
if (!openingElement) return "";
|
|
701
|
+
return openingElement.tagName.getText(options.ast);
|
|
702
|
+
}
|
|
703
|
+
var jsx_directive_default = createPlugin(({ ts, vueCompilerOptions }, options = vueCompilerOptions?.vueMacros?.jsxDirective === true ? {} : vueCompilerOptions?.vueMacros?.jsxDirective ?? {}) => {
|
|
704
|
+
if (!options) return [];
|
|
705
|
+
return {
|
|
706
|
+
name: "vue-macros-jsx-directive",
|
|
707
|
+
resolveVirtualCode({ ast, codes, lang }) {
|
|
708
|
+
if (!["jsx", "tsx"].includes(lang)) return;
|
|
709
|
+
transformJsxDirective({
|
|
710
|
+
codes,
|
|
711
|
+
ast,
|
|
712
|
+
ts,
|
|
713
|
+
prefix: options.prefix ?? "v-"
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
});
|
|
718
|
+
//#endregion
|
|
719
|
+
//#region ../../node_modules/.pnpm/@vue-macros+volar@3.1.4_patch_hash=79e6492c51b3afca57b92023dd1aae9ddd57140efa1cf2cdf74d_acc5ae0987bad0943555f2f9fe5e1571/node_modules/@vue-macros/volar/dist/jsx-ref-DZlb-F7c.js
|
|
720
|
+
function transformRef({ nodes, codes, ts }) {
|
|
721
|
+
for (const { name, initializer } of nodes) if (ts.isCallExpression(initializer)) codes.replaceRange(initializer.expression.end, initializer.expression.end, `<Parameters<typeof __VLS_ctx_${name.text}['expose']>[0] | null>`);
|
|
722
|
+
}
|
|
723
|
+
function getRefNodes(ts, sourceFile, alias) {
|
|
724
|
+
function isRefCall(node) {
|
|
725
|
+
return ts.isCallExpression(node) && ts.isIdentifier(node.expression) && !node.typeArguments?.length && alias.includes(node.expression.text);
|
|
726
|
+
}
|
|
727
|
+
const result = [];
|
|
728
|
+
function walk(node) {
|
|
729
|
+
if (ts.isVariableStatement(node)) ts.forEachChild(node.declarationList, (decl) => {
|
|
730
|
+
if (ts.isVariableDeclaration(decl) && ts.isIdentifier(decl.name) && decl.initializer && ts.isCallExpression(decl.initializer) && ts.isIdentifier(decl.initializer.expression)) {
|
|
731
|
+
const initializer = decl.initializer.expression.text === "$" ? decl.initializer.arguments[0] : decl.initializer;
|
|
732
|
+
if (isRefCall(initializer)) result.push({
|
|
733
|
+
name: decl.name,
|
|
734
|
+
initializer
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
});
|
|
738
|
+
ts.forEachChild(node, walk);
|
|
739
|
+
}
|
|
740
|
+
ts.forEachChild(sourceFile, walk);
|
|
741
|
+
return result;
|
|
742
|
+
}
|
|
743
|
+
var jsx_ref_default = createPlugin(({ ts, vueCompilerOptions }, options = vueCompilerOptions?.vueMacros?.jsxRef === true ? {} : vueCompilerOptions?.vueMacros?.jsxRef ?? {}) => {
|
|
744
|
+
if (!options) return [];
|
|
745
|
+
const alias = options.alias || ["useRef"];
|
|
746
|
+
return {
|
|
747
|
+
name: "vue-macros-jsx-ref",
|
|
748
|
+
resolveVirtualCode({ ast, codes, source, lang }) {
|
|
749
|
+
if (!["jsx", "tsx"].includes(lang)) return;
|
|
750
|
+
const nodes = getRefNodes(ts, ast, alias);
|
|
751
|
+
if (nodes.length) transformRef({
|
|
752
|
+
nodes,
|
|
753
|
+
codes,
|
|
754
|
+
ts,
|
|
755
|
+
source
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
});
|
|
760
|
+
//#endregion
|
|
761
|
+
//#region src/volar.ts
|
|
762
|
+
const plugin = createPlugin((ctx, options = ctx.vueCompilerOptions?.["vue-jsx"]) => {
|
|
763
|
+
return [
|
|
764
|
+
jsx_directive_default()(ctx),
|
|
765
|
+
options?.ref === false ? [] : jsx_ref_default(options?.ref === true ? void 0 : options?.ref)(ctx),
|
|
766
|
+
options?.macros === false ? [] : options?.macros ? jsxMacros(options.macros === true ? void 0 : options.macros)(ctx) : []
|
|
767
|
+
].flat();
|
|
768
|
+
});
|
|
769
|
+
//#endregion
|
|
770
|
+
export { plugin as default, plugin as "module.exports" };
|