vue3-yuanma/packages/runtime-core/src/warning.ts

156 lines
4.3 KiB
TypeScript
Raw Normal View History

2019-08-30 22:36:30 +08:00
import { VNode } from './vnode'
2019-10-08 21:26:09 +08:00
import { Data, ComponentInternalInstance, Component } from './component'
import { isString, isFunction } from '@vue/shared'
2019-11-03 11:21:02 +08:00
import { toRaw, isRef, pauseTracking, resumeTracking } from '@vue/reactivity'
import { callWithErrorHandling, ErrorCodes } from './errorHandling'
2019-08-30 22:36:30 +08:00
2019-10-08 21:26:09 +08:00
type ComponentVNode = VNode & {
type: Component
}
const stack: VNode[] = []
2019-08-30 22:36:30 +08:00
type TraceEntry = {
2019-10-08 21:26:09 +08:00
vnode: ComponentVNode
2019-08-30 22:36:30 +08:00
recurseCount: number
}
type ComponentTraceStack = TraceEntry[]
2019-11-02 11:04:28 +08:00
export function pushWarningContext(vnode: VNode) {
2019-08-30 22:36:30 +08:00
stack.push(vnode)
}
export function popWarningContext() {
stack.pop()
}
export function warn(msg: string, ...args: any[]) {
2019-11-03 11:21:02 +08:00
// avoid props formatting or warn handler tracking deps that might be mutated
// during patch, leading to infinite recursion.
pauseTracking()
2019-09-04 06:11:04 +08:00
const instance = stack.length ? stack[stack.length - 1].component : null
const appWarnHandler = instance && instance.appContext.config.warnHandler
const trace = getComponentTrace()
if (appWarnHandler) {
callWithErrorHandling(
appWarnHandler,
instance,
ErrorCodes.APP_WARN_HANDLER,
[
msg + args.join(''),
instance && instance.renderProxy,
2019-11-03 11:21:02 +08:00
trace
.map(({ vnode }) => `at <${formatComponentName(vnode)}>`)
.join('\n'),
trace
]
2019-09-04 06:11:04 +08:00
)
2019-08-30 22:36:30 +08:00
} else {
2019-11-03 11:21:02 +08:00
const warnArgs = [`[Vue warn]: ${msg}`, ...args]
if (
trace.length &&
// avoid spamming console during tests
2019-11-05 00:24:22 +08:00
!__TEST__
2019-11-03 11:21:02 +08:00
) {
warnArgs.push(`\n`, ...formatTrace(trace))
}
console.warn(...warnArgs)
2019-08-30 22:36:30 +08:00
}
2019-11-03 11:21:02 +08:00
resumeTracking()
2019-08-30 22:36:30 +08:00
}
function getComponentTrace(): ComponentTraceStack {
let currentVNode: VNode | null = stack[stack.length - 1]
if (!currentVNode) {
return []
}
// we can't just use the stack because it will be incomplete during updates
// that did not start from the root. Re-construct the parent chain using
// instance parent pointers.
2019-10-05 22:48:54 +08:00
const normalizedStack: ComponentTraceStack = []
2019-08-30 22:36:30 +08:00
while (currentVNode) {
2019-10-05 22:48:54 +08:00
const last = normalizedStack[0]
2019-08-30 22:36:30 +08:00
if (last && last.vnode === currentVNode) {
last.recurseCount++
} else {
2019-10-05 22:48:54 +08:00
normalizedStack.push({
2019-11-02 11:04:28 +08:00
vnode: currentVNode as ComponentVNode,
2019-08-30 22:36:30 +08:00
recurseCount: 0
})
}
2019-10-05 22:09:34 +08:00
const parentInstance: ComponentInternalInstance | null = currentVNode.component!
2019-08-30 22:36:30 +08:00
.parent
currentVNode = parentInstance && parentInstance.vnode
}
2019-10-05 22:48:54 +08:00
return normalizedStack
2019-08-30 22:36:30 +08:00
}
2019-11-03 11:21:02 +08:00
function formatTrace(trace: ComponentTraceStack): any[] {
const logs: any[] = []
2019-09-04 06:11:04 +08:00
trace.forEach((entry, i) => {
2019-11-03 11:21:02 +08:00
logs.push(...(i === 0 ? [] : [`\n`]), ...formatTraceEntry(entry))
2019-09-04 06:11:04 +08:00
})
return logs
}
2019-11-03 11:21:02 +08:00
function formatTraceEntry({ vnode, recurseCount }: TraceEntry): any[] {
2019-08-30 22:36:30 +08:00
const postfix =
recurseCount > 0 ? `... (${recurseCount} recursive calls)` : ``
2019-11-03 11:21:02 +08:00
const open = ` at <${formatComponentName(vnode)}`
2019-08-30 22:36:30 +08:00
const close = `>` + postfix
2019-10-05 22:09:34 +08:00
const rootLabel = vnode.component!.parent == null ? `(Root)` : ``
2019-08-30 22:36:30 +08:00
return vnode.props
? [open, ...formatProps(vnode.props), close, rootLabel]
: [open + close, rootLabel]
}
const classifyRE = /(?:^|[-_])(\w)/g
const classify = (str: string): string =>
str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '')
2019-10-08 21:26:09 +08:00
function formatComponentName(vnode: ComponentVNode, file?: string): string {
const Component = vnode.type as Component
2019-11-03 11:21:02 +08:00
let name = isFunction(Component)
? Component.displayName || Component.name
: Component.name
2019-08-30 22:36:30 +08:00
if (!name && file) {
const match = file.match(/([^/\\]+)\.vue$/)
if (match) {
name = match[1]
}
}
2019-11-03 11:21:02 +08:00
return name ? classify(name) : 'Anonymous'
2019-08-30 22:36:30 +08:00
}
2019-11-03 11:21:02 +08:00
function formatProps(props: Data): any[] {
const res: any[] = []
2019-08-30 22:36:30 +08:00
for (const key in props) {
2019-11-03 11:21:02 +08:00
res.push(...formatProp(key, props[key]))
2019-08-30 22:36:30 +08:00
}
return res
2019-05-28 18:06:00 +08:00
}
2019-11-03 11:21:02 +08:00
function formatProp(key: string, value: unknown): any[]
function formatProp(key: string, value: unknown, raw: true): any
function formatProp(key: string, value: unknown, raw?: boolean): any {
if (isString(value)) {
value = JSON.stringify(value)
return raw ? value : [`${key}=${value}`]
} else if (typeof value === 'number' || value == null) {
return raw ? value : [`${key}=${value}`]
} else if (isRef(value)) {
value = formatProp(key, toRaw(value.value), true)
return raw ? value : [`${key}=Ref<`, value, `>`]
} else {
value = toRaw(value)
return raw ? value : [`${key}=`, value]
}
}