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): consistent interpolation behavior of shorthand binding #4975

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 26 additions & 11 deletions packages/language-core/lib/codegen/template/elementProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,8 @@ function* generatePropExp(
isShorthand: boolean,
enableCodeFeatures: boolean
): Generator<Code> {
if (isShorthand && features.completion) {
features = {
...features,
completion: undefined,
};
if (isShorthand) {
features.completion = undefined;
}
if (exp && exp.constType !== CompilerDOM.ConstantTypes.CAN_STRINGIFY) { // style='z-index: 2' will compile to {'z-index':'2'}
if (!isShorthand) { // vue 3.4+
Expand All @@ -320,19 +317,37 @@ function* generatePropExp(
'(',
')'
);
} else {
}
else {
const propVariableName = camelize(exp.loc.source);

if (variableNameRegex.test(propVariableName)) {
if (!ctx.hasLocalVariable(propVariableName)) {
ctx.accessExternalVariable(propVariableName, exp.loc.start.offset);
yield `__VLS_ctx.`;
}
yield* generateCamelized(
const isDestructuredProp = options.destructuredPropNames?.has(propVariableName) ?? false;
const isTemplateRef = options.templateRefNames?.has(propVariableName) ?? false;

const codes = generateCamelized(
exp.loc.source,
exp.loc.start.offset,
features
);

if (ctx.hasLocalVariable(propVariableName) || isDestructuredProp) {
yield* codes;
}
else {
ctx.accessExternalVariable(propVariableName, exp.loc.start.offset);

if (isTemplateRef) {
yield `__VLS_unref(`;
yield* codes;
yield `)`;
}
else {
yield `__VLS_ctx.`;
yield* codes;
}
}

if (enableCodeFeatures) {
ctx.inlayHints.push(createVBindShorthandInlayHintInfo(prop.loc, propVariableName));
}
Expand Down
5 changes: 5 additions & 0 deletions test-workspace/tsc/passedFixtures/vue3/#4973/comp.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script setup lang="ts">
defineProps<{
condition: 'B'
}>();
</script>
10 changes: 10 additions & 0 deletions test-workspace/tsc/passedFixtures/vue3/#4973/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script setup lang="ts">
import Comp from './comp.vue';

let condition!: 'A' | 'B';
</script>

<template>
<div v-if="condition === 'A'"></div>
<Comp v-else :condition />
</template>