Skip to content

Commit

Permalink
Merge pull request #1525 from natevw/nvw-checkPropTypes
Browse files Browse the repository at this point in the history
Provide simplified checkPropTypes implementation
  • Loading branch information
marvinhagemeister committed Apr 11, 2019
2 parents df926b3 + 6735cfa commit c08ee16
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
3 changes: 0 additions & 3 deletions debug/package.json
Expand Up @@ -12,9 +12,6 @@
"mangle": {
"regex": "^(?!_renderer)^_"
},
"dependencies": {
"prop-types": "^15.6.2"
},
"peerDependencies": {
"preact": "^10.0.0-alpha.0"
}
Expand Down
18 changes: 18 additions & 0 deletions debug/src/check-props.js
@@ -0,0 +1,18 @@
const ReactPropTypesSecret = 'SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED';

let loggedTypeFailures = {};

export function checkPropTypes(typeSpecs, values, location, componentName, getStack) {
Object.keys(typeSpecs).forEach((typeSpecName) => {
let error;
try {
error = typeSpecs[typeSpecName](values, typeSpecName, componentName, location, null, ReactPropTypesSecret);
} catch (e) {
error = e;
}
if (error && !(error.message in loggedTypeFailures)) {
loggedTypeFailures[error.message] = true;
console.error(`Failed ${location} type: ${error.message}${getStack && getStack() || ''}`);
}
});
}
2 changes: 1 addition & 1 deletion debug/src/debug.js
@@ -1,4 +1,4 @@
import { checkPropTypes } from 'prop-types';
import { checkPropTypes } from './check-props';
import { getDisplayName } from './devtools/custom';
import { options, toChildArray } from 'preact';
import { ELEMENT_NODE, DOCUMENT_NODE, DOCUMENT_FRAGMENT_NODE } from './constants';
Expand Down
18 changes: 17 additions & 1 deletion debug/test/browser/debug.test.js
@@ -1,6 +1,6 @@
import { createElement as h, options, render, createRef, Component, Fragment } from 'preact';
import { useState, useEffect, useLayoutEffect, useMemo, useCallback } from 'preact/hooks';
import { setupScratch, teardown, clearOptions } from '../../../test/_util/helpers';
import { setupScratch, teardown, clearOptions, serializeHtml } from '../../../test/_util/helpers';
import { serializeVNode, initDebug } from '../../src/debug';
import * as PropTypes from 'prop-types';

Expand Down Expand Up @@ -325,6 +325,22 @@ describe('debug', () => {
expect(errors[0].includes('required')).to.equal(true);
});

it('should render with error logged when validator gets signal and throws exception', () => {
function Baz(props) {
return <h1>{props.unhappy}</h1>;
}

Baz.propTypes = {
unhappy: function alwaysThrows(obj, key) { if (obj[key] === 'signal') throw Error("got prop"); }
};

render(<Baz unhappy={'signal'} />, scratch);

expect(console.error).to.be.calledOnce;
expect(errors[0].includes('got prop')).to.equal(true);
expect(serializeHtml(scratch)).to.equal('<h1>signal</h1>');
});

it('should not print to console when types are correct', () => {
function Bar(props) {
return <h1>{props.text}</h1>;
Expand Down
4 changes: 1 addition & 3 deletions package.json
Expand Up @@ -90,9 +90,6 @@
"url": "https://github.com/developit/preact/issues"
},
"homepage": "https://github.com/developit/preact",
"dependencies": {
"prop-types": "^15.6.2"
},
"devDependencies": {
"@types/chai": "^4.1.2",
"@types/mocha": "^5.0.0",
Expand Down Expand Up @@ -129,6 +126,7 @@
"microbundle": "^0.11.0",
"mocha": "^5.2.0",
"npm-run-all": "^4.0.0",
"prop-types": "^15.7.2",
"sinon": "^6.1.3",
"sinon-chai": "^3.0.0",
"typescript": "^3.0.1",
Expand Down

0 comments on commit c08ee16

Please sign in to comment.