Skip to content

Commit

Permalink
Merge c724fe8 into 0246cd8
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Apr 4, 2019
2 parents 0246cd8 + c724fe8 commit 3dfb7c9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
21 changes: 21 additions & 0 deletions debug/src/debug.js
Expand Up @@ -7,6 +7,7 @@ export function initDebug() {
/* eslint-disable no-console */
let oldBeforeDiff = options.diff;
let oldDiffed = options.diffed;
let oldVnode = options.vnode;

options.root = (vnode, parentNode) => {
if (!parentNode) {
Expand Down Expand Up @@ -98,6 +99,26 @@ export function initDebug() {
if (oldBeforeDiff) oldBeforeDiff(vnode);
};

const warn = (property, err) => ({
get() {
throw new Error(`getting vnode.${property} is deprecated, ${err}`);
},
set() {
throw new Error(`setting vnode.${property} is not allowed, ${err}`);
}
});

const deprecatedAttributes = {
nodeName: warn('nodeName', 'use vnode.type'),
attributes: warn('attributes', 'use vnode.props'),
children: warn('children', 'use vnode.props.children')
};

options.vnode = (vnode) => {
Object.defineProperties(vnode, deprecatedAttributes);
if (oldVnode) oldVnode(vnode);
};

options.diffed = (vnode) => {
if (vnode._component && vnode._component.__hooks) {
let hooks = vnode._component.__hooks;
Expand Down
18 changes: 18 additions & 0 deletions debug/test/browser/debug.test.js
Expand Up @@ -119,6 +119,24 @@ describe('debug', () => {
expect(fn).to.throw(/createElement/);
});


it('Should throw errors when accessing certain attributes', () => {
let Foo = () => <div />;
const oldOptionsVnode = options.vnode;
options.vnode = (vnode) => {
oldOptionsVnode(vnode);
expect(() => vnode).to.not.throw();
expect(() => vnode.attributes).to.throw(/use vnode.props/);
expect(() => vnode.nodeName).to.throw(/use vnode.type/);
expect(() => vnode.children).to.throw(/use vnode.props.children/);
expect(() => vnode.attributes = {}).to.throw(/use vnode.props/);
expect(() => vnode.nodeName = 'test').to.throw(/use vnode.type/);
expect(() => vnode.children = [<div />]).to.throw(/use vnode.props.children/);
};
render(<Foo />, scratch);
options.vnode = oldOptionsVnode;
});

it('should print an error when component is an array', () => {
let fn = () => render(h([<div />]), scratch);
expect(fn).to.throw(/createElement/);
Expand Down

0 comments on commit 3dfb7c9

Please sign in to comment.