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

Method finalizeComment

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

* Assign the comments of comment whitespaces to related AST nodes. * Also adjust innerComments following trailing comma.

(commentWS: CommentWhitespace)

Source from the content-addressed store, hash-verified

153 * Also adjust innerComments following trailing comma.
154 */
155 finalizeComment(commentWS: CommentWhitespace) {
156 const { comments } = commentWS;
157 if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
158 if (commentWS.leadingNode !== null) {
159 setTrailingComments(commentWS.leadingNode, comments);
160 }
161 if (commentWS.trailingNode !== null) {
162 setLeadingComments(commentWS.trailingNode, comments);
163 }
164 } else {
165 /*:: invariant(commentWS.containingNode !== null) */
166 const node = commentWS.containingNode!;
167 const commentStart = commentWS.start;
168 if (
169 this.input.charCodeAt(this.offsetToSourcePos(commentStart) - 1) ===
170 charCodes.comma
171 ) {
172 // If a commentWhitespace follows a comma and the containingNode allows
173 // list structures with trailing comma, merge it to the trailingComment
174 // of the last non-null list element
175 switch (node.type) {
176 case "ObjectExpression":
177 case "ObjectPattern":
178 adjustInnerComments(node, node.properties, commentWS);
179 break;
180 case "CallExpression":
181 case "NewExpression":
182 case "OptionalCallExpression":
183 adjustInnerComments(node, node.arguments, commentWS);
184 break;
185 case "ImportExpression":
186 adjustInnerComments(
187 node,
188 [node.source, node.options ?? null],
189 commentWS,
190 );
191 break;
192 case "FunctionDeclaration":
193 case "FunctionExpression":
194 case "ArrowFunctionExpression":
195 case "ObjectMethod":
196 case "ClassMethod":
197 case "ClassPrivateMethod":
198 case "TSTypeParameterDeclaration":
199 adjustInnerComments(node, node.params, commentWS);
200 break;
201 case "ArrayExpression":
202 case "ArrayPattern":
203 adjustInnerComments(node, node.elements, commentWS);
204 break;
205 case "ExportNamedDeclaration":
206 case "ImportDeclaration":
207 adjustInnerComments(node, node.specifiers, commentWS);
208 break;
209 case "TSEnumBody":
210 adjustInnerComments(node, node.members, commentWS);
211 break;
212 case "TSInterfaceBody":

Callers 2

processCommentMethod · 0.95

Calls 5

setTrailingCommentsFunction · 0.85
setLeadingCommentsFunction · 0.85
adjustInnerCommentsFunction · 0.85
setInnerCommentsFunction · 0.85
offsetToSourcePosMethod · 0.80

Tested by

no test coverage detected