@speclynx/apidom-datamodel 4.0.2 → 4.0.3

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.
Files changed (95) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/package.json +4 -5
  3. package/src/KeyValuePair.cjs +31 -0
  4. package/src/KeyValuePair.mjs +27 -0
  5. package/src/KeyValuePair.ts +31 -0
  6. package/src/Metadata.cjs +91 -0
  7. package/src/Metadata.mjs +87 -0
  8. package/src/Metadata.ts +100 -0
  9. package/src/Namespace.cjs +212 -0
  10. package/src/Namespace.mjs +206 -0
  11. package/src/Namespace.ts +260 -0
  12. package/src/ObjectSlice.cjs +199 -0
  13. package/src/ObjectSlice.mjs +195 -0
  14. package/src/ObjectSlice.ts +228 -0
  15. package/src/clone/errors/CloneError.cjs +22 -0
  16. package/src/clone/errors/CloneError.mjs +19 -0
  17. package/src/clone/errors/CloneError.ts +26 -0
  18. package/src/clone/errors/DeepCloneError.cjs +11 -0
  19. package/src/clone/errors/DeepCloneError.mjs +6 -0
  20. package/src/clone/errors/DeepCloneError.ts +8 -0
  21. package/src/clone/errors/ShallowCloneError.cjs +11 -0
  22. package/src/clone/errors/ShallowCloneError.mjs +6 -0
  23. package/src/clone/errors/ShallowCloneError.ts +8 -0
  24. package/src/clone/index.cjs +188 -0
  25. package/src/clone/index.mjs +178 -0
  26. package/src/clone/index.ts +195 -0
  27. package/src/elements/Annotation.cjs +35 -0
  28. package/src/elements/Annotation.mjs +30 -0
  29. package/src/elements/Annotation.ts +35 -0
  30. package/src/elements/Comment.cjs +18 -0
  31. package/src/elements/Comment.mjs +13 -0
  32. package/src/elements/Comment.ts +16 -0
  33. package/src/elements/LinkElement.cjs +50 -0
  34. package/src/elements/LinkElement.mjs +45 -0
  35. package/src/elements/LinkElement.ts +49 -0
  36. package/src/elements/ParseResult.cjs +91 -0
  37. package/src/elements/ParseResult.mjs +86 -0
  38. package/src/elements/ParseResult.ts +94 -0
  39. package/src/elements/RefElement.cjs +34 -0
  40. package/src/elements/RefElement.mjs +29 -0
  41. package/src/elements/RefElement.ts +33 -0
  42. package/src/elements/SourceMap.cjs +140 -0
  43. package/src/elements/SourceMap.mjs +134 -0
  44. package/src/elements/SourceMap.ts +170 -0
  45. package/src/elements/Style.cjs +54 -0
  46. package/src/elements/Style.mjs +48 -0
  47. package/src/elements/Style.ts +56 -0
  48. package/src/index.cjs +58 -0
  49. package/src/index.mjs +11 -0
  50. package/src/index.ts +79 -0
  51. package/src/predicates/elements.cjs +46 -0
  52. package/src/predicates/elements.mjs +35 -0
  53. package/src/predicates/elements.ts +42 -0
  54. package/src/predicates/index.cjs +77 -0
  55. package/src/predicates/index.mjs +56 -0
  56. package/src/predicates/index.ts +89 -0
  57. package/src/predicates/primitives.cjs +69 -0
  58. package/src/predicates/primitives.mjs +56 -0
  59. package/src/predicates/primitives.ts +79 -0
  60. package/src/primitives/ArrayElement.cjs +155 -0
  61. package/src/primitives/ArrayElement.mjs +148 -0
  62. package/src/primitives/ArrayElement.ts +161 -0
  63. package/src/primitives/BooleanElement.cjs +20 -0
  64. package/src/primitives/BooleanElement.mjs +15 -0
  65. package/src/primitives/BooleanElement.ts +18 -0
  66. package/src/primitives/CollectionElement.cjs +180 -0
  67. package/src/primitives/CollectionElement.mjs +173 -0
  68. package/src/primitives/CollectionElement.ts +191 -0
  69. package/src/primitives/Element.cjs +510 -0
  70. package/src/primitives/Element.mjs +505 -0
  71. package/src/primitives/Element.ts +556 -0
  72. package/src/primitives/MemberElement.cjs +58 -0
  73. package/src/primitives/MemberElement.mjs +53 -0
  74. package/src/primitives/MemberElement.ts +61 -0
  75. package/src/primitives/NullElement.cjs +28 -0
  76. package/src/primitives/NullElement.mjs +23 -0
  77. package/src/primitives/NullElement.ts +26 -0
  78. package/src/primitives/NumberElement.cjs +20 -0
  79. package/src/primitives/NumberElement.mjs +15 -0
  80. package/src/primitives/NumberElement.ts +18 -0
  81. package/src/primitives/ObjectElement.cjs +220 -0
  82. package/src/primitives/ObjectElement.mjs +214 -0
  83. package/src/primitives/ObjectElement.ts +263 -0
  84. package/src/primitives/StringElement.cjs +27 -0
  85. package/src/primitives/StringElement.mjs +22 -0
  86. package/src/primitives/StringElement.ts +25 -0
  87. package/src/registration.cjs +101 -0
  88. package/src/registration.mjs +79 -0
  89. package/src/registration.ts +111 -0
  90. package/src/serialisers/JSONSerialiser.cjs +230 -0
  91. package/src/serialisers/JSONSerialiser.mjs +221 -0
  92. package/src/serialisers/JSONSerialiser.ts +295 -0
  93. package/src/types.cjs +3 -0
  94. package/src/types.mjs +1 -0
  95. package/src/types.ts +72 -0
@@ -0,0 +1,505 @@
1
+ import { equals } from 'ramda';
2
+ import Metadata from "../Metadata.mjs";
3
+ import KeyValuePair from "../KeyValuePair.mjs";
4
+ import ObjectSlice from "../ObjectSlice.mjs"; // shared singleton for frozen elements with no meta — avoids allocation on every access
5
+ const FROZEN_EMPTY_METADATA = Object.freeze(new Metadata());
6
+
7
+ /**
8
+ * Valid content types for an Element.
9
+ * @public
10
+ */
11
+
12
+ /**
13
+ * Base Element class that all ApiDOM elements extend.
14
+ *
15
+ * Elements are the core building blocks of ApiDOM. Each element has:
16
+ * - An `element` property identifying its type
17
+ * - Optional `content` holding the element's value
18
+ * - Optional `meta` for metadata (id, classes, title, description, links)
19
+ * - Optional `attributes` for element-specific properties
20
+ *
21
+ * @public
22
+ */
23
+ class Element {
24
+ // ============================================================
25
+ // Public Properties
26
+ // ============================================================
27
+
28
+ /**
29
+ * Parent element reference (set when tree is frozen).
30
+ */
31
+ parent;
32
+
33
+ /**
34
+ * Format-specific style information for round-trip preservation.
35
+ * Each format owns its own namespace (e.g., `yaml`, `json`).
36
+ */
37
+ style;
38
+
39
+ // ============================================================================
40
+ // Source Position (LSP-compatible, TextDocument-compatible, UTF-16 code units)
41
+ // web-tree-sitter automatically provides position data in UTF-16 code units.
42
+ // ============================================================================
43
+
44
+ /**
45
+ * Starting line number (0-based).
46
+ * Compatible with LSP Position.line.
47
+ */
48
+ startLine;
49
+
50
+ /**
51
+ * Starting character offset within the line (0-based, UTF-16 code units).
52
+ * Compatible with LSP Position.character.
53
+ */
54
+ startCharacter;
55
+
56
+ /**
57
+ * Starting offset from beginning of document (UTF-16 code units).
58
+ * Can be used directly as JavaScript string index.
59
+ */
60
+ startOffset;
61
+
62
+ /**
63
+ * Ending line number (0-based).
64
+ * Compatible with LSP Position.line.
65
+ */
66
+ endLine;
67
+
68
+ /**
69
+ * Ending character offset within the line (0-based, UTF-16 code units).
70
+ * Compatible with LSP Position.character.
71
+ */
72
+ endCharacter;
73
+
74
+ /**
75
+ * Ending offset from beginning of document (UTF-16 code units).
76
+ * Can be used directly as JavaScript string index.
77
+ */
78
+ endOffset;
79
+
80
+ // ============================================================
81
+ // Protected Properties
82
+ // ============================================================
83
+
84
+ /**
85
+ * The element type identifier.
86
+ * @internal
87
+ */
88
+ _storedElement = 'element';
89
+
90
+ /**
91
+ * The element's content/value.
92
+ * @internal
93
+ */
94
+ _content;
95
+
96
+ /**
97
+ * Metadata about this element.
98
+ * @internal
99
+ */
100
+ _meta;
101
+
102
+ /**
103
+ * Element-specific attributes.
104
+ * @internal
105
+ */
106
+ _attributes;
107
+
108
+ // ============================================================
109
+ // Prototype-assigned properties (set in registration.ts)
110
+ // Using 'declare' allows TypeScript to know about these
111
+ // without generating runtime code.
112
+ // ============================================================
113
+
114
+ /** @internal ObjectElement constructor for creating meta/attributes */
115
+
116
+ /** @internal ArrayElement constructor for creating arrays */
117
+
118
+ /** @internal MemberElement constructor for creating object members */
119
+
120
+ /** @internal RefElement constructor for creating references */
121
+
122
+ /** @internal Function to convert values to elements */
123
+
124
+ constructor(content, meta, attributes) {
125
+ if (meta !== undefined) {
126
+ this.meta = meta;
127
+ }
128
+ if (attributes !== undefined) {
129
+ this.attributes = attributes;
130
+ }
131
+ if (content !== undefined) {
132
+ this.content = content;
133
+ }
134
+ }
135
+
136
+ // ============================================================
137
+ // Core Properties
138
+ // ============================================================
139
+
140
+ /**
141
+ * The element type identifier (e.g., 'string', 'object', 'array').
142
+ */
143
+ get element() {
144
+ return this._storedElement;
145
+ }
146
+ set element(value) {
147
+ this._storedElement = value;
148
+ }
149
+
150
+ /**
151
+ * The element's content/value.
152
+ */
153
+ get content() {
154
+ return this._content;
155
+ }
156
+ set content(value) {
157
+ // Already an element
158
+ if (value instanceof Element) {
159
+ this._content = value;
160
+ return;
161
+ }
162
+
163
+ // Primitives (inlined for performance - avoids 8 function calls)
164
+ if (value === null || value === undefined || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint' || typeof value === 'symbol') {
165
+ this._content = value;
166
+ return;
167
+ }
168
+
169
+ // KeyValuePair
170
+ if (value instanceof KeyValuePair) {
171
+ this._content = value;
172
+ return;
173
+ }
174
+
175
+ // ObjectSlice - extract elements array
176
+ if (value instanceof ObjectSlice) {
177
+ this._content = value.elements;
178
+ return;
179
+ }
180
+
181
+ // Array - refract each item
182
+ if (Array.isArray(value)) {
183
+ this._content = value.map(item => this.refract(item));
184
+ return;
185
+ }
186
+
187
+ // Plain object - convert to MemberElements
188
+ if (typeof value === 'object') {
189
+ this._content = Object.entries(value).map(([key, val]) => new this.MemberElement(key, val));
190
+ return;
191
+ }
192
+ throw new Error(`Cannot set content to value of type ${typeof value}`);
193
+ }
194
+
195
+ /**
196
+ * Metadata about this element (id, classes, title, description, links).
197
+ * Lazily creates a Metadata instance if not set.
198
+ */
199
+ get meta() {
200
+ if (!this._meta) {
201
+ if (this.isFrozen) return FROZEN_EMPTY_METADATA;
202
+ this._meta = new Metadata();
203
+ }
204
+ return this._meta;
205
+ }
206
+ set meta(value) {
207
+ if (value instanceof Metadata) {
208
+ this._meta = value;
209
+ } else if (value && typeof value === 'object') {
210
+ const meta = new Metadata();
211
+ Object.assign(meta, value);
212
+ this._meta = meta;
213
+ }
214
+ }
215
+
216
+ /**
217
+ * Element-specific attributes.
218
+ * Lazily creates an ObjectElement if not set.
219
+ */
220
+ get attributes() {
221
+ if (!this._attributes) {
222
+ if (this.isFrozen) {
223
+ const attributes = new this.ObjectElement();
224
+ attributes.freeze();
225
+ return attributes;
226
+ }
227
+ this._attributes = new this.ObjectElement();
228
+ }
229
+ return this._attributes;
230
+ }
231
+ set attributes(value) {
232
+ if (value instanceof Element) {
233
+ this._attributes = value;
234
+ } else {
235
+ this.attributes.set(value ?? {});
236
+ }
237
+ }
238
+
239
+ // ============================================================
240
+ // Meta Property Shortcuts
241
+ // ============================================================
242
+
243
+ /** Unique identifier for this element. */
244
+ get id() {
245
+ if (!this.hasMetaProperty('id')) {
246
+ if (this.isFrozen) return '';
247
+ this.setMetaProperty('id', '');
248
+ }
249
+ return this.meta.get('id');
250
+ }
251
+ set id(value) {
252
+ this.setMetaProperty('id', value);
253
+ }
254
+
255
+ /** CSS-like class names. */
256
+ get classes() {
257
+ if (!this.hasMetaProperty('classes')) {
258
+ if (this.isFrozen) return [];
259
+ this.setMetaProperty('classes', []);
260
+ }
261
+ return this.meta.get('classes');
262
+ }
263
+ set classes(value) {
264
+ this.setMetaProperty('classes', value);
265
+ }
266
+
267
+ /** Hyperlinks associated with this element. */
268
+ get links() {
269
+ if (!this.hasMetaProperty('links')) {
270
+ if (this.isFrozen) {
271
+ const empty = new this.ArrayElement();
272
+ empty.freeze();
273
+ return empty;
274
+ }
275
+ this.setMetaProperty('links', new this.ArrayElement());
276
+ }
277
+ return this.meta.get('links');
278
+ }
279
+ set links(value) {
280
+ this.setMetaProperty('links', value);
281
+ }
282
+
283
+ // ============================================================
284
+ // Tree Navigation
285
+ // ============================================================
286
+
287
+ /** Returns direct children of this element. */
288
+ get children() {
289
+ const {
290
+ _content: content
291
+ } = this;
292
+ if (Array.isArray(content)) {
293
+ return content;
294
+ }
295
+ if (content instanceof KeyValuePair) {
296
+ const children = [];
297
+ if (content.key) children.push(content.key);
298
+ if (content.value) children.push(content.value);
299
+ return children;
300
+ }
301
+ if (content instanceof Element) {
302
+ return [content];
303
+ }
304
+ return [];
305
+ }
306
+
307
+ // ============================================================
308
+ // Freezable Implementation
309
+ // ============================================================
310
+
311
+ /** Whether this element is frozen (immutable). */
312
+ get isFrozen() {
313
+ return Object.isFrozen(this);
314
+ }
315
+
316
+ /**
317
+ * Freezes the element tree, making it immutable.
318
+ * Sets up parent references for tree traversal.
319
+ */
320
+ freeze() {
321
+ if (this.isFrozen) return;
322
+
323
+ // Freeze meta and attributes
324
+ if (this._meta) {
325
+ this._meta.freeze();
326
+ }
327
+ if (this._attributes) {
328
+ this._attributes.parent = this;
329
+ this._attributes.freeze();
330
+ }
331
+
332
+ // Freeze children
333
+ for (const child of this.children) {
334
+ child.parent = this;
335
+ child.freeze();
336
+ }
337
+
338
+ // Freeze content array if applicable
339
+ if (Array.isArray(this._content)) {
340
+ Object.freeze(this._content);
341
+ }
342
+ Object.freeze(this);
343
+ }
344
+
345
+ // ============================================================
346
+ // ToValue Implementation
347
+ // ============================================================
348
+
349
+ /**
350
+ * Converts the element to its JavaScript value representation.
351
+ */
352
+ toValue() {
353
+ const {
354
+ _content
355
+ } = this;
356
+ if (_content instanceof Element) {
357
+ return _content.toValue();
358
+ }
359
+ if (_content instanceof KeyValuePair) {
360
+ return _content.toValue();
361
+ }
362
+ if (Array.isArray(_content)) {
363
+ return _content.map(el => el.toValue());
364
+ }
365
+ return _content;
366
+ }
367
+
368
+ // ============================================================
369
+ // Equatable Implementation
370
+ // ============================================================
371
+
372
+ /**
373
+ * Checks deep equality with another value.
374
+ */
375
+ equals(value) {
376
+ const compareTo = value instanceof Element ? value.toValue() : value;
377
+ return equals(this.toValue(), compareTo);
378
+ }
379
+
380
+ // ============================================================
381
+ // Element Type
382
+ // ============================================================
383
+
384
+ /**
385
+ * Returns the primitive type name for this element.
386
+ * Override in subclasses (e.g., 'string', 'number', 'array').
387
+ */
388
+ primitive() {
389
+ return undefined;
390
+ }
391
+
392
+ // ============================================================
393
+ // Content Operations
394
+ // ============================================================
395
+
396
+ /**
397
+ * Sets the content of this element.
398
+ * @returns this for chaining
399
+ */
400
+ set(content) {
401
+ this.content = content;
402
+ return this;
403
+ }
404
+
405
+ // ============================================================
406
+ // Reference Creation
407
+ // ============================================================
408
+
409
+ /**
410
+ * Creates a RefElement pointing to this element.
411
+ * @param path - Optional path within the referenced element
412
+ * @throws Error if this element has no ID
413
+ */
414
+ toRef(path) {
415
+ const idValue = this.id;
416
+ if (idValue === '') {
417
+ throw new Error('Cannot create reference to an element without an ID');
418
+ }
419
+ const ref = new this.RefElement(idValue);
420
+ if (typeof path === 'string') {
421
+ ref.path = this.refract(path);
422
+ }
423
+ return ref;
424
+ }
425
+
426
+ /**
427
+ * Gets a meta property value.
428
+ *
429
+ * When the property doesn't exist:
430
+ * - With defaultValue: returns the provided default value
431
+ * - Without defaultValue: returns undefined
432
+ */
433
+
434
+ getMetaProperty(name, defaultValue) {
435
+ if (!this.hasMetaProperty(name)) {
436
+ return defaultValue;
437
+ }
438
+ return this.meta.get(name);
439
+ }
440
+
441
+ /**
442
+ * Sets a meta property.
443
+ */
444
+ setMetaProperty(name, value) {
445
+ this.meta.set(name, value);
446
+ }
447
+
448
+ /**
449
+ * Checks whether a meta property exists.
450
+ */
451
+ hasMetaProperty(name) {
452
+ return this._meta !== undefined && this._meta.hasKey(name);
453
+ }
454
+
455
+ /**
456
+ * Checks if meta is empty.
457
+ */
458
+ get isMetaEmpty() {
459
+ return this._meta === undefined || this._meta.isEmpty;
460
+ }
461
+
462
+ /**
463
+ * Gets an attribute property, creating it with default value if not present.
464
+ */
465
+ getAttributesProperty(name, defaultValue) {
466
+ if (!this.hasAttributesProperty(name)) {
467
+ if (this.isFrozen) {
468
+ const element = this.refract(defaultValue);
469
+ element.freeze();
470
+ return element;
471
+ }
472
+ this.attributes.set(name, defaultValue);
473
+ }
474
+ return this.attributes.get(name);
475
+ }
476
+
477
+ /**
478
+ * Sets an attributes property.
479
+ */
480
+ setAttributesProperty(name, value) {
481
+ this.attributes.set(name, value);
482
+ }
483
+
484
+ /**
485
+ * Checks whether an attributes property exists.
486
+ */
487
+ hasAttributesProperty(name) {
488
+ if (!this.isAttributesEmpty) {
489
+ return this.attributes.hasKey(name);
490
+ }
491
+ return false;
492
+ }
493
+
494
+ /**
495
+ * Checks if attributes is empty.
496
+ */
497
+ get isAttributesEmpty() {
498
+ return this._attributes === undefined || this.attributes.isEmpty;
499
+ }
500
+ }
501
+
502
+ // Re-export types for convenience
503
+
504
+ export { Metadata };
505
+ export default Element;