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

reactjs / react-docgen / 12954943867

24 Jan 2025 05:58PM CUT coverage: 96.391% (+0.01%) from 96.38%
12954943867

Pull #969

github

web-flow
Merge 27a26da4a into f9fc65e9c
Pull Request #969: Expose more Typescript types

1378 of 1478 branches covered (93.23%)

Branch coverage included in aggregate %.

46 of 46 new or added lines in 1 file covered. (100.0%)

7196 of 7417 relevant lines covered (97.02%)

433.68 hits per line

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

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

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

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

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

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

15✔
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,473✔
40
      result.push(path.node.name);
918✔
41
      continue;
918✔
42
    } else if (path.isTSAsExpression()) {
954✔
43
      const expression = path.get('expression');
21✔
44

21✔
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
          );
3✔
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

9✔
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,473✔
88

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

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

3✔
99
export { toString as String, toArray as Array };
3✔
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