index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.extract = extract;
  6. exports.strip = strip;
  7. exports.parse = parse;
  8. exports.parseWithComments = parseWithComments;
  9. exports.print = print;
  10. var _detectNewline;
  11. function _load_detectNewline() {
  12. return (_detectNewline = _interopRequireDefault(require('detect-newline')));
  13. }
  14. var _os;
  15. function _load_os() {
  16. return (_os = require('os'));
  17. }
  18. function _interopRequireDefault(obj) {
  19. return obj && obj.__esModule ? obj : {default: obj};
  20. }
  21. /**
  22. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  23. *
  24. * This source code is licensed under the MIT license found in the
  25. * LICENSE file in the root directory of this source tree.
  26. *
  27. *
  28. */
  29. const commentEndRe = /\*\/$/;
  30. const commentStartRe = /^\/\*\*/;
  31. const docblockRe = /^\s*(\/\*\*?(.|\r?\n)*?\*\/)/;
  32. const lineCommentRe = /(^|\s+)\/\/([^\r\n]*)/g;
  33. const ltrimNewlineRe = /^(\r?\n)+/;
  34. const multilineRe = /(?:^|\r?\n) *(@[^\r\n]*?) *\r?\n *(?![^@\r\n]*\/\/[^]*)([^@\r\n\s][^@\r\n]+?) *\r?\n/g;
  35. const propertyRe = /(?:^|\r?\n) *@(\S+) *([^\r\n]*)/g;
  36. const stringStartRe = /(\r?\n|^) *\* ?/g;
  37. function extract(contents) {
  38. const match = contents.match(docblockRe);
  39. return match ? match[0].trimLeft() : '';
  40. }
  41. function strip(contents) {
  42. const match = contents.match(docblockRe);
  43. return match && match[0] ? contents.substring(match[0].length) : contents;
  44. }
  45. function parse(docblock) {
  46. return parseWithComments(docblock).pragmas;
  47. }
  48. function parseWithComments(docblock) {
  49. const line =
  50. (0, (_detectNewline || _load_detectNewline()).default)(docblock) ||
  51. (_os || _load_os()).EOL;
  52. docblock = docblock
  53. .replace(commentStartRe, '')
  54. .replace(commentEndRe, '')
  55. .replace(stringStartRe, '$1');
  56. // Normalize multi-line directives
  57. let prev = '';
  58. while (prev !== docblock) {
  59. prev = docblock;
  60. docblock = docblock.replace(multilineRe, `${line}$1 $2${line}`);
  61. }
  62. docblock = docblock.replace(ltrimNewlineRe, '').trimRight();
  63. const result = Object.create(null);
  64. const comments = docblock
  65. .replace(propertyRe, '')
  66. .replace(ltrimNewlineRe, '')
  67. .trimRight();
  68. let match;
  69. while ((match = propertyRe.exec(docblock))) {
  70. // strip linecomments from pragmas
  71. const nextPragma = match[2].replace(lineCommentRe, '');
  72. if (
  73. typeof result[match[1]] === 'string' ||
  74. Array.isArray(result[match[1]])
  75. ) {
  76. result[match[1]] = [].concat(result[match[1]], nextPragma);
  77. } else {
  78. result[match[1]] = nextPragma;
  79. }
  80. }
  81. return {comments, pragmas: result};
  82. }
  83. function print(_ref) {
  84. var _ref$comments = _ref.comments;
  85. let comments = _ref$comments === undefined ? '' : _ref$comments;
  86. var _ref$pragmas = _ref.pragmas;
  87. let pragmas = _ref$pragmas === undefined ? {} : _ref$pragmas;
  88. const line =
  89. (0, (_detectNewline || _load_detectNewline()).default)(comments) ||
  90. (_os || _load_os()).EOL;
  91. const head = '/**';
  92. const start = ' *';
  93. const tail = ' */';
  94. const keys = Object.keys(pragmas);
  95. const printedObject = keys
  96. .map(key => printKeyValues(key, pragmas[key]))
  97. .reduce((arr, next) => arr.concat(next), [])
  98. .map(keyValue => start + ' ' + keyValue + line)
  99. .join('');
  100. if (!comments) {
  101. if (keys.length === 0) {
  102. return '';
  103. }
  104. if (keys.length === 1 && !Array.isArray(pragmas[keys[0]])) {
  105. const value = pragmas[keys[0]];
  106. return `${head} ${printKeyValues(keys[0], value)[0]}${tail}`;
  107. }
  108. }
  109. const printedComments =
  110. comments
  111. .split(line)
  112. .map(textLine => `${start} ${textLine}`)
  113. .join(line) + line;
  114. return (
  115. head +
  116. line +
  117. (comments ? printedComments : '') +
  118. (comments && keys.length ? start + line : '') +
  119. printedObject +
  120. tail
  121. );
  122. }
  123. function printKeyValues(key, valueOrArray) {
  124. return [].concat(valueOrArray).map(value => `@${key} ${value}`.trim());
  125. }