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.

parseComment.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* eslint-disable prefer-named-capture-group -- Temporary */
  2. import {
  3. parse as commentParser,
  4. tokenizers,
  5. util
  6. } from 'comment-parser';
  7. const {
  8. seedBlock,
  9. seedTokens
  10. } = util;
  11. const {
  12. name: nameTokenizer,
  13. tag: tagTokenizer,
  14. type: typeTokenizer,
  15. description: descriptionTokenizer
  16. } = tokenizers;
  17. export const hasSeeWithLink = (spec) => {
  18. return spec.tag === 'see' && (/\{@link.+?\}/u).test(spec.source[0].source);
  19. };
  20. export const defaultNoTypes = ['default', 'defaultvalue', 'see'];
  21. export const defaultNoNames = [
  22. 'access', 'author',
  23. 'default', 'defaultvalue',
  24. 'description',
  25. 'example', 'exception',
  26. 'license',
  27. 'return', 'returns',
  28. 'since', 'summary',
  29. 'throws',
  30. 'version', 'variation'
  31. ];
  32. const getTokenizers = ({
  33. noTypes = defaultNoTypes,
  34. noNames = defaultNoNames
  35. } = {}) => {
  36. // trim
  37. return [
  38. // Tag
  39. tagTokenizer(),
  40. // Type
  41. (spec) => {
  42. if (noTypes.includes(spec.tag)) {
  43. return spec;
  44. }
  45. return typeTokenizer()(spec);
  46. },
  47. // Name
  48. (spec) => {
  49. if (spec.tag === 'template') {
  50. // const preWS = spec.postTag;
  51. const remainder = spec.source[0].tokens.description;
  52. const pos = remainder.search(/(?<![\s,])\s/u);
  53. const name = pos === -1 ? remainder : remainder.slice(0, pos);
  54. const extra = remainder.slice(pos + 1);
  55. let postName = '', description = '', lineEnd = '';
  56. if (pos > -1) {
  57. [, postName, description, lineEnd] = extra.match(/(\s*)([^\r]*)(\r)?/u);
  58. }
  59. spec.name = name;
  60. spec.optional = false;
  61. const {tokens} = spec.source[0];
  62. tokens.name = name;
  63. tokens.postName = postName;
  64. tokens.description = description;
  65. tokens.lineEnd = lineEnd || '';
  66. return spec;
  67. }
  68. if (noNames.includes(spec.tag) || hasSeeWithLink(spec)) {
  69. return spec;
  70. }
  71. return nameTokenizer()(spec);
  72. },
  73. // Description
  74. (spec) => {
  75. return descriptionTokenizer('preserve')(spec);
  76. }
  77. ];
  78. };
  79. /**
  80. *
  81. * @param {PlainObject} commentNode
  82. * @param {string} indent Whitespace
  83. * @returns {PlainObject}
  84. */
  85. const parseComment = (commentNode, indent) => {
  86. // Preserve JSDoc block start/end indentation.
  87. return commentParser(`/*${commentNode.value}*/`, {
  88. // @see https://github.com/yavorskiy/comment-parser/issues/21
  89. tokenizers: getTokenizers()
  90. })[0] || seedBlock({
  91. source: [
  92. {
  93. number: 0,
  94. tokens: seedTokens({
  95. delimiter: '/**'
  96. })
  97. },
  98. {
  99. number: 1,
  100. tokens: seedTokens({
  101. end: '*/',
  102. start: indent + ' '
  103. })
  104. }
  105. ]
  106. });
  107. };
  108. export {getTokenizers, parseComment};