Skip to content

Commit

Permalink
feat: Add vm trace
Browse files Browse the repository at this point in the history
  • Loading branch information
mya-ake committed Jan 22, 2019
1 parent 79af6ed commit 7b80011
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ if (Vue.config.silent === false) {
str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '');

warn = (msg, vm) => {
const trace = vm ? generateComponentTrace(vm) : '';

if (Vue.config.warnHandler) {
Vue.config.warnHandler.call(null, msg, vm);
Vue.config.warnHandler.call(null, msg, vm, trace);
} else if (hasConsole && !Vue.config.silent) {
// eslint-disable-next-line no-console
console.error(`[vue-slot-checker warn]: ${msg}`);
const message = process.env.NODE_ENV !== 'test' ? `${msg}${trace}` : msg;
console.error(`[vue-slot-checker warn]: ${message}`);
}
};

Expand Down
19 changes: 11 additions & 8 deletions src/mixin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { warn } from './debug';

const warnRequire = slotName => {
warn(`Missing required slot: '${slotName}'`);
const warnRequire = (slotName, vm) => {
warn(`Missing required slot: '${slotName}'`, vm);
};

const warnCustomValidator = slotName => {
warn(`Invalid slot: custom validator check failed for slot '${slotName}'`);
const warnCustomValidator = (slotName, vm) => {
warn(
`Invalid slot: custom validator check failed for slot '${slotName}'`,
vm,
);
};

export const mixin = {
Expand All @@ -16,14 +19,14 @@ export const mixin = {

if (this.$options.slots === true) {
if ('default' in this.$slots === false) {
warnRequire('default');
warnRequire('default', this);
}
}

if (Array.isArray(this.$options.slots)) {
this.$options.slots.forEach(slotName => {
if (slotName in this.$slots === false) {
warnRequire(slotName);
warnRequire(slotName, this);
}
});
}
Expand All @@ -33,13 +36,13 @@ export const mixin = {
const option = this.$options.slots[slotName];
if (option.required === true) {
if (slotName in this.$slots === false) {
warnRequire(slotName);
warnRequire(slotName, this);
}
}
const validator = option.validator;
if (typeof validator === 'function') {
if (validator(this.$slots[slotName]) === false) {
warnCustomValidator(slotName);
warnCustomValidator(slotName, this);
}
}
});
Expand Down

0 comments on commit 7b80011

Please sign in to comment.