Skip to content

Commit c2dfd86

Browse files
authored
refactor(language-core): only process auto import virtual code in generateElementChildren (#5278)
1 parent da970e9 commit c2dfd86

File tree

10 files changed

+105
-120
lines changed

10 files changed

+105
-120
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
194194
},
195195
}),
196196
resolveCodeFeatures,
197+
inVFor: false,
197198
slots,
198199
dynamicSlots,
199200
dollarVars,

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

+7-16
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ import type { TemplateCodegenOptions } from './index';
1616
import { generateInterpolation } from './interpolation';
1717
import { generatePropertyAccess } from './propertyAccess';
1818
import { collectStyleScopedClassReferences } from './styleScopedClasses';
19-
import { generateImplicitDefaultSlot, generateVSlot } from './vSlot';
19+
import { generateVSlot } from './vSlot';
2020

2121
const colonReg = /:/g;
2222

2323
export function* generateComponent(
2424
options: TemplateCodegenOptions,
2525
ctx: TemplateCodegenContext,
26-
node: CompilerDOM.ElementNode,
27-
isVForChild: boolean
26+
node: CompilerDOM.ElementNode
2827
): Generator<Code> {
2928
const tagOffsets = [node.loc.start.offset + options.template.content.slice(node.loc.start.offset).indexOf(node.tag)];
3029
if (!node.isSelfClosing && options.template.lang === 'html') {
@@ -258,7 +257,7 @@ export function* generateComponent(
258257
ctx.currentComponent.used = true;
259258

260259
yield `var ${componentInstanceVar} = {} as (Parameters<NonNullable<typeof ${componentCtxVar}['expose']>>[0] | null)`;
261-
if (isVForChild) {
260+
if (ctx.inVFor) {
262261
yield `[]`;
263262
}
264263
yield `${endOfLine}`;
@@ -280,14 +279,7 @@ export function* generateComponent(
280279
collectStyleScopedClassReferences(options, ctx, node);
281280

282281
const slotDir = node.props.find(p => p.type === CompilerDOM.NodeTypes.DIRECTIVE && p.name === 'slot') as CompilerDOM.DirectiveNode;
283-
if (slotDir) {
284-
yield* generateVSlot(options, ctx, node, slotDir);
285-
}
286-
else {
287-
// #932: reference for default slot
288-
yield* generateImplicitDefaultSlot(ctx, node);
289-
yield* generateElementChildren(options, ctx, node);
290-
}
282+
yield* generateVSlot(options, ctx, node, slotDir);
291283

292284
if (ctx.currentComponent.used) {
293285
yield `var ${componentCtxVar}!: __VLS_PickFunctionalComponentCtx<typeof ${componentOriginalVar}, typeof ${componentVNodeVar}>${endOfLine}`;
@@ -297,8 +289,7 @@ export function* generateComponent(
297289
export function* generateElement(
298290
options: TemplateCodegenOptions,
299291
ctx: TemplateCodegenContext,
300-
node: CompilerDOM.ElementNode,
301-
isVForChild: boolean
292+
node: CompilerDOM.ElementNode
302293
): Generator<Code> {
303294
const startTagOffset = node.loc.start.offset + options.template.content.slice(node.loc.start.offset).indexOf(node.tag);
304295
const endTagOffset = !node.isSelfClosing && options.template.lang === 'html'
@@ -349,7 +340,7 @@ export function* generateElement(
349340
const [refName, offset] = yield* generateElementReference(options, ctx, node);
350341
if (refName && offset) {
351342
let typeExp = `__VLS_NativeElements['${node.tag}']`;
352-
if (isVForChild) {
343+
if (ctx.inVFor) {
353344
typeExp += `[]`;
354345
}
355346
ctx.addTemplateRef(refName, typeExp, offset);
@@ -364,7 +355,7 @@ export function* generateElement(
364355

365356
collectStyleScopedClassReferences(options, ctx, node);
366357

367-
yield* generateElementChildren(options, ctx, node);
358+
yield* generateElementChildren(options, ctx, node.children);
368359
}
369360

370361
function* generateFailedPropExps(

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import { generateTemplateChild } from './templateChild';
77
export function* generateElementChildren(
88
options: TemplateCodegenOptions,
99
ctx: TemplateCodegenContext,
10-
node: CompilerDOM.ElementNode
10+
children: (CompilerDOM.TemplateChildNode | CompilerDOM.SimpleExpressionNode)[],
11+
enterNode = true
1112
): Generator<Code> {
12-
for (const childNode of node.children) {
13-
yield* generateTemplateChild(options, ctx, childNode);
13+
yield* ctx.generateAutoImportCompletion();
14+
for (const childNode of children) {
15+
yield* generateTemplateChild(options, ctx, childNode, enterNode);
1416
}
1517
yield* ctx.generateAutoImportCompletion();
1618
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ export function* generateEventExpression(
161161
ctx.removeLocalVariable('$event');
162162

163163
yield endOfLine;
164-
yield* ctx.generateAutoImportCompletion();
165164
yield `}`;
166165
}
167166
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
5454
}
5555

5656
yield* generateStyleScopedClassReferences(ctx);
57-
yield* ctx.generateAutoImportCompletion();
5857
yield* ctx.generateHoistVariables();
5958

6059
const speicalTypes = [

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,5 @@ export function* generateSlotOutlet(
168168
});
169169
}
170170
}
171-
yield* ctx.generateAutoImportCompletion();
172-
yield* generateElementChildren(options, ctx, node);
171+
yield* generateElementChildren(options, ctx, node.children);
173172
}

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

+6-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { hyphenateTag } from '../../utils/shared';
44
import { endOfLine } from '../utils';
55
import type { TemplateCodegenContext } from './context';
66
import { generateComponent, generateElement } from './element';
7+
import { generateElementChildren } from './elementChildren';
78
import type { TemplateCodegenOptions } from './index';
89
import { generateInterpolation } from './interpolation';
910
import { generateSlotOutlet } from './slotOutlet';
@@ -31,8 +32,7 @@ export function* generateTemplateChild(
3132
options: TemplateCodegenOptions,
3233
ctx: TemplateCodegenContext,
3334
node: CompilerDOM.RootNode | CompilerDOM.TemplateChildNode | CompilerDOM.SimpleExpressionNode,
34-
enterNode: boolean = true,
35-
isVForChild: boolean = false
35+
enterNode: boolean = true
3636
): Generator<Code> {
3737
if (enterNode && !ctx.enter(node)) {
3838
return;
@@ -47,9 +47,7 @@ export function* generateTemplateChild(
4747
for (const item of collectSingleRootNodes(options, node.children)) {
4848
ctx.singleRootNodes.add(item);
4949
}
50-
for (const childNode of node.children) {
51-
yield* generateTemplateChild(options, ctx, childNode);
52-
}
50+
yield* generateElementChildren(options, ctx, node.children);
5351
}
5452
else if (node.type === CompilerDOM.NodeTypes.ELEMENT) {
5553
const vForNode = getVForNode(node);
@@ -76,11 +74,11 @@ export function* generateTemplateChild(
7674
node.tagType === CompilerDOM.ElementTypes.ELEMENT
7775
|| node.tagType === CompilerDOM.ElementTypes.TEMPLATE
7876
) {
79-
yield* generateElement(options, ctx, node, isVForChild);
77+
yield* generateElement(options, ctx, node);
8078
}
8179
else {
8280
const { currentComponent } = ctx;
83-
yield* generateComponent(options, ctx, node, isVForChild);
81+
yield* generateComponent(options, ctx, node);
8482
ctx.currentComponent = currentComponent;
8583
}
8684
}
@@ -91,11 +89,7 @@ export function* generateTemplateChild(
9189
}
9290
else if (node.type === CompilerDOM.NodeTypes.COMPOUND_EXPRESSION) {
9391
// {{ ... }} {{ ... }}
94-
for (const childNode of node.children) {
95-
if (typeof childNode === 'object') {
96-
yield* generateTemplateChild(options, ctx, childNode, false);
97-
}
98-
}
92+
yield* generateElementChildren(options, ctx, node.children.filter(child => typeof child === 'object'), false);
9993
}
10094
else if (node.type === CompilerDOM.NodeTypes.INTERPOLATION) {
10195
// {{ ... }}

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as CompilerDOM from '@vue/compiler-dom';
22
import type { Code } from '../../types';
33
import { collectVars, createTsAst, endOfLine, newLine } from '../utils';
44
import type { TemplateCodegenContext } from './context';
5+
import { generateElementChildren } from './elementChildren';
56
import type { TemplateCodegenOptions } from './index';
67
import { generateInterpolation } from './interpolation';
7-
import { generateTemplateChild } from './templateChild';
88

99
export function* generateVFor(
1010
options: TemplateCodegenOptions,
@@ -46,9 +46,11 @@ export function* generateVFor(
4646
yield `{} as any`;
4747
}
4848
yield `) {${newLine}`;
49+
4950
for (const varName of forBlockVars) {
5051
ctx.addLocalVariable(varName);
5152
}
53+
5254
let isFragment = true;
5355
for (const argument of node.codegenNode?.children.arguments ?? []) {
5456
if (
@@ -81,13 +83,15 @@ export function* generateVFor(
8183
}
8284
}
8385
}
84-
for (const childNode of node.children) {
85-
yield* generateTemplateChild(options, ctx, childNode, isFragment, true);
86-
}
86+
87+
const { inVFor } = ctx;
88+
ctx.inVFor = true;
89+
yield* generateElementChildren(options, ctx, node.children, isFragment);
90+
ctx.inVFor = inVFor;
91+
8792
for (const varName of forBlockVars) {
8893
ctx.removeLocalVariable(varName);
8994
}
90-
yield* ctx.generateAutoImportCompletion();
9195
yield `}${newLine}`;
9296
}
9397

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { toString } from 'muggle-string';
33
import type { Code } from '../../types';
44
import { newLine } from '../utils';
55
import type { TemplateCodegenContext } from './context';
6+
import { generateElementChildren } from './elementChildren';
67
import type { TemplateCodegenOptions } from './index';
78
import { generateInterpolation } from './interpolation';
8-
import { generateTemplateChild } from './templateChild';
99

1010
export function* generateVIf(
1111
options: TemplateCodegenOptions,
@@ -50,10 +50,7 @@ export function* generateVIf(
5050
}
5151

5252
yield `{${newLine}`;
53-
for (const childNode of branch.children) {
54-
yield* generateTemplateChild(options, ctx, childNode, isFragment(node));
55-
}
56-
yield* ctx.generateAutoImportCompletion();
53+
yield* generateElementChildren(options, ctx, branch.children, isFragment(node));
5754
yield `}${newLine}`;
5855

5956
if (addedBlockCondition) {

0 commit comments

Comments
 (0)