forked from vuejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponentSelf.ts
70 lines (67 loc) · 2.89 KB
/
componentSelf.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import * as path from 'path-browserify';
import type { Code } from '../../types';
import type { TemplateCodegenContext } from '../template/context';
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
import { generateComponentSetupReturns, generateEmitsOption, generatePropsOption } from './component';
import type { ScriptCodegenContext } from './context';
import { codeFeatures, type ScriptCodegenOptions } from './index';
import { getTemplateUsageVars } from './template';
export function* generateComponentSelf(
options: ScriptCodegenOptions,
ctx: ScriptCodegenContext,
templateCodegenCtx: TemplateCodegenContext
): Generator<Code> {
if (options.sfc.scriptSetup && options.scriptSetupRanges) {
yield `const __VLS_self = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${newLine}`;
yield `setup() {${newLine}`;
yield `return {${newLine}`;
if (ctx.bypassDefineComponent) {
yield* generateComponentSetupReturns(options.scriptSetupRanges);
}
// bindings
const templateUsageVars = getTemplateUsageVars(options, ctx);
for (const [content, bindings] of [
[options.sfc.scriptSetup.content, options.scriptSetupRanges.bindings] as const,
options.sfc.script && options.scriptRanges
? [options.sfc.script.content, options.scriptRanges.bindings] as const
: ['', []] as const,
]) {
for (const { range } of bindings) {
const varName = content.slice(range.start, range.end);
if (!templateUsageVars.has(varName) && !templateCodegenCtx.accessExternalVariables.has(varName)) {
continue;
}
const templateOffset = options.getGeneratedLength();
yield `${varName}: ${varName} as typeof `;
const scriptOffset = options.getGeneratedLength();
yield `${varName},${newLine}`;
options.linkedCodeMappings.push({
sourceOffsets: [scriptOffset],
generatedOffsets: [templateOffset],
lengths: [varName.length],
data: undefined,
});
}
}
yield `}${endOfLine}`; // return {
yield `},${newLine}`; // setup() {
if (options.sfc.scriptSetup && options.scriptSetupRanges && !ctx.bypassDefineComponent) {
const emitOptionCodes = [...generateEmitsOption(options, options.scriptSetupRanges)];
for (const code of emitOptionCodes) {
yield code;
}
yield* generatePropsOption(options, ctx, options.sfc.scriptSetup, options.scriptSetupRanges, !!emitOptionCodes.length, false);
}
if (options.sfc.script && options.scriptRanges?.exportDefault?.args) {
const { args } = options.scriptRanges.exportDefault;
yield generateSfcBlockSection(options.sfc.script, args.start + 1, args.end - 1, codeFeatures.all);
}
yield `})${endOfLine}`; // defineComponent {
}
else if (options.sfc.script) {
yield `let __VLS_self!: typeof import('./${path.basename(options.fileName)}').default${endOfLine}`;
}
else {
yield `const __VLS_self = (await import('${options.vueCompilerOptions.lib}')).defineComponent({})${endOfLine}`;
}
}