• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

reactjs / react-docgen / 15503494088

07 Jun 2025 02:52AM CUT coverage: 95.479% (-0.01%) from 95.49%
15503494088

Pull #999

github

web-flow
Merge c42d1b242 into ec11d1f1e
Pull Request #999: Packages ready to publish

1393 of 1494 branches covered (93.24%)

Branch coverage included in aggregate %.

4605 of 4788 relevant lines covered (96.18%)

522.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.0
/packages/react-docgen/src/utils/expressionTo.ts
1
/*eslint no-loop-func: 0, no-use-before-define: 0*/
2

3
import resolveToValue from './resolveToValue.js';
3✔
4
import type { NodePath } from '@babel/traverse';
5
import type { Node } from '@babel/types';
6

7
/**
8
 * Splits a MemberExpression or CallExpression into parts.
9
 * E.g. foo.bar.baz becomes ['foo', 'bar', 'baz']
10
 */
11
function toArray(path: NodePath<Node | null>): string[] {
957✔
12
  const parts = [path];
957✔
13
  let result: string[] = [];
957✔
14

15
  while (parts.length > 0) {
957✔
16
    path = parts.shift() as NodePath;
1,476✔
17
    if (path.isCallExpression()) {
1,476!
18
      parts.push(path.get('callee'));
×
19
      continue;
×
20
    } else if (path.isMemberExpression()) {
1,476✔
21
      parts.push(path.get('object'));
519✔
22
      const property = path.get('property');
519✔
23

24
      if (path.node.computed) {
519✔
25
        const resolvedPath = resolveToValue(property);
15✔
26

27
        if (resolvedPath !== undefined) {
15✔
28
          result = result.concat(toArray(resolvedPath));
15✔
29
        } else {
15!
30
          result.push('<computed>');
×
31
        }
×
32
      } else if (property.isIdentifier()) {
519✔
33
        result.push(property.node.name);
504✔
34
      } else if (property.isPrivateName()) {
504!
35
        // new test
36
        result.push(`#${property.get('id').node.name}`);
×
37
      }
×
38
      continue;
519✔
39
    } else if (path.isIdentifier()) {
1,476✔
40
      result.push(path.node.name);
921✔
41
      continue;
921✔
42
    } else if (path.isTSAsExpression()) {
957✔
43
      const expression = path.get('expression');
21✔
44

45
      if (expression.isIdentifier()) {
21✔
46
        result.push(expression.node.name);
21✔
47
      }
21✔
48
      continue;
21✔
49
    } else if (path.isLiteral() && path.node.extra?.raw) {
36✔
50
      result.push(path.node.extra.raw as string);
6✔
51
      continue;
6✔
52
    } else if (path.isThisExpression()) {
15!
53
      result.push('this');
×
54
      continue;
×
55
    } else if (path.isObjectExpression()) {
9✔
56
      const properties = path.get('properties').map(function (property) {
9✔
57
        if (property.isSpreadElement()) {
9✔
58
          return `...${toString(property.get('argument'))}`;
3✔
59
        } else if (property.isObjectProperty()) {
9✔
60
          return (
3✔
61
            toString(property.get('key')) +
3✔
62
            ': ' +
3✔
63
            toString(property.get('value'))
3✔
64
          );
65
        } else if (property.isObjectMethod()) {
3✔
66
          return toString(property.get('key')) + ': <function>';
3✔
67
        } else {
3!
68
          throw new Error('Unrecognized object property type');
×
69
        }
×
70
      });
9✔
71

72
      result.push('{' + properties.join(', ') + '}');
9✔
73
      continue;
9✔
74
    } else if (path.isArrayExpression()) {
9!
75
      result.push(
×
76
        '[' +
×
77
          path
×
78
            .get('elements')
×
79
            .map(function (el) {
×
80
              return toString(el);
×
81
            })
×
82
            .join(', ') +
×
83
          ']',
×
84
      );
×
85
      continue;
×
86
    }
×
87
  }
1,476✔
88

89
  return result.reverse();
957✔
90
}
957✔
91

92
/**
93
 * Creates a string representation of a member expression.
94
 */
95
function toString(path: NodePath<Node | null>): string {
450✔
96
  return toArray(path).join('.');
450✔
97
}
450✔
98

99
export { toString as String, toArray as Array };
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc