Skip to content
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

fix(language-core): support parse method to access ctx var in object #4609

Merged
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
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>
Loading