MCPcopy Create free account
hub / github.com/reactjs/react-docgen / getPropertyName

Function getPropertyName

packages/react-docgen/src/utils/getPropertyName.ts:24–70  ·  view source on GitHub ↗
(
  propertyPath: NodePath<
    | ClassMethod
    | ClassProperty
    | ObjectMethod
    | ObjectProperty
    | ObjectTypeProperty
    | ObjectTypeSpreadProperty
    | SpreadElement
    | TSMethodSignature
    | TSPropertySignature
  >,
)

Source from the content-addressed store, hash-verified

22 * returns the value of the literal or name of the identifier.
23 */
24export default function getPropertyName(
25 propertyPath: NodePath<
26 | ClassMethod
27 | ClassProperty
28 | ObjectMethod
29 | ObjectProperty
30 | ObjectTypeProperty
31 | ObjectTypeSpreadProperty
32 | SpreadElement
33 | TSMethodSignature
34 | TSPropertySignature
35 >,
36): string | null {
37 if (propertyPath.isObjectTypeSpreadProperty()) {
38 const argument = propertyPath.get('argument');
39
40 if (argument.isGenericTypeAnnotation()) {
41 return getNameOrValue(argument.get('id')) as string;
42 }
43
44 return null;
45 } else if (propertyPath.has('computed')) {
46 const key = propertyPath.get('key') as NodePath<Expression>;
47
48 // Try to resolve variables and member expressions
49 if (key.isIdentifier() || key.isMemberExpression()) {
50 const valuePath = resolveToValue(key);
51
52 if (valuePath.isStringLiteral() || valuePath.isNumericLiteral()) {
53 return `${valuePath.node.value}`;
54 }
55 }
56
57 // generate name for identifier
58 if (key.isIdentifier()) {
59 return `${COMPUTED_PREFIX}${key.node.name}`;
60 }
61
62 if (key.isStringLiteral() || key.isNumericLiteral()) {
63 return `${key.node.value}`;
64 }
65
66 return null;
67 }
68
69 return `${getNameOrValue(propertyPath.get('key') as NodePath)}`;
70}

Callers 11

handleTSTypeLiteralFunction · 0.85
setPropDescriptionFunction · 0.85
getFlowType.tsFile · 0.85
getMethodNameFunction · 0.85
getPropertyValuePathFunction · 0.85
getPropTypeShapishFunction · 0.85
setPropDescriptorFunction · 0.85
amendPropTypesFunction · 0.85

Calls 3

getNameOrValueFunction · 0.85
resolveToValueFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…