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