@vicin/sigil 3.3.0 → 3.4.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.
@@ -32,16 +32,29 @@
32
32
  var __SIGIL__ = /* @__PURE__ */ Symbol.for("@vicin/sigil.__SIGIL__");
33
33
  var __LABEL__ = /* @__PURE__ */ Symbol.for("@vicin/sigil.__LABEL__");
34
34
  var __EFFECTIVE_LABEL__ = /* @__PURE__ */ Symbol.for("@vicin/sigil.__EFFECTIVE_LABEL__");
35
- var __LINEAGE__ = /* @__PURE__ */ Symbol.for("@vicin/sigil.__LINEAGE__");
35
+ var __DEPTH__ = /* @__PURE__ */ Symbol.for("@vicin/sigil.__DEPTH__");
36
36
 
37
37
  // src/helpers.ts
38
38
  var AUTO_LABEL_PREFEX = "@Sigil-auto";
39
39
  var handledCtors = /* @__PURE__ */ new WeakSet();
40
- function handleSigil(ctor, label, opts) {
41
- if (handledCtors.has(ctor)) return;
40
+ var handledCtorsExplicit = /* @__PURE__ */ new WeakSet();
41
+ function handleSigilExplicit(ctor, label, opts) {
42
+ if (handledCtorsExplicit.has(ctor))
43
+ throw new Error(
44
+ `[Sigil Error] Class '${ctor.name}' with label '${ctor.SigilLabel}' is already sigilified`
45
+ );
42
46
  verifyLabel(ctor, label, opts);
43
47
  handleAncestors(ctor, opts);
44
- sigilify(ctor, label != null ? label : generateRandomLabel(ctor));
48
+ sigilify(ctor, label, true);
49
+ }
50
+ function handleSigilLazy(ctor) {
51
+ if (handledCtors.has(ctor)) return;
52
+ if (!OPTIONS.autofillLabels)
53
+ throw new Error(
54
+ `[Sigil Error] Class '${ctor == null ? void 0 : ctor.name}' is not sigilified, Make sure to sigilify all Sigil classes or set 'autofillLabels' to 'true'`
55
+ );
56
+ handleAncestors(ctor);
57
+ sigilify(ctor, generateRandomLabel(ctor), false);
45
58
  }
46
59
  function handleAncestors(ctor, opts) {
47
60
  var _a;
@@ -60,54 +73,48 @@
60
73
  throw new Error(
61
74
  `[Sigil Error] Class '${a2.name}' is not sigilified, Make sure to sigilify all Sigil classes or set 'autofillLabels' to 'true'`
62
75
  );
63
- sigilify(a2, generateRandomLabel(a2));
76
+ sigilify(a2, generateRandomLabel(a2), false);
64
77
  }
65
- labelOwner.set(labelOf(a2), a2.name);
78
+ labelOwner.set(a2.prototype[__LABEL__], a2.name);
66
79
  }
67
80
  }
68
- function sigilify(ctor, label) {
81
+ function sigilify(ctor, label, explicit) {
69
82
  var _a;
70
83
  const sym = Symbol.for(label);
71
84
  Object.defineProperty(ctor.prototype, __SIGIL__, {
72
85
  value: sym,
73
- configurable: false,
74
- enumerable: false,
75
- writable: false
76
- });
77
- Object.defineProperty(ctor.prototype, sym, {
78
- value: true,
79
- configurable: false,
86
+ configurable: !explicit,
80
87
  enumerable: false,
81
88
  writable: false
82
89
  });
83
90
  Object.defineProperty(ctor.prototype, __LABEL__, {
84
91
  value: label,
85
- configurable: false,
92
+ configurable: !explicit,
86
93
  enumerable: false,
87
94
  writable: false
88
95
  });
89
- if (!label.startsWith(AUTO_LABEL_PREFEX))
96
+ if (explicit)
90
97
  Object.defineProperty(ctor.prototype, __EFFECTIVE_LABEL__, {
91
98
  value: label,
92
99
  configurable: false,
93
100
  enumerable: false,
94
101
  writable: false
95
102
  });
96
- Object.defineProperty(ctor.prototype, __LINEAGE__, {
97
- value: /* @__PURE__ */ new Set(["Sigil", ...(_a = lineageOf(ctor)) != null ? _a : [], label]),
98
- configurable: false,
99
- enumerable: false,
100
- writable: false
101
- });
102
- const sigilSym = /* @__PURE__ */ Symbol.for("Sigil");
103
- if (ctor.prototype[sigilSym] !== true)
104
- Object.defineProperty(ctor.prototype, sigilSym, {
105
- value: true,
103
+ if (!handledCtors.has(ctor))
104
+ Object.defineProperty(ctor.prototype, __DEPTH__, {
105
+ value: ((_a = ctor.prototype[__DEPTH__]) != null ? _a : -1) + 1,
106
106
  configurable: false,
107
107
  enumerable: false,
108
108
  writable: false
109
109
  });
110
+ Object.defineProperty(ctor.prototype, sym, {
111
+ value: true,
112
+ configurable: false,
113
+ enumerable: false,
114
+ writable: false
115
+ });
110
116
  handledCtors.add(ctor);
117
+ if (explicit) handledCtorsExplicit.add(ctor);
111
118
  }
112
119
  function isSigilCtor(ctor) {
113
120
  return typeof ctor === "function" && ctor.prototype && __SIGIL__ in ctor.prototype;
@@ -115,15 +122,6 @@
115
122
  function isSigilInstance(inst) {
116
123
  return !!inst && typeof inst === "object" && __SIGIL__ in inst;
117
124
  }
118
- function hasOwnSigil(ctor) {
119
- return typeof ctor === "function" && Object.hasOwn(ctor.prototype, __SIGIL__);
120
- }
121
- function labelOf(ctor) {
122
- return ctor.prototype[__LABEL__];
123
- }
124
- function lineageOf(ctor) {
125
- return ctor.prototype[__LINEAGE__];
126
- }
127
125
  function getSigilLabels() {
128
126
  return getLabelRegistry().labels();
129
127
  }
@@ -147,22 +145,15 @@
147
145
  return labelRegistry;
148
146
  }
149
147
  function verifyLabel(ctor, label, opts) {
150
- var _a, _b, _c;
148
+ var _a, _b;
151
149
  const reg = getLabelRegistry();
152
- if (!label) {
153
- if (!((_a = opts == null ? void 0 : opts.autofillLabels) != null ? _a : OPTIONS.autofillLabels))
154
- throw new Error(
155
- `[Sigil Error] Class '${ctor == null ? void 0 : ctor.name}' is not sigilified, Make sure to sigilify all Sigil classes or set 'autofillLabels' to 'true'`
156
- );
157
- return;
158
- }
159
150
  if (label.startsWith(AUTO_LABEL_PREFEX))
160
151
  throw new Error(`'${AUTO_LABEL_PREFEX}' is a prefex reserved by the library`);
161
- if (!((_b = opts == null ? void 0 : opts.skipLabelUniquenessCheck) != null ? _b : OPTIONS.skipLabelUniquenessCheck) && reg.has(label))
152
+ if (!((_a = opts == null ? void 0 : opts.skipLabelUniquenessCheck) != null ? _a : OPTIONS.skipLabelUniquenessCheck) && reg.has(label))
162
153
  throw new Error(
163
154
  `[Sigil Error] Passed label '${label}' to class '${ctor == null ? void 0 : ctor.name}' is re-used, passed labels must be unique`
164
155
  );
165
- const labelValidation = (_c = opts == null ? void 0 : opts.labelValidation) != null ? _c : OPTIONS.labelValidation;
156
+ const labelValidation = (_b = opts == null ? void 0 : opts.labelValidation) != null ? _b : OPTIONS.labelValidation;
166
157
  if (labelValidation) {
167
158
  let valid;
168
159
  if (labelValidation instanceof RegExp) valid = labelValidation.test(label);
@@ -179,24 +170,20 @@
179
170
  }
180
171
 
181
172
  // src/mixin.ts
182
- function Sigilify(Base, label, opts) {
183
- if (hasOwnSigil(Base))
184
- throw new Error(
185
- `[Sigil Error] Class '${Base.name}' with label '${Base.SigilLabel}' is already sigilified`
186
- );
187
- class Sigilified extends Base {
173
+ function BaseSigilify(Base) {
174
+ class Sigil2 extends Base {
188
175
  /**
189
176
  * Class-level identity label constant for this sigil constructor.
190
177
  */
191
178
  static get SigilLabel() {
192
- handleSigil(this);
179
+ handleSigilLazy(this);
193
180
  return this.prototype[__LABEL__];
194
181
  }
195
182
  /**
196
183
  * Class-level human-readable label constant for this sigil constructor, last passed label in 'Sigil' chain by developer.
197
184
  */
198
185
  static get SigilEffectiveLabel() {
199
- handleSigil(this);
186
+ handleSigilLazy(this);
200
187
  return this.prototype[__EFFECTIVE_LABEL__];
201
188
  }
202
189
  /**
@@ -207,23 +194,29 @@
207
194
  * @returns An array of labels representing parent → child type labels.
208
195
  */
209
196
  static get SigilLabelLineage() {
210
- handleSigil(this);
211
- return [...this.prototype[__LINEAGE__]];
197
+ handleSigilLazy(this);
198
+ const lineage = [];
199
+ let c = this;
200
+ while (c && typeof c === "function" && c.prototype[__SIGIL__]) {
201
+ lineage.unshift(c.SigilLabel);
202
+ c = Object.getPrototypeOf(c);
203
+ }
204
+ return lineage;
212
205
  }
213
206
  /**
214
207
  * Sigil type label set for the current constructor.
215
208
  * Useful for debugging.
216
209
  *
210
+ * @deprecated To minize API and bundle size, internally this method is 'new Set(this.SigilLabelLineage)' only. will be removed in v4
217
211
  * @returns A Readonly Set of labels that represent the type lineage.
218
212
  */
219
213
  static get SigilLabelSet() {
220
- handleSigil(this);
221
- return this.prototype[__LINEAGE__];
214
+ return new Set(this.SigilLabelLineage);
222
215
  }
223
216
  constructor(...args) {
224
217
  super(...args);
225
218
  const ctor = new.target;
226
- handleSigil(ctor);
219
+ handleSigilLazy(ctor);
227
220
  }
228
221
  /**
229
222
  * Check whether `other` is (or inherits from) the instance represented by the
@@ -238,7 +231,7 @@
238
231
  * @returns A type guard asserting `other` is an instance of the constructor.
239
232
  */
240
233
  static isOfType(other) {
241
- handleSigil(this);
234
+ handleSigilLazy(this);
242
235
  if (other == null || typeof other !== "object") return false;
243
236
  return other[this.prototype[__SIGIL__]] === true;
244
237
  }
@@ -252,11 +245,9 @@
252
245
  * @returns A type guard asserting `other` is an instance of the constructor.
253
246
  */
254
247
  static isExactType(other) {
255
- var _a, _b;
256
- handleSigil(this);
248
+ handleSigilLazy(this);
257
249
  if (other == null || typeof other !== "object") return false;
258
- if (((_a = this.prototype) == null ? void 0 : _a[__LINEAGE__].size) !== ((_b = other[__LINEAGE__]) == null ? void 0 : _b.size))
259
- return false;
250
+ if (this.prototype[__DEPTH__] !== other[__DEPTH__]) return false;
260
251
  return other[this.prototype[__SIGIL__]] === true;
261
252
  }
262
253
  /**
@@ -285,9 +276,8 @@
285
276
  * @returns A type guard asserting `other` is an instance of the constructor.
286
277
  */
287
278
  isExactType(other) {
288
- var _a;
289
279
  if (other == null || typeof other !== "object") return false;
290
- if (this[__LINEAGE__].size !== ((_a = other[__LINEAGE__]) == null ? void 0 : _a.size)) return false;
280
+ if (this[__DEPTH__] !== other[__DEPTH__]) return false;
291
281
  return other[this[__SIGIL__]] === true;
292
282
  }
293
283
  /**
@@ -312,202 +302,77 @@
312
302
  * @returns readonly array of labels representing the type lineage.
313
303
  */
314
304
  getSigilLabelLineage() {
315
- return [...this[__LINEAGE__]];
305
+ const lineage = [];
306
+ let proto = Object.getPrototypeOf(this);
307
+ while (proto && proto[__SIGIL__]) {
308
+ lineage.unshift(proto[__LABEL__]);
309
+ proto = Object.getPrototypeOf(proto);
310
+ }
311
+ return lineage;
316
312
  }
317
313
  /**
318
314
  * Returns a copy of the sigil type label lineage set for this instance's constructor.
319
315
  *
316
+ * @deprecated To minize API and bundle size, internally this method is 'new Set(this.SigilLabelLineage)' only. will be removed in v4
320
317
  * @returns readonly array of labels representing the type lineage.
321
318
  */
322
319
  getSigilLabelSet() {
323
- return this[__LINEAGE__];
320
+ return new Set(this.getSigilLabelLineage());
324
321
  }
325
322
  }
326
- handleSigil(Sigilified, label, opts);
323
+ handleSigilExplicit(Sigil2, "Sigil", { skipLabelUniquenessCheck: true });
324
+ return Sigil2;
325
+ }
326
+ function Sigilify(Base, label, opts) {
327
+ if (isSigilCtor(Base))
328
+ throw new Error(
329
+ `[Sigil Error] Class '${Base.name}' with label '${Base.SigilLabel}' is already sigilified`
330
+ );
331
+ const BaseSigil = BaseSigilify(Base);
332
+ class Sigilified extends BaseSigil {
333
+ }
334
+ handleSigilExplicit(Sigilified, label, opts);
327
335
  return Sigilified;
328
336
  }
329
337
  function SigilifyAbstract(Base, label, opts) {
330
- if (hasOwnSigil(Base))
338
+ if (isSigilCtor(Base))
331
339
  throw new Error(
332
340
  `[Sigil Error] Class '${Base.name}' with label '${Base.SigilLabel}' is already sigilified`
333
341
  );
334
- class Sigilified extends Base {
335
- /**
336
- * Class-level identity label constant for this sigil constructor.
337
- */
338
- static get SigilLabel() {
339
- handleSigil(this);
340
- return this.prototype[__LABEL__];
341
- }
342
- /**
343
- * Class-level human-readable label constant for this sigil constructor, last passed label in 'Sigil' chain by developer.
344
- */
345
- static get SigilEffectiveLabel() {
346
- handleSigil(this);
347
- return this.prototype[__EFFECTIVE_LABEL__];
348
- }
349
- /**
350
- * Linearized sigil type label chain for the current constructor.
351
- *
352
- * Useful for debugging and performing strict lineage comparisons.
353
- *
354
- * @returns An array of labels representing parent → child type labels.
355
- */
356
- static get SigilLabelLineage() {
357
- handleSigil(this);
358
- return [...this.prototype[__LINEAGE__]];
359
- }
360
- /**
361
- * Sigil type label set for the current constructor.
362
- * Useful for debugging.
363
- *
364
- * @returns A Readonly Set of labels that represent the type lineage.
365
- */
366
- static get SigilLabelSet() {
367
- handleSigil(this);
368
- return this.prototype[__LINEAGE__];
369
- }
370
- constructor(...args) {
371
- super(...args);
372
- const ctor = new.target;
373
- handleSigil(ctor);
374
- }
375
- /**
376
- * Check whether `other` is (or inherits from) the instance represented by the
377
- * calling constructor.
378
- *
379
- * This replaces `instanceof` so that checks remain valid across bundles/realms
380
- * and when subclassing.
381
- *
382
- * @typeParam T - The specific sigil constructor (`this`).
383
- * @param this - The constructor performing the type check.
384
- * @param other - The object to test.
385
- * @returns A type guard asserting `other` is an instance of the constructor.
386
- */
387
- static isOfType(other) {
388
- handleSigil(this);
389
- if (other == null || typeof other !== "object") return false;
390
- return other[this.prototype[__SIGIL__]] === true;
391
- }
392
- /**
393
- * Check whether `other` is exactly the same instance represented by the
394
- * calling constructor.
395
- *
396
- * @typeParam T - The specific sigil constructor (`this`).
397
- * @param this - The constructor performing the type check.
398
- * @param other - The object to test.
399
- * @returns A type guard asserting `other` is an instance of the constructor.
400
- */
401
- static isExactType(other) {
402
- var _a, _b;
403
- handleSigil(this);
404
- if (other == null || typeof other !== "object") return false;
405
- if (((_a = this.prototype) == null ? void 0 : _a[__LINEAGE__].size) !== ((_b = other[__LINEAGE__]) == null ? void 0 : _b.size))
406
- return false;
407
- return other[this.prototype[__SIGIL__]] === true;
408
- }
409
- /**
410
- * Check whether `other` is (or inherits from) the instance represented by the
411
- * calling constructor.
412
- *
413
- * This replaces `instanceof` so that checks remain valid across bundles/realms
414
- * and when subclassing.
415
- *
416
- * @typeParam T - The specific sigil constructor (`this`).
417
- * @param this - The constructor performing the type check.
418
- * @param other - The object to test.
419
- * @returns A type guard asserting `other` is an instance of the constructor.
420
- */
421
- isOfType(other) {
422
- if (other == null || typeof other !== "object") return false;
423
- return other[this[__SIGIL__]] === true;
424
- }
425
- /**
426
- * Check whether `other` is exactly the same instance represented by the
427
- * calling constructor.
428
- *
429
- * @typeParam T - The specific sigil constructor (`this`).
430
- * @param this - The constructor performing the type check.
431
- * @param other - The object to test.
432
- * @returns A type guard asserting `other` is an instance of the constructor.
433
- */
434
- isExactType(other) {
435
- var _a;
436
- if (other == null || typeof other !== "object") return false;
437
- if (this[__LINEAGE__].size !== ((_a = other[__LINEAGE__]) == null ? void 0 : _a.size)) return false;
438
- return other[this[__SIGIL__]] === true;
439
- }
440
- /**
441
- * Returns the identity sigil label of this instance's constructor.
442
- *
443
- * @returns The label string if passed (e.g. '@scope/pkg.ClassName'), random label if not passed (e.g. '@Sigil-auto:ClassName:1:pnf11bgl').
444
- */
445
- getSigilLabel() {
446
- return this[__LABEL__];
447
- }
448
- /**
449
- * Returns the human-readable sigil label of this instance's constructor.
450
- *
451
- * @returns The last passed label string (e.g. '@scope/pkg.ClassName').
452
- */
453
- getSigilEffectiveLabel() {
454
- return this[__EFFECTIVE_LABEL__];
455
- }
456
- /**
457
- * Returns a copy of the sigil type label lineage for this instance's constructor.
458
- *
459
- * @returns readonly array of labels representing the type lineage.
460
- */
461
- getSigilLabelLineage() {
462
- return [...this[__LINEAGE__]];
463
- }
464
- /**
465
- * Returns a copy of the sigil type label lineage set for this instance's constructor.
466
- *
467
- * @returns readonly array of labels representing the type lineage.
468
- */
469
- getSigilLabelSet() {
470
- return this[__LINEAGE__];
471
- }
342
+ const BaseSigil = BaseSigilify(Base);
343
+ class Sigilified extends BaseSigil {
472
344
  }
473
- handleSigil(Sigilified, label, opts);
345
+ handleSigilExplicit(Sigilified, label, opts);
474
346
  return Sigilified;
475
347
  }
476
348
 
477
349
  // src/classes.ts
478
- var Sigil = Sigilify(class {
479
- }, "Sigil");
350
+ var Sigil = BaseSigilify(class {
351
+ });
480
352
  var SigilError = Sigilify(Error, "SigilError");
481
353
 
482
- // src/decorator.ts
483
- function WithSigil(label, opts) {
354
+ // src/attach.ts
355
+ function AttachSigil(label, opts) {
484
356
  return function(value, context) {
485
357
  if (!isSigilCtor(value))
486
358
  throw new Error(
487
- `[Sigil Error] 'WithSigil' decorator accept only Sigil classes but used on class '${value.name}'`
359
+ `[Sigil Error] 'AttachSigil' decorator accept only Sigil classes but used on class '${value.name}'`
488
360
  );
489
- if (hasOwnSigil(value))
490
- throw new Error(
491
- `[Sigil Error] Class '${value.name}' with label '${value.SigilLabel}' is already sigilified`
492
- );
493
- handleSigil(value, label, opts);
361
+ handleSigilExplicit(value, label, opts);
494
362
  };
495
363
  }
496
-
497
- // src/hof.ts
498
- function withSigil(Class, label, opts) {
364
+ var WithSigil = AttachSigil;
365
+ function attachSigil(Class, label, opts) {
499
366
  if (!isSigilCtor(Class))
500
367
  throw new Error(
501
- `[Sigil Error] 'withSigil' HOF accept only Sigil classes but used on class '${Class.name}'`
502
- );
503
- if (hasOwnSigil(Class))
504
- throw new Error(
505
- `[Sigil Error] Class '${Class.name}' with label '${Class.SigilLabel}' is already sigilified`
368
+ `[Sigil Error] 'attachSigil' function accept only Sigil classes but used on class '${Class.name}'`
506
369
  );
507
- handleSigil(Class, label, opts);
370
+ handleSigilExplicit(Class, label, opts);
508
371
  return Class;
509
372
  }
373
+ var withSigil = attachSigil;
510
374
 
375
+ exports.AttachSigil = AttachSigil;
511
376
  exports.DEFAULT_LABEL_REGEX = DEFAULT_LABEL_REGEX;
512
377
  exports.RECOMMENDED_LABEL_REGEX = RECOMMENDED_LABEL_REGEX;
513
378
  exports.Sigil = Sigil;
@@ -515,6 +380,7 @@
515
380
  exports.Sigilify = Sigilify;
516
381
  exports.SigilifyAbstract = SigilifyAbstract;
517
382
  exports.WithSigil = WithSigil;
383
+ exports.attachSigil = attachSigil;
518
384
  exports.getSigilLabels = getSigilLabels;
519
385
  exports.isSigilCtor = isSigilCtor;
520
386
  exports.isSigilInstance = isSigilInstance;