Skip to content

Commit d72700f

Browse files
authored
Remove runtime dependency on prop-types (#18127)
* Remove runtime dep on prop-types * Fix test
1 parent 549e418 commit d72700f

File tree

16 files changed

+94
-74
lines changed

16 files changed

+94
-74
lines changed

packages/react-art/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"create-react-class": "^15.6.2",
2727
"loose-envify": "^1.1.0",
2828
"object-assign": "^4.1.1",
29-
"prop-types": "^15.6.2",
3029
"scheduler": "^0.19.0"
3130
},
3231
"peerDependencies": {

packages/react-dom/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"dependencies": {
2020
"loose-envify": "^1.1.0",
2121
"object-assign": "^4.1.1",
22-
"prop-types": "^15.6.2",
2322
"scheduler": "^0.19.0"
2423
},
2524
"peerDependencies": {

packages/react-dom/src/server/ReactPartialRendererContext.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ import type {ReactContext} from 'shared/ReactTypes';
1212

1313
import {disableLegacyContext} from 'shared/ReactFeatureFlags';
1414
import {REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE} from 'shared/ReactSymbols';
15-
import ReactSharedInternals from 'shared/ReactSharedInternals';
1615
import getComponentName from 'shared/getComponentName';
17-
import checkPropTypes from 'prop-types/checkPropTypes';
16+
import checkPropTypes from 'shared/checkPropTypes';
1817

19-
let ReactDebugCurrentFrame;
2018
let didWarnAboutInvalidateContextType;
2119
if (__DEV__) {
22-
ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
2320
didWarnAboutInvalidateContextType = new Set();
2421
}
2522

@@ -42,13 +39,7 @@ function maskContext(type, context) {
4239

4340
function checkContextTypes(typeSpecs, values, location: string) {
4441
if (__DEV__) {
45-
checkPropTypes(
46-
typeSpecs,
47-
values,
48-
location,
49-
'Component',
50-
ReactDebugCurrentFrame.getCurrentStack,
51-
);
42+
checkPropTypes(typeSpecs, values, location, 'Component');
5243
}
5344
}
5445

packages/react-dom/src/shared/ReactControlledValuePropTypes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import checkPropTypes from 'prop-types/checkPropTypes';
8+
import checkPropTypes from 'shared/checkPropTypes';
99
import ReactSharedInternals from 'shared/ReactSharedInternals';
1010
import {enableDeprecatedFlareAPI} from 'shared/ReactFeatureFlags';
1111

packages/react-flight/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
},
3030
"dependencies": {
3131
"loose-envify": "^1.1.0",
32-
"object-assign": "^4.1.1",
33-
"prop-types": "^15.6.2"
32+
"object-assign": "^4.1.1"
3433
},
3534
"browserify": {
3635
"transform": [

packages/react-native-renderer/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
},
1010
"dependencies": {
1111
"object-assign": "^4.1.1",
12-
"prop-types": "^15.6.2",
1312
"scheduler": "^0.11.0"
1413
},
1514
"peerDependencies": {

packages/react-noop-renderer/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"license": "MIT",
1313
"dependencies": {
1414
"object-assign": "^4.1.1",
15-
"prop-types": "^15.6.2",
1615
"regenerator-runtime": "^0.11.0",
1716
"react-reconciler": "*",
1817
"react-flight": "*",

packages/react-reconciler/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"dependencies": {
3333
"loose-envify": "^1.1.0",
3434
"object-assign": "^4.1.1",
35-
"prop-types": "^15.6.2",
3635
"scheduler": "^0.19.0"
3736
},
3837
"browserify": {

packages/react-reconciler/src/ReactFiberBeginWork.js

+2-17
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {
1818
} from './ReactFiberSuspenseComponent';
1919
import type {SuspenseContext} from './ReactFiberSuspenseContext';
2020

21-
import checkPropTypes from 'prop-types/checkPropTypes';
21+
import checkPropTypes from 'shared/checkPropTypes';
2222

2323
import {
2424
IndeterminateComponent,
@@ -75,7 +75,6 @@ import {refineResolvedLazyComponent} from 'shared/ReactLazyComponent';
7575
import {REACT_LAZY_TYPE, getIteratorFn} from 'shared/ReactSymbols';
7676
import {
7777
getCurrentFiberOwnerNameInDevOrNull,
78-
getCurrentFiberStackInDev,
7978
setIsRendering,
8079
} from './ReactCurrentFiber';
8180
import {startWorkTimer, cancelWorkTimer} from './ReactDebugFiberPerf';
@@ -296,7 +295,6 @@ function updateForwardRef(
296295
nextProps, // Resolved props
297296
'prop',
298297
getComponentName(Component),
299-
getCurrentFiberStackInDev,
300298
);
301299
}
302300
}
@@ -414,7 +412,6 @@ function updateMemoComponent(
414412
nextProps, // Resolved props
415413
'prop',
416414
getComponentName(type),
417-
getCurrentFiberStackInDev,
418415
);
419416
}
420417
}
@@ -442,7 +439,6 @@ function updateMemoComponent(
442439
nextProps, // Resolved props
443440
'prop',
444441
getComponentName(type),
445-
getCurrentFiberStackInDev,
446442
);
447443
}
448444
}
@@ -501,7 +497,6 @@ function updateSimpleMemoComponent(
501497
nextProps, // Resolved (SimpleMemoComponent has no defaultProps)
502498
'prop',
503499
getComponentName(outerMemoType),
504-
getCurrentFiberStackInDev,
505500
);
506501
}
507502
// Inner propTypes will be validated in the function component path.
@@ -626,7 +621,6 @@ function updateFunctionComponent(
626621
nextProps, // Resolved props
627622
'prop',
628623
getComponentName(Component),
629-
getCurrentFiberStackInDev,
630624
);
631625
}
632626
}
@@ -793,7 +787,6 @@ function updateClassComponent(
793787
nextProps, // Resolved props
794788
'prop',
795789
getComponentName(Component),
796-
getCurrentFiberStackInDev,
797790
);
798791
}
799792
}
@@ -1198,7 +1191,6 @@ function mountLazyComponent(
11981191
resolvedProps, // Resolved for outer only
11991192
'prop',
12001193
getComponentName(Component),
1201-
getCurrentFiberStackInDev,
12021194
);
12031195
}
12041196
}
@@ -2622,13 +2614,7 @@ function updateContextProvider(
26222614
const providerPropTypes = workInProgress.type.propTypes;
26232615

26242616
if (providerPropTypes) {
2625-
checkPropTypes(
2626-
providerPropTypes,
2627-
newProps,
2628-
'prop',
2629-
'Context.Provider',
2630-
getCurrentFiberStackInDev,
2631-
);
2617+
checkPropTypes(providerPropTypes, newProps, 'prop', 'Context.Provider');
26322618
}
26332619
}
26342620

@@ -3208,7 +3194,6 @@ function beginWork(
32083194
resolvedProps, // Resolved for outer only
32093195
'prop',
32103196
getComponentName(type),
3211-
getCurrentFiberStackInDev,
32123197
);
32133198
}
32143199
}

packages/react-reconciler/src/ReactFiberContext.js

+3-21
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ import {disableLegacyContext} from 'shared/ReactFeatureFlags';
1515
import {ClassComponent, HostRoot} from 'shared/ReactWorkTags';
1616
import getComponentName from 'shared/getComponentName';
1717
import invariant from 'shared/invariant';
18-
import checkPropTypes from 'prop-types/checkPropTypes';
18+
import checkPropTypes from 'shared/checkPropTypes';
1919

20-
import {getCurrentFiberStackInDev} from './ReactCurrentFiber';
2120
import {startPhaseTimer, stopPhaseTimer} from './ReactDebugFiberPerf';
2221
import {createCursor, push, pop} from './ReactFiberStack';
2322

@@ -105,13 +104,7 @@ function getMaskedContext(
105104

106105
if (__DEV__) {
107106
const name = getComponentName(type) || 'Unknown';
108-
checkPropTypes(
109-
contextTypes,
110-
context,
111-
'context',
112-
name,
113-
getCurrentFiberStackInDev,
114-
);
107+
checkPropTypes(contextTypes, context, 'context', name);
115108
}
116109

117110
// Cache unmasked context so we can avoid recreating masked context unless necessary.
@@ -223,18 +216,7 @@ function processChildContext(
223216
}
224217
if (__DEV__) {
225218
const name = getComponentName(type) || 'Unknown';
226-
checkPropTypes(
227-
childContextTypes,
228-
childContext,
229-
'child context',
230-
name,
231-
// In practice, there is one case in which we won't get a stack. It's when
232-
// somebody calls unstable_renderSubtreeIntoContainer() and we process
233-
// context from the parent component instance. The stack will be missing
234-
// because it's outside of the reconciliation, and so the pointer has not
235-
// been set. This is rare and doesn't matter. We'll also remove that API.
236-
getCurrentFiberStackInDev,
237-
);
219+
checkPropTypes(childContextTypes, childContext, 'child context', name);
238220
}
239221

240222
return {...parentContext, ...childContext};

packages/react-server/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
},
3131
"dependencies": {
3232
"loose-envify": "^1.1.0",
33-
"object-assign": "^4.1.1",
34-
"prop-types": "^15.6.2"
33+
"object-assign": "^4.1.1"
3534
},
3635
"browserify": {
3736
"transform": [

packages/react-test-renderer/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"homepage": "https://reactjs.org/",
2121
"dependencies": {
2222
"object-assign": "^4.1.1",
23-
"prop-types": "^15.6.2",
2423
"react-is": "^16.8.6",
2524
"react-shallow-renderer": "^16.12.0",
2625
"scheduler": "^0.19.0"

packages/react/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
},
2828
"dependencies": {
2929
"loose-envify": "^1.1.0",
30-
"object-assign": "^4.1.1",
31-
"prop-types": "^15.6.2"
30+
"object-assign": "^4.1.1"
3231
},
3332
"browserify": {
3433
"transform": [

packages/react/src/ReactElementValidator.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
REACT_ELEMENT_TYPE,
2323
} from 'shared/ReactSymbols';
2424
import {warnAboutSpreadingKeyToJSX} from 'shared/ReactFeatureFlags';
25-
import checkPropTypes from 'prop-types/checkPropTypes';
25+
import checkPropTypes from 'shared/checkPropTypes';
2626

2727
import ReactCurrentOwner from './ReactCurrentOwner';
2828
import {
@@ -31,9 +31,7 @@ import {
3131
cloneElement,
3232
jsxDEV,
3333
} from './ReactElement';
34-
import ReactDebugCurrentFrame, {
35-
setCurrentlyValidatingElement,
36-
} from './ReactDebugCurrentFrame';
34+
import {setCurrentlyValidatingElement} from './ReactDebugCurrentFrame';
3735

3836
let propTypesMisspellWarningShown;
3937

@@ -213,13 +211,7 @@ function validatePropTypes(element) {
213211
}
214212
if (propTypes) {
215213
setCurrentlyValidatingElement(element);
216-
checkPropTypes(
217-
propTypes,
218-
element.props,
219-
'prop',
220-
name,
221-
ReactDebugCurrentFrame.getStackAddendum,
222-
);
214+
checkPropTypes(propTypes, element.props, 'prop', name);
223215
setCurrentlyValidatingElement(null);
224216
} else if (type.PropTypes !== undefined && !propTypesMisspellWarningShown) {
225217
propTypesMisspellWarningShown = true;

packages/react/src/__tests__/ReactElementValidator-test.internal.js

-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ describe('ReactElementValidator', () => {
399399
'returned a function. You may have forgotten to pass an argument to ' +
400400
'the type checker creator (arrayOf, instanceOf, objectOf, oneOf, ' +
401401
'oneOfType, and shape all require an argument).',
402-
{withoutStack: true},
403402
);
404403
});
405404

packages/shared/checkPropTypes.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
let loggedTypeFailures = {};
11+
12+
export default function checkPropTypes(
13+
typeSpecs: Object,
14+
values: Object,
15+
location: string,
16+
componentName: ?string,
17+
): void {
18+
if (__DEV__) {
19+
// $FlowFixMe This is okay but Flow doesn't know it.
20+
let has = Function.call.bind(Object.prototype.hasOwnProperty);
21+
for (let typeSpecName in typeSpecs) {
22+
if (has(typeSpecs, typeSpecName)) {
23+
let error;
24+
// Prop type validation may throw. In case they do, we don't want to
25+
// fail the render phase where it didn't fail before. So we log it.
26+
// After these have been cleaned up, we'll let them throw.
27+
try {
28+
// This is intentionally an invariant that gets caught. It's the same
29+
// behavior as without this statement except with a better message.
30+
if (typeof typeSpecs[typeSpecName] !== 'function') {
31+
let err = Error(
32+
(componentName || 'React class') +
33+
': ' +
34+
location +
35+
' type `' +
36+
typeSpecName +
37+
'` is invalid; ' +
38+
'it must be a function, usually from the `prop-types` package, but received `' +
39+
typeof typeSpecs[typeSpecName] +
40+
'`.' +
41+
'This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.',
42+
);
43+
err.name = 'Invariant Violation';
44+
throw err;
45+
}
46+
error = typeSpecs[typeSpecName](
47+
values,
48+
typeSpecName,
49+
componentName,
50+
location,
51+
null,
52+
'SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED',
53+
);
54+
} catch (ex) {
55+
error = ex;
56+
}
57+
if (error && !(error instanceof Error)) {
58+
console.error(
59+
'%s: type specification of %s' +
60+
' `%s` is invalid; the type checker ' +
61+
'function must return `null` or an `Error` but returned a %s. ' +
62+
'You may have forgotten to pass an argument to the type checker ' +
63+
'creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and ' +
64+
'shape all require an argument).',
65+
componentName || 'React class',
66+
location,
67+
typeSpecName,
68+
typeof error,
69+
);
70+
}
71+
if (error instanceof Error && !(error.message in loggedTypeFailures)) {
72+
// Only monitor this failure once because there tends to be a lot of the
73+
// same error.
74+
loggedTypeFailures[error.message] = true;
75+
console.error('Failed %s type: %s', location, error.message);
76+
}
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)