• 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

85.48
/packages/react-docgen/src/FileState.ts
1
import type { HubInterface, Scope, Visitor } from '@babel/traverse';
2
import babelTraverse, { NodePath } from '@babel/traverse';
3✔
3
import type { File, Node, Program } from '@babel/types';
4
import type { Importer, ImportPath } from './importer/index.js';
5
import babelParse from './babelParser.js';
3✔
6
import type { TransformOptions } from '@babel/core';
7

8
// Workaround while babel is not a proper ES module
9
const traverse = babelTraverse.default ?? (babelTraverse as never);
3!
10

11
export default class FileState {
3✔
12
  opts: TransformOptions;
13
  path: NodePath<Program>;
14
  ast: File;
15
  scope: Scope;
16
  code: string;
17

18
  #importer: Importer;
5,415✔
19

20
  hub: HubInterface = {
5,415✔
21
    // keep it for the usage in babel-core, ex: path.hub.file.opts.filename
22
    file: this,
5,415✔
23
    parse: this.parse.bind(this),
5,415✔
24
    import: this.import.bind(this),
5,415✔
25
    getCode: () => this.code,
5,415✔
26
    getScope: () => this.scope,
5,415✔
27
    addHelper: () => undefined,
5,415✔
28
    buildError: <E extends Error>(
5,415✔
29
      node: Node,
×
30
      msg: string,
×
31
      Error: new (message?: string) => E,
×
32
    ): E & { node: Node } => {
×
33
      const err = new Error(msg);
×
34

35
      (err as E & { node: Node }).node = node;
×
36

37
      return err as E & { node: Node };
×
38
    },
×
39
  };
5,415✔
40

41
  constructor(
5,415✔
42
    options: TransformOptions,
5,415✔
43
    { code, ast, importer }: { code: string; ast: File; importer: Importer },
5,415✔
44
  ) {
5,415✔
45
    this.opts = options;
5,415✔
46
    this.code = code;
5,415✔
47
    this.ast = ast;
5,415✔
48
    this.#importer = importer;
5,415✔
49

50
    this.path = NodePath.get({
5,415✔
51
      hub: this.hub,
5,415✔
52
      parentPath: null,
5,415✔
53
      parent: this.ast,
5,415✔
54
      container: this.ast,
5,415✔
55
      key: 'program',
5,415✔
56
    }).setContext() as NodePath<Program>;
5,415✔
57
    this.scope = this.path.scope;
5,415✔
58
  }
5,415✔
59

60
  /**
61
   * Try to resolve and import the ImportPath with the `name`
62
   */
63
  import(path: ImportPath, name: string): NodePath | null {
5,415✔
64
    return this.#importer(path, name, this);
4,023✔
65
  }
4,023✔
66

67
  /**
68
   * Parse the content of a new file
69
   * The `filename` is required so that potential imports inside the content can be correctly resolved and
70
   * the correct babel config file could be loaded. `filename` needs to be an absolute path.
71
   */
72
  parse(code: string, filename: string): FileState {
5,415✔
73
    const newOptions = { ...this.opts, filename };
69✔
74
    // We need to build a new parser, because there might be a new
75
    // babel config file in effect, so we need to load it
76
    const ast = babelParse(code, newOptions);
69✔
77

78
    return new FileState(newOptions, {
69✔
79
      ast,
69✔
80
      code,
69✔
81
      importer: this.#importer,
69✔
82
    });
69✔
83
  }
69✔
84

85
  traverse<S>(visitors: Visitor<S>, state?: S): void;
86
  traverse(visitors: Visitor): void;
87
  /**
88
   * Traverse the current file
89
   */
90
  traverse(visitors: Visitor, state?: unknown): void {
5,415✔
91
    traverse(this.ast, visitors, this.scope, state);
2,841✔
92
  }
2,841✔
93
}
5,415✔
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