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

homer0 / jsdoc-ts-utils / 4457244662

pending completion
4457244662

push

github

GitHub
Merge pull request #40 from homer0/next

103 of 103 branches covered (100.0%)

Branch coverage included in aggregate %.

1011 of 1011 relevant lines covered (100.0%)

2.42 hits per line

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

100.0
/src/features/modulesTypesShortName.js
1
// @ts-check
1✔
2
/**
1✔
3
 * This class monkey-patches the function JSDoc uses to register links in order to create "aliases"
1✔
4
 * for external types and modules' types so they can be used without the `external:` and
1✔
5
 * `module:[name].` prefixes.
1✔
6
 * For example: `module:shared/apiClient.APIClient` would also register `APIClient`; and
1✔
7
 * `external:Express` would also register `Express`.
1✔
8
 */
1✔
9
class ModulesTypesShortName {
1✔
10
  /**
1✔
11
   * @param {EventEmitter}        events               To hook to the event triggered when
1✔
12
   *                                                   parsing begins.
1✔
13
   * @param {JSDocTemplateHelper} jsdocTemplateHelper  To monkey-patch the `registerLink`
1✔
14
   *                                                   function.
1✔
15
   * @param {EventNames}          EVENT_NAMES          To get the name of the event the
1✔
16
   *                                                   class needs to listen for.
1✔
17
   */
1✔
18
  constructor(events, jsdocTemplateHelper, EVENT_NAMES) {
1✔
19
    /**
5✔
20
     * A local reference for the JSDoc template helper, where the `registerLink` function
5✔
21
     * will be monkey-patched.
5✔
22
     *
5✔
23
     * @type {JSDocTemplateHelper}
5✔
24
     * @access protected
5✔
25
     * @ignore
5✔
26
     */
5✔
27
    this._jsdocTemplateHelper = jsdocTemplateHelper;
5✔
28
    // Setup the listener.
5✔
29
    events.on(EVENT_NAMES.parseBegin, this._monkyPatchRegisterLink.bind(this));
5✔
30
  }
5✔
31
  /**
1✔
32
   * This is called by the plugin before starting to parse the files; the method takes
1✔
33
   * care of validating if the `registerLink` function of the template helper was already
1✔
34
   * patched and patch it if it wasn't.
1✔
35
   *
1✔
36
   * @access protected
1✔
37
   * @ignore
1✔
38
   */
1✔
39
  _monkyPatchRegisterLink() {
1✔
40
    // Check if the function needs patching.
4✔
41
    if (!('monkey' in this._jsdocTemplateHelper.registerLink)) {
4✔
42
      // Get a reference for the original.
4✔
43
      const original = this._jsdocTemplateHelper.registerLink;
4✔
44
      /**
4✔
45
       * The patch.
4✔
46
       *
4✔
47
       * @type {JSDocTemplateHelperRegisterLink}
4✔
48
       * @ignore
4✔
49
       */
4✔
50
      const patch = (longname, fileUrl) => {
4✔
51
        // Call the original function.
3✔
52
        const result = original(longname, fileUrl);
3✔
53
        // Extract the short name.
3✔
54
        const match =
3✔
55
          /module:[^\.]+\.((?:[^\.]+\.)?[^\.]+)$/i.exec(longname) ||
3✔
56
          /external:([^\.]+)$/i.exec(longname);
3✔
57
        if (match) {
3✔
58
          // If a short name was found, register it.
2✔
59
          const [, shortname] = match;
2✔
60
          original(shortname, fileUrl);
2✔
61
        }
2✔
62

3✔
63
        // Return the original result.
3✔
64
        return result;
3✔
65
      };
4✔
66
      // Add the flag.
4✔
67
      // @ts-ignore
4✔
68
      patch.monkey = true;
4✔
69
      // Replace the function on the helper.
4✔
70
      this._jsdocTemplateHelper.registerLink = patch;
4✔
71
    }
4✔
72
  }
4✔
73
}
1✔
74

1✔
75
module.exports.ModulesTypesShortName = ModulesTypesShortName;
1✔
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