getComments.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. module.exports = {
  2. beforeNode,
  3. afterNode,
  4. beforeDeclaration,
  5. afterDeclaration,
  6. };
  7. function beforeNode(comments, previousNode, node, currentInitialIndex) {
  8. if (!previousNode || previousNode.type !== 'comment') {
  9. return comments;
  10. }
  11. if (
  12. !previousNode.raws.before ||
  13. (previousNode.raws.before.indexOf('\n') === -1 && previousNode.prev())
  14. ) {
  15. return comments;
  16. }
  17. currentInitialIndex = currentInitialIndex || node.initialIndex;
  18. previousNode.position = node.position;
  19. previousNode.initialIndex = currentInitialIndex - 0.0001;
  20. const newComments = [previousNode].concat(comments);
  21. return beforeNode(newComments, previousNode.prev(), node, previousNode.initialIndex);
  22. }
  23. function afterNode(comments, nextNode, node, currentInitialIndex) {
  24. if (!nextNode || nextNode.type !== 'comment') {
  25. return comments;
  26. }
  27. if (!nextNode.raws.before || nextNode.raws.before.indexOf('\n') >= 0) {
  28. return comments;
  29. }
  30. currentInitialIndex = currentInitialIndex || node.initialIndex;
  31. nextNode.position = node.position;
  32. nextNode.initialIndex = currentInitialIndex + 0.0001;
  33. return afterNode(comments.concat(nextNode), nextNode.next(), node, nextNode.initialIndex);
  34. }
  35. function beforeDeclaration(comments, previousNode, nodeData, currentInitialIndex) {
  36. if (!previousNode || previousNode.type !== 'comment') {
  37. return comments;
  38. }
  39. if (!previousNode.raws.before || previousNode.raws.before.indexOf('\n') === -1) {
  40. return comments;
  41. }
  42. currentInitialIndex = currentInitialIndex || nodeData.initialIndex;
  43. const commentData = {
  44. orderData: nodeData.orderData,
  45. node: previousNode,
  46. unprefixedName: nodeData.unprefixedName, // related property name for alphabetical order
  47. unspecifiedPropertiesPosition: nodeData.unspecifiedPropertiesPosition,
  48. };
  49. commentData.initialIndex = currentInitialIndex - 0.0001;
  50. // add a marker
  51. previousNode.sortProperty = true;
  52. const newComments = [commentData].concat(comments);
  53. return beforeDeclaration(newComments, previousNode.prev(), nodeData, commentData.initialIndex);
  54. }
  55. function afterDeclaration(comments, nextNode, nodeData, currentInitialIndex) {
  56. if (!nextNode || nextNode.type !== 'comment') {
  57. return comments;
  58. }
  59. if (!nextNode.raws.before || nextNode.raws.before.indexOf('\n') >= 0) {
  60. return comments;
  61. }
  62. currentInitialIndex = currentInitialIndex || nodeData.initialIndex;
  63. const commentData = {
  64. orderData: nodeData.orderData,
  65. node: nextNode,
  66. unprefixedName: nodeData.unprefixedName, // related property name for alphabetical order
  67. unspecifiedPropertiesPosition: nodeData.unspecifiedPropertiesPosition,
  68. };
  69. commentData.initialIndex = currentInitialIndex + 0.0001;
  70. // add a marker
  71. nextNode.sortProperty = true;
  72. return afterDeclaration(
  73. comments.concat(commentData),
  74. nextNode.next(),
  75. nodeData,
  76. commentData.initialIndex
  77. );
  78. }