Remove ReactPropTypeLocationNames module

ReactPropTypeLocationNames was a map of location identifiers to location
display names, for use in warnings. But in practice, this did nothing
except rename "childContext" to "child context." Not worth it, IMO.

Since we are going to add an API for running prop type checks manually,
we need the ability to support arbitrary prop type locations. So
each place that used to accept a ReactPropTypeLocation now just accepts
a string.
This commit is contained in:
Andrew Clark
2017-02-23 11:14:14 -08:00
parent 91e8081cf0
commit bef723e47f
9 changed files with 23 additions and 91 deletions
@@ -261,7 +261,7 @@ describe('ReactContextValidator', () => {
ReactTestUtils.renderIntoDocument(<Component testContext={{bar: 123}} />);
expectDev(console.error.calls.count()).toBe(1);
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Failed childContext type: ' +
'Warning: Failed child context type: ' +
'The child context `foo` is marked as required in `Component`, but its ' +
'value is `undefined`.\n' +
' in Component (at **)'
@@ -271,7 +271,7 @@ describe('ReactContextValidator', () => {
expectDev(console.error.calls.count()).toBe(2);
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toBe(
'Warning: Failed childContext type: ' +
'Warning: Failed child context type: ' +
'Invalid child context `foo` of type `number` ' +
'supplied to `Component`, expected `string`.\n' +
' in Component (at **)'
+3 -6
View File
@@ -13,7 +13,6 @@
var ReactBaseClasses = require('ReactBaseClasses');
var ReactElement = require('ReactElement');
var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');
var ReactNoopUpdateQueue = require('ReactNoopUpdateQueue');
var emptyObject = require('emptyObject');
@@ -22,8 +21,6 @@ var warning = require('warning');
var ReactComponent = ReactBaseClasses.Component;
import type { ReactPropTypeLocations } from 'ReactPropTypeLocations';
var MIXINS_KEY = 'mixins';
// Helper function to allow the creation of anonymous functions which do not
@@ -330,7 +327,7 @@ var RESERVED_SPEC_KEYS = {
validateTypeDef(
Constructor,
childContextTypes,
'childContext'
'child context'
);
}
Constructor.childContextTypes = Object.assign(
@@ -390,7 +387,7 @@ var RESERVED_SPEC_KEYS = {
function validateTypeDef(
Constructor,
typeDef,
location: ReactPropTypeLocations,
location: string,
) {
for (var propName in typeDef) {
if (typeDef.hasOwnProperty(propName)) {
@@ -401,7 +398,7 @@ function validateTypeDef(
'%s: %s type `%s` is invalid; it must be a function, usually from ' +
'React.PropTypes.',
Constructor.displayName || 'ReactClass',
ReactPropTypeLocationNames[location],
location,
propName
);
}
+11 -22
View File
@@ -12,7 +12,6 @@
'use strict';
var ReactElement = require('ReactElement');
var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');
var ReactPropTypesSecret = require('ReactPropTypesSecret');
var emptyFunction = require('emptyFunction');
@@ -192,16 +191,15 @@ function createChainableTypeChecker(validate) {
}
}
if (props[propName] == null) {
var locationName = ReactPropTypeLocationNames[location];
if (isRequired) {
if (props[propName] === null) {
return new PropTypeError(
`The ${locationName} \`${propFullName}\` is marked as required ` +
`The ${location} \`${propFullName}\` is marked as required ` +
`in \`${componentName}\`, but its value is \`null\`.`
);
}
return new PropTypeError(
`The ${locationName} \`${propFullName}\` is marked as required in ` +
`The ${location} \`${propFullName}\` is marked as required in ` +
`\`${componentName}\`, but its value is \`undefined\`.`
);
}
@@ -235,14 +233,13 @@ function createPrimitiveTypeChecker(expectedType) {
var propValue = props[propName];
var propType = getPropType(propValue);
if (propType !== expectedType) {
var locationName = ReactPropTypeLocationNames[location];
// `propValue` being instance of, say, date/regexp, pass the 'object'
// check, but we can offer a more precise error message here rather than
// 'of type `object`'.
var preciseType = getPreciseType(propValue);
return new PropTypeError(
`Invalid ${locationName} \`${propFullName}\` of type ` +
`Invalid ${location} \`${propFullName}\` of type ` +
`\`${preciseType}\` supplied to \`${componentName}\`, expected ` +
`\`${expectedType}\`.`
);
@@ -265,10 +262,9 @@ function createArrayOfTypeChecker(typeChecker) {
}
var propValue = props[propName];
if (!Array.isArray(propValue)) {
var locationName = ReactPropTypeLocationNames[location];
var propType = getPropType(propValue);
return new PropTypeError(
`Invalid ${locationName} \`${propFullName}\` of type ` +
`Invalid ${location} \`${propFullName}\` of type ` +
`\`${propType}\` supplied to \`${componentName}\`, expected an array.`
);
}
@@ -294,10 +290,9 @@ function createElementTypeChecker() {
function validate(props, propName, componentName, location, propFullName) {
var propValue = props[propName];
if (!ReactElement.isValidElement(propValue)) {
var locationName = ReactPropTypeLocationNames[location];
var propType = getPropType(propValue);
return new PropTypeError(
`Invalid ${locationName} \`${propFullName}\` of type ` +
`Invalid ${location} \`${propFullName}\` of type ` +
`\`${propType}\` supplied to \`${componentName}\`, expected a single ReactElement.`
);
}
@@ -309,11 +304,10 @@ function createElementTypeChecker() {
function createInstanceTypeChecker(expectedClass) {
function validate(props, propName, componentName, location, propFullName) {
if (!(props[propName] instanceof expectedClass)) {
var locationName = ReactPropTypeLocationNames[location];
var expectedClassName = expectedClass.name || ANONYMOUS;
var actualClassName = getClassName(props[propName]);
return new PropTypeError(
`Invalid ${locationName} \`${propFullName}\` of type ` +
`Invalid ${location} \`${propFullName}\` of type ` +
`\`${actualClassName}\` supplied to \`${componentName}\`, expected ` +
`instance of \`${expectedClassName}\`.`
);
@@ -337,10 +331,9 @@ function createEnumTypeChecker(expectedValues) {
}
}
var locationName = ReactPropTypeLocationNames[location];
var valuesString = JSON.stringify(expectedValues);
return new PropTypeError(
`Invalid ${locationName} \`${propFullName}\` of value \`${propValue}\` ` +
`Invalid ${location} \`${propFullName}\` of value \`${propValue}\` ` +
`supplied to \`${componentName}\`, expected one of ${valuesString}.`
);
}
@@ -357,9 +350,8 @@ function createObjectOfTypeChecker(typeChecker) {
var propValue = props[propName];
var propType = getPropType(propValue);
if (propType !== 'object') {
var locationName = ReactPropTypeLocationNames[location];
return new PropTypeError(
`Invalid ${locationName} \`${propFullName}\` of type ` +
`Invalid ${location} \`${propFullName}\` of type ` +
`\`${propType}\` supplied to \`${componentName}\`, expected an object.`
);
}
@@ -406,9 +398,8 @@ function createUnionTypeChecker(arrayOfTypeCheckers) {
}
}
var locationName = ReactPropTypeLocationNames[location];
return new PropTypeError(
`Invalid ${locationName} \`${propFullName}\` supplied to ` +
`Invalid ${location} \`${propFullName}\` supplied to ` +
`\`${componentName}\`.`
);
}
@@ -418,9 +409,8 @@ function createUnionTypeChecker(arrayOfTypeCheckers) {
function createNodeChecker() {
function validate(props, propName, componentName, location, propFullName) {
if (!isNode(props[propName])) {
var locationName = ReactPropTypeLocationNames[location];
return new PropTypeError(
`Invalid ${locationName} \`${propFullName}\` supplied to ` +
`Invalid ${location} \`${propFullName}\` supplied to ` +
`\`${componentName}\`, expected a ReactNode.`
);
}
@@ -434,9 +424,8 @@ function createShapeTypeChecker(shapeTypes) {
var propValue = props[propName];
var propType = getPropType(propValue);
if (propType !== 'object') {
var locationName = ReactPropTypeLocationNames[location];
return new PropTypeError(
`Invalid ${locationName} \`${propFullName}\` of type \`${propType}\` ` +
`Invalid ${location} \`${propFullName}\` of type \`${propType}\` ` +
`supplied to \`${componentName}\`, expected \`object\`.`
);
}
@@ -14,7 +14,6 @@
describe('ReactPropTypesProduction', function() {
var PropTypes;
var React;
var ReactPropTypeLocations;
var ReactTestUtils;
var oldProcess;
@@ -32,7 +31,6 @@ describe('ReactPropTypesProduction', function() {
jest.resetModules();
PropTypes = require('ReactPropTypes');
React = require('React');
ReactPropTypeLocations = require('ReactPropTypeLocations');
ReactTestUtils = require('ReactTestUtils');
});
@@ -48,7 +46,7 @@ describe('ReactPropTypesProduction', function() {
props,
'testProp',
'testComponent',
ReactPropTypeLocations.prop
'prop'
);
}).toThrowError(
'React.PropTypes type checking code is stripped in production.'
@@ -182,7 +182,7 @@ function processChildContext(fiber : Fiber, parentContext : Object, isReconcilin
// assume anything about the given fiber. We won't pass it down if we aren't sure.
// TODO: remove this hack when we delete unstable_renderSubtree in Fiber.
const workInProgress = isReconciling ? fiber : null;
checkReactTypeSpec(childContextTypes, childContext, 'childContext', name, null, workInProgress);
checkReactTypeSpec(childContextTypes, childContext, 'child context', name, null, workInProgress);
}
return {...parentContext, ...childContext};
}
@@ -33,8 +33,6 @@ var shallowEqual = require('shallowEqual');
var shouldUpdateReactComponent = require('shouldUpdateReactComponent');
var warning = require('warning');
import type { ReactPropTypeLocations } from 'ReactPropTypeLocations';
function StatelessComponent(Component) {
}
StatelessComponent.prototype.render = function() {
@@ -687,7 +685,7 @@ var ReactCompositeComponent = {
this._checkContextTypes(
Component.childContextTypes,
childContext,
'childContext'
'child context'
);
}
for (var name in childContext) {
@@ -730,7 +728,7 @@ var ReactCompositeComponent = {
_checkContextTypes: function(
typeSpecs,
values,
location: ReactPropTypeLocations,
location: string,
) {
if (__DEV__) {
checkReactTypeSpec(
@@ -1,29 +0,0 @@
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
* @providesModule ReactPropTypeLocationNames
*/
'use strict';
import type { ReactPropTypeLocations } from 'ReactPropTypeLocations';
type NamesType = {[key: ReactPropTypeLocations]: string};
var ReactPropTypeLocationNames: NamesType = {};
if (__DEV__) {
ReactPropTypeLocationNames = {
prop: 'prop',
context: 'context',
childContext: 'child context',
};
}
module.exports = ReactPropTypeLocationNames;
@@ -1,18 +0,0 @@
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
* @providesModule ReactPropTypeLocations
*/
'use strict';
export type ReactPropTypeLocations =
'prop' |
'context' |
'childContext';
+3 -6
View File
@@ -11,14 +11,11 @@
'use strict';
var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');
var ReactPropTypesSecret = require('ReactPropTypesSecret');
var invariant = require('invariant');
var warning = require('warning');
import type { ReactPropTypeLocations } from 'ReactPropTypeLocations';
var ReactComponentTreeHook;
if (
@@ -51,7 +48,7 @@ var loggedTypeFailures = {};
function checkReactTypeSpec(
typeSpecs,
values,
location: ReactPropTypeLocations,
location: string,
componentName,
element,
// It is only safe to pass fiber if it is the work-in-progress version, and
@@ -72,7 +69,7 @@ function checkReactTypeSpec(
'%s: %s type `%s` is invalid; it must be a function, usually from ' +
'React.PropTypes.',
componentName || 'React class',
ReactPropTypeLocationNames[location],
location,
typeSpecName
);
error = typeSpecs[typeSpecName](values, typeSpecName, componentName, location, null, ReactPropTypesSecret);
@@ -87,7 +84,7 @@ function checkReactTypeSpec(
'creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and ' +
'shape all require an argument).',
componentName || 'React class',
ReactPropTypeLocationNames[location],
location,
typeSpecName,
typeof error
);