don't validate propTypes in production

Switch to warnings so code execution doesn't differ between prod and
dev.
This commit is contained in:
Jamison Dance 2015-01-26 15:54:50 -07:00
parent 16a56afada
commit f61ca8d307
3 changed files with 69 additions and 40 deletions

View File

@ -327,11 +327,13 @@ var RESERVED_SPEC_KEYS = {
}
},
childContextTypes: function(Constructor, childContextTypes) {
validateTypeDef(
Constructor,
childContextTypes,
ReactPropTypeLocations.childContext
);
if (__DEV__) {
validateTypeDef(
Constructor,
childContextTypes,
ReactPropTypeLocations.childContext
);
}
Constructor.childContextTypes = assign(
{},
Constructor.childContextTypes,
@ -339,11 +341,13 @@ var RESERVED_SPEC_KEYS = {
);
},
contextTypes: function(Constructor, contextTypes) {
validateTypeDef(
Constructor,
contextTypes,
ReactPropTypeLocations.context
);
if (__DEV__) {
validateTypeDef(
Constructor,
contextTypes,
ReactPropTypeLocations.context
);
}
Constructor.contextTypes = assign(
{},
Constructor.contextTypes,
@ -365,11 +369,13 @@ var RESERVED_SPEC_KEYS = {
}
},
propTypes: function(Constructor, propTypes) {
validateTypeDef(
Constructor,
propTypes,
ReactPropTypeLocations.prop
);
if (__DEV__) {
validateTypeDef(
Constructor,
propTypes,
ReactPropTypeLocations.prop
);
}
Constructor.propTypes = assign(
{},
Constructor.propTypes,
@ -384,7 +390,9 @@ var RESERVED_SPEC_KEYS = {
function validateTypeDef(Constructor, typeDef, location) {
for (var propName in typeDef) {
if (typeDef.hasOwnProperty(propName)) {
invariant(
// use a warning instead of an invariant so components
// don't show up in prod but not in __DEV__
warning(
typeof typeDef[propName] === 'function',
'%s: %s type `%s` is invalid; it must be a function, usually from ' +
'React.PropTypes.',

View File

@ -74,8 +74,11 @@ describe('ReactClass-spec', function() {
.toBe(propValidator);
});
it('should throw on invalid prop types', function() {
expect(function() {
it('should warn on invalid prop types', function() {
var warn = console.warn;
console.warn = mocks.getMockFunction();
try {
React.createClass({
displayName: 'Component',
propTypes: {
@ -85,14 +88,20 @@ describe('ReactClass-spec', function() {
return <span>{this.props.prop}</span>;
}
});
}).toThrow(
'Invariant Violation: Component: prop type `prop` is invalid; ' +
'it must be a function, usually from React.PropTypes.'
);
expect(console.warn.mock.calls.length).toBe(1);
expect(console.warn.mock.calls[0][0]).toBe(
'Warning: Component: prop type `prop` is invalid; ' +
'it must be a function, usually from React.PropTypes.'
);
} finally {
console.warn = warn;
}
});
it('should throw on invalid context types', function() {
expect(function() {
it('should warn on invalid context types', function() {
var warn = console.warn;
console.warn = mocks.getMockFunction();
try {
React.createClass({
displayName: 'Component',
contextTypes: {
@ -102,14 +111,20 @@ describe('ReactClass-spec', function() {
return <span>{this.props.prop}</span>;
}
});
}).toThrow(
'Invariant Violation: Component: context type `prop` is invalid; ' +
'it must be a function, usually from React.PropTypes.'
);
expect(console.warn.mock.calls.length).toBe(1);
expect(console.warn.mock.calls[0][0]).toBe(
'Warning: Component: context type `prop` is invalid; ' +
'it must be a function, usually from React.PropTypes.'
);
} finally {
console.warn = warn;
}
});
it('should throw on invalid child context types', function() {
expect(function() {
var warn = console.warn;
console.warn = mocks.getMockFunction();
try {
React.createClass({
displayName: 'Component',
childContextTypes: {
@ -119,10 +134,14 @@ describe('ReactClass-spec', function() {
return <span>{this.props.prop}</span>;
}
});
}).toThrow(
'Invariant Violation: Component: child context type `prop` is invalid; ' +
'it must be a function, usually from React.PropTypes.'
);
expect(console.warn.mock.calls.length).toBe(1);
expect(console.warn.mock.calls[0][0]).toBe(
'Warning: Component: child context type `prop` is invalid; ' +
'it must be a function, usually from React.PropTypes.'
);
} finally {
console.warn = warn;
}
});
it('should warn when mispelling shouldComponentUpdate', function() {

View File

@ -381,13 +381,15 @@ var ReactElementValidator = {
element
);
var name = componentClass.displayName || componentClass.name;
if (componentClass.propTypes) {
checkPropTypes(
name,
componentClass.propTypes,
element.props,
ReactPropTypeLocations.prop
);
if (__DEV__) {
if (componentClass.propTypes) {
checkPropTypes(
name,
componentClass.propTypes,
element.props,
ReactPropTypeLocations.prop
);
}
}
if (typeof componentClass.getDefaultProps === 'function') {
warning(