From 3b5258aa9dc7acdf1ef5d0632ca2b8dc09ec6fa5 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Fri, 23 Aug 2024 02:27:51 +0800 Subject: [PATCH] only generate auto import virtual code for editing scripts --- packages/component-meta/lib/base.ts | 3 +- .../language-core/lib/codegen/script/index.ts | 2 +- .../lib/codegen/script/template.ts | 2 +- .../lib/codegen/template/context.ts | 4 +- .../lib/codegen/template/element.ts | 56 +++++++++---------- .../lib/codegen/template/index.ts | 2 +- packages/language-core/lib/languagePlugin.ts | 10 +--- packages/language-core/lib/plugins/vue-tsx.ts | 7 ++- packages/language-core/lib/types.ts | 1 - packages/language-server/lib/initialize.ts | 3 +- packages/language-server/node.ts | 3 +- packages/tsc/index.ts | 3 +- packages/typescript-plugin/index.ts | 3 +- 13 files changed, 45 insertions(+), 54 deletions(-) diff --git a/packages/component-meta/lib/base.ts b/packages/component-meta/lib/base.ts index 0abd8e47ae..e71738e5e4 100644 --- a/packages/component-meta/lib/base.ts +++ b/packages/component-meta/lib/base.ts @@ -92,8 +92,7 @@ export function baseCreate( ts.sys.useCaseSensitiveFileNames ), projectHost.getCompilationSettings(), - commandLine.vueOptions, - true + commandLine.vueOptions ); const language = vue.createLanguage( [ diff --git a/packages/language-core/lib/codegen/script/index.ts b/packages/language-core/lib/codegen/script/index.ts index dd6f4093ef..92da84d736 100644 --- a/packages/language-core/lib/codegen/script/index.ts +++ b/packages/language-core/lib/codegen/script/index.ts @@ -45,7 +45,7 @@ export interface ScriptCodegenOptions { scriptSetupRanges: ScriptSetupRanges | undefined; templateCodegen: TemplateCodegenContext & { codes: Code[]; } | undefined; globalTypes: boolean; - typeCheckOnly: boolean; + edited: boolean; getGeneratedLength: () => number; linkedCodeMappings: Mapping[]; } diff --git a/packages/language-core/lib/codegen/script/template.ts b/packages/language-core/lib/codegen/script/template.ts index d742be2e59..c60bc11e52 100644 --- a/packages/language-core/lib/codegen/script/template.ts +++ b/packages/language-core/lib/codegen/script/template.ts @@ -22,7 +22,7 @@ export function* generateTemplate( else { yield `function __VLS_template() {${newLine}`; } - const templateCodegenCtx = createTemplateCodegenContext({ scriptSetupBindingNames: new Set(), typeCheckOnly: options.typeCheckOnly }); + const templateCodegenCtx = createTemplateCodegenContext({ scriptSetupBindingNames: new Set(), edited: options.edited }); yield* generateCtx(options, ctx, isClassComponent); yield* generateTemplateContext(options, templateCodegenCtx); yield* generateExportOptions(options); diff --git a/packages/language-core/lib/codegen/template/context.ts b/packages/language-core/lib/codegen/template/context.ts index 5ef1666545..2ecca7891c 100644 --- a/packages/language-core/lib/codegen/template/context.ts +++ b/packages/language-core/lib/codegen/template/context.ts @@ -57,7 +57,7 @@ const _codeFeatures = { export type TemplateCodegenContext = ReturnType; -export function createTemplateCodegenContext(options: Pick) { +export function createTemplateCodegenContext(options: Pick) { let ignoredError = false; let expectErrorToken: { errors: number; @@ -184,7 +184,7 @@ export function createTemplateCodegenContext(options: Pick { - if (options.typeCheckOnly) { + if (!options.edited) { return; } const all = [...accessExternalVariables.entries()]; diff --git a/packages/language-core/lib/codegen/template/element.ts b/packages/language-core/lib/codegen/template/element.ts index e876325800..047314bed7 100644 --- a/packages/language-core/lib/codegen/template/element.ts +++ b/packages/language-core/lib/codegen/template/element.ts @@ -137,39 +137,37 @@ export function* generateComponent( yield* generateCanonicalComponentName( node.tag, startTagOffset, - options.typeCheckOnly - ? ctx.codeFeatures.verification - : { - // hover support - ...ctx.codeFeatures.withoutHighlightAndCompletionAndNavigation, - ...ctx.codeFeatures.verification, - } + { + // with hover support + ...ctx.codeFeatures.withoutHighlightAndCompletionAndNavigation, + ...ctx.codeFeatures.verification, + } ); yield `${endOfLine}`; - if (!options.typeCheckOnly) { - const camelizedTag = camelize(node.tag); - if (variableNameRegex.test(camelizedTag)) { - // renaming / find references support - for (const tagOffset of tagOffsets) { - for (const shouldCapitalize of (node.tag[0] === node.tag[0].toUpperCase() ? [false] : [true, false])) { - const expectName = shouldCapitalize ? capitalize(camelizedTag) : camelizedTag; - yield `__VLS_components.`; - yield* generateCamelized( - shouldCapitalize ? capitalize(node.tag) : node.tag, - tagOffset, - { - navigation: { - resolveRenameNewName: node.tag !== expectName ? camelizeComponentName : undefined, - resolveRenameEditText: getTagRenameApply(node.tag), - }, - } - ); - yield `;`; - } + const camelizedTag = camelize(node.tag); + if (variableNameRegex.test(camelizedTag)) { + // renaming / find references support + for (const tagOffset of tagOffsets) { + for (const shouldCapitalize of (node.tag[0] === node.tag[0].toUpperCase() ? [false] : [true, false])) { + const expectName = shouldCapitalize ? capitalize(camelizedTag) : camelizedTag; + yield `__VLS_components.`; + yield* generateCamelized( + shouldCapitalize ? capitalize(node.tag) : node.tag, + tagOffset, + { + navigation: { + resolveRenameNewName: node.tag !== expectName ? camelizeComponentName : undefined, + resolveRenameEditText: getTagRenameApply(node.tag), + }, + } + ); + yield `;`; } - yield `${newLine}`; - // auto import support + } + yield `${newLine}`; + // auto import support + if (options.edited) { yield `// @ts-ignore${newLine}`; // #2304 yield* generateCamelized( capitalize(node.tag), diff --git a/packages/language-core/lib/codegen/template/index.ts b/packages/language-core/lib/codegen/template/index.ts index ecaf9009b3..02f1b71dbd 100644 --- a/packages/language-core/lib/codegen/template/index.ts +++ b/packages/language-core/lib/codegen/template/index.ts @@ -14,7 +14,7 @@ export interface TemplateCodegenOptions { template: NonNullable; scriptSetupBindingNames: Set; scriptSetupImportComponentNames: Set; - typeCheckOnly: boolean; + edited: boolean; hasDefineSlots?: boolean; slotsAssignName?: string; propsAssignName?: string; diff --git a/packages/language-core/lib/languagePlugin.ts b/packages/language-core/lib/languagePlugin.ts index 0e315f1ea8..d6bb0ff457 100644 --- a/packages/language-core/lib/languagePlugin.ts +++ b/packages/language-core/lib/languagePlugin.ts @@ -76,16 +76,14 @@ export function createVueLanguagePlugin( _getProjectVersion: (() => string) | undefined, isRootFile: (fileName: string) => boolean, compilerOptions: ts.CompilerOptions, - vueCompilerOptions: VueCompilerOptions, - typeCheckOnly = true + vueCompilerOptions: VueCompilerOptions ): LanguagePlugin { return createVueLanguagePlugin2( ts, asFileName, isRootFile, compilerOptions, - vueCompilerOptions, - typeCheckOnly + vueCompilerOptions ); } @@ -94,8 +92,7 @@ export function createVueLanguagePlugin2( asFileName: (scriptId: T) => string, isRootFile: (fileName: string) => boolean, compilerOptions: ts.CompilerOptions, - vueCompilerOptions: VueCompilerOptions, - typeCheckOnly = true + vueCompilerOptions: VueCompilerOptions ): LanguagePlugin { const pluginContext: Parameters[0] = { modules: { @@ -109,7 +106,6 @@ export function createVueLanguagePlugin2( }, compilerOptions, vueCompilerOptions, - typeCheckOnly, globalTypesHolder: undefined, }; const plugins = createPlugins(pluginContext); diff --git a/packages/language-core/lib/plugins/vue-tsx.ts b/packages/language-core/lib/plugins/vue-tsx.ts index dd99302a86..861e056269 100644 --- a/packages/language-core/lib/plugins/vue-tsx.ts +++ b/packages/language-core/lib/plugins/vue-tsx.ts @@ -9,6 +9,8 @@ import type { Code, Sfc, VueLanguagePlugin } from '../types'; export const tsCodegen = new WeakMap>(); +const fileEditTimes = new Map(); + const plugin: VueLanguagePlugin = ctx => { return { @@ -91,8 +93,8 @@ function createTsx( ts, compilerOptions: ctx.compilerOptions, vueCompilerOptions: ctx.vueCompilerOptions, - typeCheckOnly: ctx.typeCheckOnly, template: _sfc.template, + edited: (fileEditTimes.get(fileName) ?? 0) >= 2, scriptSetupBindingNames: scriptSetupBindingNames(), scriptSetupImportComponentNames: scriptSetupImportComponentNames(), hasDefineSlots: hasDefineSlots(), @@ -152,10 +154,11 @@ function createTsx( templateCodegen: _template, compilerOptions: ctx.compilerOptions, vueCompilerOptions: ctx.vueCompilerOptions, - typeCheckOnly: ctx.typeCheckOnly, + edited: (fileEditTimes.get(fileName) ?? 0) >= 2, getGeneratedLength: () => generatedLength, linkedCodeMappings, }); + fileEditTimes.set(fileName, (fileEditTimes.get(fileName) ?? 0) + 1); let current = codegen.next(); diff --git a/packages/language-core/lib/types.ts b/packages/language-core/lib/types.ts index aa8ffcc96e..ac0e9faca8 100644 --- a/packages/language-core/lib/types.ts +++ b/packages/language-core/lib/types.ts @@ -78,7 +78,6 @@ export type VueLanguagePlugin = (ctx: { compilerOptions: ts.CompilerOptions; vueCompilerOptions: VueCompilerOptions; globalTypesHolder: string | undefined; - typeCheckOnly: boolean; }) => VueLanguagePluginReturn | VueLanguagePluginReturn[]; export interface SfcBlock { diff --git a/packages/language-server/lib/initialize.ts b/packages/language-server/lib/initialize.ts index ad0838c570..0ef344ed69 100644 --- a/packages/language-server/lib/initialize.ts +++ b/packages/language-server/lib/initialize.ts @@ -48,8 +48,7 @@ export function initialize( sys.useCaseSensitiveFileNames ), compilerOptions, - vueCompilerOptions, - false + vueCompilerOptions )], setup({ project }) { project.vue = { compilerOptions: vueCompilerOptions }; diff --git a/packages/language-server/node.ts b/packages/language-server/node.ts index 0aa8d6cd00..541467169d 100644 --- a/packages/language-server/node.ts +++ b/packages/language-server/node.ts @@ -32,8 +32,7 @@ connection.onInitialize(params => { asFileName, () => false, commandLine.options, - commandLine.vueOptions, - false + commandLine.vueOptions )], setup({ project }) { project.vue = { compilerOptions: commandLine.vueOptions }; diff --git a/packages/tsc/index.ts b/packages/tsc/index.ts index fd03862199..c82cbc4ac5 100644 --- a/packages/tsc/index.ts +++ b/packages/tsc/index.ts @@ -34,8 +34,7 @@ export function run() { options.host?.useCaseSensitiveFileNames?.() ?? false ), options.options, - vueOptions, - true + vueOptions ); return { languagePlugins: [vueLanguagePlugin] }; } diff --git a/packages/typescript-plugin/index.ts b/packages/typescript-plugin/index.ts index ef09b01c96..baa5b8a4db 100644 --- a/packages/typescript-plugin/index.ts +++ b/packages/typescript-plugin/index.ts @@ -19,8 +19,7 @@ const plugin = createLanguageServicePlugin( info.languageServiceHost.useCaseSensitiveFileNames?.() ?? false ), info.languageServiceHost.getCompilationSettings(), - vueOptions, - false + vueOptions ); return {