index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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) { return obj && obj.__esModule ? obj : { default: obj }; }
  19. /**
  20. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  21. *
  22. * This source code is licensed under the MIT license found in the
  23. * LICENSE file in the root directory of this source tree.
  24. *
  25. *
  26. */
  27. const commentEndRe = /\*\/$/;
  28. const commentStartRe = /^\/\*\*/;
  29. const docblockRe = /^\s*(\/\*\*?(.|\r?\n)*?\*\/)/;
  30. const lineCommentRe = /(^|\s+)\/\/([^\r\n]*)/g;
  31. const ltrimNewlineRe = /^(\r?\n)+/;
  32. const multilineRe = /(?:^|\r?\n) *(@[^\r\n]*?) *\r?\n *(?![^@\r\n]*\/\/[^]*)([^@\r\n\s][^@\r\n]+?) *\r?\n/g;
  33. const propertyRe = /(?:^|\r?\n) *@(\S+) *([^\r\n]*)/g;
  34. const stringStartRe = /(\r?\n|^) *\* ?/g;
  35. function extract(contents) {
  36. const match = contents.match(docblockRe);
  37. return match ? match[0].trimLeft() : '';
  38. }
  39. function strip(contents) {
  40. const match = contents.match(docblockRe);
  41. return match && match[0] ? contents.substring(match[0].length) : contents;
  42. }
  43. function parse(docblock) {
  44. return parseWithComments(docblock).pragmas;
  45. }
  46. function parseWithComments(docblock) {
  47. const line = (0, (_detectNewline || _load_detectNewline()).default)(docblock) || (_os || _load_os()).EOL;
  48. docblock = docblock.replace(commentStartRe, '').replace(commentEndRe, '').replace(stringStartRe, '$1');
  49. // Normalize multi-line directives
  50. let prev = '';
  51. while (prev !== docblock) {
  52. prev = docblock;
  53. docblock = docblock.replace(multilineRe, `${line}$1 $2${line}`);
  54. }
  55. docblock = docblock.replace(ltrimNewlineRe, '').trimRight();
  56. const result = Object.create(null);
  57. const comments = docblock.replace(propertyRe, '').replace(ltrimNewlineRe, '').trimRight();
  58. let match;
  59. while (match = propertyRe.exec(docblock)) {
  60. // strip linecomments from pragmas
  61. const nextPragma = match[2].replace(lineCommentRe, '');
  62. if (typeof result[match[1]] === 'string' || Array.isArray(result[match[1]])) {
  63. result[match[1]] = [].concat(result[match[1]], nextPragma);
  64. } else {
  65. result[match[1]] = nextPragma;
  66. }
  67. }
  68. return { comments, pragmas: result };
  69. }
  70. function print(_ref) {
  71. var _ref$comments = _ref.comments;
  72. let comments = _ref$comments === undefined ? '' : _ref$comments;
  73. var _ref$pragmas = _ref.pragmas;
  74. let pragmas = _ref$pragmas === undefined ? {} : _ref$pragmas;
  75. const line = (0, (_detectNewline || _load_detectNewline()).default)(comments) || (_os || _load_os()).EOL;
  76. const head = '/**';
  77. const start = ' *';
  78. const tail = ' */';
  79. const keys = Object.keys(pragmas);
  80. const printedObject = keys.map(key => printKeyValues(key, pragmas[key])).reduce((arr, next) => arr.concat(next), []).map(keyValue => start + ' ' + keyValue + line).join('');
  81. if (!comments) {
  82. if (keys.length === 0) {
  83. return '';
  84. }
  85. if (keys.length === 1 && !Array.isArray(pragmas[keys[0]])) {
  86. const value = pragmas[keys[0]];
  87. return `${head} ${printKeyValues(keys[0], value)[0]}${tail}`;
  88. }
  89. }
  90. const printedComments = comments.split(line).map(textLine => `${start} ${textLine}`).join(line) + line;
  91. return head + line + (comments ? printedComments : '') + (comments && keys.length ? start + line : '') + printedObject + tail;
  92. }
  93. function printKeyValues(key, valueOrArray) {
  94. return [].concat(valueOrArray).map(value => `@${key} ${value}`.trim());
  95. }