1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.extract = extract;
|
7 | exports.strip = strip;
|
8 | exports.parse = parse;
|
9 | exports.parseWithComments = parseWithComments;
|
10 | exports.print = print;
|
11 |
|
12 | function _os() {
|
13 | const data = require('os');
|
14 |
|
15 | _os = function () {
|
16 | return data;
|
17 | };
|
18 |
|
19 | return data;
|
20 | }
|
21 |
|
22 | function _detectNewline() {
|
23 | const data = _interopRequireDefault(require('detect-newline'));
|
24 |
|
25 | _detectNewline = function () {
|
26 | return data;
|
27 | };
|
28 |
|
29 | return data;
|
30 | }
|
31 |
|
32 | function _interopRequireDefault(obj) {
|
33 | return obj && obj.__esModule ? obj : {default: obj};
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const commentEndRe = /\*\/$/;
|
43 | const commentStartRe = /^\/\*\*/;
|
44 | const docblockRe = /^\s*(\/\*\*?(.|\r?\n)*?\*\/)/;
|
45 | const lineCommentRe = /(^|\s+)\/\/([^\r\n]*)/g;
|
46 | const ltrimNewlineRe = /^(\r?\n)+/;
|
47 | const multilineRe =
|
48 | /(?:^|\r?\n) *(@[^\r\n]*?) *\r?\n *(?![^@\r\n]*\/\/[^]*)([^@\r\n\s][^@\r\n]+?) *\r?\n/g;
|
49 | const propertyRe = /(?:^|\r?\n) *@(\S+) *([^\r\n]*)/g;
|
50 | const stringStartRe = /(\r?\n|^) *\* ?/g;
|
51 | const STRING_ARRAY = [];
|
52 |
|
53 | function extract(contents) {
|
54 | const match = contents.match(docblockRe);
|
55 | return match ? match[0].trimLeft() : '';
|
56 | }
|
57 |
|
58 | function strip(contents) {
|
59 | const match = contents.match(docblockRe);
|
60 | return match && match[0] ? contents.substring(match[0].length) : contents;
|
61 | }
|
62 |
|
63 | function parse(docblock) {
|
64 | return parseWithComments(docblock).pragmas;
|
65 | }
|
66 |
|
67 | function parseWithComments(docblock) {
|
68 | const line = (0, _detectNewline().default)(docblock) || _os().EOL;
|
69 |
|
70 | docblock = docblock
|
71 | .replace(commentStartRe, '')
|
72 | .replace(commentEndRe, '')
|
73 | .replace(stringStartRe, '$1');
|
74 |
|
75 | let prev = '';
|
76 |
|
77 | while (prev !== docblock) {
|
78 | prev = docblock;
|
79 | docblock = docblock.replace(multilineRe, `${line}$1 $2${line}`);
|
80 | }
|
81 |
|
82 | docblock = docblock.replace(ltrimNewlineRe, '').trimRight();
|
83 | const result = Object.create(null);
|
84 | const comments = docblock
|
85 | .replace(propertyRe, '')
|
86 | .replace(ltrimNewlineRe, '')
|
87 | .trimRight();
|
88 | let match;
|
89 |
|
90 | while ((match = propertyRe.exec(docblock))) {
|
91 |
|
92 | const nextPragma = match[2].replace(lineCommentRe, '');
|
93 |
|
94 | if (
|
95 | typeof result[match[1]] === 'string' ||
|
96 | Array.isArray(result[match[1]])
|
97 | ) {
|
98 | result[match[1]] = STRING_ARRAY.concat(result[match[1]], nextPragma);
|
99 | } else {
|
100 | result[match[1]] = nextPragma;
|
101 | }
|
102 | }
|
103 |
|
104 | return {
|
105 | comments,
|
106 | pragmas: result
|
107 | };
|
108 | }
|
109 |
|
110 | function print({comments = '', pragmas = {}}) {
|
111 | const line = (0, _detectNewline().default)(comments) || _os().EOL;
|
112 |
|
113 | const head = '/**';
|
114 | const start = ' *';
|
115 | const tail = ' */';
|
116 | const keys = Object.keys(pragmas);
|
117 | const printedObject = keys
|
118 | .map(key => printKeyValues(key, pragmas[key]))
|
119 | .reduce((arr, next) => arr.concat(next), [])
|
120 | .map(keyValue => start + ' ' + keyValue + line)
|
121 | .join('');
|
122 |
|
123 | if (!comments) {
|
124 | if (keys.length === 0) {
|
125 | return '';
|
126 | }
|
127 |
|
128 | if (keys.length === 1 && !Array.isArray(pragmas[keys[0]])) {
|
129 | const value = pragmas[keys[0]];
|
130 | return `${head} ${printKeyValues(keys[0], value)[0]}${tail}`;
|
131 | }
|
132 | }
|
133 |
|
134 | const printedComments =
|
135 | comments
|
136 | .split(line)
|
137 | .map(textLine => `${start} ${textLine}`)
|
138 | .join(line) + line;
|
139 | return (
|
140 | head +
|
141 | line +
|
142 | (comments ? printedComments : '') +
|
143 | (comments && keys.length ? start + line : '') +
|
144 | printedObject +
|
145 | tail
|
146 | );
|
147 | }
|
148 |
|
149 | function printKeyValues(key, valueOrArray) {
|
150 | return STRING_ARRAY.concat(valueOrArray).map(value =>
|
151 | `@${key} ${value}`.trim()
|
152 | );
|
153 | }
|