MCPcopy Create free account
hub / github.com/babel/babel / processComment

Method processComment

packages/babel-parser/src/parser/comments.ts:114–149  ·  view source on GitHub ↗

* Given a newly created AST node _n_, attach _n_ to a comment whitespace _w_ if applicable * {@see CommentWhitespace}

(node: Node)

Source from the content-addressed store, hash-verified

112 * {@see {@link CommentWhitespace}}
113 */
114 processComment(node: Node): void {
115 const { commentStack } = this.state;
116 const commentStackLength = commentStack.length;
117 if (commentStackLength === 0) return;
118 let i = commentStackLength - 1;
119 const lastCommentWS = commentStack[i];
120
121 if (lastCommentWS.start === node.end) {
122 lastCommentWS.leadingNode = node;
123 i--;
124 }
125
126 const nodeStart = node.start!;
127 // invariant: for all 0 <= j <= i, let c = commentStack[j], c must satisfy c.end < node.end
128 for (; i >= 0; i--) {
129 const commentWS = commentStack[i];
130 const commentEnd = commentWS.end;
131 if (commentEnd > nodeStart) {
132 // by definition of commentWhiteSpace, this implies commentWS.start > nodeStart
133 // so node can be a containingNode candidate. At this time we can finalize the comment
134 // whitespace, because
135 // 1) its leadingNode or trailingNode, if exists, will not change
136 // 2) its containingNode have been assigned and will not change because it is the
137 // innermost minimal-sized AST node
138 commentWS.containingNode = node;
139 this.finalizeComment(commentWS);
140 commentStack.splice(i, 1);
141 } else {
142 if (commentEnd === nodeStart) {
143 commentWS.trailingNode = node;
144 }
145 // stop the loop when commentEnd <= nodeStart
146 break;
147 }
148 }
149 }
150
151 /**
152 * Assign the comments of comment whitespaces to related AST nodes.

Callers 2

finishNodeAtFunction · 0.80
finishNodeAtNodeFunction · 0.80

Calls 1

finalizeCommentMethod · 0.95

Tested by

no test coverage detected