12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import getClassMemberValuePath from './getClassMemberValuePath.js';
- import getMemberExpressionValuePath from './getMemberExpressionValuePath.js';
- import getPropertyValuePath from './getPropertyValuePath.js';
- import resolveFunctionDefinitionToReturnValue from '../utils/resolveFunctionDefinitionToReturnValue.js';
- const postprocessPropTypes = (path) => path.isFunction() ? resolveFunctionDefinitionToReturnValue(path) : path;
- const POSTPROCESS_MEMBERS = new Map([['propTypes', postprocessPropTypes]]);
- const SUPPORTED_DEFINITION_TYPES = [
-
- 'ArrowFunctionExpression',
-
- 'CallExpression',
- 'ClassDeclaration',
- 'ClassExpression',
-
- 'FunctionDeclaration',
-
- 'FunctionExpression',
- 'ObjectExpression',
-
- 'ObjectMethod',
-
- 'TaggedTemplateExpression',
- 'VariableDeclaration',
- ];
- export function isSupportedDefinitionType(path) {
- return SUPPORTED_DEFINITION_TYPES.includes(path.node.type);
- }
- export default function getMemberValuePath(componentDefinition, memberName) {
- let result;
- if (componentDefinition.isObjectExpression()) {
- result = getPropertyValuePath(componentDefinition, memberName);
- if (!result && memberName === 'defaultProps') {
- result = getPropertyValuePath(componentDefinition, 'getDefaultProps');
- }
- }
- else if (componentDefinition.isClassDeclaration() ||
- componentDefinition.isClassExpression()) {
- result = getClassMemberValuePath(componentDefinition, memberName);
- if (!result && memberName === 'defaultProps') {
- result = getClassMemberValuePath(componentDefinition, 'getDefaultProps');
- }
- }
- else {
- result = getMemberExpressionValuePath(componentDefinition, memberName);
- if (!result && memberName === 'defaultProps') {
- result = getMemberExpressionValuePath(componentDefinition, 'getDefaultProps');
- }
- }
- const postprocessMethod = POSTPROCESS_MEMBERS.get(memberName);
- if (result && postprocessMethod) {
- result = postprocessMethod(result);
- }
- return result;
- }
|