Skip to content

Commit 769c133

Browse files
committed
refactor(language-core): simplify bindings iteration
1 parent bfdbaf0 commit 769c133

File tree

2 files changed

+14
-46
lines changed

2 files changed

+14
-46
lines changed

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

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { camelize, capitalize } from '@vue/shared';
12
import * as path from 'path-browserify';
23
import type { Code } from '../../types';
34
import { codeFeatures } from '../codeFeatures';
@@ -6,7 +7,6 @@ import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
67
import { generateComponentSetupReturns, generateEmitsOption, generatePropsOption } from './component';
78
import type { ScriptCodegenContext } from './context';
89
import type { ScriptCodegenOptions } from './index';
9-
import { getTemplateUsageVars } from './template';
1010

1111
export function* generateComponentSelf(
1212
options: ScriptCodegenOptions,
@@ -21,27 +21,20 @@ export function* generateComponentSelf(
2121
yield* generateComponentSetupReturns(options.scriptSetupRanges);
2222
}
2323
// bindings
24-
const templateUsageVars = getTemplateUsageVars(options, ctx);
25-
for (
26-
const [content, bindings] of [
27-
[options.sfc.scriptSetup.content, options.scriptSetupRanges.bindings] as const,
28-
options.sfc.script && options.scriptRanges
29-
? [options.sfc.script.content, options.scriptRanges.bindings] as const
30-
: ['', []] as const,
31-
]
32-
) {
33-
for (const { range } of bindings) {
34-
const varName = content.slice(range.start, range.end);
35-
if (!templateUsageVars.has(varName) && !templateCodegenCtx.accessExternalVariables.has(varName)) {
36-
continue;
37-
}
38-
39-
const token = Symbol(varName.length);
40-
yield ['', undefined, 0, { __linkedToken: token }];
41-
yield `${varName}: ${varName} as typeof `;
42-
yield ['', undefined, 0, { __linkedToken: token }];
43-
yield `${varName},${newLine}`;
24+
const templateUsageVars = new Set([
25+
...options.sfc.template?.ast?.components.flatMap(c => [camelize(c), capitalize(camelize(c))]) ?? [],
26+
...options.templateCodegen?.accessExternalVariables.keys() ?? [],
27+
...templateCodegenCtx.accessExternalVariables.keys(),
28+
]);
29+
for (const varName of ctx.bindingNames) {
30+
if (!templateUsageVars.has(varName)) {
31+
continue;
4432
}
33+
const token = Symbol(varName.length);
34+
yield ['', undefined, 0, { __linkedToken: token }];
35+
yield `${varName}: ${varName} as typeof `;
36+
yield ['', undefined, 0, { __linkedToken: token }];
37+
yield `${varName},${newLine}`;
4538
}
4639
yield `}${endOfLine}`; // return {
4740
yield `},${newLine}`; // setup() {

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Code } from '../../types';
2-
import { hyphenateTag } from '../../utils/shared';
32
import { codeFeatures } from '../codeFeatures';
43
import { generateStyleModules } from '../style/modules';
54
import { generateStyleScopedClasses } from '../style/scopedClasses';
@@ -137,27 +136,3 @@ function* generateCssVars(options: ScriptCodegenOptions, ctx: TemplateCodegenCon
137136
}
138137
yield `// CSS variable injection end ${newLine}`;
139138
}
140-
141-
export function getTemplateUsageVars(options: ScriptCodegenOptions, ctx: ScriptCodegenContext) {
142-
const usageVars = new Set<string>();
143-
const components = new Set(options.sfc.template?.ast?.components);
144-
145-
if (options.templateCodegen) {
146-
// fix import components unused report
147-
for (const varName of ctx.bindingNames) {
148-
if (components.has(varName) || components.has(hyphenateTag(varName))) {
149-
usageVars.add(varName);
150-
}
151-
}
152-
for (const component of components) {
153-
if (component.includes('.')) {
154-
usageVars.add(component.split('.')[0]);
155-
}
156-
}
157-
for (const [varName] of options.templateCodegen.accessExternalVariables) {
158-
usageVars.add(varName);
159-
}
160-
}
161-
162-
return usageVars;
163-
}

0 commit comments

Comments
 (0)