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

Function toArray

packages/react-docgen/src/utils/expressionTo.ts:11–90  ·  view source on GitHub ↗

* Splits a MemberExpression or CallExpression into parts. * E.g. foo.bar.baz becomes ['foo', 'bar', 'baz']

(path: NodePath<Node | null>)

Source from the content-addressed store, hash-verified

9 * E.g. foo.bar.baz becomes ['foo', 'bar', 'baz']
10 */
11function toArray(path: NodePath<Node | null>): string[] {
12 const parts = [path];
13 let result: string[] = [];
14
15 while (parts.length > 0) {
16 path = parts.shift() as NodePath;
17 if (path.isCallExpression()) {
18 parts.push(path.get('callee'));
19 continue;
20 } else if (path.isMemberExpression()) {
21 parts.push(path.get('object'));
22 const property = path.get('property');
23
24 if (path.node.computed) {
25 const resolvedPath = resolveToValue(property);
26
27 if (resolvedPath !== undefined) {
28 result = result.concat(toArray(resolvedPath));
29 } else {
30 result.push('<computed>');
31 }
32 } else if (property.isIdentifier()) {
33 result.push(property.node.name);
34 } else if (property.isPrivateName()) {
35 // new test
36 result.push(`#${property.get('id').node.name}`);
37 }
38 continue;
39 } else if (path.isIdentifier()) {
40 result.push(path.node.name);
41 continue;
42 } else if (path.isTSAsExpression()) {
43 const expression = path.get('expression');
44
45 if (expression.isIdentifier()) {
46 result.push(expression.node.name);
47 }
48 continue;
49 } else if (path.isLiteral() && path.node.extra?.raw) {
50 result.push(path.node.extra.raw as string);
51 continue;
52 } else if (path.isThisExpression()) {
53 result.push('this');
54 continue;
55 } else if (path.isObjectExpression()) {
56 const properties = path.get('properties').map(function (property) {
57 if (property.isSpreadElement()) {
58 return `...${toString(property.get('argument'))}`;
59 } else if (property.isObjectProperty()) {
60 return (
61 toString(property.get('key')) +
62 ': ' +
63 toString(property.get('value'))
64 );
65 } else if (property.isObjectMethod()) {
66 return toString(property.get('key')) + ': <function>';
67 } else {
68 throw new Error('Unrecognized object property type');

Callers 2

toStringFunction · 0.85
resolveToValueFunction · 0.85

Calls 3

resolveToValueFunction · 0.85
toStringFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…