Skip to content

compiler-sfc: Reactive Props Destructure rewrites local references to var declaration #14932

Description

@guyutongxue

Vue version

3.6.0-beta.14

Link to minimal reproduction

https://play.vuejs.org/#eNqFUk1PwzAM/StRLtuk0YI2cRjdEKAd4AATcIyEutYtGWkSJWmZVPW/46TsQ3zeYr9n+9kvLb3SOmpqoDOa2Mxw7YgFV+sFk7zSyjhyoypNCqMqMohiH3j64ILJJO4LkIqBg0qL1AFGhCShKMZ3Eh8BdEydzZQseBltrJI4tPV0RjPkcwHmQTuupGV0RgLisVQI9X4Xcs7UMN7ls1fI3n7Ib+zW5xhdGbBgGmB0j7nUlOB6ePl0D1t878FK5bVA9h/gI1glaq+xp13XMkfZR7yg9jacjsvy2S63DqTdLeWFemYX+IziKf2pflv9IHcSTUMdkx1ecWfDV9eISGU5xzpsc3CwJQYK0vUmhpmMon9ohPVgoRSZY14ZXnKZCkaROyc5FFzCyihtkyAIeZczYp3BvVB+txiO9l3WqcESHJP0eI8Vtcz8UoRL7oajfq/P5Rqs2E3G2/KCQx5khSWJ7xg1qagBGchDANN9n3/+Xs6bRdsGSV2XxD78/g9fGjDeFbzgJDqPTk/W4NLobEq7D4b3Bc4=

Steps to reproduce

For following <script setup>:

const { foo = "a" } = defineProps<{ foo?: string; }>();
const bar = ref<string>();
function init() {
  {
    var foo = "b";    // <- (1)
  }
  bar.value = foo;    // <- (2)
}
init();

What is expected?

The foo at (2) is referencing var declaration at (1), although out of (1)'s definition lexical scope, since it is a var-declaration which could be accessible in the whole init function, that (2) should not be rewrite to __props.foo.

What is actually happening?

compiler-sfc rewrite foo at (2) to __props.foo:

const bar = ref();
function init() {
  {
    var foo = "b";
  }
  bar.value = __props.foo;
}
init();

and this behavior might be a little bit surprising (and differs from Language Tools' hint).

System Info

Details
System:
    OS: Windows 11 10.0.26200
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 5.10 GB / 15.82 GB
  Binaries:
    Node: 26.1.0 - C:\Users\guyutongxue\AppData\Local\fnm_multishells\18540_1780671611255\node.EXE
    Yarn: 1.22.22 - C:\Users\guyutongxue\AppData\Local\fnm_multishells\18540_1780671611255\yarn.CMD
    npm: 11.13.0 - C:\Users\guyutongxue\AppData\Local\fnm_multishells\18540_1780671611255\npm.CMD
    pnpm: 11.1.2 - C:\Users\guyutongxue\AppData\Local\fnm_multishells\18540_1780671611255\pnpm.CMD
    bun: 1.3.12 - C:\Users\guyutongxue\.bun\bin\bun.EXE
  Browsers:
    Edge: Chromium (140.0.3485.66)

Any additional comments?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions