Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

comment-attachment.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @fileoverview Attaches comments to the AST.
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. var astNodeTypes = require("./ast-node-types");
  10. //------------------------------------------------------------------------------
  11. // Private
  12. //------------------------------------------------------------------------------
  13. var extra = {
  14. trailingComments: [],
  15. leadingComments: [],
  16. bottomRightStack: [],
  17. previousNode: null
  18. };
  19. //------------------------------------------------------------------------------
  20. // Public
  21. //------------------------------------------------------------------------------
  22. module.exports = {
  23. reset: function() {
  24. extra.trailingComments = [];
  25. extra.leadingComments = [];
  26. extra.bottomRightStack = [];
  27. extra.previousNode = null;
  28. },
  29. addComment: function(comment) {
  30. extra.trailingComments.push(comment);
  31. extra.leadingComments.push(comment);
  32. },
  33. processComment: function(node) {
  34. var lastChild,
  35. trailingComments,
  36. i,
  37. j;
  38. if (node.type === astNodeTypes.Program) {
  39. if (node.body.length > 0) {
  40. return;
  41. }
  42. }
  43. if (extra.trailingComments.length > 0) {
  44. /*
  45. * If the first comment in trailingComments comes after the
  46. * current node, then we're good - all comments in the array will
  47. * come after the node and so it's safe to add then as official
  48. * trailingComments.
  49. */
  50. if (extra.trailingComments[0].range[0] >= node.range[1]) {
  51. trailingComments = extra.trailingComments;
  52. extra.trailingComments = [];
  53. } else {
  54. /*
  55. * Otherwise, if the first comment doesn't come after the
  56. * current node, that means we have a mix of leading and trailing
  57. * comments in the array and that leadingComments contains the
  58. * same items as trailingComments. Reset trailingComments to
  59. * zero items and we'll handle this by evaluating leadingComments
  60. * later.
  61. */
  62. extra.trailingComments.length = 0;
  63. }
  64. } else {
  65. if (extra.bottomRightStack.length > 0 &&
  66. extra.bottomRightStack[extra.bottomRightStack.length - 1].trailingComments &&
  67. extra.bottomRightStack[extra.bottomRightStack.length - 1].trailingComments[0].range[0] >= node.range[1]) {
  68. trailingComments = extra.bottomRightStack[extra.bottomRightStack.length - 1].trailingComments;
  69. delete extra.bottomRightStack[extra.bottomRightStack.length - 1].trailingComments;
  70. }
  71. }
  72. // Eating the stack.
  73. while (extra.bottomRightStack.length > 0 && extra.bottomRightStack[extra.bottomRightStack.length - 1].range[0] >= node.range[0]) {
  74. lastChild = extra.bottomRightStack.pop();
  75. }
  76. if (lastChild) {
  77. if (lastChild.leadingComments) {
  78. if (lastChild.leadingComments[lastChild.leadingComments.length - 1].range[1] <= node.range[0]) {
  79. node.leadingComments = lastChild.leadingComments;
  80. delete lastChild.leadingComments;
  81. } else {
  82. // A leading comment for an anonymous class had been stolen by its first MethodDefinition,
  83. // so this takes back the leading comment.
  84. // See Also: https://github.com/eslint/espree/issues/158
  85. for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {
  86. if (lastChild.leadingComments[i].range[1] <= node.range[0]) {
  87. node.leadingComments = lastChild.leadingComments.splice(0, i + 1);
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. } else if (extra.leadingComments.length > 0) {
  94. if (extra.leadingComments[extra.leadingComments.length - 1].range[1] <= node.range[0]) {
  95. if (extra.previousNode) {
  96. for (j = 0; j < extra.leadingComments.length; j++) {
  97. if (extra.leadingComments[j].end < extra.previousNode.end) {
  98. extra.leadingComments.splice(j, 1);
  99. j--;
  100. }
  101. }
  102. }
  103. if (extra.leadingComments.length > 0) {
  104. node.leadingComments = extra.leadingComments;
  105. extra.leadingComments = [];
  106. }
  107. } else {
  108. // https://github.com/eslint/espree/issues/2
  109. /*
  110. * In special cases, such as return (without a value) and
  111. * debugger, all comments will end up as leadingComments and
  112. * will otherwise be eliminated. This extra step runs when the
  113. * bottomRightStack is empty and there are comments left
  114. * in leadingComments.
  115. *
  116. * This loop figures out the stopping point between the actual
  117. * leading and trailing comments by finding the location of the
  118. * first comment that comes after the given node.
  119. */
  120. for (i = 0; i < extra.leadingComments.length; i++) {
  121. if (extra.leadingComments[i].range[1] > node.range[0]) {
  122. break;
  123. }
  124. }
  125. /*
  126. * Split the array based on the location of the first comment
  127. * that comes after the node. Keep in mind that this could
  128. * result in an empty array, and if so, the array must be
  129. * deleted.
  130. */
  131. node.leadingComments = extra.leadingComments.slice(0, i);
  132. if (node.leadingComments.length === 0) {
  133. delete node.leadingComments;
  134. }
  135. /*
  136. * Similarly, trailing comments are attached later. The variable
  137. * must be reset to null if there are no trailing comments.
  138. */
  139. trailingComments = extra.leadingComments.slice(i);
  140. if (trailingComments.length === 0) {
  141. trailingComments = null;
  142. }
  143. }
  144. }
  145. extra.previousNode = node;
  146. if (trailingComments) {
  147. node.trailingComments = trailingComments;
  148. }
  149. extra.bottomRightStack.push(node);
  150. }
  151. };