Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

commentParserToESTree.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {parse as jsdocTypePrattParse} from 'jsdoc-type-pratt-parser';
  2. const stripEncapsulatingBrackets = (container, isArr) => {
  3. if (isArr) {
  4. const firstItem = container[0];
  5. firstItem.rawType = firstItem.rawType.replace(
  6. /^\{/u, ''
  7. );
  8. const lastItem = container[container.length - 1];
  9. lastItem.rawType = lastItem.rawType.replace(/\}$/u, '');
  10. return;
  11. }
  12. container.rawType = container.rawType.replace(
  13. /^\{/u, ''
  14. ).replace(/\}$/u, '');
  15. };
  16. const commentParserToESTree = (jsdoc, mode) => {
  17. const {tokens: {
  18. delimiter: delimiterRoot,
  19. lineEnd: lineEndRoot,
  20. postDelimiter: postDelimiterRoot,
  21. end: endRoot,
  22. description: descriptionRoot
  23. }} = jsdoc.source[0];
  24. const ast = {
  25. delimiter: delimiterRoot,
  26. description: descriptionRoot,
  27. descriptionLines: [],
  28. // `end` will be overwritten if there are other entries
  29. end: endRoot,
  30. postDelimiter: postDelimiterRoot,
  31. lineEnd: lineEndRoot,
  32. type: 'JsdocBlock'
  33. };
  34. const tags = [];
  35. let lastDescriptionLine;
  36. let lastTag = null;
  37. jsdoc.source.slice(1).forEach((info, idx) => {
  38. const {tokens} = info;
  39. const {
  40. delimiter,
  41. description,
  42. postDelimiter,
  43. start,
  44. tag,
  45. end,
  46. type: rawType
  47. } = tokens;
  48. if (tag || end) {
  49. if (lastDescriptionLine === undefined) {
  50. lastDescriptionLine = idx;
  51. }
  52. // Clean-up with last tag before end or new tag
  53. if (lastTag) {
  54. // Strip out `}` that encapsulates and is not part of
  55. // the type
  56. stripEncapsulatingBrackets(lastTag);
  57. if (lastTag.typeLines.length) {
  58. stripEncapsulatingBrackets(lastTag.typeLines, true);
  59. }
  60. // With even a multiline type now in full, add parsing
  61. let parsedType = null;
  62. try {
  63. parsedType = jsdocTypePrattParse(lastTag.rawType, mode);
  64. } catch {
  65. // Ignore
  66. }
  67. lastTag.parsedType = parsedType;
  68. }
  69. if (end) {
  70. ast.end = end;
  71. return;
  72. }
  73. const {
  74. end: ed,
  75. ...tkns
  76. } = tokens;
  77. const tagObj = {
  78. ...tkns,
  79. descriptionLines: [],
  80. rawType: '',
  81. type: 'JsdocTag',
  82. typeLines: []
  83. };
  84. tagObj.tag = tagObj.tag.replace(/^@/u, '');
  85. lastTag = tagObj;
  86. tags.push(tagObj);
  87. }
  88. if (rawType) {
  89. // Will strip rawType brackets after this tag
  90. lastTag.typeLines.push(
  91. {
  92. delimiter,
  93. postDelimiter,
  94. rawType,
  95. start,
  96. type: 'JsdocTypeLine'
  97. }
  98. );
  99. lastTag.rawType += rawType;
  100. }
  101. if (description) {
  102. const holder = lastTag || ast;
  103. holder.descriptionLines.push({
  104. delimiter,
  105. description,
  106. postDelimiter,
  107. start,
  108. type: 'JsdocDescriptionLine'
  109. });
  110. holder.description += holder.description
  111. ? '\n' + description
  112. : description;
  113. }
  114. });
  115. ast.lastDescriptionLine = lastDescriptionLine;
  116. ast.tags = tags;
  117. return ast;
  118. };
  119. const jsdocVisitorKeys = {
  120. JsdocBlock: ['tags', 'descriptionLines'],
  121. JsdocDescriptionLine: [],
  122. JsdocTypeLine: [],
  123. JsdocTag: ['descriptionLines', 'typeLines', 'parsedType']
  124. };
  125. export {commentParserToESTree, jsdocVisitorKeys};