feat(sfc): support $shallowRef ref sugar
This commit is contained in:
@@ -65,6 +65,7 @@ const DEFINE_EXPOSE = 'defineExpose'
|
||||
const WITH_DEFAULTS = 'withDefaults'
|
||||
|
||||
const $REF = `$ref`
|
||||
const $SHALLOW_REF = '$shallowRef'
|
||||
const $COMPUTED = `$computed`
|
||||
const $FROM_REFS = `$fromRefs`
|
||||
const $RAW = `$raw`
|
||||
@@ -531,7 +532,12 @@ export function compileScript(
|
||||
}
|
||||
|
||||
function isRefSugarCall(callee: string) {
|
||||
return callee === $REF || callee === $COMPUTED || callee === $FROM_REFS
|
||||
return (
|
||||
callee === $REF ||
|
||||
callee === $COMPUTED ||
|
||||
callee === $FROM_REFS ||
|
||||
callee === $SHALLOW_REF
|
||||
)
|
||||
}
|
||||
|
||||
function processRefSugar(
|
||||
@@ -558,24 +564,28 @@ export function compileScript(
|
||||
|
||||
const callee = (decl.init.callee as Identifier).name
|
||||
const start = decl.init.start! + startOffset
|
||||
if (callee === $REF) {
|
||||
if (callee === $REF || callee === $SHALLOW_REF) {
|
||||
if (statement.kind !== 'let') {
|
||||
error(`${$REF}() bindings can only be declared with let.`, decl)
|
||||
error(`${callee}() bindings can only be declared with let.`, decl)
|
||||
}
|
||||
if (decl.id.type !== 'Identifier') {
|
||||
error(
|
||||
`${$REF}() bindings cannot be used with destructuring. ` +
|
||||
`${callee}() bindings cannot be used with destructuring. ` +
|
||||
`If you are trying to destructure from an object of refs, ` +
|
||||
`use \`let { x } = $fromRefs(obj)\`.`,
|
||||
decl.id
|
||||
)
|
||||
}
|
||||
registerRefBinding(decl.id)
|
||||
s.overwrite(start, start + $REF.length, helper('ref'))
|
||||
s.overwrite(
|
||||
start,
|
||||
start + callee.length,
|
||||
helper(callee === $REF ? 'ref' : 'shallowRef')
|
||||
)
|
||||
} else if (callee === $COMPUTED) {
|
||||
if (decl.id.type !== 'Identifier') {
|
||||
error(
|
||||
`${$COMPUTED}() bindings cannot be used with destructuring.`,
|
||||
`${callee}() bindings cannot be used with destructuring.`,
|
||||
decl.id
|
||||
)
|
||||
}
|
||||
@@ -584,7 +594,7 @@ export function compileScript(
|
||||
} else if (callee === $FROM_REFS) {
|
||||
if (!decl.id.type.endsWith('Pattern')) {
|
||||
error(
|
||||
`${$FROM_REFS}() declaration must be used with destructure patterns.`,
|
||||
`${callee}() declaration must be used with destructure patterns.`,
|
||||
decl
|
||||
)
|
||||
}
|
||||
@@ -1124,10 +1134,7 @@ export function compileScript(
|
||||
return false // skip walk
|
||||
} else if (
|
||||
parent &&
|
||||
isCallOf(
|
||||
node,
|
||||
id => id === $REF || id === $FROM_REFS || id === $COMPUTED
|
||||
) &&
|
||||
isCallOf(node, isRefSugarCall) &&
|
||||
(parent.type !== 'VariableDeclarator' || node !== parent.init)
|
||||
) {
|
||||
error(
|
||||
|
||||
Reference in New Issue
Block a user