Skip to content

Commit

Permalink
Properly check render result type and show component name in case of …
Browse files Browse the repository at this point in the history
…error
  • Loading branch information
dfilatov committed Jul 20, 2017
1 parent 509a258 commit 074135f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/createComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import console from './utils/console';
import emptyObj from './utils/emptyObj';
import restrictObjProp from './utils/restrictObjProp';
import { IS_DEBUG } from './utils/debug';
import isNode from './nodes/utils/isNode';
import globalHook from './globalHook';

function mountComponent() {
Expand Down Expand Up @@ -86,7 +87,9 @@ function patchComponent(nextAttrs, nextChildren, nextContext, callReceivers) {
const shouldRerenderResType = typeof shouldRerender;

if(shouldRerenderResType !== 'boolean') {
console.warn(`Component#shouldRerender() should return boolean instead of ${shouldRerenderResType}`);
const name = this.constructor.name || 'Component';

console.warn(`${name}#shouldRerender() should return boolean instead of ${shouldRerenderResType}`);
}
}

Expand Down Expand Up @@ -164,8 +167,10 @@ function renderComponent() {
rootNode = onRenderRes === null? createNode('!') : onRenderRes;

if(IS_DEBUG) {
if(typeof rootNode !== 'object' || Array.isArray(rootNode)) {
throw TypeError('vidom: Component#onRender must return a single node on the top level or null.');
if(!isNode(rootNode)) {
const name = this.constructor.name || 'Component';

throw TypeError(`vidom: ${name}#onRender must return a single node or null on the top level.`);
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/nodes/FunctionComponentNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import restrictObjProp from '../utils/restrictObjProp';
import { IS_DEBUG } from '../utils/debug';
import { NODE_TYPE_FUNCTION_COMPONENT } from './utils/nodeTypes';
import { setKey } from './utils/setters';
import isNode from './utils/isNode';

const ATTRS_SET = 4,
CHILDREN_SET = 8;
Expand Down Expand Up @@ -176,11 +177,14 @@ FunctionComponentNode.prototype = {
Object.freeze(resAttrs);
}

const rootNode = component(resAttrs, this.children, this._ctx) || createNode('!');
const res = component(resAttrs, this.children, this._ctx),
rootNode = res === null? createNode('!') : res;

if(IS_DEBUG) {
if(typeof rootNode !== 'object' || Array.isArray(rootNode)) {
throw Error('vidom: Function component must return a single node on the top level.');
if(!isNode(rootNode)) {
const name = component.name || 'Function';

throw Error(`vidom: ${name} component must return a single node or null on the top level.`);
}
}

Expand Down

0 comments on commit 074135f

Please sign in to comment.