diff --git a/packages/language-core/lib/codegen/template/interpolation.ts b/packages/language-core/lib/codegen/template/interpolation.ts index 7367549734..3e2760f576 100644 --- a/packages/language-core/lib/codegen/template/interpolation.ts +++ b/packages/language-core/lib/codegen/template/interpolation.ts @@ -177,25 +177,7 @@ function walkIdentifiers( } } else if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) { - - const functionArgs: string[] = []; - - for (const param of node.parameters) { - collectVars(ts, param.name, ast, functionArgs); - if (param.type) { - walkIdentifiers(ts, param.type, ast, cb, ctx, blockVars, false); - } - } - - for (const varName of functionArgs) { - ctx.addLocalVariable(varName); - } - - walkIdentifiers(ts, node.body, ast, cb, ctx, blockVars, false); - - for (const varName of functionArgs) { - ctx.removeLocalVariable(varName); - } + processFunction(ts, node, ast, cb, ctx); } else if (ts.isObjectLiteralExpression(node)) { for (const prop of node.properties) { @@ -215,6 +197,10 @@ function walkIdentifiers( // TODO: cannot report "Spread types may only be created from object types.ts(2698)" walkIdentifiers(ts, prop.expression, ast, cb, ctx, blockVars, false); } + // fix https://github.com/vuejs/language-tools/issues/4604 + else if (ts.isFunctionLike(prop) && prop.body) { + processFunction(ts, prop, ast, cb, ctx); + } } } else if (ts.isTypeReferenceNode(node)) { @@ -242,6 +228,31 @@ function walkIdentifiers( } } +function processFunction( + ts: typeof import('typescript'), + node: ts.ArrowFunction | ts.FunctionExpression | ts.AccessorDeclaration | ts.MethodDeclaration, + ast: ts.SourceFile, + cb: (varNode: ts.Identifier, isShorthand: boolean) => void, + ctx: TemplateCodegenContext +) { + const functionArgs: string[] = []; + for (const param of node.parameters) { + collectVars(ts, param.name, ast, functionArgs); + if (param.type) { + walkIdentifiers(ts, param.type, ast, cb, ctx); + } + } + for (const varName of functionArgs) { + ctx.addLocalVariable(varName); + } + if (node.body) { + walkIdentifiers(ts, node.body, ast, cb, ctx); + } + for (const varName of functionArgs) { + ctx.removeLocalVariable(varName); + } +} + function walkIdentifiersInTypeReference( ts: typeof import('typescript'), node: ts.Node, diff --git a/test-workspace/tsc/vue3/#4604/main.vue b/test-workspace/tsc/vue3/#4604/main.vue new file mode 100644 index 0000000000..ca7cb40861 --- /dev/null +++ b/test-workspace/tsc/vue3/#4604/main.vue @@ -0,0 +1,14 @@ + + +