Skip to content

Commit

Permalink
perf(react:can): moves prop type checks undef if, so they can be re…
Browse files Browse the repository at this point in the history
…moved for production builds
  • Loading branch information
stalniy committed Aug 31, 2018
1 parent 045318c commit 4bebf0b
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions packages/casl-react/src/Can.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ const noop = () => {};
const renderChildren = Fragment
? children => createElement.apply(null, [Fragment, null].concat(children))
: React.Children.only;
const REQUIRED_OBJECT_OR_STRING = PropTypes
.oneOfType([PropTypes.object, PropTypes.string])
.isRequired;

function alias(names, validate) {
return (props, ...args) => { // eslint-disable-line
if (!names.split(' ').some(name => props[name])) {
return validate(props, ...args);
}
};
}
let propTypes = {};

if (process.env.NODE_ENV !== 'production') {
const REQUIRED_OBJECT_OR_STRING = PropTypes
.oneOfType([PropTypes.object, PropTypes.string])
.isRequired;

function alias(names, validate) {
return (props, ...args) => { // eslint-disable-line
if (!names.split(' ').some(name => props[name])) {
return validate(props, ...args);
}
};
}

export default class Can extends PureComponent {
static propTypes = {
propTypes = {
I: alias('do', PropTypes.string.isRequired),
a: alias('on this of an', REQUIRED_OBJECT_OR_STRING),
an: alias('on this of a', REQUIRED_OBJECT_OR_STRING),
Expand All @@ -32,6 +34,10 @@ export default class Can extends PureComponent {
children: PropTypes.any.isRequired,
ability: PropTypes.instanceOf(Ability).isRequired
};
}

export default class Can extends PureComponent {
static propTypes = propTypes;

constructor(...args) {
super(...args);
Expand Down

0 comments on commit 4bebf0b

Please sign in to comment.