Blame view

node_modules/eslint/lib/util/comment-event-generator.js 3.45 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  /**
   * @fileoverview The event generator for comments.
   * @author Toru Nagashima
   */
  
  "use strict";
  
  //------------------------------------------------------------------------------
  // Helpers
  //------------------------------------------------------------------------------
  
  /**
   * Check collection of comments to prevent double event for comment as
   * leading and trailing, then emit event if passing
   * @param {ASTNode[]} comments - Collection of comment nodes
   * @param {EventEmitter} emitter - The event emitter which is the destination of events.
   * @param {Object[]} locs - List of locations of previous comment nodes
   * @param {string} eventName - Event name postfix
   * @returns {void}
   */
  function emitComments(comments, emitter, locs, eventName) {
      if (comments.length > 0) {
          comments.forEach(node => {
              const index = locs.indexOf(node.loc);
  
              if (index >= 0) {
                  locs.splice(index, 1);
              } else {
                  locs.push(node.loc);
                  emitter.emit(node.type + eventName, node);
              }
          });
      }
  }
  
  /**
   * Shortcut to check and emit enter of comment nodes
   * @param {CommentEventGenerator} generator - A generator to emit.
   * @param {ASTNode[]} comments - Collection of comment nodes
   * @returns {void}
   */
  function emitCommentsEnter(generator, comments) {
      emitComments(
          comments,
          generator.emitter,
          generator.commentLocsEnter,
          "Comment");
  }
  
  /**
   * Shortcut to check and emit exit of comment nodes
   * @param {CommentEventGenerator} generator - A generator to emit.
   * @param {ASTNode[]} comments Collection of comment nodes
   * @returns {void}
   */
  function emitCommentsExit(generator, comments) {
      emitComments(
          comments,
          generator.emitter,
          generator.commentLocsExit,
          "Comment:exit");
  }
  
  //------------------------------------------------------------------------------
  // Public Interface
  //------------------------------------------------------------------------------
  
  /**
   * The event generator for comments.
   * This is the decorator pattern.
   * This generates events of comments before/after events which are generated the original generator.
   *
   * Comment event generator class
   */
  class CommentEventGenerator {
  
      /**
       * @param {EventGenerator} originalEventGenerator - An event generator which is the decoration target.
       * @param {SourceCode} sourceCode - A source code which has comments.
       */
      constructor(originalEventGenerator, sourceCode) {
          this.original = originalEventGenerator;
          this.emitter = originalEventGenerator.emitter;
          this.sourceCode = sourceCode;
          this.commentLocsEnter = [];
          this.commentLocsExit = [];
      }
  
      /**
       * Emits an event of entering comments.
       * @param {ASTNode} node - A node which was entered.
       * @returns {void}
       */
      enterNode(node) {
          const comments = this.sourceCode.getComments(node);
  
          emitCommentsEnter(this, comments.leading);
          this.original.enterNode(node);
          emitCommentsEnter(this, comments.trailing);
      }
  
      /**
       * Emits an event of leaving comments.
       * @param {ASTNode} node - A node which was left.
       * @returns {void}
       */
      leaveNode(node) {
          const comments = this.sourceCode.getComments(node);
  
          emitCommentsExit(this, comments.trailing);
          this.original.leaveNode(node);
          emitCommentsExit(this, comments.leading);
      }
  }
  
  module.exports = CommentEventGenerator;