1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.splitSummary = exports.getReferencedDocParams = exports.parseSymbolDocumentation = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | const spec = require("@jsii/spec");
|
37 | const ts = require("typescript");
|
38 |
|
39 |
|
40 |
|
41 | var DocTag;
|
42 | (function (DocTag) {
|
43 | DocTag["PARAM"] = "param";
|
44 | DocTag["DEFAULT"] = "default";
|
45 | DocTag["DEFAULT_VALUE"] = "defaultValue";
|
46 | DocTag["DEPRECATED"] = "deprecated";
|
47 | DocTag["RETURNS"] = "returns";
|
48 | DocTag["RETURN"] = "return";
|
49 | DocTag["STABLE"] = "stable";
|
50 | DocTag["EXPERIMENTAL"] = "experimental";
|
51 | DocTag["SEE"] = "see";
|
52 | DocTag["SUBCLASSABLE"] = "subclassable";
|
53 | DocTag["EXAMPLE"] = "example";
|
54 | DocTag["STABILITY"] = "stability";
|
55 | DocTag["STRUCT"] = "struct";
|
56 |
|
57 | DocTag["JSII"] = "jsii";
|
58 | })(DocTag || (DocTag = {}));
|
59 | const RECOGNIZED_TAGS = new Set(Object.values(DocTag));
|
60 |
|
61 |
|
62 |
|
63 | function parseSymbolDocumentation(sym, typeChecker) {
|
64 | const comment = ts.displayPartsToString(sym.getDocumentationComment(typeChecker)).trim();
|
65 | const tags = reabsorbExampleTags(sym.getJsDocTags());
|
66 |
|
67 | return parseDocParts(comment, tags);
|
68 | }
|
69 | exports.parseSymbolDocumentation = parseSymbolDocumentation;
|
70 |
|
71 |
|
72 |
|
73 | function getReferencedDocParams(sym) {
|
74 | const ret = new Array();
|
75 | for (const tag of sym.getJsDocTags()) {
|
76 | if (tag.name === DocTag.PARAM) {
|
77 | const parts = ts.displayPartsToString(tag.text).split(' ');
|
78 | ret.push(parts[0]);
|
79 | }
|
80 | }
|
81 | return ret;
|
82 | }
|
83 | exports.getReferencedDocParams = getReferencedDocParams;
|
84 | function parseDocParts(comments, tags) {
|
85 | const diagnostics = new Array();
|
86 | const docs = {};
|
87 | const hints = {};
|
88 | [docs.summary, docs.remarks] = splitSummary(comments);
|
89 | const tagNames = new Map();
|
90 | for (const tag of tags) {
|
91 |
|
92 |
|
93 | if (tag.name !== DocTag.PARAM && tag.name !== DocTag.JSII) {
|
94 | tagNames.set(tag.name, tag.text && ts.displayPartsToString(tag.text));
|
95 | }
|
96 | }
|
97 | function eatTag(...names) {
|
98 | for (const name of names) {
|
99 | if (tagNames.has(name)) {
|
100 | const ret = tagNames.get(name);
|
101 | tagNames.delete(name);
|
102 | return ret ?? '';
|
103 | }
|
104 | }
|
105 | return undefined;
|
106 | }
|
107 | if (eatTag(DocTag.STRUCT) != null) {
|
108 | hints.struct = true;
|
109 | }
|
110 | docs.default = eatTag(DocTag.DEFAULT, DocTag.DEFAULT_VALUE);
|
111 | docs.deprecated = eatTag(DocTag.DEPRECATED);
|
112 | docs.example = eatTag(DocTag.EXAMPLE);
|
113 | docs.returns = eatTag(DocTag.RETURNS, DocTag.RETURN);
|
114 | docs.see = eatTag(DocTag.SEE);
|
115 | docs.subclassable = eatTag(DocTag.SUBCLASSABLE) !== undefined ? true : undefined;
|
116 | docs.stability = parseStability(eatTag(DocTag.STABILITY), diagnostics);
|
117 |
|
118 | const experimental = eatTag(DocTag.EXPERIMENTAL) !== undefined;
|
119 | const stable = eatTag(DocTag.STABLE) !== undefined;
|
120 |
|
121 | if (countBools(docs.stability !== undefined, experimental, stable) > 1) {
|
122 | diagnostics.push('Use only one of @stability, @experimental or @stable');
|
123 | }
|
124 | if (experimental) {
|
125 | docs.stability = spec.Stability.Experimental;
|
126 | }
|
127 | if (stable) {
|
128 | docs.stability = spec.Stability.Stable;
|
129 | }
|
130 |
|
131 | if (docs.deprecated !== undefined) {
|
132 | if (docs.stability !== undefined && docs.stability !== spec.Stability.Deprecated) {
|
133 | diagnostics.push("@deprecated tag requires '@stability deprecated' or no @stability at all.");
|
134 | }
|
135 | docs.stability = spec.Stability.Deprecated;
|
136 | }
|
137 | if (docs.example?.includes('```')) {
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | diagnostics.push('@example must be code only, no code block fences allowed.');
|
144 | }
|
145 | if (docs.deprecated?.trim() === '') {
|
146 | diagnostics.push('@deprecated tag needs a reason and/or suggested alternatives.');
|
147 | }
|
148 | if (tagNames.size > 0) {
|
149 | docs.custom = {};
|
150 | for (const [key, value] of tagNames.entries()) {
|
151 | docs.custom[key] = value ?? 'true';
|
152 | }
|
153 | }
|
154 | return { docs, diagnostics, hints };
|
155 | }
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | function splitSummary(docBlock) {
|
165 | if (!docBlock) {
|
166 | return [undefined, undefined];
|
167 | }
|
168 | const summary = summaryLine(docBlock);
|
169 | const remarks = uberTrim(docBlock.slice(summary.length));
|
170 | return [endWithPeriod(noNewlines(summary.trim())), remarks];
|
171 | }
|
172 | exports.splitSummary = splitSummary;
|
173 |
|
174 |
|
175 |
|
176 | function noNewlines(s) {
|
177 | return s.replace(/\r?\n/g, ' ');
|
178 | }
|
179 | function endWithPeriod(s) {
|
180 | return ENDS_WITH_PUNCTUATION_REGEX.test(s) ? s : `${s}.`;
|
181 | }
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | function uberTrim(str) {
|
187 | str = str.trim();
|
188 | return str === '' ? undefined : str;
|
189 | }
|
190 | const SUMMARY_MAX_WORDS = 20;
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | function summaryLine(str) {
|
199 | const paras = str.split('\n\n');
|
200 | if (paras.length > 1 && paras[0].split(' ').length < SUMMARY_MAX_WORDS) {
|
201 | return paras[0];
|
202 | }
|
203 | const m = FIRST_SENTENCE_REGEX.exec(str);
|
204 | if (m) {
|
205 | return m[1];
|
206 | }
|
207 | return paras[0];
|
208 | }
|
209 | const PUNCTUATION = ['!', '?', '.', ';'].map((s) => `\\${s}`).join('');
|
210 | const ENDS_WITH_PUNCTUATION_REGEX = new RegExp(`[${PUNCTUATION}]$`);
|
211 | const FIRST_SENTENCE_REGEX = new RegExp(`^([^${PUNCTUATION}]+[${PUNCTUATION}][ \n\r])`);
|
212 | function intBool(x) {
|
213 | return x ? 1 : 0;
|
214 | }
|
215 | function countBools(...x) {
|
216 | return x.map(intBool).reduce((a, b) => a + b, 0);
|
217 | }
|
218 | function parseStability(s, diagnostics) {
|
219 | if (s === undefined) {
|
220 | return undefined;
|
221 | }
|
222 | switch (s) {
|
223 | case 'stable':
|
224 | return spec.Stability.Stable;
|
225 | case 'experimental':
|
226 | return spec.Stability.Experimental;
|
227 | case 'external':
|
228 | return spec.Stability.External;
|
229 | case 'deprecated':
|
230 | return spec.Stability.Deprecated;
|
231 | }
|
232 | diagnostics.push(`Unrecognized @stability: '${s}'`);
|
233 | return undefined;
|
234 | }
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | function reabsorbExampleTags(tags) {
|
243 | var _a;
|
244 | const ret = [...tags];
|
245 | let i = 0;
|
246 | while (i < ret.length) {
|
247 | if (ret[i].name === 'example') {
|
248 | while (i + 1 < ret.length && !RECOGNIZED_TAGS.has(ret[i + 1].name)) {
|
249 |
|
250 | (_a = ret[i]).text ?? (_a.text = []);
|
251 | ret[i].text.push({
|
252 | text: `@${ret[i + 1].name}${ret[i + 1].text}`,
|
253 | kind: '',
|
254 | });
|
255 | ret.splice(i + 1, 1);
|
256 | }
|
257 | }
|
258 | i++;
|
259 | }
|
260 | return ret;
|
261 | }
|
262 |
|
\ | No newline at end of file |