Skip to content

Commit 587b333

Browse files
refactor(language-core): remove wrapWith() (#5778)
1 parent a7020c6 commit 587b333

24 files changed

+380
-464
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { Code, Sfc, SfcBlock, VueCompilerOptions } from '../../types';
66
import { codeFeatures } from '../codeFeatures';
77
import type { TemplateCodegenContext } from '../template/context';
88
import { endOfLine, generatePartiallyEnding, generateSfcBlockSection, newLine } from '../utils';
9-
import { wrapWith } from '../utils/wrapWith';
9+
import { endBoundary, startBoundary } from '../utils/boundary';
1010
import { createScriptCodegenContext, type ScriptCodegenContext } from './context';
1111
import { generateScriptSetup, generateScriptSetupImports } from './scriptSetup';
1212
import { generateSrc } from './src';
@@ -155,13 +155,9 @@ export function* generateConstExport(
155155
);
156156
}
157157
yield `const `;
158-
yield* wrapWith(
159-
block.name,
160-
0,
161-
block.content.length,
162-
codeFeatures.doNotReportTs6133,
163-
`__VLS_export`,
164-
);
158+
const token = yield* startBoundary(block.name, 0, codeFeatures.doNotReportTs6133);
159+
yield `__VLS_export`;
160+
yield endBoundary(token, block.content.length);
165161
yield ` = `;
166162
}
167163

@@ -192,12 +188,15 @@ function* generateExportDefault(options: ScriptCodegenOptions): Generator<Code>
192188
else {
193189
yield `export `;
194190
if (options.templateStartTagOffset !== undefined) {
191+
const token = Symbol();
195192
for (let i = 0; i < 'template'.length + 1; i++) {
196193
yield [
197194
``,
198195
'template',
199196
options.templateStartTagOffset + 1 + i,
200-
i ? { __combineOffset: i } : codeFeatures.navigationWithoutRename,
197+
i === 0
198+
? { ...codeFeatures.navigationWithoutRename, __combineToken: token }
199+
: { __combineToken: token },
201200
];
202201
}
203202
}

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

Lines changed: 127 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
33
import type { Code, Sfc, TextRange } from '../../types';
44
import { codeFeatures } from '../codeFeatures';
55
import { endOfLine, generatePartiallyEnding, generateSfcBlockSection, identifierRegex, newLine } from '../utils';
6+
import { endBoundary, startBoundary } from '../utils/boundary';
67
import { generateCamelized } from '../utils/camelized';
7-
import { wrapWith } from '../utils/wrapWith';
88
import { generateComponent } from './component';
99
import type { ScriptCodegenContext } from './context';
1010
import { generateConstExport, type ScriptCodegenOptions } from './index';
@@ -129,7 +129,7 @@ function* generateSetupFunction(
129129
scriptSetupRanges: ScriptSetupRanges,
130130
syntax: 'return' | 'export default' | undefined,
131131
): Generator<Code> {
132-
let setupCodeModifies: [Code[], number, number][] = [];
132+
let setupCodeModifies: [() => Generator<Code>, number, number][] = [];
133133
if (scriptSetupRanges.defineProps) {
134134
const { name, statement, callExp, typeArg } = scriptSetupRanges.defineProps;
135135
setupCodeModifies.push(...generateDefineWithType(
@@ -170,37 +170,43 @@ function* generateSetupFunction(
170170
const { callExp, arg, typeArg } = scriptSetupRanges.defineExpose;
171171
if (typeArg) {
172172
setupCodeModifies.push([
173-
[
174-
`let __VLS_exposed!: `,
175-
generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all),
176-
endOfLine,
177-
],
173+
function*() {
174+
yield `let __VLS_exposed!: `;
175+
yield generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all);
176+
yield endOfLine;
177+
},
178178
callExp.start,
179179
callExp.start,
180180
], [
181-
[`typeof __VLS_exposed`],
181+
function*() {
182+
yield `typeof __VLS_exposed`;
183+
},
182184
typeArg.start,
183185
typeArg.end,
184186
]);
185187
}
186188
else if (arg) {
187189
setupCodeModifies.push([
188-
[
189-
`const __VLS_exposed = `,
190-
generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.all),
191-
endOfLine,
192-
],
190+
function*() {
191+
yield `const __VLS_exposed = `;
192+
yield generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.all);
193+
yield endOfLine;
194+
},
193195
callExp.start,
194196
callExp.start,
195197
], [
196-
[`__VLS_exposed`],
198+
function*() {
199+
yield `__VLS_exposed`;
200+
},
197201
arg.start,
198202
arg.end,
199203
]);
200204
}
201205
else {
202206
setupCodeModifies.push([
203-
[`const __VLS_exposed = {}${endOfLine}`],
207+
function*() {
208+
yield `const __VLS_exposed = {}${endOfLine}`;
209+
},
204210
callExp.start,
205211
callExp.start,
206212
]);
@@ -209,58 +215,71 @@ function* generateSetupFunction(
209215
if (options.vueCompilerOptions.inferTemplateDollarAttrs) {
210216
for (const { callExp } of scriptSetupRanges.useAttrs) {
211217
setupCodeModifies.push([
212-
[`(`],
218+
function*() {
219+
yield `(`;
220+
},
213221
callExp.start,
214222
callExp.start,
215223
], [
216-
[` as typeof __VLS_dollars.$attrs)`],
224+
function*() {
225+
yield ` as typeof __VLS_dollars.$attrs)`;
226+
},
217227
callExp.end,
218228
callExp.end,
219229
]);
220230
}
221231
}
222232
for (const { callExp, exp, arg } of scriptSetupRanges.useCssModule) {
223233
setupCodeModifies.push([
224-
[`(`],
234+
function*() {
235+
yield `(`;
236+
},
225237
callExp.start,
226238
callExp.start,
227-
], [
228-
arg
229-
? [
230-
` as Omit<__VLS_StyleModules, '$style'>[`,
231-
generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.withoutSemantic),
232-
`])`,
233-
]
234-
: [
235-
` as __VLS_StyleModules[`,
236-
...wrapWith(
237-
scriptSetup.name,
238-
exp.start,
239-
exp.end,
240-
codeFeatures.verification,
241-
`'$style'`,
242-
),
243-
`])`,
244-
],
245-
callExp.end,
246-
callExp.end,
247239
]);
248240
if (arg) {
249241
setupCodeModifies.push([
250-
[`__VLS_placeholder`],
242+
function*() {
243+
yield ` as Omit<__VLS_StyleModules, '$style'>[`;
244+
yield generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.withoutSemantic);
245+
yield `])`;
246+
},
247+
callExp.end,
248+
callExp.end,
249+
], [
250+
function*() {
251+
yield `__VLS_placeholder`;
252+
},
251253
arg.start,
252254
arg.end,
253255
]);
254256
}
257+
else {
258+
setupCodeModifies.push([
259+
function*() {
260+
yield ` as __VLS_StyleModules[`;
261+
const token = yield* startBoundary(scriptSetup.name, exp.start, codeFeatures.verification);
262+
yield `'$style'`;
263+
yield endBoundary(token, exp.end);
264+
yield `])`;
265+
},
266+
callExp.end,
267+
callExp.end,
268+
]);
269+
}
255270
}
256271
if (options.vueCompilerOptions.inferTemplateDollarSlots) {
257272
for (const { callExp } of scriptSetupRanges.useSlots) {
258273
setupCodeModifies.push([
259-
[`(`],
274+
function*() {
275+
yield `(`;
276+
},
260277
callExp.start,
261278
callExp.start,
262279
], [
263-
[` as typeof __VLS_dollars.$slots)`],
280+
function*() {
281+
yield ` as typeof __VLS_dollars.$slots)`;
282+
},
264283
callExp.end,
265284
callExp.end,
266285
]);
@@ -277,33 +296,37 @@ function* generateSetupFunction(
277296
: [`unknown`];
278297
if (isTs) {
279298
setupCodeModifies.push([
280-
[
281-
`<`,
282-
...templateRefType,
283-
`>`,
284-
],
299+
function*() {
300+
yield `<`;
301+
yield* templateRefType;
302+
yield `>`;
303+
},
285304
exp.end,
286305
exp.end,
287306
]);
288307
}
289308
else {
290309
setupCodeModifies.push([
291-
[`(`],
310+
function*() {
311+
yield `(`;
312+
},
292313
callExp.start,
293314
callExp.start,
294315
], [
295-
[
296-
` as __VLS_UseTemplateRef<`,
297-
...templateRefType,
298-
`>)`,
299-
],
316+
function*() {
317+
yield ` as __VLS_UseTemplateRef<`;
318+
yield* templateRefType;
319+
yield `>)`;
320+
},
300321
callExp.end,
301322
callExp.end,
302323
]);
303324
}
304325
if (arg) {
305326
setupCodeModifies.push([
306-
[`__VLS_placeholder`],
327+
function*() {
328+
yield `__VLS_placeholder`;
329+
},
307330
arg.start,
308331
arg.end,
309332
]);
@@ -312,9 +335,9 @@ function* generateSetupFunction(
312335
setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);
313336

314337
let nextStart = Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset);
315-
for (const [codes, start, end] of setupCodeModifies) {
338+
for (const [generate, start, end] of setupCodeModifies) {
316339
yield generateSfcBlockSection(scriptSetup, nextStart, start, codeFeatures.all);
317-
yield* codes;
340+
yield* generate();
318341
nextStart = end;
319342
}
320343
yield generateSfcBlockSection(scriptSetup, nextStart, scriptSetup.content.length, codeFeatures.all);
@@ -375,65 +398,83 @@ function* generateDefineWithType(
375398
name: string | undefined,
376399
defaultName: string,
377400
typeName: string,
378-
): Generator<[Code[], number, number]> {
401+
): Generator<[() => Generator<Code>, number, number]> {
379402
if (typeArg) {
380403
yield [
381-
[
382-
`type ${typeName} = `,
383-
generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all),
384-
endOfLine,
385-
],
404+
function*() {
405+
yield `type ${typeName} = `;
406+
yield generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all);
407+
yield endOfLine;
408+
},
386409
statement.start,
387410
statement.start,
388411
];
389-
yield [[typeName], typeArg.start, typeArg.end];
412+
yield [
413+
function*() {
414+
yield typeName;
415+
},
416+
typeArg.start,
417+
typeArg.end,
418+
];
390419
}
391420
if (!name) {
392421
if (statement.start === callExp.start && statement.end === callExp.end) {
393-
yield [[`const ${defaultName} = `], callExp.start, callExp.start];
422+
yield [
423+
function*() {
424+
yield `const ${defaultName} = `;
425+
},
426+
callExp.start,
427+
callExp.start,
428+
];
394429
}
395430
else if (typeArg) {
396431
yield [
397-
[
398-
`const ${defaultName} = `,
399-
generateSfcBlockSection(scriptSetup, callExp.start, typeArg.start, codeFeatures.all),
400-
],
432+
function*() {
433+
yield `const ${defaultName} = `;
434+
yield generateSfcBlockSection(scriptSetup, callExp.start, typeArg.start, codeFeatures.all);
435+
},
401436
statement.start,
402437
typeArg.start,
403438
];
404439
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-
],
440+
function*() {
441+
yield generateSfcBlockSection(scriptSetup, typeArg.end, callExp.end, codeFeatures.all);
442+
yield endOfLine;
443+
yield generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all);
444+
yield defaultName;
445+
},
411446
typeArg.end,
412447
callExp.end,
413448
];
414449
}
415450
else {
416451
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-
],
452+
function*() {
453+
yield `const ${defaultName} = `;
454+
yield generateSfcBlockSection(scriptSetup, callExp.start, callExp.end, codeFeatures.all);
455+
yield endOfLine;
456+
yield generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all);
457+
yield defaultName;
458+
},
424459
statement.start,
425460
callExp.end,
426461
];
427462
}
428463
}
429464
else if (!identifierRegex.test(name)) {
430-
yield [[`const ${defaultName} = `], statement.start, callExp.start];
431465
yield [
432-
[
433-
endOfLine,
434-
generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all),
435-
defaultName,
436-
],
466+
function*() {
467+
yield `const ${defaultName} = `;
468+
},
469+
statement.start,
470+
callExp.start,
471+
];
472+
yield [
473+
function*() {
474+
yield endOfLine;
475+
yield generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all);
476+
yield defaultName;
477+
},
437478
statement.end,
438479
statement.end,
439480
];

0 commit comments

Comments
 (0)