comments.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. exports.setInnerComments = setInnerComments;
  7. var _base = require("./base");
  8. function setTrailingComments(node, comments) {
  9. if (node.trailingComments === undefined) {
  10. node.trailingComments = comments;
  11. } else {
  12. node.trailingComments.unshift(...comments);
  13. }
  14. }
  15. function setLeadingComments(node, comments) {
  16. if (node.leadingComments === undefined) {
  17. node.leadingComments = comments;
  18. } else {
  19. node.leadingComments.unshift(...comments);
  20. }
  21. }
  22. function setInnerComments(node, comments) {
  23. if (node.innerComments === undefined) {
  24. node.innerComments = comments;
  25. } else {
  26. node.innerComments.unshift(...comments);
  27. }
  28. }
  29. function adjustInnerComments(node, elements, commentWS) {
  30. let lastElement = null;
  31. let i = elements.length;
  32. while (lastElement === null && i > 0) {
  33. lastElement = elements[--i];
  34. }
  35. if (lastElement === null || lastElement.start > commentWS.start) {
  36. setInnerComments(node, commentWS.comments);
  37. } else {
  38. setTrailingComments(lastElement, commentWS.comments);
  39. }
  40. }
  41. class CommentsParser extends _base.default {
  42. addComment(comment) {
  43. if (this.filename) comment.loc.filename = this.filename;
  44. this.state.comments.push(comment);
  45. }
  46. processComment(node) {
  47. const {
  48. commentStack
  49. } = this.state;
  50. const commentStackLength = commentStack.length;
  51. if (commentStackLength === 0) return;
  52. let i = commentStackLength - 1;
  53. const lastCommentWS = commentStack[i];
  54. if (lastCommentWS.start === node.end) {
  55. lastCommentWS.leadingNode = node;
  56. i--;
  57. }
  58. const {
  59. start: nodeStart
  60. } = node;
  61. for (; i >= 0; i--) {
  62. const commentWS = commentStack[i];
  63. const commentEnd = commentWS.end;
  64. if (commentEnd > nodeStart) {
  65. commentWS.containingNode = node;
  66. this.finalizeComment(commentWS);
  67. commentStack.splice(i, 1);
  68. } else {
  69. if (commentEnd === nodeStart) {
  70. commentWS.trailingNode = node;
  71. }
  72. break;
  73. }
  74. }
  75. }
  76. finalizeComment(commentWS) {
  77. const {
  78. comments
  79. } = commentWS;
  80. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  81. if (commentWS.leadingNode !== null) {
  82. setTrailingComments(commentWS.leadingNode, comments);
  83. }
  84. if (commentWS.trailingNode !== null) {
  85. setLeadingComments(commentWS.trailingNode, comments);
  86. }
  87. } else {
  88. const {
  89. containingNode: node,
  90. start: commentStart
  91. } = commentWS;
  92. if (this.input.charCodeAt(commentStart - 1) === 44) {
  93. switch (node.type) {
  94. case "ObjectExpression":
  95. case "ObjectPattern":
  96. case "RecordExpression":
  97. adjustInnerComments(node, node.properties, commentWS);
  98. break;
  99. case "CallExpression":
  100. case "OptionalCallExpression":
  101. adjustInnerComments(node, node.arguments, commentWS);
  102. break;
  103. case "FunctionDeclaration":
  104. case "FunctionExpression":
  105. case "ArrowFunctionExpression":
  106. case "ObjectMethod":
  107. case "ClassMethod":
  108. case "ClassPrivateMethod":
  109. adjustInnerComments(node, node.params, commentWS);
  110. break;
  111. case "ArrayExpression":
  112. case "ArrayPattern":
  113. case "TupleExpression":
  114. adjustInnerComments(node, node.elements, commentWS);
  115. break;
  116. case "ExportNamedDeclaration":
  117. case "ImportDeclaration":
  118. adjustInnerComments(node, node.specifiers, commentWS);
  119. break;
  120. default:
  121. {
  122. setInnerComments(node, comments);
  123. }
  124. }
  125. } else {
  126. setInnerComments(node, comments);
  127. }
  128. }
  129. }
  130. finalizeRemainingComments() {
  131. const {
  132. commentStack
  133. } = this.state;
  134. for (let i = commentStack.length - 1; i >= 0; i--) {
  135. this.finalizeComment(commentStack[i]);
  136. }
  137. this.state.commentStack = [];
  138. }
  139. resetPreviousNodeTrailingComments(node) {
  140. const {
  141. commentStack
  142. } = this.state;
  143. const {
  144. length
  145. } = commentStack;
  146. if (length === 0) return;
  147. const commentWS = commentStack[length - 1];
  148. if (commentWS.leadingNode === node) {
  149. commentWS.leadingNode = null;
  150. }
  151. }
  152. resetPreviousIdentifierLeadingComments(node) {
  153. const {
  154. commentStack
  155. } = this.state;
  156. const {
  157. length
  158. } = commentStack;
  159. if (length === 0) return;
  160. if (commentStack[length - 1].trailingNode === node) {
  161. commentStack[length - 1].trailingNode = null;
  162. } else if (length >= 2 && commentStack[length - 2].trailingNode === node) {
  163. commentStack[length - 2].trailingNode = null;
  164. }
  165. }
  166. takeSurroundingComments(node, start, end) {
  167. const {
  168. commentStack
  169. } = this.state;
  170. const commentStackLength = commentStack.length;
  171. if (commentStackLength === 0) return;
  172. let i = commentStackLength - 1;
  173. for (; i >= 0; i--) {
  174. const commentWS = commentStack[i];
  175. const commentEnd = commentWS.end;
  176. const commentStart = commentWS.start;
  177. if (commentStart === end) {
  178. commentWS.leadingNode = node;
  179. } else if (commentEnd === start) {
  180. commentWS.trailingNode = node;
  181. } else if (commentEnd < start) {
  182. break;
  183. }
  184. }
  185. }
  186. }
  187. exports.default = CommentsParser;
  188. //# sourceMappingURL=comments.js.map