-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
feat(compiler-sfc): withDefaults
supports the use of local variables
#8592
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaults
should not be hoisted if it's used by other assignments.
https://deploy-preview-8592--vue-sfc-playground.netlify.app/#eNo9jkEKgzAQRa8yZKVYtGuJQqEH6AFmY+3YCjoJyaRdhNy9oVZ38/mPeT+qi7X1O5Bqlfajm62AJwkWloGfHSrxqHrk0bAXeNA0hEU8dBBhMqaFMyTkzyyv678qMjMz3ZyxXkdk2DgO650ccuqL8nT8KZH3s85YVSHrZluRnTkIrXYZhHICiD8npGzUzdGo9AW/zUYh
Ok, with this situation in mind, I think there might be a few things to confirm:
|
❌ Deploy Preview for vue-sfc-playground failed.
|
❌ Deploy Preview for vue-next-template-explorer failed.
|
@zqran No, if there's any reference not inside of |
There are two different cases depending on the operation:
For the
// hoist
const defaults = { baz: false }
const props = withDefaults(defineProps<{
baz: boolean
}>(), defaults)
// If there are still the following operations:
// hoist
console.log(defaults)
someFunc(defaults)
// hoist
let defaults = { baz: false }
const props = withDefaults(defineProps<{
baz: boolean
}>(), defaults)
// If there are still the following operations:
// need to confirm
console.log(defaults) // Special case? need to be excluded?
someFunc(defaults) These different rules(scenarios that need to be confirmed above) may confuse users. Therefore, since it is used as the default value, it is agreed that it cannot be modified. Then can we agree: If you want to use local variables as parameters of |
Even tough it's const obj = { a : 1 }
obj.a = 2 I think the better way for avoiding confusing is that local variables as parameters of const defaults = {}
withDefaults(defineProps(), defaults)
// defaults is never be used.
// console.log(defaults) // ❌
// defaults.add() // ❌
// someFunc(defaults) // ❌ |
It is worth mentioning that hoist will take effect when using reference: // foo.ts
export const defaults = { baz: false } import { defaults } from './foo'
const props = withDefaults(defineProps<{bar: boolean}>(), defaults)
// defaults is used:
console.log(defaults)
defaults.baz = true If so, it may behave inconsistently with local variables. |
@zqran Cause exported variable is top level and always inited once, but in |
Thank you for your explanation, I will modify it according to the above content. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should hoist the first one statement instead of the second one
https://deploy-preview-8592--vue-sfc-playground.netlify.app/#eNqNjzELwjAQhf/KkaUtiKJjaQuCzroKt8Sa1kh7CcmlDiX/3Uh1cfK47b3vPd4s9taup6BEKSrfOm0ZvOJgYZDU1yjYo2iQ9GiNY5jBqQ4idM6MkCUsQ0JqDXmGm+pkGNifrg/VMtTJTHJUJaA4TpLgYsIOBUSkGQnS/Y1tFyz9glhnrE/Op+b74YPnKUeTOr+l6hvh2WnqITZ5sfopKpCqzTK4EfEFQEBcJQ==
node: Statement | ||
needHoist?: boolean | ||
} | ||
> = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
> = {} | |
> = Object.create(null) |
I think it's very hard to figure out which variable should be hoisted without scope stack (like we did in Reactivity Transform package). But if we introduce scope stack, it will greatly increase the complexity of the SFC compiler. Maybe we need a complete refactor, I'm considering. |
It's awesome! Looking forward to its progress. |
Hi, @zqran is this PR planning to merge? Looks like i need it very much) |
close: #8517
Usage: