feat: use internal warning

This commit is contained in:
Evan You 2018-10-11 17:21:13 -04:00
parent 5327abb249
commit 7e6fdb8cc4
7 changed files with 23 additions and 23 deletions

View File

@ -8,6 +8,7 @@ import {
PropType PropType
} from './componentOptions' } from './componentOptions'
import { EMPTY_OBJ, camelize, hyphenate, capitalize } from './utils' import { EMPTY_OBJ, camelize, hyphenate, capitalize } from './utils'
import { warn } from './warning'
const EMPTY_PROPS = { props: EMPTY_OBJ } const EMPTY_PROPS = { props: EMPTY_OBJ }
@ -159,13 +160,13 @@ function normalizePropsOptions(
if (Array.isArray(raw)) { if (Array.isArray(raw)) {
for (let i = 0; i < raw.length; i++) { for (let i = 0; i < raw.length; i++) {
if (__DEV__ && typeof raw !== 'string') { if (__DEV__ && typeof raw !== 'string') {
console.warn(`props must be strings when using array syntax.`) warn(`props must be strings when using array syntax.`)
} }
normalized[camelize(raw[i])] = EMPTY_OBJ normalized[camelize(raw[i])] = EMPTY_OBJ
} }
} else { } else {
if (__DEV__ && typeof raw !== 'object') { if (__DEV__ && typeof raw !== 'object') {
console.warn(`invalid props options: `, raw) warn(`invalid props options`, raw)
} }
for (const key in raw) { for (const key in raw) {
const opt = raw[key] const opt = raw[key]
@ -224,7 +225,7 @@ function validateProp(
const { type, required, validator } = prop const { type, required, validator } = prop
// required! // required!
if (required && isAbsent) { if (required && isAbsent) {
console.warn('Missing required prop: "' + name + '"') warn('Missing required prop: "' + name + '"')
return return
} }
// missing but optional // missing but optional
@ -243,15 +244,13 @@ function validateProp(
isValid = valid isValid = valid
} }
if (!isValid) { if (!isValid) {
console.warn(getInvalidTypeMessage(name, value, expectedTypes)) warn(getInvalidTypeMessage(name, value, expectedTypes))
return return
} }
} }
// custom validator // custom validator
if (validator && !validator(value)) { if (validator && !validator(value)) {
console.warn( warn('Invalid prop: custom validator check failed for prop "' + name + '".')
'Invalid prop: custom validator check failed for prop "' + name + '".'
)
} }
} }

View File

@ -20,6 +20,7 @@ import { initializeWatch, teardownWatch } from './componentWatch'
import { ComponentOptions } from './componentOptions' import { ComponentOptions } from './componentOptions'
import { createRenderProxy } from './componentProxy' import { createRenderProxy } from './componentProxy'
import { handleError, ErrorTypes } from './errorHandling' import { handleError, ErrorTypes } from './errorHandling'
import { warn } from './warning'
let currentVNode: VNode | null = null let currentVNode: VNode | null = null
let currentContextVNode: MountedVNode | null = null let currentContextVNode: MountedVNode | null = null
@ -244,7 +245,7 @@ export function createComponentClassFromOptions(
} else if (key === 'methods') { } else if (key === 'methods') {
for (const method in value) { for (const method in value) {
if (__DEV__ && proto.hasOwnProperty(method)) { if (__DEV__ && proto.hasOwnProperty(method)) {
console.warn( warn(
`Object syntax contains method name that conflicts with ` + `Object syntax contains method name that conflicts with ` +
`lifecycle hook: "${method}"` `lifecycle hook: "${method}"`
) )

View File

@ -4,6 +4,7 @@ import { ComponentWatchOptions, WatchOptions } from './componentOptions'
import { autorun, stop } from '@vue/observer' import { autorun, stop } from '@vue/observer'
import { queueJob } from '@vue/scheduler' import { queueJob } from '@vue/scheduler'
import { handleError, ErrorTypes } from './errorHandling' import { handleError, ErrorTypes } from './errorHandling'
import { warn } from './warning'
export function initializeWatch( export function initializeWatch(
instance: ComponentInstance, instance: ComponentInstance,
@ -40,7 +41,7 @@ export function setupWatcher(
: () => keyOrFn.call(proxy) : () => keyOrFn.call(proxy)
if (__DEV__ && rawGetter === NOOP) { if (__DEV__ && rawGetter === NOOP) {
console.warn( warn(
`Failed watching expression: "${keyOrFn}". ` + `Failed watching expression: "${keyOrFn}". ` +
`Watch expressions can only be dot-delimited paths. ` + `Watch expressions can only be dot-delimited paths. ` +
`For more complex expressions, use $watch with a function instead.` `For more complex expressions, use $watch with a function instead.`

View File

@ -10,6 +10,7 @@ import {
createPortal createPortal
} from './vdom' } from './vdom'
import { isObservable } from '@vue/observer' import { isObservable } from '@vue/observer'
import { warn } from './warning'
export const Fragment = Symbol() export const Fragment = Symbol()
export const Portal = Symbol() export const Portal = Symbol()
@ -79,14 +80,12 @@ export const h = ((tag: ElementType, data?: any, children?: any): VNode => {
) )
} else if (tag === Fragment) { } else if (tag === Fragment) {
if (__DEV__ && ref) { if (__DEV__ && ref) {
console.warn( warn('Ref cannot be used on Fragments. Use it on inner elements instead.')
'Ref cannot be used on Fragments. Use it on inner elements instead.'
)
} }
return createFragment(children, ChildrenFlags.UNKNOWN_CHILDREN, key) return createFragment(children, ChildrenFlags.UNKNOWN_CHILDREN, key)
} else if (tag === Portal) { } else if (tag === Portal) {
if (__DEV__ && !portalTarget) { if (__DEV__ && !portalTarget) {
console.warn('Portal must have a target: ', portalTarget) warn('Portal must have a target: ', portalTarget)
} }
return createPortal( return createPortal(
portalTarget, portalTarget,
@ -100,7 +99,7 @@ export const h = ((tag: ElementType, data?: any, children?: any): VNode => {
__DEV__ && __DEV__ &&
(!tag || (typeof tag !== 'function' && typeof tag !== 'object')) (!tag || (typeof tag !== 'function' && typeof tag !== 'object'))
) { ) {
console.warn('Invalid component passed to h(): ', tag) warn('Invalid component passed to h(): ', tag)
} }
// component // component
return createComponentVNode( return createComponentVNode(

View File

@ -1,5 +1,6 @@
import { observable } from '@vue/observer' import { observable } from '@vue/observer'
import { Component } from '../component' import { Component } from '../component'
import { warn } from '../warning'
const contextStore = observable() as Record<string | symbol, any> const contextStore = observable() as Record<string | symbol, any>
@ -28,14 +29,12 @@ export class Provide extends Component<ProviderProps> {
if (__DEV__) { if (__DEV__) {
const { id } = this.$props const { id } = this.$props
if (contextStore.hasOwnProperty(id)) { if (contextStore.hasOwnProperty(id)) {
console.warn( warn(`A context provider with id ${id.toString()} already exists.`)
`A context provider with id ${id.toString()} already exists.`
)
} }
this.$watch( this.$watch(
() => this.$props.id, () => this.$props.id,
(id: string, oldId: string) => { (id: string, oldId: string) => {
console.warn( warn(
`Context provider id change detected (from "${oldId}" to "${id}"). ` + `Context provider id change detected (from "${oldId}" to "${id}"). ` +
`This is not supported and should be avoided.` `This is not supported and should be avoided.`
) )

View File

@ -1,6 +1,7 @@
import { Component, ComponentClass, ComponentInstance } from '../component' import { Component, ComponentClass, ComponentInstance } from '../component'
import { VNode, Slots, cloneVNode } from '../vdom' import { VNode, Slots, cloneVNode } from '../vdom'
import { VNodeFlags } from '../flags' import { VNodeFlags } from '../flags'
import { warn } from '../warning'
type MatchPattern = string | RegExp | string[] | RegExp[] type MatchPattern = string | RegExp | string[] | RegExp[]
@ -57,12 +58,12 @@ export class KeepAlive extends Component<KeepAliveProps> {
let vnode = children[0] let vnode = children[0]
if (children.length > 1) { if (children.length > 1) {
if (__DEV__) { if (__DEV__) {
console.warn(`KeepAlive can only have a single child.`) warn(`KeepAlive can only have a single child.`)
} }
return children return children
} else if ((vnode.flags & VNodeFlags.COMPONENT_STATEFUL) === 0) { } else if ((vnode.flags & VNodeFlags.COMPONENT_STATEFUL) === 0) {
if (__DEV__) { if (__DEV__) {
console.warn(`KeepAlive child must be a stateful component.`) warn(`KeepAlive child must be a stateful component.`)
} }
return children return children
} }

View File

@ -12,15 +12,15 @@ export function popContext() {
stack.pop() stack.pop()
} }
export function warn(msg: string) { export function warn(msg: string, ...args: any[]) {
// TODO warn handler? // TODO warn handler?
console.warn(`[Vue warn]: ${msg}${getComponentTrace()}`) warn(`[Vue warn]: ${msg}${getComponentTrace()}`, ...args)
} }
function getComponentTrace(): string { function getComponentTrace(): string {
let current: VNode | null | undefined = stack[stack.length - 1] let current: VNode | null | undefined = stack[stack.length - 1]
if (!current) { if (!current) {
return '\nat <Root/>' return ''
} }
// we can't just use the stack because it will be incomplete during updates // we can't just use the stack because it will be incomplete during updates