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
8 changes: 4 additions & 4 deletions packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 15 additions & 13 deletions packages/language-core/lib/codegen/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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`;
Expand Down Expand Up @@ -35,23 +35,25 @@ export function getTypeScriptAST(ts: typeof import('typescript'), block: SfcBloc
}

export function* generateSfcBlockSection(
block: SfcBlock,
block: NonNullable<Sfc['script' | 'scriptSetup']>,
start: number,
end: number,
features: VueCodeInformation,
partiallyEnd = false,
): Generator<Code> {
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;
}
}
}
}

Expand Down