Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('context.attrs', async () => {
const toggle = ref(true)
const Parent = {
render: () => h(Child, toggle.value ? { id: 'foo' } : { class: 'baz' })
}
const Child = {
// explicit empty props declaration
// puts everything received in attrs
// disable implicit fallthrough
inheritAttrs: false,
props: {},
setup(props: any, { attrs }: any) {
return () => h('div', attrs)
}
}
setup() {
return {
// ref should auto-unwrap
ref: ref('foo'),
// object exposed as-is
object: reactive({ msg: 'bar' }),
// primitive value exposed as-is
value: 'baz'
}
},
render() {
setup() {
return {
// ref should auto-unwrap
ref: ref('foo'),
// object exposed as-is
object: reactive({ msg: 'bar' }),
// primitive value exposed as-is
value: 'baz'
}
},
render() {
function createComment(text: string): TestComment {
const node: TestComment = {
id: nodeId++,
type: NodeTypes.COMMENT,
text,
parentNode: null
}
logNodeOp({
type: NodeOpTypes.CREATE,
nodeType: NodeTypes.COMMENT,
targetNode: node,
text
})
// avoid test nodes from being observed
markNonReactive(node)
return node
}
id: nodeId++,
type: NodeTypes.ELEMENT,
tag,
children: [],
props: {},
parentNode: null,
eventListeners: null
}
logNodeOp({
type: NodeOpTypes.CREATE,
nodeType: NodeTypes.ELEMENT,
targetNode: node,
tag
})
// avoid test nodes from being observed
markNonReactive(node)
return node
}
function createText(text: string): TestText {
const node: TestText = {
id: nodeId++,
type: NodeTypes.TEXT,
text,
parentNode: null
}
logNodeOp({
type: NodeOpTypes.CREATE,
nodeType: NodeTypes.TEXT,
targetNode: node,
text
})
// avoid test nodes from being observed
markNonReactive(node)
return node
}
trace
]
)
} else {
const warnArgs = [`[Vue warn]: ${msg}`, ...args]
if (
trace.length &&
// avoid spamming console during tests
!__TEST__
) {
warnArgs.push(`\n`, ...formatTrace(trace))
}
console.warn(...warnArgs)
}
resumeTracking()
}
cb: WatchCallback | null,
{ lazy, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ
): StopHandle {
const instance = currentInstance
const suspense = currentSuspense
let getter: () => any
if (isArray(source)) {
getter = () =>
source.map(
s =>
isRef(s)
? s.value
: callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER)
)
} else if (isRef(source)) {
getter = () => source.value
} else if (cb) {
// getter with cb
getter = () =>
callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER)
} else {
// no cb -> simple effect
getter = () => {
if (instance && instance.isUnmounted) {
return
}
if (cleanup) {
cleanup()
}
return callWithErrorHandling(
source,
oldRef: string | Function | Ref | null,
parent: ComponentInternalInstance,
value: HostNode | ComponentPublicInstance | null
) {
const refs = parent.refs === EMPTY_OBJ ? (parent.refs = {}) : parent.refs
const renderContext = toRaw(parent.renderContext)
// unset old ref
if (oldRef !== null && oldRef !== ref) {
if (isString(oldRef)) {
refs[oldRef] = null
const oldSetupRef = renderContext[oldRef]
if (isRef(oldSetupRef)) {
oldSetupRef.value = null
}
} else if (isRef(oldRef)) {
oldRef.value = null
}
}
if (isString(ref)) {
const setupRef = renderContext[ref]
if (isRef(setupRef)) {
setupRef.value = value
}
refs[ref] = value
} else if (isRef(ref)) {
ref.value = value
} else if (isFunction(ref)) {
callWithErrorHandling(ref, parent, ErrorCodes.FUNCTION_REF, [value, refs])
} else if (__DEV__) {
warn('Invalid template ref type:', value, `(${typeof value})`)
function setRef(
ref: string | Function | Ref,
oldRef: string | Function | Ref | null,
parent: ComponentInternalInstance,
value: HostNode | ComponentPublicInstance | null
) {
const refs = parent.refs === EMPTY_OBJ ? (parent.refs = {}) : parent.refs
const renderContext = toRaw(parent.renderContext)
// unset old ref
if (oldRef !== null && oldRef !== ref) {
if (isString(oldRef)) {
refs[oldRef] = null
const oldSetupRef = renderContext[oldRef]
if (isRef(oldSetupRef)) {
oldSetupRef.value = null
}
} else if (isRef(oldRef)) {
oldRef.value = null
}
}
if (isString(ref)) {
const setupRef = renderContext[ref]
if (isRef(setupRef)) {
setupRef.value = value
}
refs[ref] = value
} else if (isRef(ref)) {
ref.value = value
} else if (isFunction(ref)) {