Skip to content

Commit

Permalink
fix(language-core): support parse method to access ctx var in object (v…
Browse files Browse the repository at this point in the history
  • Loading branch information
linghaoSu authored Jul 26, 2024
1 parent 9a12092 commit dc6f943
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
49 changes: 30 additions & 19 deletions packages/language-core/lib/codegen/template/interpolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions test-workspace/tsc/vue3/#4604/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<template>
<div v-bind="{
onClick() {
exactType(msg, {} as string);
}
}"></div>
</template>

<script setup lang="ts">
import { exactType } from 'tsc/shared';
import { ref } from 'vue';
const msg = ref('Hello Vue 3 + TypeScript + Vite');
</script>

0 comments on commit dc6f943

Please sign in to comment.