Skip to content

Commit 349856a

Browse files
KazariEXserkodev
andauthored
fix(language-core): strip expose from internal component options (#5763)
Co-authored-by: serkodev <[email protected]>
1 parent c25d7e9 commit 349856a

File tree

5 files changed

+190
-158
lines changed

5 files changed

+190
-158
lines changed

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

Lines changed: 104 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { camelize } from '@vue/shared';
22
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
33
import type { Code, Sfc, TextRange } from '../../types';
44
import { codeFeatures } from '../codeFeatures';
5-
import { endOfLine, generatePartiallyEnding, generateSfcBlockSection, identifierRegex, newLine } from '../utils';
5+
import {
6+
createSfcBlockGenerator,
7+
endOfLine,
8+
generatePartiallyEnding,
9+
generateSfcBlockSection,
10+
identifierRegex,
11+
newLine,
12+
} from '../utils';
613
import { generateCamelized } from '../utils/camelized';
714
import { wrapWith } from '../utils/wrapWith';
815
import { generateComponent } from './component';
@@ -129,103 +136,99 @@ function* generateSetupFunction(
129136
scriptSetupRanges: ScriptSetupRanges,
130137
syntax: 'return' | 'export default' | undefined,
131138
): Generator<Code> {
132-
let setupCodeModifies: [Code[], number, number][] = [];
139+
const { replace, generate } = createSfcBlockGenerator(
140+
scriptSetup,
141+
Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset),
142+
scriptSetup.content.length,
143+
codeFeatures.all,
144+
);
145+
133146
if (scriptSetupRanges.defineProps) {
134147
const { name, statement, callExp, typeArg } = scriptSetupRanges.defineProps;
135-
setupCodeModifies.push(...generateDefineWithType(
136-
scriptSetup,
137-
statement,
138-
scriptSetupRanges.withDefaults?.callExp ?? callExp,
139-
typeArg,
140-
name,
141-
`__VLS_props`,
142-
`__VLS_Props`,
143-
));
148+
for (
149+
const replacement of generateDefineWithType(
150+
scriptSetup,
151+
statement,
152+
scriptSetupRanges.withDefaults?.callExp ?? callExp,
153+
typeArg,
154+
name,
155+
`__VLS_props`,
156+
`__VLS_Props`,
157+
)
158+
) {
159+
replace(...replacement);
160+
}
144161
}
145162
if (scriptSetupRanges.defineEmits) {
146163
const { name, statement, callExp, typeArg } = scriptSetupRanges.defineEmits;
147-
setupCodeModifies.push(...generateDefineWithType(
148-
scriptSetup,
149-
statement,
150-
callExp,
151-
typeArg,
152-
name,
153-
`__VLS_emit`,
154-
`__VLS_Emit`,
155-
));
164+
for (
165+
const replacement of generateDefineWithType(
166+
scriptSetup,
167+
statement,
168+
callExp,
169+
typeArg,
170+
name,
171+
`__VLS_emit`,
172+
`__VLS_Emit`,
173+
)
174+
) {
175+
replace(...replacement);
176+
}
156177
}
157178
if (scriptSetupRanges.defineSlots) {
158179
const { name, statement, callExp, typeArg } = scriptSetupRanges.defineSlots;
159-
setupCodeModifies.push(...generateDefineWithType(
160-
scriptSetup,
161-
statement,
162-
callExp,
163-
typeArg,
164-
name,
165-
`__VLS_slots`,
166-
`__VLS_Slots`,
167-
));
180+
for (
181+
const replacement of generateDefineWithType(
182+
scriptSetup,
183+
statement,
184+
callExp,
185+
typeArg,
186+
name,
187+
`__VLS_slots`,
188+
`__VLS_Slots`,
189+
)
190+
) {
191+
replace(...replacement);
192+
}
168193
}
169194
if (scriptSetupRanges.defineExpose) {
170195
const { callExp, arg, typeArg } = scriptSetupRanges.defineExpose;
171196
if (typeArg) {
172-
setupCodeModifies.push([
173-
[
174-
`let __VLS_exposed!: `,
175-
generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all),
176-
endOfLine,
177-
],
197+
replace(
178198
callExp.start,
179199
callExp.start,
180-
], [
181-
[`typeof __VLS_exposed`],
182-
typeArg.start,
183-
typeArg.end,
184-
]);
200+
`let __VLS_exposed!: `,
201+
generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all),
202+
endOfLine,
203+
);
204+
replace(typeArg.start, typeArg.end, `typeof __VLS_exposed`);
185205
}
186206
else if (arg) {
187-
setupCodeModifies.push([
188-
[
189-
`const __VLS_exposed = `,
190-
generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.all),
191-
endOfLine,
192-
],
207+
replace(
193208
callExp.start,
194209
callExp.start,
195-
], [
196-
[`__VLS_exposed`],
197-
arg.start,
198-
arg.end,
199-
]);
210+
`const __VLS_exposed = `,
211+
generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.all),
212+
endOfLine,
213+
);
214+
replace(arg.start, arg.end, `__VLS_exposed`);
200215
}
201216
else {
202-
setupCodeModifies.push([
203-
[`const __VLS_exposed = {}${endOfLine}`],
204-
callExp.start,
205-
callExp.start,
206-
]);
217+
replace(callExp.start, callExp.start, `const __VLS_exposed = {}${endOfLine}`);
207218
}
208219
}
209220
if (options.vueCompilerOptions.inferTemplateDollarAttrs) {
210221
for (const { callExp } of scriptSetupRanges.useAttrs) {
211-
setupCodeModifies.push([
212-
[`(`],
213-
callExp.start,
214-
callExp.start,
215-
], [
216-
[` as typeof __VLS_dollars.$attrs)`],
217-
callExp.end,
218-
callExp.end,
219-
]);
222+
replace(callExp.start, callExp.start, `(`);
223+
replace(callExp.end, callExp.end, ` as typeof __VLS_dollars.$attrs)`);
220224
}
221225
}
222226
for (const { callExp, exp, arg } of scriptSetupRanges.useCssModule) {
223-
setupCodeModifies.push([
224-
[`(`],
225-
callExp.start,
226-
callExp.start,
227-
], [
228-
arg
227+
replace(callExp.start, callExp.start, `(`);
228+
replace(
229+
callExp.end,
230+
callExp.end,
231+
...arg
229232
? [
230233
` as Omit<__VLS_StyleModules, '$style'>[`,
231234
generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.withoutSemantic),
@@ -242,28 +245,15 @@ function* generateSetupFunction(
242245
),
243246
`])`,
244247
],
245-
callExp.end,
246-
callExp.end,
247-
]);
248+
);
248249
if (arg) {
249-
setupCodeModifies.push([
250-
[`__VLS_placeholder`],
251-
arg.start,
252-
arg.end,
253-
]);
250+
replace(arg.start, arg.end, `__VLS_placeholder`);
254251
}
255252
}
256253
if (options.vueCompilerOptions.inferTemplateDollarSlots) {
257254
for (const { callExp } of scriptSetupRanges.useSlots) {
258-
setupCodeModifies.push([
259-
[`(`],
260-
callExp.start,
261-
callExp.start,
262-
], [
263-
[` as typeof __VLS_dollars.$slots)`],
264-
callExp.end,
265-
callExp.end,
266-
]);
255+
replace(callExp.start, callExp.start, `(`);
256+
replace(callExp.end, callExp.end, ` as typeof __VLS_dollars.$slots)`);
267257
}
268258
}
269259
const isTs = options.lang !== 'js' && options.lang !== 'jsx';
@@ -276,49 +266,18 @@ function* generateSetupFunction(
276266
]
277267
: [`unknown`];
278268
if (isTs) {
279-
setupCodeModifies.push([
280-
[
281-
`<`,
282-
...templateRefType,
283-
`>`,
284-
],
285-
exp.end,
286-
exp.end,
287-
]);
269+
replace(exp.end, exp.end, `<`, ...templateRefType, `>`);
288270
}
289271
else {
290-
setupCodeModifies.push([
291-
[`(`],
292-
callExp.start,
293-
callExp.start,
294-
], [
295-
[
296-
` as __VLS_UseTemplateRef<`,
297-
...templateRefType,
298-
`>)`,
299-
],
300-
callExp.end,
301-
callExp.end,
302-
]);
272+
replace(callExp.start, callExp.start, `(`);
273+
replace(callExp.end, callExp.end, ` as __VLS_UseTemplateRef<`, ...templateRefType, `>)`);
303274
}
304275
if (arg) {
305-
setupCodeModifies.push([
306-
[`__VLS_placeholder`],
307-
arg.start,
308-
arg.end,
309-
]);
276+
replace(arg.start, arg.end, `__VLS_placeholder`);
310277
}
311278
}
312-
setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);
313-
314-
let nextStart = Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset);
315-
for (const [codes, start, end] of setupCodeModifies) {
316-
yield generateSfcBlockSection(scriptSetup, nextStart, start, codeFeatures.all);
317-
yield* codes;
318-
nextStart = end;
319-
}
320-
yield generateSfcBlockSection(scriptSetup, nextStart, scriptSetup.content.length, codeFeatures.all);
321279

280+
yield* generate();
322281
yield* generatePartiallyEnding(scriptSetup.name, scriptSetup.content.length, '#3632/scriptSetup.vue');
323282
yield* generateMacros(options, ctx);
324283

@@ -375,67 +334,57 @@ function* generateDefineWithType(
375334
name: string | undefined,
376335
defaultName: string,
377336
typeName: string,
378-
): Generator<[Code[], number, number]> {
337+
): Generator<[number, number, ...Code[]]> {
379338
if (typeArg) {
380339
yield [
381-
[
382-
`type ${typeName} = `,
383-
generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all),
384-
endOfLine,
385-
],
386340
statement.start,
387341
statement.start,
342+
`type ${typeName} = `,
343+
generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all),
344+
endOfLine,
388345
];
389-
yield [[typeName], typeArg.start, typeArg.end];
346+
yield [typeArg.start, typeArg.end, typeName];
390347
}
391348
if (!name) {
392349
if (statement.start === callExp.start && statement.end === callExp.end) {
393-
yield [[`const ${defaultName} = `], callExp.start, callExp.start];
350+
yield [callExp.start, callExp.start, `const ${defaultName} = `];
394351
}
395352
else if (typeArg) {
396353
yield [
397-
[
398-
`const ${defaultName} = `,
399-
generateSfcBlockSection(scriptSetup, callExp.start, typeArg.start, codeFeatures.all),
400-
],
401354
statement.start,
402355
typeArg.start,
356+
`const ${defaultName} = `,
357+
generateSfcBlockSection(scriptSetup, callExp.start, typeArg.start, codeFeatures.all),
403358
];
404359
yield [
405-
[
406-
generateSfcBlockSection(scriptSetup, typeArg.end, callExp.end, codeFeatures.all),
407-
endOfLine,
408-
generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all),
409-
defaultName,
410-
],
411360
typeArg.end,
412361
callExp.end,
362+
generateSfcBlockSection(scriptSetup, typeArg.end, callExp.end, codeFeatures.all),
363+
endOfLine,
364+
generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all),
365+
defaultName,
413366
];
414367
}
415368
else {
416369
yield [
417-
[
418-
`const ${defaultName} = `,
419-
generateSfcBlockSection(scriptSetup, callExp.start, callExp.end, codeFeatures.all),
420-
endOfLine,
421-
generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all),
422-
defaultName,
423-
],
424370
statement.start,
425371
callExp.end,
372+
`const ${defaultName} = `,
373+
generateSfcBlockSection(scriptSetup, callExp.start, callExp.end, codeFeatures.all),
374+
endOfLine,
375+
generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all),
376+
defaultName,
426377
];
427378
}
428379
}
429380
else if (!identifierRegex.test(name)) {
430-
yield [[`const ${defaultName} = `], statement.start, callExp.start];
381+
yield [statement.start, callExp.start, `const ${defaultName} = `];
431382
yield [
432-
[
433-
endOfLine,
434-
generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all),
435-
defaultName,
436-
],
437383
statement.end,
438384
statement.end,
385+
endOfLine,
386+
generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all),
387+
defaultName,
439388
];
440389
}
441390
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { generateStyleScopedClasses } from '../style/scopedClasses';
77
import { createTemplateCodegenContext, type TemplateCodegenContext } from '../template/context';
88
import { generateInterpolation } from '../template/interpolation';
99
import { generateStyleScopedClassReferences } from '../template/styleScopedClasses';
10-
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
10+
import { createSfcBlockGenerator, endOfLine, generateSfcBlockSection, newLine } from '../utils';
1111
import { generateSpreadMerge } from '../utils/merge';
1212
import type { ScriptCodegenContext } from './context';
1313
import type { ScriptCodegenOptions } from './index';
@@ -27,9 +27,20 @@ export function* generateTemplate(
2727

2828
function* generateSelf(options: ScriptCodegenOptions): Generator<Code> {
2929
if (options.sfc.script && options.scriptRanges?.componentOptions) {
30+
const { args, expose } = options.scriptRanges.componentOptions;
31+
const { replace, generate } = createSfcBlockGenerator(
32+
options.sfc.script,
33+
args.start,
34+
args.end,
35+
codeFeatures.navigation,
36+
);
37+
38+
if (expose) {
39+
replace(expose.start, expose.end, `undefined`);
40+
}
41+
3042
yield `const __VLS_self = (await import('${options.vueCompilerOptions.lib}')).defineComponent(`;
31-
const { args } = options.scriptRanges.componentOptions;
32-
yield generateSfcBlockSection(options.sfc.script, args.start, args.end, codeFeatures.all);
43+
yield* generate();
3344
yield `)${endOfLine}`;
3445
}
3546
else if (options.sfc.script && options.scriptRanges?.exportDefault) {

0 commit comments

Comments
 (0)