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