@vicin/sigil 2.1.0 → 2.2.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.
package/dist/index.js CHANGED
@@ -6,25 +6,24 @@ var cuid2 = require('@paralleldrive/cuid2');
6
6
  var OPTIONS = {
7
7
  labelValidation: null,
8
8
  skipLabelInheritanceCheck: false,
9
- autofillLabels: false
9
+ autofillLabels: true
10
10
  };
11
11
  var updateSigilOptions = (opts) => {
12
- if (opts.autofillLabels) {
12
+ if ("autofillLabels" in opts) {
13
13
  if (typeof opts.autofillLabels !== "boolean")
14
14
  throw new Error("'updateSigilOptions.autofillLabels' must be boolean");
15
15
  OPTIONS.autofillLabels = opts.autofillLabels;
16
16
  }
17
- if (opts.skipLabelInheritanceCheck) {
17
+ if ("skipLabelInheritanceCheck" in opts) {
18
18
  if (typeof opts.skipLabelInheritanceCheck !== "boolean")
19
19
  throw new Error("'updateSigilOptions.skipLabelInheritanceCheck' must be boolean");
20
20
  OPTIONS.skipLabelInheritanceCheck = opts.skipLabelInheritanceCheck;
21
21
  }
22
- if (opts.labelValidation) {
23
- if (opts.labelValidation !== null && typeof opts.labelValidation !== "function" && !(opts.labelValidation instanceof RegExp))
24
- throw new Error(
25
- "'updateSigilOptions.labelValidation' must be null, function or regex expression"
26
- );
27
- OPTIONS.labelValidation = opts.labelValidation;
22
+ if ("labelValidation" in opts) {
23
+ const val = opts.labelValidation;
24
+ if (val !== null && typeof val !== "function" && !(val instanceof RegExp))
25
+ throw new Error("'updateSigilOptions.labelValidation' must be null, function or RegExp");
26
+ OPTIONS.labelValidation = val != null ? val : null;
28
27
  }
29
28
  };
30
29
  var DEFAULT_LABEL_REGEX = /^@[\w-]+(?:\/[\w-]+)*\.[A-Z][A-Za-z0-9]*$/;
@@ -35,47 +34,50 @@ var __SIGIL_BASE__ = /* @__PURE__ */ Symbol.for("@Sigil.__SIGIL_BASE__");
35
34
  var __DECORATED__ = /* @__PURE__ */ Symbol.for("@Sigil.__DECORATED__");
36
35
  var __INHERITANCE_CHECKED__ = /* @__PURE__ */ Symbol.for("@Sigil.__INHERITANCE_CHECKED__");
37
36
  var __LABEL__ = /* @__PURE__ */ Symbol.for("@Sigil.__LABEL__");
37
+ var __EFFECTIVE_LABEL__ = /* @__PURE__ */ Symbol.for("@Sigil.__EFFECTIVE_LABEL__");
38
38
  var __LABEL_LINEAGE__ = /* @__PURE__ */ Symbol.for("@Sigil.__LABEL_LINEAGE__");
39
39
  var __LABEL_SET__ = /* @__PURE__ */ Symbol.for("@Sigil.__LABEL_SET__");
40
-
41
- // src/core/constants.ts
42
- var __DEV__ = typeof process !== "undefined" && process.env.NODE_ENV === "development";
43
-
44
- // src/core/helpers.ts
45
- function decorateCtor(ctor, label, isMixin = false) {
46
- if (isDecorated(ctor))
47
- throw new Error(
48
- `Constructor ${ctor} is already decorated. if you are using 'withSigilTyped()' & '@WithSigil()' at the same time remove one of them.`
49
- );
40
+ function decorateCtor(ctor, label, runtime) {
41
+ if (process.env.NODE_ENV !== "production") {
42
+ if (isDecorated(ctor))
43
+ throw new Error(
44
+ `Constructor ${ctor} is already decorated. if you are using 'withSigilTyped()' & '@WithSigil()' at the same time remove one of them.`
45
+ );
46
+ }
50
47
  Object.defineProperty(ctor, __LABEL__, {
51
48
  value: label,
52
- configurable: false,
49
+ configurable: true,
53
50
  enumerable: false,
54
51
  writable: false
55
52
  });
53
+ if (!(runtime == null ? void 0 : runtime.isInheritanceCheck))
54
+ Object.defineProperty(ctor, __EFFECTIVE_LABEL__, {
55
+ value: label,
56
+ configurable: true,
57
+ enumerable: false,
58
+ writable: false
59
+ });
56
60
  const parent = Object.getPrototypeOf(ctor);
57
61
  const parentChain = parent && parent[__LABEL_LINEAGE__] ? parent[__LABEL_LINEAGE__] : [];
58
- const ctorChain = isMixin && label !== "Sigil" ? ["Sigil", ...parentChain, label] : [...parentChain, label];
62
+ const ctorChain = (runtime == null ? void 0 : runtime.isMixin) && label !== "Sigil" ? ["Sigil", ...parentChain, label] : [...parentChain, label];
59
63
  Object.defineProperty(ctor, __LABEL_LINEAGE__, {
60
64
  value: ctorChain,
61
- configurable: false,
65
+ configurable: true,
62
66
  enumerable: false,
63
67
  writable: false
64
68
  });
65
69
  Object.defineProperty(ctor, __LABEL_SET__, {
66
70
  value: new Set(ctorChain),
67
- configurable: false,
71
+ configurable: true,
68
72
  enumerable: false,
69
73
  writable: false
70
74
  });
71
- markDecorated(ctor);
75
+ if (!(runtime == null ? void 0 : runtime.isInheritanceCheck)) markDecorated(ctor);
72
76
  }
73
77
  function checkInheritance(ctor, opts) {
74
78
  var _a, _b;
75
- const skipLabelInheritanceCheck = (_a = opts == null ? void 0 : opts.skipLabelInheritanceCheck) != null ? _a : OPTIONS.skipLabelInheritanceCheck;
76
- const autofillLabels = (_b = opts == null ? void 0 : opts.autofillLabels) != null ? _b : OPTIONS.autofillLabels;
77
- if (!isSigilCtor(ctor)) return;
78
- if (isInheritanceChecked(ctor) || skipLabelInheritanceCheck) return;
79
+ if (isInheritanceChecked(ctor) || ((_a = opts == null ? void 0 : opts.skipLabelInheritanceCheck) != null ? _a : OPTIONS.skipLabelInheritanceCheck))
80
+ return;
79
81
  const ctors = [ctor];
80
82
  let ancestor = Object.getPrototypeOf(ctor);
81
83
  while (isSigilCtor(ancestor)) {
@@ -86,16 +88,16 @@ function checkInheritance(ctor, opts) {
86
88
  for (let i = ctors.length - 1; i >= 0; i--) {
87
89
  const ctor2 = ctors[i];
88
90
  if (!ctor2) continue;
89
- let label = ctor2.SigilLabel;
91
+ let label = ctor2[__LABEL__];
90
92
  if (labelOwner.has(label)) {
91
- if (isDecorated(ctor2) || !autofillLabels) {
92
- const ancestorName = labelOwner.get(label);
93
- throw new Error(
94
- `[Sigil Error] Class "${ctor2.name}" re-uses Sigil label "${label}" from ancestor "${ancestorName}". Each Sigil subclass must use a unique label. Did you forget to use "WithSigil(newLabel)" on the subclass?`
95
- );
93
+ if (process.env.NODE_ENV !== "production") {
94
+ if (isDecorated(ctor2) || !((_b = opts == null ? void 0 : opts.autofillLabels) != null ? _b : OPTIONS.autofillLabels))
95
+ throw new Error(
96
+ `[Sigil Error] Class "${ctor2.name}" re-uses Sigil label "${label}" from ancestor "${labelOwner.get(label)}". Each Sigil subclass must use a unique label. Did you forget to use "WithSigil(newLabel)" on the subclass?`
97
+ );
96
98
  }
97
99
  label = generateRandomLabel();
98
- decorateCtor(ctor2, label);
100
+ decorateCtor(ctor2, label, { isInheritanceCheck: true });
99
101
  }
100
102
  labelOwner.set(label, ctor2.name);
101
103
  }
@@ -108,10 +110,12 @@ function verifyLabel(label, opts) {
108
110
  let valid;
109
111
  if (labelValidation instanceof RegExp) valid = labelValidation.test(label);
110
112
  else valid = labelValidation(label);
111
- if (!valid)
112
- throw new Error(
113
- `[Sigil] Invalid identity label "${label}". Make sure that supplied label matches validation regex or function.`
114
- );
113
+ if (process.env.NODE_ENV !== "production") {
114
+ if (!valid)
115
+ throw new Error(
116
+ `[Sigil] Invalid identity label "${label}". Make sure that supplied label matches validation regex or function.`
117
+ );
118
+ }
115
119
  }
116
120
  }
117
121
  function generateRandomLabel() {
@@ -180,7 +184,7 @@ function getConstructor(obj) {
180
184
 
181
185
  // src/core/mixin.ts
182
186
  function Sigilify(Base, label, opts) {
183
- if (isSigilCtor(Base)) throw new Error(`[Sigil Error] 'Sigilify(${label})' already siglified.`);
187
+ if (isSigilCtor(Base)) throw new Error(`[Sigil Error] 'Sigilify(${label})' already sigilified.`);
184
188
  let l;
185
189
  if (label) {
186
190
  verifyLabel(label, opts);
@@ -188,11 +192,18 @@ function Sigilify(Base, label, opts) {
188
192
  } else l = generateRandomLabel();
189
193
  class Sigilified extends Base {
190
194
  /**
191
- * Class-level human-readable label constant for this sigil constructor.
195
+ * Class-level identity label constant for this sigil constructor.
192
196
  */
193
197
  static get SigilLabel() {
198
+ if (!isInheritanceChecked(this)) checkInheritance(this);
194
199
  return this[__LABEL__];
195
200
  }
201
+ /**
202
+ * Class-level human-readable label constant for this sigil constructor, last passed label in 'Sigil' chain by developer.
203
+ */
204
+ static get SigilEffectiveLabel() {
205
+ return this[__EFFECTIVE_LABEL__];
206
+ }
196
207
  /**
197
208
  * Copy of the linearized sigil type label chain for the current constructor.
198
209
  *
@@ -202,6 +213,7 @@ function Sigilify(Base, label, opts) {
202
213
  */
203
214
  static get SigilLabelLineage() {
204
215
  var _a;
216
+ if (!isInheritanceChecked(this)) checkInheritance(this);
205
217
  return [...(_a = this[__LABEL_LINEAGE__]) != null ? _a : []];
206
218
  }
207
219
  /**
@@ -212,6 +224,7 @@ function Sigilify(Base, label, opts) {
212
224
  * @returns A Readonly Set of labels that represent the type lineage.
213
225
  */
214
226
  static get SigilLabelSet() {
227
+ if (!isInheritanceChecked(this)) checkInheritance(this);
215
228
  const set = /* @__PURE__ */ new Set();
216
229
  for (const s of this[__LABEL_SET__]) set.add(s);
217
230
  return set;
@@ -222,11 +235,11 @@ function Sigilify(Base, label, opts) {
222
235
  Object.setPrototypeOf(this, new.target.prototype);
223
236
  const ctor = getConstructor(this);
224
237
  if (!ctor) {
225
- if (__DEV__)
238
+ if (process.env.NODE_ENV !== "production")
226
239
  throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
227
240
  return;
228
241
  }
229
- if (__DEV__) checkInheritance(ctor);
242
+ checkInheritance(ctor);
230
243
  }
231
244
  /**
232
245
  * Runtime predicate indicating whether `obj` is an instance produced by a sigil class.
@@ -305,19 +318,33 @@ function Sigilify(Base, label, opts) {
305
318
  return !!otherLineage && thisLineage.every((s, i) => s === otherLineage[i]);
306
319
  }
307
320
  /**
308
- * Returns the human-readable sigil label of this instance's constructor.
321
+ * Returns the identity sigil label of this instance's constructor.
309
322
  *
310
- * @returns The label string (e.g. '@scope/pkg.ClassName') or '@Sigil.unknown' in DEV when constructor is missing.
323
+ * @returns The label string if passed (e.g. '@scope/pkg.ClassName'), random label if not passed (e.g. '@Sigil.auto-dq62ib6jnvmmlfbjhxh2937h') or '@Sigil.unknown' if constructor is missing.
311
324
  */
312
325
  getSigilLabel() {
313
326
  const ctor = getConstructor(this);
314
327
  if (!ctor) {
315
- if (__DEV__)
316
- throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
328
+ if (process.env.NODE_ENV !== "production")
329
+ throw new Error(`[Sigil Error] Sigil class instance without constructor`);
317
330
  return "@Sigil.unknown";
318
331
  }
319
332
  return ctor.SigilLabel;
320
333
  }
334
+ /**
335
+ * Returns the human-readable sigil label of this instance's constructor.
336
+ *
337
+ * @returns The last passed label string (e.g. '@scope/pkg.ClassName') or '@Sigil.unknown' if constructor is missing.
338
+ */
339
+ getSigilEffectiveLabel() {
340
+ const ctor = getConstructor(this);
341
+ if (!ctor) {
342
+ if (process.env.NODE_ENV !== "production")
343
+ throw new Error(`[Sigil Error] Sigil class instance without constructor`);
344
+ return "@Sigil.unknown";
345
+ }
346
+ return ctor.SigilEffectiveLabel;
347
+ }
321
348
  /**
322
349
  * Returns a copy of the sigil type label lineage for this instance's constructor.
323
350
  *
@@ -326,8 +353,8 @@ function Sigilify(Base, label, opts) {
326
353
  getSigilLabelLineage() {
327
354
  const ctor = getConstructor(this);
328
355
  if (!ctor) {
329
- if (__DEV__)
330
- throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
356
+ if (process.env.NODE_ENV !== "production")
357
+ throw new Error(`[Sigil Error] Sigil class instance without constructor`);
331
358
  return ["@Sigil.unknown"];
332
359
  }
333
360
  return ctor.SigilLabelLineage;
@@ -340,20 +367,20 @@ function Sigilify(Base, label, opts) {
340
367
  getSigilLabelSet() {
341
368
  const ctor = getConstructor(this);
342
369
  if (!ctor) {
343
- if (__DEV__)
344
- throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
370
+ if (process.env.NODE_ENV !== "production")
371
+ throw new Error(`[Sigil Error] Sigil class instance without constructor`);
345
372
  return /* @__PURE__ */ new Set(["@Sigil.unknown"]);
346
373
  }
347
374
  return ctor.SigilLabelSet;
348
375
  }
349
376
  }
350
- decorateCtor(Sigilified, l, true);
377
+ decorateCtor(Sigilified, l, { isMixin: true });
351
378
  markSigil(Sigilified);
352
379
  markSigilBase(Sigilified);
353
380
  return Sigilified;
354
381
  }
355
382
  function SigilifyAbstract(Base, label, opts) {
356
- if (isSigilCtor(Base)) throw new Error(`[Sigil Error] 'Sigilify(${label})' already siglified.`);
383
+ if (isSigilCtor(Base)) throw new Error(`[Sigil Error] 'Sigilify(${label})' already sigilified.`);
357
384
  let l;
358
385
  if (label) {
359
386
  verifyLabel(label, opts);
@@ -361,11 +388,18 @@ function SigilifyAbstract(Base, label, opts) {
361
388
  } else l = generateRandomLabel();
362
389
  class Sigilified extends Base {
363
390
  /**
364
- * Class-level human-readable label constant for this sigil constructor.
391
+ * Class-level identity label constant for this sigil constructor.
365
392
  */
366
393
  static get SigilLabel() {
394
+ if (!isInheritanceChecked(this)) checkInheritance(this);
367
395
  return this[__LABEL__];
368
396
  }
397
+ /**
398
+ * Class-level human-readable label constant for this sigil constructor, last passed label in 'Sigil' chain by developer.
399
+ */
400
+ static get SigilEffectiveLabel() {
401
+ return this[__EFFECTIVE_LABEL__];
402
+ }
369
403
  /**
370
404
  * Copy of the linearized sigil type label chain for the current constructor.
371
405
  *
@@ -375,6 +409,7 @@ function SigilifyAbstract(Base, label, opts) {
375
409
  */
376
410
  static get SigilLabelLineage() {
377
411
  var _a;
412
+ if (!isInheritanceChecked(this)) checkInheritance(this);
378
413
  return [...(_a = this[__LABEL_LINEAGE__]) != null ? _a : []];
379
414
  }
380
415
  /**
@@ -385,6 +420,7 @@ function SigilifyAbstract(Base, label, opts) {
385
420
  * @returns A Readonly Set of labels that represent the type lineage.
386
421
  */
387
422
  static get SigilLabelSet() {
423
+ if (!isInheritanceChecked(this)) checkInheritance(this);
388
424
  const set = /* @__PURE__ */ new Set();
389
425
  for (const s of this[__LABEL_SET__]) set.add(s);
390
426
  return set;
@@ -395,11 +431,11 @@ function SigilifyAbstract(Base, label, opts) {
395
431
  Object.setPrototypeOf(this, new.target.prototype);
396
432
  const ctor = getConstructor(this);
397
433
  if (!ctor) {
398
- if (__DEV__)
434
+ if (process.env.NODE_ENV !== "production")
399
435
  throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
400
436
  return;
401
437
  }
402
- if (__DEV__) checkInheritance(ctor);
438
+ checkInheritance(ctor);
403
439
  }
404
440
  /**
405
441
  * Runtime predicate indicating whether `obj` is an instance produced by a sigil class.
@@ -427,7 +463,7 @@ function SigilifyAbstract(Base, label, opts) {
427
463
  */
428
464
  static isOfType(other) {
429
465
  var _a;
430
- if (!isSigilInstance(other) || !isSigilCtor(this)) return false;
466
+ if (!isSigilInstance(other)) return false;
431
467
  const otherSet = (_a = getConstructor(other)) == null ? void 0 : _a[__LABEL_SET__];
432
468
  const thisType = this[__LABEL__];
433
469
  return !!otherSet && otherSet.has(thisType);
@@ -446,7 +482,7 @@ function SigilifyAbstract(Base, label, opts) {
446
482
  */
447
483
  static isOfTypeStrict(other) {
448
484
  var _a;
449
- if (!isSigilInstance(other) || !isSigilCtor(this)) return false;
485
+ if (!isSigilInstance(other)) return false;
450
486
  const otherLineage = (_a = getConstructor(other)) == null ? void 0 : _a[__LABEL_LINEAGE__];
451
487
  const thisLineage = this[__LABEL_LINEAGE__];
452
488
  return !!otherLineage && thisLineage.every((s, i) => s === otherLineage[i]);
@@ -463,7 +499,7 @@ function SigilifyAbstract(Base, label, opts) {
463
499
  */
464
500
  isOfType(other) {
465
501
  var _a;
466
- if (!isSigilInstance(other) || !isSigilInstance(this)) return false;
502
+ if (!isSigilInstance(other)) return false;
467
503
  const otherSet = (_a = getConstructor(other)) == null ? void 0 : _a[__LABEL_SET__];
468
504
  const thisType = getConstructor(this)[__LABEL__];
469
505
  return !!otherSet && otherSet.has(thisType);
@@ -480,25 +516,39 @@ function SigilifyAbstract(Base, label, opts) {
480
516
  */
481
517
  isOfTypeStrict(other) {
482
518
  var _a, _b;
483
- if (!isSigilInstance(other) || !isSigilInstance(this)) return false;
519
+ if (!isSigilInstance(other)) return false;
484
520
  const otherLineage = (_a = getConstructor(other)) == null ? void 0 : _a[__LABEL_LINEAGE__];
485
521
  const thisLineage = (_b = getConstructor(this)) == null ? void 0 : _b[__LABEL_LINEAGE__];
486
522
  return !!otherLineage && thisLineage.every((s, i) => s === otherLineage[i]);
487
523
  }
488
524
  /**
489
- * Returns the human-readable sigil label of this instance's constructor.
525
+ * Returns the identity sigil label of this instance's constructor.
490
526
  *
491
- * @returns The label string (e.g. '@scope/pkg.ClassName') or '@Sigil.unknown' in DEV when constructor is missing.
527
+ * @returns The label string if passed (e.g. '@scope/pkg.ClassName'), random label if not passed (e.g. '@Sigil.auto-dq62ib6jnvmmlfbjhxh2937h') or '@Sigil.unknown' if constructor is missing.
492
528
  */
493
529
  getSigilLabel() {
494
530
  const ctor = getConstructor(this);
495
531
  if (!ctor) {
496
- if (__DEV__)
497
- throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
532
+ if (process.env.NODE_ENV !== "production")
533
+ throw new Error(`[Sigil Error] Sigil class instance without constructor`);
498
534
  return "@Sigil.unknown";
499
535
  }
500
536
  return ctor.SigilLabel;
501
537
  }
538
+ /**
539
+ * Returns the human-readable sigil label of this instance's constructor.
540
+ *
541
+ * @returns The last passed label string (e.g. '@scope/pkg.ClassName') or '@Sigil.unknown' if constructor is missing.
542
+ */
543
+ getSigilEffectiveLabel() {
544
+ const ctor = getConstructor(this);
545
+ if (!ctor) {
546
+ if (process.env.NODE_ENV !== "production")
547
+ throw new Error(`[Sigil Error] Sigil class instance without constructor`);
548
+ return "@Sigil.unknown";
549
+ }
550
+ return ctor.SigilEffectiveLabel;
551
+ }
502
552
  /**
503
553
  * Returns a copy of the sigil type label lineage for this instance's constructor.
504
554
  *
@@ -507,8 +557,8 @@ function SigilifyAbstract(Base, label, opts) {
507
557
  getSigilLabelLineage() {
508
558
  const ctor = getConstructor(this);
509
559
  if (!ctor) {
510
- if (__DEV__)
511
- throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
560
+ if (process.env.NODE_ENV !== "production")
561
+ throw new Error(`[Sigil Error] Sigil class instance without constructor`);
512
562
  return ["@Sigil.unknown"];
513
563
  }
514
564
  return ctor.SigilLabelLineage;
@@ -521,14 +571,14 @@ function SigilifyAbstract(Base, label, opts) {
521
571
  getSigilLabelSet() {
522
572
  const ctor = getConstructor(this);
523
573
  if (!ctor) {
524
- if (__DEV__)
525
- throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
574
+ if (process.env.NODE_ENV !== "production")
575
+ throw new Error(`[Sigil Error] Sigil class instance without constructor`);
526
576
  return /* @__PURE__ */ new Set(["@Sigil.unknown"]);
527
577
  }
528
578
  return ctor.SigilLabelSet;
529
579
  }
530
580
  }
531
- decorateCtor(Sigilified, l, true);
581
+ decorateCtor(Sigilified, l, { isMixin: true });
532
582
  markSigil(Sigilified);
533
583
  markSigilBase(Sigilified);
534
584
  return Sigilified;
@@ -553,7 +603,7 @@ function WithSigil(label, opts) {
553
603
  `[Sigil Error] 'WithSigil' decorator accept only Sigil classes but used on class ${value.name}`
554
604
  );
555
605
  decorateCtor(value, l);
556
- if (__DEV__) checkInheritance(value, opts);
606
+ checkInheritance(value, opts);
557
607
  };
558
608
  }
559
609
 
@@ -571,7 +621,7 @@ function withSigil(Class, label, opts) {
571
621
  } else l = generateRandomLabel();
572
622
  const ctor = Class;
573
623
  decorateCtor(ctor, l);
574
- if (__DEV__) checkInheritance(ctor, opts);
624
+ checkInheritance(ctor, opts);
575
625
  return Class;
576
626
  }
577
627
  function withSigilTyped(Class, label, opts) {
@@ -587,7 +637,7 @@ function withSigilTyped(Class, label, opts) {
587
637
  } else l = generateRandomLabel();
588
638
  const ctor = Class;
589
639
  decorateCtor(ctor, l);
590
- if (__DEV__) checkInheritance(ctor, opts);
640
+ checkInheritance(ctor, opts);
591
641
  return Class;
592
642
  }
593
643