Skip to content

Commit c65e5a5

Browse files
fix(language-core): generate script separator on demand (#5816)
1 parent 4a947ef commit c65e5a5

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

packages/language-core/lib/codegen/script/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ function* generateWorker(
6060
const { expression: options } = componentOptions ?? exportDefault;
6161
yield* generateSfcBlockSection(script, 0, options.start, codeFeatures.all);
6262
yield exportExpression;
63-
yield* generateSfcBlockSection(script, options.end, script.content.length, codeFeatures.all, true);
63+
yield* generateSfcBlockSection(script, options.end, script.content.length, codeFeatures.all);
6464
}
6565
else {
66-
yield* generateSfcBlockSection(script, 0, script.content.length, codeFeatures.all, true);
66+
yield* generateSfcBlockSection(script, 0, script.content.length, codeFeatures.all);
6767
yield `export default ${exportExpression}${endOfLine}`;
6868
}
6969

@@ -160,7 +160,7 @@ function* generateWorker(
160160
});
161161
}
162162

163-
yield* generateSfcBlockSection(script, 0, exportDefault.start, codeFeatures.all, true);
163+
yield* generateSfcBlockSection(script, 0, exportDefault.start, codeFeatures.all);
164164
yield* generateExportDeclareEqual(script);
165165
if (wrapLeft) {
166166
yield wrapLeft;
@@ -176,7 +176,7 @@ function* generateWorker(
176176
yield* generateSfcBlockSection(script, expression.end, script.content.length, codeFeatures.all);
177177
}
178178
else {
179-
yield* generateSfcBlockSection(script, 0, script.content.length, codeFeatures.all, true);
179+
yield* generateSfcBlockSection(script, 0, script.content.length, codeFeatures.all);
180180
yield* generateExportDeclareEqual(script);
181181
yield `(await import('${vueCompilerOptions.lib}')).defineComponent({})${endOfLine}`;
182182
yield* generateTemplate(options, ctx);

packages/language-core/lib/codegen/script/scriptSetup.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,7 @@ export function* generateSetupFunction(
274274
Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset),
275275
scriptSetup.content.length,
276276
transforms,
277-
(start, end) =>
278-
generateSfcBlockSection(scriptSetup, start, end, codeFeatures.all, end === scriptSetup.content.length),
277+
(start, end) => generateSfcBlockSection(scriptSetup, start, end, codeFeatures.all),
279278
);
280279
yield* generateMacros(options);
281280
yield* generateModels(scriptSetup, scriptSetupRanges);

packages/language-core/lib/codegen/utils/index.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type * as ts from 'typescript';
2-
import type { Code, SfcBlock, VueCodeInformation } from '../../types';
2+
import type { Code, Sfc, SfcBlock, VueCodeInformation } from '../../types';
33
import { codeFeatures } from '../codeFeatures';
44

55
export const newLine = `\n`;
@@ -35,23 +35,25 @@ export function getTypeScriptAST(ts: typeof import('typescript'), block: SfcBloc
3535
}
3636

3737
export function* generateSfcBlockSection(
38-
block: SfcBlock,
38+
block: NonNullable<Sfc['script' | 'scriptSetup']>,
3939
start: number,
4040
end: number,
4141
features: VueCodeInformation,
42-
partiallyEnd = false,
4342
): Generator<Code> {
44-
yield [
45-
block.content.slice(start, end),
46-
block.name,
47-
start,
48-
features,
49-
];
43+
const text = block.content.slice(start, end);
44+
yield [text, block.name, start, features];
45+
5046
// #3632
51-
if (partiallyEnd) {
52-
yield `;`;
53-
yield ['', block.name, end, codeFeatures.verification];
54-
yield newLine;
47+
if ('parseDiagnostics' in block.ast) {
48+
const emptyEndLength = text.length - text.trimEnd().length;
49+
for (const diag of block.ast.parseDiagnostics as ts.DiagnosticWithLocation[]) {
50+
if (diag.start >= end - emptyEndLength) {
51+
yield `;`;
52+
yield ['', block.name, end, codeFeatures.verification];
53+
yield newLine;
54+
break;
55+
}
56+
}
5557
}
5658
}
5759

0 commit comments

Comments
 (0)