• 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/removeTags.js
1
// @ts-check
1✔
2
/**
1✔
3
 * This class removes tag lines after a  `jsdoc-remove-next-tag` tag is found.
1✔
4
 */
1✔
5
class RemoveTags {
1✔
6
  /**
1✔
7
   * @param {EventEmitter} events       To hook to the necessary events to parse the code.
1✔
8
   * @param {EventNames}   EVENT_NAMES  To get the name of the events the class needs to
1✔
9
   *                                    listen for.
1✔
10
   */
1✔
11
  constructor(events, EVENT_NAMES) {
1✔
12
    /**
4✔
13
     * The list of comments that use `jsdoc-remove`.
4✔
14
     *
4✔
15
     * @type {string[]}
4✔
16
     * @access protected
4✔
17
     * @ignore
4✔
18
     */
4✔
19
    this._comments = [];
4✔
20
    /**
4✔
21
     * The expression that validates that the `jsdoc-remove-next-tag` tag is being used.
4✔
22
     * This expression is used on multiple places, that's why it's declared as a property.
4✔
23
     *
4✔
24
     * @type {RegExp}
4✔
25
     * @access protected
4✔
26
     * @ignore
4✔
27
     */
4✔
28
    this._removeExpression = /\s*\* @jsdoc-remove-next-tag\s*/;
4✔
29
    // Setup the listeners.
4✔
30
    events.on(EVENT_NAMES.newComment, this._readComment.bind(this));
4✔
31
    events.on(EVENT_NAMES.commentsReady, this._replaceComments.bind(this));
4✔
32
  }
4✔
33
  /**
1✔
34
   * This is called every time a new JSDoc comment block is found on a file. It validates
1✔
35
   * if the block uses `jsdoc-remove-next-tag` and saves it so it can be parsed later.
1✔
36
   *
1✔
37
   * @param {string} comment  The comment to analyze.
1✔
38
   * @access protected
1✔
39
   * @ignore
1✔
40
   */
1✔
41
  _readComment(comment) {
1✔
42
    if (comment.match(this._removeExpression)) {
3✔
43
      this._comments.push(comment);
2✔
44
    }
2✔
45
  }
3✔
46
  /**
1✔
47
   * This is called after all the JSDoc comments block for a file were found and the
1✔
48
   * plugin is ready to make changes.
1✔
49
   * The method takes all the comments that were found before and, if the comment includes
1✔
50
   * the `jsdoc-remove-next-tag` tag, it will remove the next lines until it finds a new
1✔
51
   * tag.
1✔
52
   *
1✔
53
   * @param {string} source  The code of the file being parsed.
1✔
54
   * @returns {string}
1✔
55
   * @access protected
1✔
56
   * @ignore
1✔
57
   */
1✔
58
  _replaceComments(source) {
1✔
59
    const result = this._comments.reduce((acc, comment) => {
3✔
60
      const lines = comment.split('\n');
2✔
61
      let removing = false;
2✔
62
      let removedAtLeastOneLine = false;
2✔
63
      /**
2✔
64
       * @type {string[]}
2✔
65
       */
2✔
66
      const newLinesAcc = [];
2✔
67
      const newLines = lines.reduce((sacc, line, index) => {
2✔
68
        if (index === lines.length - 1) {
18✔
69
          sacc.push(line);
2✔
70
          return sacc;
2✔
71
        }
2✔
72

16✔
73
        if (line.match(this._removeExpression)) {
18✔
74
          removing = true;
4✔
75
          removedAtLeastOneLine = false;
4✔
76
          return sacc;
4✔
77
        }
4✔
78

12✔
79
        if (removing && line.match(/^\s*\* @/)) {
18✔
80
          if (removedAtLeastOneLine) {
7✔
81
            removing = false;
3✔
82
          } else {
7✔
83
            removedAtLeastOneLine = true;
4✔
84
          }
4✔
85
        }
7✔
86

12✔
87
        if (!removing) {
18✔
88
          sacc.push(line);
8✔
89
        }
8✔
90

12✔
91
        return sacc;
12✔
92
      }, newLinesAcc);
2✔
93
      return acc.replace(comment, newLines.join('\n'));
2✔
94
    }, source);
3✔
95

3✔
96
    this._comments = [];
3✔
97
    return result;
3✔
98
  }
3✔
99
}
1✔
100

1✔
101
module.exports.RemoveTags = RemoveTags;
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

© 2026 Coveralls, Inc