@mandolin/jsdoc-plugin-hia-sys 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,374 @@
1
+ "use strict";
2
+
3
+ const {
4
+ buildDefinedIn,
5
+ buildPrimaryBlock,
6
+ buildSourceLink,
7
+ buildSourcePreview,
8
+ getLineColumn,
9
+ getRelativePath,
10
+ inferLanguage,
11
+ trimOneOuterNewline
12
+ } = require("../runtime/source.cjs");
13
+
14
+ const MARKER_PATTERN = /\/\*+\s*@(codeblock|codeblockend)\b([^*]*?)\*\//g;
15
+ const CODEREF_PATTERN = /(?:\/\/\s*)?@coderef\s+([^\r\n]+)/g;
16
+
17
+ function cleanId(value) {
18
+ return String(value || "").trim();
19
+ }
20
+
21
+ function appendTag(doclet, name, value) {
22
+ doclet.hiaTags = doclet.hiaTags || {};
23
+ doclet.hiaTags[name] = doclet.hiaTags[name] || [];
24
+ doclet.hiaTags[name].push(value);
25
+ }
26
+
27
+ function createFragment(openMarker, closeMarker, source, filePath, context) {
28
+ const rawContent = source.slice(openMarker.endIndex, closeMarker.startIndex);
29
+ const content = trimOneOuterNewline(rawContent);
30
+ const relativePath = getRelativePath(filePath, context.config);
31
+ const startLine = openMarker.location.line + 1;
32
+ const endLine = Math.max(startLine, closeMarker.location.line - 1);
33
+ const fragment = {
34
+ kind: "source-fragment",
35
+ id: openMarker.id,
36
+ filePath,
37
+ relativePath,
38
+ language: inferLanguage(filePath),
39
+ range: {
40
+ start: {
41
+ line: startLine,
42
+ column: 1
43
+ },
44
+ end: {
45
+ line: endLine,
46
+ column: 1
47
+ }
48
+ },
49
+ content,
50
+ origin: {
51
+ marker: "codeblock",
52
+ startMarkerRange: {
53
+ start: openMarker.location,
54
+ end: getLineColumn(source, openMarker.endIndex)
55
+ },
56
+ endMarkerRange: {
57
+ start: closeMarker.location,
58
+ end: getLineColumn(source, closeMarker.endIndex)
59
+ }
60
+ }
61
+ };
62
+
63
+ fragment.link = buildSourceLink(fragment, context.config);
64
+ fragment.preview = buildSourcePreview(fragment, context.config);
65
+
66
+ return fragment;
67
+ }
68
+
69
+ function addFragment(context, fragment) {
70
+ const registry = context.state.registries.sourceFragments;
71
+
72
+ if (registry.has(fragment.id)) {
73
+ context.addDiagnostic({
74
+ code: "HIA_SOURCE_FRAGMENT_DUPLICATE",
75
+ severity: "error",
76
+ message: `Duplicate source fragment id: ${fragment.id}`,
77
+ plugin: "code-fragment",
78
+ filePath: fragment.filePath,
79
+ range: fragment.range,
80
+ data: {
81
+ id: fragment.id
82
+ }
83
+ });
84
+ return;
85
+ }
86
+
87
+ registry.set(fragment.id, fragment);
88
+ }
89
+
90
+ function scanSourceFragments(event, context) {
91
+ const source = event.source || "";
92
+ const filePath = event.filename;
93
+ const openMarkers = new Map();
94
+ let match;
95
+
96
+ while ((match = MARKER_PATTERN.exec(source))) {
97
+ const kind = match[1];
98
+ const id = cleanId(match[2]);
99
+ const marker = {
100
+ id,
101
+ kind,
102
+ startIndex: match.index,
103
+ endIndex: match.index + match[0].length,
104
+ location: getLineColumn(source, match.index)
105
+ };
106
+
107
+ if (!id) {
108
+ context.addDiagnostic({
109
+ code: "HIA_SOURCE_FRAGMENT_ID_MISSING",
110
+ severity: "error",
111
+ message: `Missing source fragment id for @${kind}.`,
112
+ plugin: "code-fragment",
113
+ filePath,
114
+ range: {
115
+ start: marker.location,
116
+ end: marker.location
117
+ }
118
+ });
119
+ continue;
120
+ }
121
+
122
+ if (kind === "codeblock") {
123
+ if (openMarkers.has(id)) {
124
+ context.addDiagnostic({
125
+ code: "HIA_SOURCE_FRAGMENT_START_DUPLICATE",
126
+ severity: "error",
127
+ message: `Duplicate open @codeblock id: ${id}`,
128
+ plugin: "code-fragment",
129
+ filePath,
130
+ data: {
131
+ id
132
+ }
133
+ });
134
+ continue;
135
+ }
136
+
137
+ openMarkers.set(id, marker);
138
+ continue;
139
+ }
140
+
141
+ const openMarker = openMarkers.get(id);
142
+
143
+ if (!openMarker) {
144
+ context.addDiagnostic({
145
+ code: "HIA_SOURCE_FRAGMENT_END_WITHOUT_START",
146
+ severity: "error",
147
+ message: `@codeblockend has no matching @codeblock: ${id}`,
148
+ plugin: "code-fragment",
149
+ filePath,
150
+ data: {
151
+ id
152
+ }
153
+ });
154
+ continue;
155
+ }
156
+
157
+ addFragment(context, createFragment(openMarker, marker, source, filePath, context));
158
+ openMarkers.delete(id);
159
+ }
160
+
161
+ for (const openMarker of openMarkers.values()) {
162
+ context.addDiagnostic({
163
+ code: "HIA_SOURCE_FRAGMENT_END_MISSING",
164
+ severity: "error",
165
+ message: `Missing @codeblockend for source fragment: ${openMarker.id}`,
166
+ plugin: "code-fragment",
167
+ filePath,
168
+ data: {
169
+ id: openMarker.id
170
+ }
171
+ });
172
+ }
173
+ }
174
+
175
+ function summarizeFragment(fragment) {
176
+ return {
177
+ kind: fragment.kind || "source-fragment",
178
+ id: fragment.id,
179
+ filePath: fragment.filePath,
180
+ relativePath: fragment.relativePath,
181
+ language: fragment.language,
182
+ range: fragment.range,
183
+ content: fragment.content,
184
+ link: fragment.link,
185
+ preview: fragment.preview
186
+ };
187
+ }
188
+
189
+ function createReference(targetId, doclet, fieldPath, context) {
190
+ const fragment = context.state.registries.sourceFragments.get(targetId);
191
+ const reference = {
192
+ kind: "source-reference",
193
+ referenceKind: "coderef",
194
+ targetId,
195
+ sourceNodeId: doclet.longname || doclet.name || "",
196
+ fieldPath,
197
+ resolved: Boolean(fragment),
198
+ fragment: fragment ? summarizeFragment(fragment) : null,
199
+ diagnostics: []
200
+ };
201
+
202
+ if (!fragment) {
203
+ const diagnostic = context.addDiagnostic({
204
+ code: "HIA_SOURCE_REFERENCE_MISSING",
205
+ severity: "error",
206
+ message: `Cannot resolve @coderef target: ${targetId}`,
207
+ plugin: "code-fragment",
208
+ data: {
209
+ targetId,
210
+ sourceNodeId: reference.sourceNodeId,
211
+ fieldPath
212
+ }
213
+ });
214
+ reference.diagnostics.push(diagnostic);
215
+ }
216
+
217
+ return reference;
218
+ }
219
+
220
+ function formatReplacement(fragment, fieldKind) {
221
+ if (!fragment) {
222
+ return "";
223
+ }
224
+
225
+ if (fieldKind === "example") {
226
+ return fragment.content;
227
+ }
228
+
229
+ return `\n\n\`\`\`${fragment.language}\n${fragment.content}\n\`\`\`\n`;
230
+ }
231
+
232
+ function transformText(text, doclet, fieldPath, fieldKind, context, references) {
233
+ if (typeof text !== "string" || !text.includes("@coderef")) {
234
+ return text;
235
+ }
236
+
237
+ return text.replace(CODEREF_PATTERN, (_match, rawId) => {
238
+ const targetId = cleanId(rawId);
239
+ const reference = createReference(targetId, doclet, fieldPath, context);
240
+ references.push(reference);
241
+
242
+ if (!reference.fragment) {
243
+ return `@coderef ${targetId}`;
244
+ }
245
+
246
+ const fragment = context.state.registries.sourceFragments.get(targetId);
247
+ return formatReplacement(fragment, fieldKind);
248
+ });
249
+ }
250
+
251
+ function resolveDocletReferences(doclet, context) {
252
+ const metadata = context.markMicroPlugin(doclet, "code-fragment");
253
+ const references = [];
254
+ const tagRefs = doclet.hiaTags && Array.isArray(doclet.hiaTags.coderef)
255
+ ? doclet.hiaTags.coderef
256
+ : [];
257
+
258
+ doclet.description = transformText(
259
+ doclet.description,
260
+ doclet,
261
+ "description",
262
+ "description",
263
+ context,
264
+ references
265
+ );
266
+
267
+ if (Array.isArray(doclet.examples)) {
268
+ doclet.examples = doclet.examples.map((example, index) => {
269
+ return transformText(
270
+ example,
271
+ doclet,
272
+ `examples.${index}`,
273
+ "example",
274
+ context,
275
+ references
276
+ );
277
+ });
278
+ }
279
+
280
+ if (Array.isArray(doclet.properties)) {
281
+ for (const [index, property] of doclet.properties.entries()) {
282
+ property.description = transformText(
283
+ property.description,
284
+ doclet,
285
+ `properties.${index}.description`,
286
+ "description",
287
+ context,
288
+ references
289
+ );
290
+ }
291
+ }
292
+
293
+ for (const targetId of tagRefs) {
294
+ references.push(createReference(cleanId(targetId), doclet, "tags.coderef", context));
295
+ }
296
+
297
+ metadata.source.references = references;
298
+ metadata.source.fragments = references
299
+ .filter((reference) => reference.fragment)
300
+ .map((reference) => reference.fragment);
301
+ }
302
+
303
+ module.exports = {
304
+ name: "code-fragment",
305
+ order: 10,
306
+
307
+ setup(context) {
308
+ context.state.registries.sourceFragments = new Map();
309
+ },
310
+
311
+ defineTags(event) {
312
+ const dictionary = event && event.dictionary;
313
+
314
+ if (!dictionary || typeof dictionary.defineTag !== "function") {
315
+ return;
316
+ }
317
+
318
+ dictionary.defineTag("codeblock", {
319
+ canHaveValue: true
320
+ });
321
+
322
+ dictionary.defineTag("codeblockend", {
323
+ canHaveValue: true
324
+ });
325
+
326
+ dictionary.defineTag("coderef", {
327
+ canHaveValue: true,
328
+ onTagged(doclet, tag) {
329
+ appendTag(doclet, "coderef", tag.value || tag.text || "");
330
+ }
331
+ });
332
+ },
333
+
334
+ beforeParse(event, context) {
335
+ if (!event || !event.filename) {
336
+ return;
337
+ }
338
+
339
+ context.state.sourceFiles.set(event.filename, {
340
+ filePath: event.filename,
341
+ source: event.source || ""
342
+ });
343
+
344
+ scanSourceFragments(event, context);
345
+ },
346
+
347
+ newDoclet(event, context) {
348
+ if (!event || !event.doclet) {
349
+ return;
350
+ }
351
+
352
+ const metadata = context.markMicroPlugin(event.doclet, this.name);
353
+ metadata.source.model = "hia-jsdoc-source";
354
+ metadata.source.modelVersion = "0.2.0";
355
+ metadata.source.mode = context.config.source.mode || "all";
356
+ metadata.source.definedIn = buildDefinedIn(event.doclet, context);
357
+ metadata.source.primaryBlock = buildPrimaryBlock(event.doclet, context);
358
+ metadata.source.fragments = metadata.source.fragments || [];
359
+ metadata.source.references = metadata.source.references || [];
360
+ metadata.source.diagnostics = metadata.source.diagnostics || [];
361
+ },
362
+
363
+ parseComplete(event, context) {
364
+ const doclets = event && Array.isArray(event.doclets) ? event.doclets : [];
365
+
366
+ for (const doclet of doclets) {
367
+ resolveDocletReferences(doclet, context);
368
+ }
369
+
370
+ context.state.output.sourceFragments = Array.from(
371
+ context.state.registries.sourceFragments.values()
372
+ ).map(summarizeFragment);
373
+ }
374
+ };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ module.exports = {
4
+ name: "diagnostics",
5
+ order: 100,
6
+
7
+ processingComplete(event, context) {
8
+ const doclets = event && Array.isArray(event.doclets) ? event.doclets : [];
9
+
10
+ for (const doclet of doclets) {
11
+ context.markMicroPlugin(doclet, this.name);
12
+ context.attachDiagnostics(doclet);
13
+ }
14
+
15
+ context.state.output.diagnostics = context.diagnostics.list();
16
+ context.state.output.diagnosticCounts = context.diagnostics.countBySeverity();
17
+ }
18
+ };