-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add context logger to PropTypes.run helper, rename value to def…
…aultValue and rename validate to customValidator
- Loading branch information
Showing
5 changed files
with
252 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
let config = {} | ||
try { | ||
config = require('vue') | ||
} catch (e) {} | ||
|
||
const hasConsole = typeof console !== 'undefined' | ||
const classifyRE = /(?:^|[-_])(\w)/g | ||
const classify = str => | ||
str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '') | ||
|
||
export const warn = (msg, vm) => { | ||
const trace = vm ? generateComponentTrace(vm) : '' | ||
|
||
if (config.warnHandler) { | ||
config.warnHandler.call(null, msg, vm, trace) | ||
} else if (hasConsole && !config.silent) { | ||
console.error(`[Vue warn]: ${msg}${trace}`) | ||
} | ||
} | ||
export const error = (msg, vm) => { | ||
const trace = vm ? generateComponentTrace(vm) : '' | ||
|
||
if (config.errorHandler) { | ||
config.errorHandler.call(null, msg, vm, trace) | ||
} else if (hasConsole && !config.silent) { | ||
console.error(`[Vue error]: ${msg}${trace}`) | ||
} | ||
} | ||
|
||
export const tip = (msg, vm) => { | ||
if (hasConsole && !config.silent) { | ||
console.warn(`[Vue tip]: ${msg}` + (vm ? generateComponentTrace(vm) : '')) | ||
} | ||
} | ||
|
||
export const formatComponentName = (vm, includeFile) => { | ||
if (vm.$root === vm) { | ||
return '<Root>' | ||
} | ||
const options = | ||
typeof vm === 'function' && vm.cid != null | ||
? vm.options | ||
: vm._isVue | ||
? vm.$options || vm.constructor.options | ||
: vm | ||
let name = options.name || options._componentTag | ||
const file = options.__file | ||
if (!name && file) { | ||
const match = file.match(/([^/\\]+)\.vue$/) | ||
name = match && match[1] | ||
} | ||
|
||
return ( | ||
(name ? `<${classify(name)}>` : `<Anonymous>`) + | ||
(file && includeFile !== false ? ` at ${file}` : '') | ||
) | ||
} | ||
|
||
const repeat = (str, n) => { | ||
let res = '' | ||
while (n) { | ||
if (n % 2 === 1) res += str | ||
if (n > 1) str += str | ||
n >>= 1 | ||
} | ||
return res | ||
} | ||
|
||
export const generateComponentTrace = vm => { | ||
if (vm._isVue && vm.$parent) { | ||
const tree = [] | ||
let currentRecursiveSequence = 0 | ||
while (vm) { | ||
if (tree.length > 0) { | ||
const last = tree[tree.length - 1] | ||
if (last.constructor === vm.constructor) { | ||
currentRecursiveSequence++ | ||
vm = vm.$parent | ||
continue | ||
} else if (currentRecursiveSequence > 0) { | ||
tree[tree.length - 1] = [last, currentRecursiveSequence] | ||
currentRecursiveSequence = 0 | ||
} | ||
} | ||
tree.push(vm) | ||
vm = vm.$parent | ||
} | ||
return ( | ||
'\n\nfound in\n\n' + | ||
tree | ||
.map( | ||
(vm, i) => | ||
`${i === 0 ? '---> ' : repeat(' ', 5 + i * 2)}${ | ||
Array.isArray(vm) | ||
? `${formatComponentName(vm[0])}... (${vm[1]} recursive calls)` | ||
: formatComponentName(vm) | ||
}` | ||
) | ||
.join('\n') | ||
) | ||
} else { | ||
return `\n\n(found in ${formatComponentName(vm)})` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.