Skip to content

Commit 5a35d2a

Browse files
committed
refactor(language-core): do not destructure element reference
1 parent ea3a954 commit 5a35d2a

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ export function* generateComponent(
240240
);
241241
yield* generateElementDirectives(options, ctx, node);
242242

243-
const [refName, offset] = yield* generateElementReference(options, ctx, node);
243+
const reference = yield* generateElementReference(options, ctx, node);
244244
const tag = hyphenateTag(node.tag);
245245
const isRootNode = ctx.singleRootNodes.has(node)
246246
&& !options.vueCompilerOptions.fallthroughComponentNames.includes(tag);
247247

248-
if (refName || isRootNode) {
248+
if (reference || isRootNode) {
249249
const componentInstanceVar = ctx.getInternalVariable();
250250
ctx.currentComponent.used = true;
251251

@@ -255,8 +255,9 @@ export function* generateComponent(
255255
}
256256
yield endOfLine;
257257

258-
if (refName && offset) {
259-
ctx.addTemplateRef(refName, `typeof ${ctx.getHoistVariable(componentInstanceVar)}`, offset);
258+
if (reference) {
259+
const typeExp = `typeof ${ctx.getHoistVariable(componentInstanceVar)}`;
260+
ctx.addTemplateRef(reference.name, typeExp, reference.offset);
260261
}
261262
if (isRootNode) {
262263
ctx.singleRootElTypes.push(`NonNullable<typeof ${componentInstanceVar}>['$el']`);
@@ -331,13 +332,13 @@ export function* generateElement(
331332
yield* generateFailedPropExps(options, ctx, failedPropExps);
332333
yield* generateElementDirectives(options, ctx, node);
333334

334-
const [refName, offset] = yield* generateElementReference(options, ctx, node);
335-
if (refName && offset) {
335+
const reference = yield* generateElementReference(options, ctx, node);
336+
if (reference) {
336337
let typeExp = `__VLS_NativeElements['${node.tag}']`;
337338
if (ctx.inVFor) {
338339
typeExp += `[]`;
339340
}
340-
ctx.addTemplateRef(refName, typeExp, offset);
341+
ctx.addTemplateRef(reference.name, typeExp, reference.offset);
341342
}
342343
if (ctx.singleRootNodes.has(node)) {
343344
ctx.singleRootElTypes.push(`__VLS_NativeElements['${node.tag}']`);
@@ -438,34 +439,33 @@ function* generateElementReference(
438439
options: TemplateCodegenOptions,
439440
ctx: TemplateCodegenContext,
440441
node: CompilerDOM.ElementNode,
441-
): Generator<Code, [refName: string, offset: number] | []> {
442+
): Generator<Code, { name: string; offset: number } | void> {
442443
for (const prop of node.props) {
443444
if (
444445
prop.type === CompilerDOM.NodeTypes.ATTRIBUTE
445446
&& prop.name === 'ref'
446447
&& prop.value
447448
) {
448-
const [content, startOffset] = normalizeAttributeValue(prop.value);
449+
const [name, offset] = normalizeAttributeValue(prop.value);
449450

450451
// navigation support for `const foo = ref()`
451452
yield `/** @type {typeof __VLS_ctx`;
452453
yield* generatePropertyAccess(
453454
options,
454455
ctx,
455-
content,
456-
startOffset,
456+
name,
457+
offset,
457458
codeFeatures.navigation,
458459
);
459460
yield `} */${endOfLine}`;
460461

461-
if (identifierRegex.test(content) && !options.templateRefNames.has(content)) {
462-
ctx.accessExternalVariable(content, startOffset);
462+
if (identifierRegex.test(name) && !options.templateRefNames.has(name)) {
463+
ctx.accessExternalVariable(name, offset);
463464
}
464465

465-
return [content, startOffset];
466+
return { name, offset };
466467
}
467468
}
468-
return [];
469469
}
470470

471471
function hasVBindAttrs(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const endOfLine = `;${newLine}`;
88
export const combineLastMapping: VueCodeInformation = { __combineOffset: 1 };
99
export const identifierRegex = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;
1010

11-
export function normalizeAttributeValue(node: CompilerDOM.TextNode): [string, number] {
11+
export function normalizeAttributeValue(node: CompilerDOM.TextNode) {
1212
let offset = node.loc.start.offset;
1313
let content = node.loc.source;
1414
if (
@@ -18,7 +18,7 @@ export function normalizeAttributeValue(node: CompilerDOM.TextNode): [string, nu
1818
offset++;
1919
content = content.slice(1, -1);
2020
}
21-
return [content, offset];
21+
return [content, offset] as const;
2222
}
2323

2424
export function createTsAst(

0 commit comments

Comments
 (0)