Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Removing __VLS_types.d.ts #3534

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 9 additions & 2 deletions packages/vue-language-core/src/generators/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { ScriptSetupRanges } from '../parsers/scriptSetupRanges';
import type { TextRange, VueCompilerOptions } from '../types';
import { Sfc } from '../types';
import * as sharedTypes from '../utils/globalTypes';
import { geneateGlobalTypes } from '../utils/globalTypes';
import { getSlotsPropertyName, hyphenateTag } from '../utils/shared';
import { walkInterpolationFragment } from '../utils/transform';

Expand All @@ -24,6 +25,7 @@ export function generate(
compilerOptions: ts.CompilerOptions,
vueCompilerOptions: VueCompilerOptions,
codegenStack: boolean,
withGlobalTypes: boolean,
) {

const [codes, codeStacks] = codegenStack ? muggle.track([] as Segment<FileRangeCapabilities>[]) : [[], []];
Expand Down Expand Up @@ -108,6 +110,11 @@ export function generate(
};

function generateHelperTypes() {
// global types
if (withGlobalTypes) {
codes.push(geneateGlobalTypes(vueCompilerOptions));
}
// local types
let usedPrettify = false;
if (usedHelperTypes.DefinePropsToOptions) {
if (compilerOptions.exactOptionalPropertyTypes) {
Expand All @@ -132,10 +139,10 @@ export function generate(
codes.push('type __VLS_UnionToIntersection<U> = __VLS_Prettify<(U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never>;\n');
usedPrettify = true;
if (scriptSetupRanges && scriptSetupRanges.emitsTypeNums !== -1) {
codes.push(sharedTypes.genConstructorOverloads('__VLS_ConstructorOverloads', scriptSetupRanges.emitsTypeNums));
codes.push(sharedTypes.generateConstructorOverloads('__VLS_ConstructorOverloads', scriptSetupRanges.emitsTypeNums));
}
else {
codes.push(sharedTypes.genConstructorOverloads('__VLS_ConstructorOverloads'));
codes.push(sharedTypes.generateConstructorOverloads('__VLS_ConstructorOverloads'));
}
codes.push(`type __VLS_NormalizeEmits<T> = __VLS_ConstructorOverloads<T> & {
[K in keyof T]: T[K] extends any[] ? { (...args: T[K]): void } : never
Expand Down
34 changes: 15 additions & 19 deletions packages/vue-language-core/src/languageModule.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { Language } from '@volar/language-core';
import { posix as path } from 'path';
import { getDefaultVueLanguagePlugins } from './plugins';
import { VueFile } from './sourceFile';
import { VueCompilerOptions, VueLanguagePlugin } from './types';
import * as sharedTypes from './utils/globalTypes';
import type * as ts from 'typescript/lib/tsserverlibrary';
import { resolveVueCompilerOptions } from './utils/ts';

Expand Down Expand Up @@ -33,12 +31,16 @@ function getVueFileRegistry(key: string, plugins: VueLanguagePlugin[]) {
return fileRegistry;
}

export type VueLanguage = ReturnType<typeof createVueLanguage>;

export function createVueLanguage(
ts: typeof import('typescript/lib/tsserverlibrary'),
compilerOptions: ts.CompilerOptions = {},
_vueCompilerOptions: Partial<VueCompilerOptions> = {},
codegenStack: boolean = false,
): Language<VueFile> {
): Language<VueFile> & {
getGlobalTypesFile(): VueFile | undefined;
} {

const vueCompilerOptions = resolveVueCompilerOptions(_vueCompilerOptions);
const plugins = getDefaultVueLanguagePlugins(
Expand All @@ -57,7 +59,6 @@ export function createVueLanguage(
.map(key => [key, compilerOptions[key as keyof ts.CompilerOptions]]),
];
const fileRegistry = getVueFileRegistry(JSON.stringify(keys), _vueCompilerOptions.plugins ?? []);

const allowLanguageIds = new Set(['vue']);

if (vueCompilerOptions.extensions.includes('.md')) {
Expand All @@ -67,7 +68,12 @@ export function createVueLanguage(
allowLanguageIds.add('html');
}

let globalTypesFile: VueFile | undefined;

return {
getGlobalTypesFile() {
return globalTypesFile;
},
createVirtualFile(fileName, snapshot, languageId) {
if (
(languageId && allowLanguageIds.has(languageId))
Expand All @@ -78,17 +84,19 @@ export function createVueLanguage(
reusedVueFile.update(snapshot);
return reusedVueFile;
}
const vueFile = new VueFile(fileName, snapshot, vueCompilerOptions, plugins, ts, codegenStack);

const vueFile = new VueFile(fileName, snapshot, vueCompilerOptions, plugins, ts, codegenStack, !globalTypesFile);
fileRegistry.set(fileName, vueFile);

globalTypesFile ??= vueFile;

return vueFile;
}
},
updateVirtualFile(sourceFile, snapshot) {
sourceFile.update(snapshot);
},
resolveHost(host) {
const sharedTypesSnapshot = ts.ScriptSnapshot.fromString(sharedTypes.getTypesCode(vueCompilerOptions));
const sharedTypesFileName = path.join(host.rootPath, sharedTypes.baseName);
return {
...host,
resolveModuleName(moduleName, impliedNodeFormat) {
Expand All @@ -97,18 +105,6 @@ export function createVueLanguage(
}
return host.resolveModuleName?.(moduleName, impliedNodeFormat) ?? moduleName;
},
getScriptFileNames() {
return [
sharedTypesFileName,
...host.getScriptFileNames(),
];
},
getScriptSnapshot(fileName) {
if (fileName === sharedTypesFileName) {
return sharedTypesSnapshot;
}
return host.getScriptSnapshot(fileName);
},
};
},
};
Expand Down
20 changes: 13 additions & 7 deletions packages/vue-language-core/src/plugins/vue-tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const plugin: VueLanguagePlugin = (ctx) => {
'exactOptionalPropertyTypes',
],

getEmbeddedFileNames(fileName, sfc) {
getEmbeddedFileNames(fileName, sfc, vueFile) {

const tsx = useTsx(fileName, sfc);
const tsx = useTsx(fileName, sfc, vueFile.withGlobalTypes);
const fileNames: string[] = [];

if (['js', 'ts', 'jsx', 'tsx'].includes(tsx.lang.value)) {
Expand All @@ -40,9 +40,9 @@ const plugin: VueLanguagePlugin = (ctx) => {
return fileNames;
},

resolveEmbeddedFile(fileName, sfc, embeddedFile) {
resolveEmbeddedFile(fileName, sfc, embeddedFile, vueFile) {

const _tsx = useTsx(fileName, sfc);
const _tsx = useTsx(fileName, sfc, vueFile.withGlobalTypes);
const suffix = embeddedFile.fileName.replace(fileName, '');

if (suffix === '.' + _tsx.lang.value) {
Expand Down Expand Up @@ -109,17 +109,22 @@ const plugin: VueLanguagePlugin = (ctx) => {
},
};

function useTsx(fileName: string, sfc: Sfc) {
function useTsx(fileName: string, sfc: Sfc, withGlobalTypes: boolean) {
if (!tsCodegen.has(sfc)) {
tsCodegen.set(sfc, createTsx(fileName, sfc, ctx));
tsCodegen.set(sfc, createTsx(fileName, sfc, ctx, withGlobalTypes));
}
return tsCodegen.get(sfc)!;
}
};

export default plugin;

function createTsx(fileName: string, _sfc: Sfc, { vueCompilerOptions, compilerOptions, codegenStack, modules }: Parameters<VueLanguagePlugin>[0]) {
function createTsx(
fileName: string,
_sfc: Sfc,
{ vueCompilerOptions, compilerOptions, codegenStack, modules }: Parameters<VueLanguagePlugin>[0],
withGlobalTypes: boolean
) {

const ts = modules.typescript;
const lang = computed(() => {
Expand Down Expand Up @@ -168,6 +173,7 @@ function createTsx(fileName: string, _sfc: Sfc, { vueCompilerOptions, compilerOp
compilerOptions,
vueCompilerOptions,
codegenStack,
withGlobalTypes,
);
});

Expand Down
5 changes: 3 additions & 2 deletions packages/vue-language-core/src/sourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class VueFile implements VirtualFile {
const files = computed(() => {
try {
if (plugin.getEmbeddedFileNames) {
const embeddedFileNames = plugin.getEmbeddedFileNames(this.fileName, this.sfc);
const embeddedFileNames = plugin.getEmbeddedFileNames(this.fileName, this.sfc, this);
for (const oldFileName of Object.keys(embeddedFiles)) {
if (!embeddedFileNames.includes(oldFileName)) {
delete embeddedFiles[oldFileName];
Expand All @@ -215,7 +215,7 @@ export class VueFile implements VirtualFile {
for (const plugin of this.plugins) {
if (plugin.resolveEmbeddedFile) {
try {
plugin.resolveEmbeddedFile(this.fileName, this.sfc, file);
plugin.resolveEmbeddedFile(this.fileName, this.sfc, file, this);
}
catch (e) {
console.error(e);
Expand Down Expand Up @@ -394,6 +394,7 @@ export class VueFile implements VirtualFile {
public plugins: ReturnType<VueLanguagePlugin>[],
public ts: typeof import('typescript/lib/tsserverlibrary'),
public codegenStack: boolean,
public withGlobalTypes: boolean,
) {
this.onUpdate();
}
Expand Down
6 changes: 3 additions & 3 deletions packages/vue-language-core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as CompilerDOM from '@vue/compiler-dom';
import type { SFCParseResult } from '@vue/compiler-sfc';
import type * as ts from 'typescript/lib/tsserverlibrary';
import type { VueEmbeddedFile } from './sourceFile';
import { VueEmbeddedFile, VueFile } from './sourceFile';

export type { SFCParseResult } from '@vue/compiler-sfc';

Expand Down Expand Up @@ -59,8 +59,8 @@ export type VueLanguagePlugin = (ctx: {
resolveTemplateCompilerOptions?(options: CompilerDOM.CompilerOptions): CompilerDOM.CompilerOptions;
compileSFCTemplate?(lang: string, template: string, options: CompilerDOM.CompilerOptions): CompilerDOM.CodegenResult | undefined;
updateSFCTemplate?(oldResult: CompilerDOM.CodegenResult, textChange: { start: number, end: number, newText: string; }): CompilerDOM.CodegenResult | undefined;
getEmbeddedFileNames?(fileName: string, sfc: Sfc): string[];
resolveEmbeddedFile?(fileName: string, sfc: Sfc, embeddedFile: VueEmbeddedFile): void;
getEmbeddedFileNames?(fileName: string, sfc: Sfc, vueFile: VueFile): string[];
resolveEmbeddedFile?(fileName: string, sfc: Sfc, embeddedFile: VueEmbeddedFile, vueFile: VueFile): void;
};

export interface SfcBlock {
Expand Down
Loading
Loading