Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
) || null;
}
if (!React.isValidElement(propValue)) {
return new TypeError(
`${componentName}.${propName} is not a valid React element`,
);
}
const { type } = propValue;
const componentNameFromType = getComponentName(type);
const innerComponentName = namesOfHOCsToStrip.length > 0
? stripHOCs(componentNameFromType, namesOfHOCsToStrip)
: componentNameFromType;
if (isRegex(name) && !name.test(innerComponentName)) {
return new TypeError(
`\`${componentName}.${propName}\` only accepts components matching the regular expression ${name}`,
);
}
if (!isRegex(name) && innerComponentName !== name) {
return new TypeError(
`\`${componentName}.${propName}\` only accepts components named ${name}, got ${innerComponentName}`,
);
}
return null;
}
);
}
const { type } = propValue;
const componentNameFromType = getComponentName(type);
const innerComponentName = namesOfHOCsToStrip.length > 0
? stripHOCs(componentNameFromType, namesOfHOCsToStrip)
: componentNameFromType;
if (isRegex(name) && !name.test(innerComponentName)) {
return new TypeError(
`\`${componentName}.${propName}\` only accepts components matching the regular expression ${name}`,
);
}
if (!isRegex(name) && innerComponentName !== name) {
return new TypeError(
`\`${componentName}.${propName}\` only accepts components named ${name}, got ${innerComponentName}`,
);
}
return null;
}
export function hasClassName(node, className) {
let classes = propsOfNode(node).className || '';
classes = String(classes).replace(/\s/g, ' ');
if (isRegex(className)) return className.test(classes);
return ` ${classes} `.indexOf(` ${className} `) > -1;
}
export const functionThrows = (fn, context, args, value) => {
try {
fn.apply(context, args)
} catch (error) {
if (value == null)
return true
if (isFunction(value) && error instanceof value)
return true
const message = error.message || error
if (typeof message === 'string') {
if (isRegExp(value) && value.test(error.message))
return true
if (typeof value === 'string' && message.indexOf(value) !== -1)
return true
}
}
return false
}
export default function componentWithName(
name,
options = {},
) {
if (typeof name !== 'string' && !isRegex(name)) {
throw new TypeError('name must be a string or a regex');
}
const passedOptions = Object.keys(options);
if (passedOptions.length > 1 || (passedOptions.length === 1 && passedOptions[0] !== 'stripHOCs')) {
throw new TypeError(`The only options supported are: “stripHOCs”, got: “${passedOptions.join('”, “')}”`);
}
const { stripHOCs: namesOfHOCsToStrip = [] } = options;
const allHOCNamesAreValid = namesOfHOCsToStrip.every((x) => {
if (typeof x !== 'string' || /[()]/g.test(x)) {
return false;
}
return /^(?:[a-z][a-zA-Z0-9]+|[A-Z][a-z][a-zA-Z0-9]+)$/.test(x);
});
if (!allHOCNamesAreValid) {
pr = Promise.map(options.ignoreRegexp, (item) => {
if (!isRegex(item)) {
item = new RegExp(item);
}
if (item.test(selector)) {
return Promise.reject();
}
});
} else {