Skip to content

Commit dc6f943

Browse files
authored
fix(language-core): support parse method to access ctx var in object (#4609)
1 parent 9a12092 commit dc6f943

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

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

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -177,25 +177,7 @@ function walkIdentifiers(
177177
}
178178
}
179179
else if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
180-
181-
const functionArgs: string[] = [];
182-
183-
for (const param of node.parameters) {
184-
collectVars(ts, param.name, ast, functionArgs);
185-
if (param.type) {
186-
walkIdentifiers(ts, param.type, ast, cb, ctx, blockVars, false);
187-
}
188-
}
189-
190-
for (const varName of functionArgs) {
191-
ctx.addLocalVariable(varName);
192-
}
193-
194-
walkIdentifiers(ts, node.body, ast, cb, ctx, blockVars, false);
195-
196-
for (const varName of functionArgs) {
197-
ctx.removeLocalVariable(varName);
198-
}
180+
processFunction(ts, node, ast, cb, ctx);
199181
}
200182
else if (ts.isObjectLiteralExpression(node)) {
201183
for (const prop of node.properties) {
@@ -215,6 +197,10 @@ function walkIdentifiers(
215197
// TODO: cannot report "Spread types may only be created from object types.ts(2698)"
216198
walkIdentifiers(ts, prop.expression, ast, cb, ctx, blockVars, false);
217199
}
200+
// fix https://github.com/vuejs/language-tools/issues/4604
201+
else if (ts.isFunctionLike(prop) && prop.body) {
202+
processFunction(ts, prop, ast, cb, ctx);
203+
}
218204
}
219205
}
220206
else if (ts.isTypeReferenceNode(node)) {
@@ -242,6 +228,31 @@ function walkIdentifiers(
242228
}
243229
}
244230

231+
function processFunction(
232+
ts: typeof import('typescript'),
233+
node: ts.ArrowFunction | ts.FunctionExpression | ts.AccessorDeclaration | ts.MethodDeclaration,
234+
ast: ts.SourceFile,
235+
cb: (varNode: ts.Identifier, isShorthand: boolean) => void,
236+
ctx: TemplateCodegenContext
237+
) {
238+
const functionArgs: string[] = [];
239+
for (const param of node.parameters) {
240+
collectVars(ts, param.name, ast, functionArgs);
241+
if (param.type) {
242+
walkIdentifiers(ts, param.type, ast, cb, ctx);
243+
}
244+
}
245+
for (const varName of functionArgs) {
246+
ctx.addLocalVariable(varName);
247+
}
248+
if (node.body) {
249+
walkIdentifiers(ts, node.body, ast, cb, ctx);
250+
}
251+
for (const varName of functionArgs) {
252+
ctx.removeLocalVariable(varName);
253+
}
254+
}
255+
245256
function walkIdentifiersInTypeReference(
246257
ts: typeof import('typescript'),
247258
node: ts.Node,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<template>
2+
<div v-bind="{
3+
onClick() {
4+
exactType(msg, {} as string);
5+
}
6+
}"></div>
7+
</template>
8+
9+
<script setup lang="ts">
10+
import { exactType } from 'tsc/shared';
11+
import { ref } from 'vue';
12+
13+
const msg = ref('Hello Vue 3 + TypeScript + Vite');
14+
</script>

0 commit comments

Comments
 (0)