diff --git a/packages/language-core/lib/codegen/script/index.ts b/packages/language-core/lib/codegen/script/index.ts index 9ac857e07a..0afa05dbf9 100644 --- a/packages/language-core/lib/codegen/script/index.ts +++ b/packages/language-core/lib/codegen/script/index.ts @@ -60,10 +60,10 @@ function* generateWorker( const { expression: options } = componentOptions ?? exportDefault; yield* generateSfcBlockSection(script, 0, options.start, codeFeatures.all); yield exportExpression; - yield* generateSfcBlockSection(script, options.end, script.content.length, codeFeatures.all, true); + yield* generateSfcBlockSection(script, options.end, script.content.length, codeFeatures.all); } else { - yield* generateSfcBlockSection(script, 0, script.content.length, codeFeatures.all, true); + yield* generateSfcBlockSection(script, 0, script.content.length, codeFeatures.all); yield `export default ${exportExpression}${endOfLine}`; } @@ -160,7 +160,7 @@ function* generateWorker( }); } - yield* generateSfcBlockSection(script, 0, exportDefault.start, codeFeatures.all, true); + yield* generateSfcBlockSection(script, 0, exportDefault.start, codeFeatures.all); yield* generateExportDeclareEqual(script); if (wrapLeft) { yield wrapLeft; @@ -176,7 +176,7 @@ function* generateWorker( yield* generateSfcBlockSection(script, expression.end, script.content.length, codeFeatures.all); } else { - yield* generateSfcBlockSection(script, 0, script.content.length, codeFeatures.all, true); + yield* generateSfcBlockSection(script, 0, script.content.length, codeFeatures.all); yield* generateExportDeclareEqual(script); yield `(await import('${vueCompilerOptions.lib}')).defineComponent({})${endOfLine}`; yield* generateTemplate(options, ctx); diff --git a/packages/language-core/lib/codegen/script/scriptSetup.ts b/packages/language-core/lib/codegen/script/scriptSetup.ts index 8afc5a213e..de383d50d2 100644 --- a/packages/language-core/lib/codegen/script/scriptSetup.ts +++ b/packages/language-core/lib/codegen/script/scriptSetup.ts @@ -274,8 +274,7 @@ export function* generateSetupFunction( Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset), scriptSetup.content.length, transforms, - (start, end) => - generateSfcBlockSection(scriptSetup, start, end, codeFeatures.all, end === scriptSetup.content.length), + (start, end) => generateSfcBlockSection(scriptSetup, start, end, codeFeatures.all), ); yield* generateMacros(options); yield* generateModels(scriptSetup, scriptSetupRanges); diff --git a/packages/language-core/lib/codegen/utils/index.ts b/packages/language-core/lib/codegen/utils/index.ts index 918a22180e..78370c69d1 100644 --- a/packages/language-core/lib/codegen/utils/index.ts +++ b/packages/language-core/lib/codegen/utils/index.ts @@ -1,5 +1,5 @@ import type * as ts from 'typescript'; -import type { Code, SfcBlock, VueCodeInformation } from '../../types'; +import type { Code, Sfc, SfcBlock, VueCodeInformation } from '../../types'; import { codeFeatures } from '../codeFeatures'; export const newLine = `\n`; @@ -35,23 +35,25 @@ export function getTypeScriptAST(ts: typeof import('typescript'), block: SfcBloc } export function* generateSfcBlockSection( - block: SfcBlock, + block: NonNullable, start: number, end: number, features: VueCodeInformation, - partiallyEnd = false, ): Generator { - yield [ - block.content.slice(start, end), - block.name, - start, - features, - ]; + const text = block.content.slice(start, end); + yield [text, block.name, start, features]; + // #3632 - if (partiallyEnd) { - yield `;`; - yield ['', block.name, end, codeFeatures.verification]; - yield newLine; + if ('parseDiagnostics' in block.ast) { + const emptyEndLength = text.length - text.trimEnd().length; + for (const diag of block.ast.parseDiagnostics as ts.DiagnosticWithLocation[]) { + if (diag.start >= end - emptyEndLength) { + yield `;`; + yield ['', block.name, end, codeFeatures.verification]; + yield newLine; + break; + } + } } }