mirror of
https://github.com/swc-project/swc.git
synced 2024-12-22 21:21:31 +03:00
f44e25c3af
swc_common: - Add `Span.has_mark`. swc_ecma_codegen: - Emit `1e3` for `1000`. - Optimize output. (#1986) swc_ecma_minifier: - name mangler: Don't use keywords as an id. - `properties`: Optimize member expression with string properties. - `inline`: Inline some function expressions even if it's not fn-local. - `analyzer`: Track reassignment correctly. - `analyzer`: Track fn-local correctly. - `sequences`: Inject `void` if required. - `inline`: Inline function declarations correctly. - `sequences`: Merge expressions into test of if statements. - `sequences`: Reduce calls to an assigned variable. - Use `Marks` instead of `&dyn Comments`. swc_ecma_transforms_optimization: - `expr_simplifier`: Fix infinite loops. node/swc: - Ensure that `.transform` performs minification. (#1989)
86 lines
4.3 KiB
JavaScript
86 lines
4.3 KiB
JavaScript
function validatePropTypes(element) {
|
|
function checkPropTypes(typeSpecs, values, location, componentName, element) {
|
|
{
|
|
// $FlowFixMe This is okay but Flow doesn't know it.
|
|
var has = Function.call.bind(Object.prototype.hasOwnProperty);
|
|
|
|
for (var typeSpecName in typeSpecs) {
|
|
if (has(typeSpecs, typeSpecName)) {
|
|
var error$1 = void 0; // Prop type validation may throw. In case they do, we don't want to
|
|
// fail the render phase where it didn't fail before. So we log it.
|
|
// After these have been cleaned up, we'll let them throw.
|
|
|
|
try {
|
|
// This is intentionally an invariant that gets caught. It's the same
|
|
// behavior as without this statement except with a better message.
|
|
if (typeof typeSpecs[typeSpecName] !== 'function') {
|
|
var err = Error((componentName || 'React class') + ': ' + location + ' type `' + typeSpecName + '` is invalid; ' + 'it must be a function, usually from the `prop-types` package, but received `' + typeof typeSpecs[typeSpecName] + '`.' + 'This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.');
|
|
err.name = 'Invariant Violation';
|
|
throw err;
|
|
}
|
|
|
|
error$1 = typeSpecs[typeSpecName](values, typeSpecName, componentName, location, null, 'SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED');
|
|
} catch (ex) {
|
|
error$1 = ex;
|
|
}
|
|
|
|
if (error$1 && !(error$1 instanceof Error)) {
|
|
setCurrentlyValidatingElement(element);
|
|
|
|
error('%s: type specification of %s' + ' `%s` is invalid; the type checker ' + 'function must return `null` or an `Error` but returned a %s. ' + 'You may have forgotten to pass an argument to the type checker ' + 'creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and ' + 'shape all require an argument).', componentName || 'React class', location, typeSpecName, typeof error$1);
|
|
|
|
setCurrentlyValidatingElement(null);
|
|
}
|
|
|
|
if (error$1 instanceof Error && !(error$1.message in loggedTypeFailures)) {
|
|
// Only monitor this failure once because there tends to be a lot of the
|
|
// same error.
|
|
loggedTypeFailures[error$1.message] = true;
|
|
setCurrentlyValidatingElement(element);
|
|
|
|
error('Failed %s type: %s', location, error$1.message);
|
|
|
|
setCurrentlyValidatingElement(null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
{
|
|
var type = element.type;
|
|
|
|
if (type === null || type === undefined || typeof type === 'string') {
|
|
return;
|
|
}
|
|
|
|
var propTypes;
|
|
|
|
if (typeof type === 'function') {
|
|
propTypes = type.propTypes;
|
|
} else if (typeof type === 'object' && (type.$$typeof === REACT_FORWARD_REF_TYPE || // Note: Memo only checks outer props here.
|
|
// Inner props are checked in the reconciler.
|
|
type.$$typeof === REACT_MEMO_TYPE)) {
|
|
propTypes = type.propTypes;
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
if (propTypes) {
|
|
// Intentionally inside to avoid triggering lazy initializers:
|
|
var name = getComponentName(type);
|
|
checkPropTypes(propTypes, element.props, 'prop', name, element);
|
|
} else if (type.PropTypes !== undefined && !propTypesMisspellWarningShown) {
|
|
propTypesMisspellWarningShown = true; // Intentionally inside to avoid triggering lazy initializers:
|
|
|
|
var _name = getComponentName(type);
|
|
|
|
error('Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?', _name || 'Unknown');
|
|
}
|
|
|
|
if (typeof type.getDefaultProps === 'function' && !type.getDefaultProps.isReactClassApproved) {
|
|
error('getDefaultProps is only used on classic React.createClass ' + 'definitions. Use a static property named `defaultProps` instead.');
|
|
}
|
|
}
|
|
} |