Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(language-core): avoid redundant increment of block variable depth #5511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions packages/language-core/lib/codegen/template/interpolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type * as ts from 'typescript';
import type { Code, VueCodeInformation } from '../../types';
import { getNodeText, getStartEnd } from '../../utils/shared';
import type { ScriptCodegenOptions } from '../script';
import { collectVars, createTsAst, identifierRegex } from '../utils';
import { collectBindingNames, createTsAst, identifierRegex } from '../utils';
import type { TemplateCodegenContext } from './context';
import type { TemplateCodegenOptions } from './index';

Expand Down Expand Up @@ -126,7 +126,7 @@ function* forEachInterpolationSegment(
});
}
};
ts.forEachChild(ast, node => walkIdentifiers(ts, node, ast, varCb, ctx));
ts.forEachChild(ast, node => walkIdentifiers(ts, node, ast, varCb, ctx, [], true));
}

ctxVars = ctxVars.sort((a, b) => a.offset - b.offset);
Expand Down Expand Up @@ -198,8 +198,8 @@ function walkIdentifiers(
ast: ts.SourceFile,
cb: (varNode: ts.Identifier, isShorthand: boolean) => void,
ctx: TemplateCodegenContext,
blockVars: string[] = [],
isRoot: boolean = true,
blockVars: string[],
isRoot: boolean = false,
) {
if (ts.isIdentifier(node)) {
cb(node, false);
Expand All @@ -208,56 +208,57 @@ function walkIdentifiers(
cb(node.name, true);
}
else if (ts.isPropertyAccessExpression(node)) {
walkIdentifiers(ts, node.expression, ast, cb, ctx, blockVars, false);
walkIdentifiers(ts, node.expression, ast, cb, ctx, blockVars);
}
else if (ts.isVariableDeclaration(node)) {
collectVars(ts, node.name, ast, blockVars);
const bindingNames = collectBindingNames(ts, node.name, ast);

for (const varName of blockVars) {
ctx.addLocalVariable(varName);
for (const name of bindingNames) {
ctx.addLocalVariable(name);
blockVars.push(name);
}

if (node.initializer) {
walkIdentifiers(ts, node.initializer, ast, cb, ctx, blockVars, false);
walkIdentifiers(ts, node.initializer, ast, cb, ctx, blockVars);
}
}
else if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
processFunction(ts, node, ast, cb, ctx);
walkIdentifiersInFunction(ts, node, ast, cb, ctx);
}
else if (ts.isObjectLiteralExpression(node)) {
for (const prop of node.properties) {
if (ts.isPropertyAssignment(prop)) {
// fix https://github.com/vuejs/language-tools/issues/1176
if (ts.isComputedPropertyName(prop.name)) {
walkIdentifiers(ts, prop.name.expression, ast, cb, ctx, blockVars, false);
walkIdentifiers(ts, prop.name.expression, ast, cb, ctx, blockVars);
}
walkIdentifiers(ts, prop.initializer, ast, cb, ctx, blockVars, false);
walkIdentifiers(ts, prop.initializer, ast, cb, ctx, blockVars);
}
// fix https://github.com/vuejs/language-tools/issues/1156
else if (ts.isShorthandPropertyAssignment(prop)) {
walkIdentifiers(ts, prop, ast, cb, ctx, blockVars, false);
walkIdentifiers(ts, prop, ast, cb, ctx, blockVars);
}
// fix https://github.com/vuejs/language-tools/issues/1148#issuecomment-1094378126
else if (ts.isSpreadAssignment(prop)) {
// TODO: cannot report "Spread types may only be created from object types.ts(2698)"
walkIdentifiers(ts, prop.expression, ast, cb, ctx, blockVars, false);
walkIdentifiers(ts, prop.expression, ast, cb, ctx, blockVars);
}
// fix https://github.com/vuejs/language-tools/issues/4604
else if (ts.isFunctionLike(prop) && prop.body) {
processFunction(ts, prop, ast, cb, ctx);
walkIdentifiersInFunction(ts, prop, ast, cb, ctx);
}
}
}
// fix https://github.com/vuejs/language-tools/issues/1422
else if (ts.isTypeNode(node)) {
// fix https://github.com/vuejs/language-tools/issues/1422
walkIdentifiersInTypeNode(ts, node, cb);
}
else {
const _blockVars = blockVars;
if (ts.isBlock(node)) {
blockVars = [];
}
ts.forEachChild(node, node => walkIdentifiers(ts, node, ast, cb, ctx, blockVars, false));
ts.forEachChild(node, node => walkIdentifiers(ts, node, ast, cb, ctx, blockVars));
if (ts.isBlock(node)) {
for (const varName of blockVars) {
ctx.removeLocalVariable(varName);
Expand All @@ -273,7 +274,7 @@ function walkIdentifiers(
}
}

function processFunction(
function walkIdentifiersInFunction(
ts: typeof import('typescript'),
node: ts.ArrowFunction | ts.FunctionExpression | ts.AccessorDeclaration | ts.MethodDeclaration,
ast: ts.SourceFile,
Expand All @@ -282,16 +283,16 @@ function processFunction(
) {
const functionArgs: string[] = [];
for (const param of node.parameters) {
collectVars(ts, param.name, ast, functionArgs);
functionArgs.push(...collectBindingNames(ts, param.name, ast));
if (param.type) {
walkIdentifiers(ts, param.type, ast, cb, ctx);
walkIdentifiersInTypeNode(ts, param.type, cb);
}
}
for (const varName of functionArgs) {
ctx.addLocalVariable(varName);
}
if (node.body) {
walkIdentifiers(ts, node.body, ast, cb, ctx);
walkIdentifiers(ts, node.body, ast, cb, ctx, [], true);
}
for (const varName of functionArgs) {
ctx.removeLocalVariable(varName);
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/codegen/template/vFor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as CompilerDOM from '@vue/compiler-dom';
import type { Code } from '../../types';
import { collectVars, createTsAst, endOfLine, newLine } from '../utils';
import { collectBindingNames, createTsAst, endOfLine, newLine } from '../utils';
import type { TemplateCodegenContext } from './context';
import { generateElementChildren } from './elementChildren';
import type { TemplateCodegenOptions } from './index';
Expand All @@ -18,7 +18,7 @@ export function* generateVFor(
yield `for (const [`;
if (leftExpressionRange && leftExpressionText) {
const collectAst = createTsAst(options.ts, ctx.inlineTsAsts, `const [${leftExpressionText}]`);
collectVars(options.ts, collectAst, collectAst, forBlockVars);
forBlockVars.push(...collectBindingNames(options.ts, collectAst, collectAst));
yield [
leftExpressionText,
'template',
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/codegen/template/vSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as CompilerDOM from '@vue/compiler-dom';
import type * as ts from 'typescript';
import type { Code } from '../../types';
import { getStartEnd } from '../../utils/shared';
import { collectVars, createTsAst, endOfLine, newLine } from '../utils';
import { collectBindingNames, createTsAst, endOfLine, newLine } from '../utils';
import { wrapWith } from '../utils/wrapWith';
import type { TemplateCodegenContext } from './context';
import { generateElementChildren } from './elementChildren';
Expand Down Expand Up @@ -64,7 +64,7 @@ export function* generateVSlot(

if (slotDir?.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
const slotAst = createTsAst(options.ts, ctx.inlineTsAsts, `(${slotDir.exp.content}) => {}`);
collectVars(options.ts, slotAst, slotAst, slotBlockVars);
slotBlockVars.push(...collectBindingNames(options.ts, slotAst, slotAst));
yield* generateSlotParameters(options, ctx, slotAst, slotDir.exp, slotVar);
}

Expand Down
9 changes: 2 additions & 7 deletions packages/language-core/lib/codegen/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ export const endOfLine = `;${newLine}`;
export const combineLastMapping: VueCodeInformation = { __combineOffset: 1 };
export const identifierRegex = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;

export function collectVars(
export function collectBindingNames(
ts: typeof import('typescript'),
node: ts.Node,
ast: ts.SourceFile,
results: string[] = [],
) {
const identifiers = collectIdentifiers(ts, node, []);
for (const { id } of identifiers) {
results.push(getNodeText(ts, id, ast));
}
return results;
return collectIdentifiers(ts, node).map(({ id }) => getNodeText(ts, id, ast));
}

export function collectIdentifiers(
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/parsers/scriptSetupRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export function parseScriptSetupRanges(
};
if (ts.isVariableDeclaration(parent) && ts.isObjectBindingPattern(parent.name)) {
defineProps.destructured = new Map();
const identifiers = collectIdentifiers(ts, parent.name, []);
const identifiers = collectIdentifiers(ts, parent.name);
for (const { id, isRest, initializer } of identifiers) {
const name = _getNodeText(id);
if (isRest) {
Expand Down
Loading