Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/language-core/lib/codegen/names.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const ctx = '__VLS_ctx';
export const self = '__VLS_self';
export const dollars = '__VLS_dollars';
export const slots = '__VLS_slots';
export const props = '__VLS_props';
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function* generateWorker(
yield* generateSfcBlockSection(script, 0, expression.start, codeFeatures.all);
yield exportExpression;
yield endOfLine;
yield* generateTemplate(options, ctx);
yield* generateTemplate(options, ctx, names._export);
yield* generateExportDeclareEqual(script);
if (wrapLeft && wrapRight) {
yield wrapLeft;
Expand All @@ -179,7 +179,7 @@ function* generateWorker(
yield* generateSfcBlockSection(script, 0, script.content.length, codeFeatures.all);
yield* generateExportDeclareEqual(script);
yield `(await import('${vueCompilerOptions.lib}')).defineComponent({})${endOfLine}`;
yield* generateTemplate(options, ctx);
yield* generateTemplate(options, ctx, names._export);
yield `export default ${exportExpression}${endOfLine}`;
}
}
Expand Down
31 changes: 17 additions & 14 deletions packages/language-core/lib/codegen/script/src.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,7 @@ export function* generateSrc(src: SfcBlockAttr): Generator<Code> {
return;
}
let { text } = src;

if (text.endsWith('.d.ts')) {
text = text.slice(0, -'.d.ts'.length);
}
else if (text.endsWith('.ts')) {
text = text.slice(0, -'.ts'.length);
}
else if (text.endsWith('.tsx')) {
text = text.slice(0, -'.tsx'.length) + '.jsx';
}

if (!text.endsWith('.js') && !text.endsWith('.jsx')) {
text = text + '.js';
}
text = resolveSrcPath(text);

yield `export * from `;
const wrapCodeFeatures: VueCodeInformation = {
Expand All @@ -37,3 +24,19 @@ export function* generateSrc(src: SfcBlockAttr): Generator<Code> {
yield endOfLine;
yield `export { default } from '${text}'${endOfLine}`;
}

export function resolveSrcPath(text: string): string {
if (text.endsWith('.d.ts')) {
text = text.slice(0, -'.d.ts'.length);
}
else if (text.endsWith('.ts')) {
text = text.slice(0, -'.ts'.length);
}
else if (text.endsWith('.tsx')) {
text = text.slice(0, -'.tsx'.length) + '.jsx';
}
if (!text.endsWith('.js') && !text.endsWith('.jsx')) {
text = text + '.js';
}
return text;
}
29 changes: 17 additions & 12 deletions packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import * as path from 'path-browserify';
import type { Code } from '../../types';
import { codeFeatures } from '../codeFeatures';
import * as names from '../names';
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
import { generateSpreadMerge } from '../utils/merge';
import type { ScriptCodegenContext } from './context';
import type { ScriptCodegenOptions } from './index';
import { resolveSrcPath } from './src';

export function* generateTemplate(
options: ScriptCodegenOptions,
ctx: ScriptCodegenContext,
selfType?: string,
): Generator<Code> {
yield* generateSelf(options);
selfType ??= yield* generateSelf(options);
yield* generateSetupExposed(options, ctx);
yield* generateTemplateCtx(options, ctx);
yield* generateTemplateCtx(options, ctx, selfType);
yield* generateTemplateComponents(options);
yield* generateTemplateDirectives(options);

Expand All @@ -25,27 +26,28 @@ export function* generateTemplate(
}
}

function* generateSelf({ script, scriptRanges, vueCompilerOptions, fileName }: ScriptCodegenOptions): Generator<Code> {
function* generateSelf({ script, scriptRanges, vueCompilerOptions }: ScriptCodegenOptions): Generator<Code> {
const varName = '__VLS_self';
if (script && scriptRanges?.componentOptions) {
yield `const ${names.self} = (await import('${vueCompilerOptions.lib}')).defineComponent(`;
yield `const ${varName} = (await import('${vueCompilerOptions.lib}')).defineComponent(`;
const { args } = scriptRanges.componentOptions;
yield* generateSfcBlockSection(script, args.start, args.end, codeFeatures.all);
yield `)${endOfLine}`;
return varName;
}
else if (script && scriptRanges?.exportDefault) {
yield `const ${names.self} = `;
yield `const ${varName} = `;
const { expression } = scriptRanges.exportDefault;
yield* generateSfcBlockSection(script, expression.start, expression.end, codeFeatures.all);
yield endOfLine;
}
else if (script?.src) {
yield `let ${names.self}!: typeof import('./${path.basename(fileName)}').default${endOfLine}`;
return varName;
}
}

function* generateTemplateCtx(
{ vueCompilerOptions, script, scriptRanges, styleCodegen, scriptSetupRanges, fileName }: ScriptCodegenOptions,
{ vueCompilerOptions, script, styleCodegen, scriptSetupRanges, fileName }: ScriptCodegenOptions,
ctx: ScriptCodegenContext,
selfType: string | undefined,
): Generator<Code> {
const exps: Iterable<Code>[] = [];
const emitTypes: string[] = [];
Expand All @@ -54,8 +56,11 @@ function* generateTemplateCtx(
if (vueCompilerOptions.petiteVueExtensions.some(ext => fileName.endsWith(ext))) {
exps.push([`globalThis`]);
}
if (script?.src || scriptRanges?.exportDefault) {
exps.push([`{} as InstanceType<__VLS_PickNotAny<typeof ${names.self}, new () => {}>>`]);
if (selfType) {
exps.push([`{} as InstanceType<__VLS_PickNotAny<typeof ${selfType}, new () => {}>>`]);
}
else if (typeof script?.src === 'object') {
exps.push([`{} as typeof import('${resolveSrcPath(script.src.text)}').default`]);
}
else {
exps.push([`{} as import('${vueCompilerOptions.lib}').ComponentPublicInstance`]);
Expand Down
20 changes: 10 additions & 10 deletions packages/language-server/tests/renaming.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,16 +693,6 @@ test('Component returns', async () => {
{
"file": "\${testWorkspacePath}/tsconfigProject/fixture.vue",
"locs": [
{
"end": {
"line": 3,
"offset": 11,
},
"start": {
"line": 3,
"offset": 8,
},
},
{
"contextEnd": {
"line": 12,
Expand All @@ -721,6 +711,16 @@ test('Component returns', async () => {
"offset": 7,
},
},
{
"end": {
"line": 3,
"offset": 11,
},
"start": {
"line": 3,
"offset": 8,
},
},
],
},
],
Expand Down