• 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

81.65
/packages/react-docgen/src/utils/getMemberExpressionValuePath.ts
1
import type { NodePath } from '@babel/traverse';
3✔
2
import { visitors } from '@babel/traverse';
3✔
3
import type { Expression } from '@babel/types';
3✔
4
import getNameOrValue from './getNameOrValue.js';
3✔
5
import { String as toString } from './expressionTo.js';
3✔
6
import isReactForwardRefCall from './isReactForwardRefCall.js';
3✔
7

3✔
8
function resolveName(path: NodePath): string | undefined {
1,749✔
9
  if (path.isVariableDeclaration()) {
1,749✔
10
    const declarations = path.get('declarations');
48✔
11

48✔
12
    if (declarations.length > 1) {
48!
13
      throw new TypeError(
×
14
        'Got unsupported VariableDeclaration. VariableDeclaration must only ' +
×
15
          'have a single VariableDeclarator. Got ' +
×
16
          declarations.length +
×
17
          ' declarations.',
×
18
      );
×
19
    }
×
20
    // VariableDeclarator always has at least one declaration, hence the non-null-assertion
48✔
21
    const id = declarations[0]!.get('id');
48✔
22

48✔
23
    if (id.isIdentifier()) {
48✔
24
      return id.node.name;
48✔
25
    }
48!
26

×
27
    return;
×
28
  }
✔
29

1,701✔
30
  if (path.isFunctionDeclaration()) {
1,731✔
31
    const id = path.get('id');
477✔
32

477✔
33
    if (id.isIdentifier()) {
477✔
34
      return id.node.name;
474✔
35
    }
474✔
36

3✔
37
    return;
3✔
38
  }
3✔
39

1,224✔
40
  if (
1,224✔
41
    path.isFunctionExpression() ||
1,224✔
42
    path.isArrowFunctionExpression() ||
1,185✔
43
    path.isTaggedTemplateExpression() ||
858✔
44
    path.isCallExpression() ||
858!
45
    isReactForwardRefCall(path)
×
46
  ) {
1,749✔
47
    let currentPath: NodePath = path;
1,224✔
48

1,224✔
49
    while (currentPath.parentPath) {
1,224✔
50
      if (currentPath.parentPath.isVariableDeclarator()) {
2,043✔
51
        const id = currentPath.parentPath.get('id');
1,029✔
52

1,029✔
53
        if (id.isIdentifier()) {
1,029✔
54
          return id.node.name;
1,029✔
55
        }
1,029!
56

×
57
        return;
×
58
      }
✔
59

1,014✔
60
      currentPath = currentPath.parentPath;
1,014✔
61
    }
1,014✔
62

195✔
63
    return;
195✔
64
  }
195!
65

×
66
  throw new TypeError(
×
67
    'Attempted to resolveName for an unsupported path. resolveName does not accept ' +
×
68
      path.node.type +
×
69
      '".',
×
70
  );
×
71
}
×
72

3✔
73
interface TraverseState {
3✔
74
  readonly memberName: string;
3✔
75
  readonly localName: string;
3✔
76
  result: NodePath<Expression> | null;
3✔
77
}
3✔
78

3✔
79
const explodedVisitors = visitors.explode<TraverseState>({
3✔
80
  AssignmentExpression: {
3✔
81
    enter: function (path, state) {
3✔
82
      const memberPath = path.get('left');
1,218✔
83

1,218✔
84
      if (!memberPath.isMemberExpression()) {
1,218!
85
        return;
×
86
      }
×
87
      const property = memberPath.get('property');
1,218✔
88

1,218✔
89
      if (
1,218✔
90
        (!memberPath.node.computed ||
1,218✔
91
          property.isStringLiteral() ||
9✔
92
          property.isNumericLiteral()) &&
6✔
93
        getNameOrValue(property) === state.memberName &&
1,212✔
94
        toString(memberPath.get('object')) === state.localName
435✔
95
      ) {
1,218✔
96
        state.result = path.get('right');
423✔
97
        path.stop();
423✔
98
      }
423✔
99
    },
1,218✔
100
  },
3✔
101
});
3✔
102

3✔
103
export default function getMemberExpressionValuePath(
1,749✔
104
  variableDefinition: NodePath,
1,749✔
105
  memberName: string,
1,749✔
106
): NodePath<Expression> | null {
1,749✔
107
  const localName = resolveName(variableDefinition);
1,749✔
108

1,749✔
109
  if (!localName) {
1,749✔
110
    // likely an immediately exported and therefore nameless/anonymous node
198✔
111
    // passed in
198✔
112
    return null;
198✔
113
  }
198✔
114

1,551✔
115
  const state: TraverseState = {
1,551✔
116
    localName,
1,551✔
117
    memberName,
1,551✔
118
    result: null,
1,551✔
119
  };
1,551✔
120

1,551✔
121
  variableDefinition.hub.file.traverse(explodedVisitors, state);
1,551✔
122

1,551✔
123
  return state.result;
1,551✔
124
}
1,551✔
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