From f2b373af2fc86acc23488f7b865277333bb85013 Mon Sep 17 00:00:00 2001 From: Blake Newman Date: Tue, 2 Apr 2024 21:40:05 +0100 Subject: [PATCH] fix: remove global placeholder file remove global placeholder fake file logic, as adding a fake file will cause composite projects to always rebuild as the fake file is not in the root files for the project build. it's not viable to create a fake file to host the global types, due to the fact tsc composite projects can't work in this format without the file actually existing. --- packages/component-meta/lib/base.ts | 14 ------- .../lib/generators/globalTypes.ts | 4 +- .../language-core/lib/generators/script.ts | 4 +- packages/language-core/lib/languageModule.ts | 24 +++--------- packages/language-core/lib/types.ts | 1 - packages/language-server/node.ts | 14 ------- .../tests/utils/createTester.ts | 14 ------- .../language-service/tests/utils/format.ts | 1 - packages/tsc/index.ts | 37 +------------------ packages/tsc/tests/dts.spec.ts | 5 --- packages/typescript-plugin/index.ts | 14 ------- 11 files changed, 10 insertions(+), 122 deletions(-) diff --git a/packages/component-meta/lib/base.ts b/packages/component-meta/lib/base.ts index a6d2f499e0..90ee8fc05d 100644 --- a/packages/component-meta/lib/base.ts +++ b/packages/component-meta/lib/base.ts @@ -160,20 +160,6 @@ export function baseCreate( const vueLanguagePlugin = vue.createVueLanguagePlugin( ts, id => id, - fileName => { - if (ts.sys.useCaseSensitiveFileNames) { - return host.getScriptFileNames().includes(fileName) ?? false; - } - else { - const lowerFileName = fileName.toLowerCase(); - for (const rootFile of host.getScriptFileNames()) { - if (rootFile.toLowerCase() === lowerFileName) { - return true; - } - } - return false; - } - }, host.getCompilationSettings(), vueCompilerOptions, ); diff --git a/packages/language-core/lib/generators/globalTypes.ts b/packages/language-core/lib/generators/globalTypes.ts index 48fb161d8c..5c83387969 100644 --- a/packages/language-core/lib/generators/globalTypes.ts +++ b/packages/language-core/lib/generators/globalTypes.ts @@ -4,7 +4,7 @@ import { getSlotsPropertyName } from '../utils/shared'; export function generateGlobalTypes(vueCompilerOptions: VueCompilerOptions) { const fnPropsType = `(K extends { $props: infer Props } ? Props : any)${vueCompilerOptions.strictTemplates ? '' : ' & Record'}`; return ` -; declare global { +; // @ts-ignore type __VLS_IntrinsicElements = __VLS_PickNotAny>>; // @ts-ignore @@ -125,5 +125,5 @@ type __VLS_NormalizeEmits = __VLS_PrettifyGlobal< > >; type __VLS_PrettifyGlobal = { [K in keyof T]: T[K]; } & {}; -}`; +`; }; diff --git a/packages/language-core/lib/generators/script.ts b/packages/language-core/lib/generators/script.ts index e0a21abaf5..b75d0cc2a9 100644 --- a/packages/language-core/lib/generators/script.ts +++ b/packages/language-core/lib/generators/script.ts @@ -185,9 +185,7 @@ export function* generate( yield* generateScriptContentBeforeExportDefault(); yield* generateScriptSetupAndTemplate(); yield* generateScriptContentAfterExportDefault(); - if (globalTypesHolder === fileName) { - yield _(generateGlobalTypes(vueCompilerOptions)); - } + yield _(generateGlobalTypes(vueCompilerOptions)); yield* generateLocalHelperTypes(); yield _(`\ntype __VLS_IntrinsicElementsCompletion = __VLS_IntrinsicElements;\n`); diff --git a/packages/language-core/lib/languageModule.ts b/packages/language-core/lib/languageModule.ts index 9dc34395ec..fbf62116d2 100644 --- a/packages/language-core/lib/languageModule.ts +++ b/packages/language-core/lib/languageModule.ts @@ -6,15 +6,13 @@ import { VueGeneratedCode } from './virtualFile/vueFile'; import * as CompilerDOM from '@vue/compiler-dom'; import * as CompilerVue2 from './utils/vue2TemplateCompiler'; -const normalFileRegistries: { +const fileRegistries: { key: string; plugins: VueLanguagePlugin[]; files: Map; }[] = []; -const holderFileRegistries: typeof normalFileRegistries = []; -function getVueFileRegistry(isGlobalTypesHolder: boolean, key: string, plugins: VueLanguagePlugin[]) { - const fileRegistries = isGlobalTypesHolder ? holderFileRegistries : normalFileRegistries; +function getVueFileRegistry(key: string, plugins: VueLanguagePlugin[]) { let fileRegistry = fileRegistries.find(r => r.key === key && r.plugins.length === plugins.length @@ -51,7 +49,6 @@ function getFileRegistryKey( export function createVueLanguagePlugin( ts: typeof import('typescript'), getFileName: (fileId: string) => string, - isValidGlobalTypesHolder: (fileName: string) => boolean, compilerOptions: ts.CompilerOptions, vueCompilerOptions: VueCompilerOptions, codegenStack: boolean = false, @@ -70,7 +67,6 @@ export function createVueLanguagePlugin( compilerOptions, vueCompilerOptions, codegenStack, - globalTypesHolder: undefined, }; const plugins = getDefaultVueLanguagePlugins(pluginContext); @@ -85,10 +81,10 @@ export function createVueLanguagePlugin( createVirtualCode(fileId, languageId, snapshot) { if (allowLanguageIds.has(languageId)) { const fileName = getFileName(fileId); - if (!pluginContext.globalTypesHolder && isValidGlobalTypesHolder(fileName)) { - pluginContext.globalTypesHolder = fileName; - } - const fileRegistry = getFileRegistry(pluginContext.globalTypesHolder === fileName); + const fileRegistry = getVueFileRegistry( + getFileRegistryKey(compilerOptions, vueCompilerOptions, plugins), + vueCompilerOptions.plugins, + ); const code = fileRegistry.get(fileId); if (code) { code.update(snapshot); @@ -160,12 +156,4 @@ export function createVueLanguagePlugin( }, }, }; - - function getFileRegistry(isGlobalTypesHolder: boolean) { - return getVueFileRegistry( - isGlobalTypesHolder, - getFileRegistryKey(compilerOptions, vueCompilerOptions, plugins), - vueCompilerOptions.plugins, - ); - } } diff --git a/packages/language-core/lib/types.ts b/packages/language-core/lib/types.ts index 42900598a7..daa5e34ba5 100644 --- a/packages/language-core/lib/types.ts +++ b/packages/language-core/lib/types.ts @@ -67,7 +67,6 @@ export type VueLanguagePlugin = (ctx: { compilerOptions: ts.CompilerOptions; vueCompilerOptions: VueCompilerOptions; codegenStack: boolean; - globalTypesHolder: string | undefined; }) => { version: typeof pluginVersion; name?: string; diff --git a/packages/language-server/node.ts b/packages/language-server/node.ts index 1ba352f7b3..9a398516c5 100644 --- a/packages/language-server/node.ts +++ b/packages/language-server/node.ts @@ -80,20 +80,6 @@ connection.onInitialize(async params => { const vueLanguagePlugin = createVueLanguagePlugin( tsdk.typescript, serviceEnv.typescript!.uriToFileName, - fileName => { - if (projectContext.typescript?.sys.useCaseSensitiveFileNames ?? false) { - return projectContext.typescript?.host.getScriptFileNames().includes(fileName) ?? false; - } - else { - const lowerFileName = fileName.toLowerCase(); - for (const rootFile of projectContext.typescript?.host.getScriptFileNames() ?? []) { - if (rootFile.toLowerCase() === lowerFileName) { - return true; - } - } - return false; - } - }, commandLine?.options ?? {}, vueOptions, options.codegenStack, diff --git a/packages/language-service/tests/utils/createTester.ts b/packages/language-service/tests/utils/createTester.ts index 5e80a559a4..4c49f2b785 100644 --- a/packages/language-service/tests/utils/createTester.ts +++ b/packages/language-service/tests/utils/createTester.ts @@ -32,20 +32,6 @@ function createTester(rootUri: string) { const vueLanguagePlugin = createVueLanguagePlugin( ts, serviceEnv.typescript!.uriToFileName, - fileName => { - if (ts.sys.useCaseSensitiveFileNames) { - return projectHost.getScriptFileNames().includes(fileName); - } - else { - const lowerFileName = fileName.toLowerCase(); - for (const rootFile of projectHost.getScriptFileNames()) { - if (rootFile.toLowerCase() === lowerFileName) { - return true; - } - } - return false; - } - }, parsedCommandLine.options, parsedCommandLine.vueOptions, ); diff --git a/packages/language-service/tests/utils/format.ts b/packages/language-service/tests/utils/format.ts index d1b5242766..af5bd8ca82 100644 --- a/packages/language-service/tests/utils/format.ts +++ b/packages/language-service/tests/utils/format.ts @@ -7,7 +7,6 @@ const resolvedVueOptions = resolveVueCompilerOptions({}); const vueLanguagePlugin = createVueLanguagePlugin( ts, fileId => formatter.env.typescript!.uriToFileName(fileId), - () => false, {}, resolvedVueOptions, ); diff --git a/packages/tsc/index.ts b/packages/tsc/index.ts index bfebda7a66..85eb1a46ee 100644 --- a/packages/tsc/index.ts +++ b/packages/tsc/index.ts @@ -1,6 +1,7 @@ import { runTsc } from '@volar/typescript/lib/quickstart/runTsc'; import * as vue from '@vue/language-core'; import type * as ts from 'typescript'; +import path from 'path'; const windowsPathReg = /\\/g; @@ -17,7 +18,6 @@ export function run() { const vueOptions = typeof configFilePath === 'string' ? vue.createParsedCommandLine(ts, ts.sys, configFilePath.replace(windowsPathReg, '/')).vueOptions : vue.resolveVueCompilerOptions({}); - const fakeGlobalTypesHolder = createFakeGlobalTypesHolder(options); if ( runExtensions.length === vueOptions.extensions.length && runExtensions.every(ext => vueOptions.extensions.includes(ext)) @@ -25,7 +25,6 @@ export function run() { const vueLanguagePlugin = vue.createVueLanguagePlugin( ts, id => id, - fileName => fileName === fakeGlobalTypesHolder, options.options, vueOptions, false, @@ -53,37 +52,3 @@ export function run() { } } } - -export function createFakeGlobalTypesHolder(options: ts.CreateProgramOptions) { - const firstVueFile = options.rootNames.find(fileName => fileName.endsWith('.vue')); - if (firstVueFile) { - const fakeFileName = firstVueFile + '__VLS_globalTypes.vue'; - - (options.rootNames as string[]).push(fakeFileName); - - const fileExists = options.host!.fileExists.bind(options.host); - const readFile = options.host!.readFile.bind(options.host); - const writeFile = options.host!.writeFile.bind(options.host); - - options.host!.fileExists = fileName => { - if (fileName.endsWith('__VLS_globalTypes.vue')) { - return true; - } - return fileExists(fileName); - }; - options.host!.readFile = fileName => { - if (fileName.endsWith('__VLS_globalTypes.vue')) { - return ''; - } - return readFile(fileName); - }; - options.host!.writeFile = (fileName, ...args) => { - if (fileName.endsWith('__VLS_globalTypes.vue.d.ts')) { - return; - } - return writeFile(fileName, ...args); - }; - - return fakeFileName.replace(windowsPathReg, '/'); - } -} diff --git a/packages/tsc/tests/dts.spec.ts b/packages/tsc/tests/dts.spec.ts index a3024df974..6e4bd14040 100644 --- a/packages/tsc/tests/dts.spec.ts +++ b/packages/tsc/tests/dts.spec.ts @@ -24,7 +24,6 @@ describe('vue-tsc-dts', () => { rootNames: readFilesRecursive(workspace), options: compilerOptions }; - const fakeGlobalTypesHolder = createFakeGlobalTypesHolder(options); let vueExts: string[] = []; const createProgram = proxyCreateProgram(ts, ts.createProgram, (ts, options) => { @@ -36,7 +35,6 @@ describe('vue-tsc-dts', () => { const vueLanguagePlugin = vue.createVueLanguagePlugin( ts, id => id, - fileName => fileName === fakeGlobalTypesHolder, options.options, vueOptions, false, @@ -52,9 +50,6 @@ describe('vue-tsc-dts', () => { for (const intputFile of options.rootNames) { - if (intputFile.endsWith('__VLS_globalTypes.vue')) - continue; - const expectedOutputFile = intputFile.endsWith('.ts') ? intputFile.slice(0, -'.ts'.length) + '.d.ts' : intputFile.endsWith('.tsx') diff --git a/packages/typescript-plugin/index.ts b/packages/typescript-plugin/index.ts index 756885472f..ebf0253a19 100644 --- a/packages/typescript-plugin/index.ts +++ b/packages/typescript-plugin/index.ts @@ -30,20 +30,6 @@ function createLanguageServicePlugin(): ts.server.PluginModuleFactory { const languagePlugin = vue.createVueLanguagePlugin( ts, id => id, - fileName => { - if (info.languageServiceHost.useCaseSensitiveFileNames?.() ?? false) { - return externalFiles.get(info.project)?.has(fileName) ?? false; - } - else { - const lowerFileName = fileName.toLowerCase(); - for (const externalFile of externalFiles.get(info.project) ?? []) { - if (externalFile.toLowerCase() === lowerFileName) { - return true; - } - } - return false; - } - }, info.languageServiceHost.getCompilationSettings(), vueOptions, );