UNPKG

3.76 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.extract = extract;
7exports.strip = strip;
8exports.parse = parse;
9exports.parseWithComments = parseWithComments;
10exports.print = print;
11
12function _os() {
13 const data = require('os');
14
15 _os = function () {
16 return data;
17 };
18
19 return data;
20}
21
22function _detectNewline() {
23 const data = _interopRequireDefault(require('detect-newline'));
24
25 _detectNewline = function () {
26 return data;
27 };
28
29 return data;
30}
31
32function _interopRequireDefault(obj) {
33 return obj && obj.__esModule ? obj : {default: obj};
34}
35
36/**
37 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
38 *
39 * This source code is licensed under the MIT license found in the
40 * LICENSE file in the root directory of this source tree.
41 */
42const commentEndRe = /\*\/$/;
43const commentStartRe = /^\/\*\*/;
44const docblockRe = /^\s*(\/\*\*?(.|\r?\n)*?\*\/)/;
45const lineCommentRe = /(^|\s+)\/\/([^\r\n]*)/g;
46const ltrimNewlineRe = /^(\r?\n)+/;
47const multilineRe =
48 /(?:^|\r?\n) *(@[^\r\n]*?) *\r?\n *(?![^@\r\n]*\/\/[^]*)([^@\r\n\s][^@\r\n]+?) *\r?\n/g;
49const propertyRe = /(?:^|\r?\n) *@(\S+) *([^\r\n]*)/g;
50const stringStartRe = /(\r?\n|^) *\* ?/g;
51const STRING_ARRAY = [];
52
53function extract(contents) {
54 const match = contents.match(docblockRe);
55 return match ? match[0].trimLeft() : '';
56}
57
58function strip(contents) {
59 const match = contents.match(docblockRe);
60 return match && match[0] ? contents.substring(match[0].length) : contents;
61}
62
63function parse(docblock) {
64 return parseWithComments(docblock).pragmas;
65}
66
67function 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'); // Normalize multi-line directives
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 // strip linecomments from pragmas
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
110function 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
149function printKeyValues(key, valueOrArray) {
150 return STRING_ARRAY.concat(valueOrArray).map(value =>
151 `@${key} ${value}`.trim()
152 );
153}