react-docgen
Version:
A library to extract information from React components for documentation generation.
35 lines (34 loc) • 1.33 kB
JavaScript
import isUnreachableFlowType from '../utils/isUnreachableFlowType.js';
import resolveToValue from '../utils/resolveToValue.js';
import { unwrapUtilityType } from './flowUtilityTypes.js';
import getTypeIdentifier from './getTypeIdentifier.js';
function tryResolveGenericTypeAnnotation(path, seen = new WeakSet()) {
let typePath = unwrapUtilityType(path);
const idPath = getTypeIdentifier(typePath);
if (idPath) {
typePath = resolveToValue(idPath);
if (isUnreachableFlowType(typePath) || seen.has(typePath.node)) {
return;
}
seen.add(typePath.node);
if (typePath.isTypeAlias()) {
return tryResolveGenericTypeAnnotation(typePath.get('right'), seen);
}
else if (typePath.isTSTypeAliasDeclaration()) {
return tryResolveGenericTypeAnnotation(typePath.get('typeAnnotation'), seen);
}
return typePath;
}
return typePath;
}
/**
* Given an React component (stateless or class) tries to find the
* flow or ts type for the props. If not found or not one of the supported
* component types returns undefined.
*/
export default function resolveGenericTypeAnnotation(path) {
const typePath = tryResolveGenericTypeAnnotation(path);
if (!typePath || typePath === path)
return;
return typePath;
}