mnemonica 1.3.7 → 1.3.9
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/FOR_HUMANS.md +79 -7
- package/build/api/errors/exceptionConstructor.js +3 -3
- package/build/api/errors/index.d.ts +3 -7
- package/build/api/errors/index.js +3 -5
- package/build/api/errors/throwModificationError.js +2 -4
- package/build/api/hooks/HookInvocation.js +1 -1
- package/build/api/hooks/flowCheckers.d.ts +3 -3
- package/build/api/hooks/flowCheckers.js +1 -1
- package/build/api/hooks/invokeHook.js +1 -1
- package/build/api/index.d.ts +3 -3
- package/build/api/types/InstanceCreator.js +6 -7
- package/build/api/types/InstanceModificator.js +6 -3
- package/build/api/types/TypeProxy.js +2 -2
- package/build/api/types/compileNewModificatorFunctionBody.js +1 -1
- package/build/api/types/index.d.ts +3 -3
- package/build/api/types/index.js +32 -19
- package/build/api/utils/index.d.ts +2 -2
- package/build/api/utils/index.js +31 -18
- package/build/constants/index.js +4 -1
- package/build/descriptors/errors/index.js +1 -1
- package/build/descriptors/types/index.js +21 -8
- package/build/index.d.ts +11 -13
- package/build/index.js +17 -18
- package/build/types/index.d.ts +116 -23
- package/build/types/index.js +1 -1
- package/build/utils/collectConstructors.js +1 -1
- package/build/utils/fork.d.ts +2 -1
- package/build/utils/fork.js +1 -1
- package/build/utils/index.js +3 -1
- package/build/utils/lineage.d.ts +2 -0
- package/build/utils/lineage.js +156 -0
- package/build/utils/parse.js +1 -1
- package/docs/UTILS.md +41 -2
- package/docs/async-constructors.md +6 -0
- package/package.json +4 -2
- package/src/api/errors/exceptionConstructor.ts +7 -5
- package/src/api/errors/index.ts +17 -16
- package/src/api/errors/throwModificationError.ts +4 -5
- package/src/api/hooks/HookInvocation.ts +2 -1
- package/src/api/hooks/flowCheckers.ts +8 -3
- package/src/api/hooks/invokeHook.ts +1 -1
- package/src/api/types/InstanceCreator.ts +20 -10
- package/src/api/types/InstanceModificator.ts +19 -4
- package/src/api/types/TypeProxy.ts +11 -2
- package/src/api/types/compileNewModificatorFunctionBody.ts +6 -0
- package/src/api/types/index.ts +84 -28
- package/src/api/utils/index.ts +60 -24
- package/src/constants/index.ts +6 -0
- package/src/descriptors/errors/index.ts +4 -2
- package/src/descriptors/types/index.ts +44 -17
- package/src/index.ts +86 -52
- package/src/types/index.ts +262 -36
- package/src/utils/collectConstructors.ts +0 -9
- package/src/utils/fork.ts +2 -1
- package/src/utils/index.ts +6 -1
- package/src/utils/lineage.ts +220 -0
- package/src/utils/parse.ts +2 -1
|
@@ -16,15 +16,30 @@ export const makeInstanceModificator = ( self: InstanceCreatorContext ): Mnemoni
|
|
|
16
16
|
existentInstance,
|
|
17
17
|
ModificatorType,
|
|
18
18
|
proto,
|
|
19
|
+
config,
|
|
19
20
|
} = self;
|
|
20
21
|
|
|
22
|
+
// Class-handler prototypes need the FULL-descriptor copy: class body
|
|
23
|
+
// methods are non-enumerable, and an enumerable-only Object.assign copy
|
|
24
|
+
// silently dropped them — Type.define('X', class { m() {} }) lost m on
|
|
25
|
+
// the subtype while lazy kept it. Function-handler prototypes keep the
|
|
26
|
+
// historical enumerable-only copy: their non-enumerable props (legacy
|
|
27
|
+
// opt-in instance methods, exception wiring) were never copied, and
|
|
28
|
+
// pins depend on that (exception instances expose no bound methods)
|
|
29
|
+
const ModificatorTypePrototype = config.asClass
|
|
30
|
+
? Object.defineProperties(
|
|
31
|
+
{},
|
|
32
|
+
Object.getOwnPropertyDescriptors( proto )
|
|
33
|
+
)
|
|
34
|
+
: Object.assign(
|
|
35
|
+
{},
|
|
36
|
+
proto
|
|
37
|
+
);
|
|
38
|
+
|
|
21
39
|
const result = ModificationConstructor.call(
|
|
22
40
|
existentInstance,
|
|
23
41
|
ModificatorType,
|
|
24
|
-
|
|
25
|
-
{},
|
|
26
|
-
proto
|
|
27
|
-
),
|
|
42
|
+
ModificatorTypePrototype,
|
|
28
43
|
( __proto_proto__: unknown ) => {
|
|
29
44
|
self.__proto_proto__ = __proto_proto__ as object;
|
|
30
45
|
_addProps.call( self );
|
|
@@ -105,9 +105,15 @@ TypeProxy.prototype.set = function (this: TypeProxyInstance, _target: unknown, n
|
|
|
105
105
|
// is about setting a prototype to Type
|
|
106
106
|
if (name === 'prototype') {
|
|
107
107
|
checkProto(value);
|
|
108
|
-
Object.assign
|
|
108
|
+
// the FULL-descriptor copy, not Object.assign: an enumerable-only
|
|
109
|
+
// copy silently drops non-enumerable props — assigning a class
|
|
110
|
+
// prototype object lost its methods — and INVOKES getters, freezing
|
|
111
|
+
// them to a value instead of keeping them getters. Object.assign
|
|
112
|
+
// does copy enumerable symbol-keyed props; what it loses is the
|
|
113
|
+
// non-enumerables and the getters
|
|
114
|
+
Object.defineProperties(
|
|
109
115
|
type.proto,
|
|
110
|
-
value
|
|
116
|
+
Object.getOwnPropertyDescriptors( value )
|
|
111
117
|
);
|
|
112
118
|
return true;
|
|
113
119
|
}
|
|
@@ -140,6 +146,9 @@ const subTypeApply = (
|
|
|
140
146
|
cstr,
|
|
141
147
|
cfg
|
|
142
148
|
);
|
|
149
|
+
// decorator contract: define() returns the constructor wrapping cstr
|
|
150
|
+
// (construction still runs through cstr); the class binding is
|
|
151
|
+
// replaced by that constructor, so it is presented as T
|
|
143
152
|
const result = defineResult as unknown as T;
|
|
144
153
|
return result;
|
|
145
154
|
};
|
|
@@ -161,6 +161,9 @@ const compileNewModificatorFunctionBody = function ( FunctionName: string, asCla
|
|
|
161
161
|
const innerResult = function (): ModificationBody {
|
|
162
162
|
let ModificationBody: ModificationBody;
|
|
163
163
|
if ( asClass ) {
|
|
164
|
+
// this branch runs only when isClass() verified at define
|
|
165
|
+
// time that the handler is a class — the cast names the class
|
|
166
|
+
// view (construct signature) of that same function value
|
|
164
167
|
ModificationBody = getClassConstructor(
|
|
165
168
|
ConstructHandler as unknown as ClassConstructHandler,
|
|
166
169
|
CreationHandler
|
|
@@ -169,6 +172,9 @@ const compileNewModificatorFunctionBody = function ( FunctionName: string, asCla
|
|
|
169
172
|
// const ReNamedConstructHandler = {} as unknown;
|
|
170
173
|
// ReNamedConstructHandler[FunctionName] = ConstructHandler;
|
|
171
174
|
// ModificationBody = getFunctionConstructor(ReNamedConstructHandler[FunctionName], CreationHandler);
|
|
175
|
+
// the compiled body is invoked with `new` by the pipeline;
|
|
176
|
+
// a function expression carries no construct signature, so
|
|
177
|
+
// the `new`-able view of the same value is named here
|
|
172
178
|
ModificationBody = getFunctionConstructor(
|
|
173
179
|
ConstructHandler,
|
|
174
180
|
CreationHandler
|
package/src/api/types/index.ts
CHANGED
|
@@ -31,6 +31,9 @@ import {
|
|
|
31
31
|
TypeAbsorber,
|
|
32
32
|
ModificationConstructorFactory,
|
|
33
33
|
MnemonicaConstructorFactory,
|
|
34
|
+
DefineNewableOrCallable,
|
|
35
|
+
DefineNewable,
|
|
36
|
+
LazyDef,
|
|
34
37
|
} from '../../types';
|
|
35
38
|
|
|
36
39
|
// Lazy getter: a function that, when called, returns the actual constructor function.
|
|
@@ -64,6 +67,7 @@ const {
|
|
|
64
67
|
WRONG_TYPE_DEFINITION,
|
|
65
68
|
TYPENAME_MUST_BE_A_STRING,
|
|
66
69
|
HANDLER_MUST_BE_A_FUNCTION,
|
|
70
|
+
OPTIONS_ERROR,
|
|
67
71
|
} = ErrorsTypes;
|
|
68
72
|
|
|
69
73
|
// invokeHook
|
|
@@ -95,7 +99,7 @@ export type TypesMap = Map<string, object> & {
|
|
|
95
99
|
[MNEMOSYNE]?: CollectionDef;
|
|
96
100
|
};
|
|
97
101
|
|
|
98
|
-
const
|
|
102
|
+
const typeDescriptorCreator = function (
|
|
99
103
|
this: TypeDescriptorInstance,
|
|
100
104
|
defineOrigin: TypeAbsorber,
|
|
101
105
|
types: TypesMap,
|
|
@@ -124,11 +128,20 @@ const TypeDescriptor = function (
|
|
|
124
128
|
|
|
125
129
|
const title = `${TYPE_TITLE_PREFIX}${TypeName}`;
|
|
126
130
|
|
|
131
|
+
// 'name' is a COLLECTION option only: a user passing it here meant to
|
|
132
|
+
// name the collection, not the type — say so readably
|
|
133
|
+
if ( config !== undefined && typeof config === 'object' && 'name' in config ) {
|
|
134
|
+
const nameError = '\"name\" is a collection option ' +
|
|
135
|
+
'(createTypesCollection({ name })) — it cannot be set on a type';
|
|
136
|
+
throw new OPTIONS_ERROR( nameError );
|
|
137
|
+
}
|
|
127
138
|
config = Object.assign(
|
|
128
139
|
{},
|
|
129
140
|
(collection as Record<symbol, unknown>)[ SymbolConfig ],
|
|
130
141
|
config
|
|
131
142
|
);
|
|
143
|
+
// never inherited into the type's config either
|
|
144
|
+
Reflect.deleteProperty( config, 'name' );
|
|
132
145
|
|
|
133
146
|
const type = Object.assign(
|
|
134
147
|
this,
|
|
@@ -188,7 +201,13 @@ const TypeDescriptor = function (
|
|
|
188
201
|
const result = types.get(TypeName);
|
|
189
202
|
return result;
|
|
190
203
|
|
|
191
|
-
}
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
// the creator is only ever invoked with `new` (the createFrom* call sites
|
|
207
|
+
// below); a function expression carries no construct signature, so it is
|
|
208
|
+
// named once as the constructor interface it is used through — the
|
|
209
|
+
// prototype contract is attached immediately below
|
|
210
|
+
const TypeDescriptor = typeDescriptorCreator as _Internal_TC_<TypeDescriptorInstance>;
|
|
192
211
|
|
|
193
212
|
Object.assign(
|
|
194
213
|
TypeDescriptor.prototype,
|
|
@@ -197,8 +216,8 @@ Object.assign(
|
|
|
197
216
|
|
|
198
217
|
TypeDescriptor.prototype.define = function (
|
|
199
218
|
this: TypeDescriptorInstance,
|
|
200
|
-
TypeOrTypeName: string |
|
|
201
|
-
constructHandlerOrConfig?:
|
|
219
|
+
TypeOrTypeName: string | DefineNewableOrCallable,
|
|
220
|
+
constructHandlerOrConfig?: DefineNewableOrCallable | object,
|
|
202
221
|
config?: object
|
|
203
222
|
) {
|
|
204
223
|
const result = define.call(
|
|
@@ -213,20 +232,20 @@ TypeDescriptor.prototype.define = function (
|
|
|
213
232
|
|
|
214
233
|
TypeDescriptor.prototype.lazy = function (
|
|
215
234
|
this: TypeDescriptorInstance,
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
235
|
+
TypeNameOrGetter: string | LazyDef<object>,
|
|
236
|
+
getterOrConfig?: LazyDef<object> | object,
|
|
237
|
+
namedFormConfig?: object
|
|
219
238
|
) {
|
|
220
239
|
let name: string | undefined;
|
|
221
240
|
let getter: LazyTypeGetter;
|
|
222
241
|
let config: object | undefined;
|
|
223
|
-
if (typeof
|
|
224
|
-
name =
|
|
225
|
-
getter =
|
|
226
|
-
config =
|
|
242
|
+
if (typeof TypeNameOrGetter === 'string') {
|
|
243
|
+
name = TypeNameOrGetter;
|
|
244
|
+
getter = getterOrConfig as LazyTypeGetter;
|
|
245
|
+
config = namedFormConfig;
|
|
227
246
|
} else {
|
|
228
|
-
getter =
|
|
229
|
-
config =
|
|
247
|
+
getter = TypeNameOrGetter as LazyTypeGetter;
|
|
248
|
+
config = getterOrConfig as object;
|
|
230
249
|
}
|
|
231
250
|
let result: TypeClass;
|
|
232
251
|
if (name) {
|
|
@@ -281,15 +300,16 @@ odp(
|
|
|
281
300
|
get (this: TypeDescriptorInstance) {
|
|
282
301
|
const self = this;
|
|
283
302
|
const result = function (options?: object) {
|
|
284
|
-
const decorator = function (cstr:
|
|
303
|
+
const decorator = function (cstr: DefineNewable) {
|
|
285
304
|
const { name } = cstr;
|
|
305
|
+
// define() returns the registered constructor itself —
|
|
306
|
+
// the full TypeClass is a richer honest type than any cast
|
|
286
307
|
const defineResult = self.define(
|
|
287
308
|
name,
|
|
288
309
|
cstr,
|
|
289
310
|
options
|
|
290
311
|
);
|
|
291
|
-
|
|
292
|
-
return decoratedResult;
|
|
312
|
+
return defineResult;
|
|
293
313
|
};
|
|
294
314
|
return decorator;
|
|
295
315
|
};
|
|
@@ -428,6 +448,9 @@ const createFromDirectHandler = function (
|
|
|
428
448
|
makeConstructHandler,
|
|
429
449
|
proto,
|
|
430
450
|
config
|
|
451
|
+
// new TypeDescriptor() returns the TypeProxy it registers in the
|
|
452
|
+
// subtypes map — a Proxy materializing the TypeClass surface
|
|
453
|
+
// dynamically — so the public constructor contract is named here
|
|
431
454
|
) as unknown as TypeClass;
|
|
432
455
|
return result;
|
|
433
456
|
};
|
|
@@ -456,8 +479,22 @@ const createFromLazyGetter = function (
|
|
|
456
479
|
TypeName
|
|
457
480
|
);
|
|
458
481
|
|
|
482
|
+
// reject the unsupported handler shapes at DEFINE time too — the lazy
|
|
483
|
+
// path used to check only typeof/name, so a getter returning an arrow,
|
|
484
|
+
// a method, a bound function or a generator defined fine and failed
|
|
485
|
+
// (or misbehaved) at construction. Same readable errors as the direct
|
|
486
|
+
// path; checkDuplicate runs first so a taken name still wins with the
|
|
487
|
+
// canonical ALREADY_DECLARED
|
|
488
|
+
classifyConstructHandler(
|
|
489
|
+
TypeName,
|
|
490
|
+
type as ConstructHandler
|
|
491
|
+
);
|
|
492
|
+
|
|
459
493
|
const asClass = isClass(type);
|
|
460
494
|
|
|
495
|
+
// classifyConstructHandler above rejects only no-own-prototype
|
|
496
|
+
// functions; a function with an own NON-OBJECT prototype (prototype = 123)
|
|
497
|
+
// still passes and needs the default-prototype fallback
|
|
461
498
|
const proto = (
|
|
462
499
|
hop(
|
|
463
500
|
type,
|
|
@@ -469,6 +506,14 @@ const createFromLazyGetter = function (
|
|
|
469
506
|
const makeConstructHandler = () => {
|
|
470
507
|
const constructHandler = getter();
|
|
471
508
|
|
|
509
|
+
// and on EVERY construction: the getter may return a new function
|
|
510
|
+
// each time, so the shape check cannot be cached from define time.
|
|
511
|
+
// This is what makes lazy slower than a direct define
|
|
512
|
+
classifyConstructHandler(
|
|
513
|
+
TypeName,
|
|
514
|
+
constructHandler as ConstructHandler
|
|
515
|
+
);
|
|
516
|
+
|
|
472
517
|
odp(
|
|
473
518
|
constructHandler,
|
|
474
519
|
SymbolConstructorName, {
|
|
@@ -491,6 +536,12 @@ const createFromLazyGetter = function (
|
|
|
491
536
|
return handlerResult;
|
|
492
537
|
};
|
|
493
538
|
|
|
539
|
+
// 'name' is a COLLECTION option only (same rejection as the direct path)
|
|
540
|
+
if ( config !== undefined && typeof config === 'object' && 'name' in config ) {
|
|
541
|
+
const nameError = '\"name\" is a collection option ' +
|
|
542
|
+
'(createTypesCollection({ name })) — it cannot be set on a type';
|
|
543
|
+
throw new OPTIONS_ERROR( nameError );
|
|
544
|
+
}
|
|
494
545
|
config = Object.assign(
|
|
495
546
|
{},
|
|
496
547
|
config
|
|
@@ -505,6 +556,8 @@ const createFromLazyGetter = function (
|
|
|
505
556
|
makeConstructHandler,
|
|
506
557
|
proto,
|
|
507
558
|
config
|
|
559
|
+
// same TypeProxy return as createFromDirectHandler: the public
|
|
560
|
+
// TypeClass contract is materialized dynamically by the Proxy
|
|
508
561
|
) as unknown as TypeClass;
|
|
509
562
|
return result;
|
|
510
563
|
};
|
|
@@ -519,8 +572,8 @@ const createFromLazyGetter = function (
|
|
|
519
572
|
export const define = function (
|
|
520
573
|
this: unknown,
|
|
521
574
|
subtypes: TypesMap,
|
|
522
|
-
TypeOrTypeName: string |
|
|
523
|
-
constructHandlerOrConfig?:
|
|
575
|
+
TypeOrTypeName: string | DefineNewableOrCallable,
|
|
576
|
+
constructHandlerOrConfig?: DefineNewableOrCallable | object,
|
|
524
577
|
config?: object
|
|
525
578
|
): TypeClass {
|
|
526
579
|
|
|
@@ -588,21 +641,21 @@ export const define = function (
|
|
|
588
641
|
export const lazy = function (
|
|
589
642
|
this: unknown,
|
|
590
643
|
subtypes: TypesMap,
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
644
|
+
TypeNameOrGetter: string | LazyTypeGetter | undefined,
|
|
645
|
+
getterOrConfig?: LazyTypeGetter | object,
|
|
646
|
+
namedFormConfig?: object
|
|
594
647
|
): TypeClass {
|
|
595
648
|
|
|
596
649
|
let name: string | undefined;
|
|
597
650
|
let getter: LazyTypeGetter;
|
|
598
651
|
let config: object | undefined;
|
|
599
|
-
if (typeof
|
|
600
|
-
name =
|
|
601
|
-
getter =
|
|
602
|
-
config =
|
|
652
|
+
if (typeof TypeNameOrGetter === 'string') {
|
|
653
|
+
name = TypeNameOrGetter;
|
|
654
|
+
getter = getterOrConfig as LazyTypeGetter;
|
|
655
|
+
config = namedFormConfig;
|
|
603
656
|
} else {
|
|
604
|
-
getter =
|
|
605
|
-
config =
|
|
657
|
+
getter = TypeNameOrGetter as LazyTypeGetter;
|
|
658
|
+
config = getterOrConfig as object;
|
|
606
659
|
}
|
|
607
660
|
|
|
608
661
|
if (typeof getter !== 'function') {
|
|
@@ -648,8 +701,11 @@ export const lookup = function (
|
|
|
648
701
|
if (!type) {
|
|
649
702
|
return undefined;
|
|
650
703
|
}
|
|
704
|
+
// a type's subtypes map IS the runtime TypesMap (SymbolParentType and
|
|
705
|
+
// MNEMOSYNE are installed at definition time), so the single cast only
|
|
706
|
+
// names that view
|
|
651
707
|
const result = lookup.call(
|
|
652
|
-
type.subtypes as
|
|
708
|
+
type.subtypes as TypesMap,
|
|
653
709
|
NextNestedPath
|
|
654
710
|
);
|
|
655
711
|
return result;
|
package/src/api/utils/index.ts
CHANGED
|
@@ -16,13 +16,19 @@ const { WRONG_TYPE_DEFINITION, } = ErrorsTypes;
|
|
|
16
16
|
import {
|
|
17
17
|
_getProps, Props
|
|
18
18
|
} from '../types/Props';
|
|
19
|
-
import type {
|
|
19
|
+
import type {
|
|
20
|
+
InstanceConstructor,
|
|
21
|
+
MnemonicaConstructor
|
|
22
|
+
} from '../../types';
|
|
20
23
|
|
|
21
24
|
import compileNewModificatorFunctionBody, { ConstructHandler } from '../types/compileNewModificatorFunctionBody';
|
|
22
25
|
|
|
23
26
|
// CreationHandler - handles constructor return values
|
|
24
27
|
// Moved to api/types/index.ts as per refactoring plan
|
|
25
|
-
export const CreationHandler = function (
|
|
28
|
+
export const CreationHandler = function (
|
|
29
|
+
this: object & { constructor: InstanceConstructor },
|
|
30
|
+
constructionAnswer: unknown
|
|
31
|
+
) {
|
|
26
32
|
return constructionAnswer;
|
|
27
33
|
};
|
|
28
34
|
|
|
@@ -274,23 +280,59 @@ const makeErrorModificatorType = (
|
|
|
274
280
|
};
|
|
275
281
|
|
|
276
282
|
|
|
277
|
-
//
|
|
283
|
+
// a null or primitive-wrapper parent reflects its primitive to the instance
|
|
284
|
+
// built over it: coercion (+inst, `${inst}`) through Symbol.toPrimitive, and
|
|
285
|
+
// explicit inst.valueOf() / inst.toString() — the wrapper's own prototype
|
|
286
|
+
// methods throw when `this` is the instance rather than the wrapper, and an
|
|
287
|
+
// Object.create(null) parent has none at all
|
|
288
|
+
const reflectPrimitive = (thisArg: object, primitive: null | string | number | boolean) => {
|
|
289
|
+
odp(
|
|
290
|
+
thisArg,
|
|
291
|
+
Symbol.toPrimitive,
|
|
292
|
+
{
|
|
293
|
+
get () {
|
|
294
|
+
const result = () => {
|
|
295
|
+
return primitive;
|
|
296
|
+
};
|
|
297
|
+
return result;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
);
|
|
301
|
+
odp(
|
|
302
|
+
thisArg,
|
|
303
|
+
'valueOf',
|
|
304
|
+
{
|
|
305
|
+
get () {
|
|
306
|
+
const result = () => {
|
|
307
|
+
return primitive;
|
|
308
|
+
};
|
|
309
|
+
return result;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
);
|
|
313
|
+
odp(
|
|
314
|
+
thisArg,
|
|
315
|
+
'toString',
|
|
316
|
+
{
|
|
317
|
+
get () {
|
|
318
|
+
const result = () => {
|
|
319
|
+
const stringResult = String( primitive );
|
|
320
|
+
return stringResult;
|
|
321
|
+
};
|
|
322
|
+
return result;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
);
|
|
326
|
+
};
|
|
327
|
+
|
|
278
328
|
const reflectPrimitiveWrappers = (_thisArg: unknown) => {
|
|
279
329
|
let thisArg: object = _thisArg as object;
|
|
280
330
|
|
|
281
331
|
if (_thisArg === null) {
|
|
282
332
|
thisArg = Object.create(null);
|
|
283
|
-
|
|
333
|
+
reflectPrimitive(
|
|
284
334
|
thisArg,
|
|
285
|
-
|
|
286
|
-
{
|
|
287
|
-
get () {
|
|
288
|
-
const result = () => {
|
|
289
|
-
return _thisArg;
|
|
290
|
-
};
|
|
291
|
-
return result;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
335
|
+
null
|
|
294
336
|
);
|
|
295
337
|
}
|
|
296
338
|
|
|
@@ -299,18 +341,12 @@ const reflectPrimitiveWrappers = (_thisArg: unknown) => {
|
|
|
299
341
|
_thisArg instanceof Boolean ||
|
|
300
342
|
_thisArg instanceof String
|
|
301
343
|
) {
|
|
302
|
-
|
|
344
|
+
// read once, before the wrapper gets its own valueOf below;
|
|
345
|
+
// a wrapper's primitive never changes
|
|
346
|
+
const primitive = (_thisArg as String | Number | Boolean).valueOf();
|
|
347
|
+
reflectPrimitive(
|
|
303
348
|
thisArg,
|
|
304
|
-
|
|
305
|
-
{
|
|
306
|
-
get () {
|
|
307
|
-
const result = () => {
|
|
308
|
-
const valueResult = (_thisArg as String | Number | Boolean).valueOf();
|
|
309
|
-
return valueResult;
|
|
310
|
-
};
|
|
311
|
-
return result;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
349
|
+
primitive
|
|
314
350
|
);
|
|
315
351
|
}
|
|
316
352
|
|
package/src/constants/index.ts
CHANGED
|
@@ -81,6 +81,12 @@ const defaultOptions = {
|
|
|
81
81
|
return false;
|
|
82
82
|
},
|
|
83
83
|
|
|
84
|
+
// the collection's name for lineage exports (lethe type.collection);
|
|
85
|
+
// unnamed collections export as 'defaultTypes'
|
|
86
|
+
get name () {
|
|
87
|
+
return '';
|
|
88
|
+
},
|
|
89
|
+
|
|
84
90
|
} as Record<string, unknown>;
|
|
85
91
|
|
|
86
92
|
export const constants = {
|
|
@@ -10,7 +10,9 @@ const { ErrorMessages, } = constants;
|
|
|
10
10
|
|
|
11
11
|
// ErrorsTypes is dynamically built - using MnemonicaErrorConstructor to indicate these are constructable
|
|
12
12
|
export const ErrorsTypes: { [ index: string ]: MnemonicaErrorConstructor } = {
|
|
13
|
-
|
|
13
|
+
// the base class takes the whole message (not an addition) as its
|
|
14
|
+
// first argument — it has no fixed message of its own
|
|
15
|
+
BASE_MNEMONICA_ERROR : BASE_MNEMONICA_ERROR
|
|
14
16
|
};
|
|
15
17
|
|
|
16
18
|
Object.entries( ErrorMessages ).forEach( entry => {
|
|
@@ -20,7 +22,7 @@ Object.entries( ErrorMessages ).forEach( entry => {
|
|
|
20
22
|
ErrorConstructorName,
|
|
21
23
|
message
|
|
22
24
|
);
|
|
23
|
-
ErrorsTypes[ ErrorConstructorName ] = ErrorCtor
|
|
25
|
+
ErrorsTypes[ ErrorConstructorName ] = ErrorCtor;
|
|
24
26
|
} );
|
|
25
27
|
|
|
26
28
|
|
|
@@ -7,7 +7,11 @@ import type {
|
|
|
7
7
|
TypeClass,
|
|
8
8
|
IDEF,
|
|
9
9
|
hooksOpts,
|
|
10
|
-
hook
|
|
10
|
+
hook,
|
|
11
|
+
StackBoundary,
|
|
12
|
+
DefineNewable,
|
|
13
|
+
DefineNewableOrCallable,
|
|
14
|
+
FlowChecker
|
|
11
15
|
} from '../../types';
|
|
12
16
|
|
|
13
17
|
import { constants } from '../../constants';
|
|
@@ -151,9 +155,9 @@ odp(
|
|
|
151
155
|
get (this: { subtypes: Map<string, object> }) {
|
|
152
156
|
const { subtypes } = this;
|
|
153
157
|
const result = function (
|
|
154
|
-
this:
|
|
155
|
-
TypeOrTypeName: string |
|
|
156
|
-
constructHandlerOrConfig?:
|
|
158
|
+
this: StackBoundary,
|
|
159
|
+
TypeOrTypeName: string | DefineNewableOrCallable,
|
|
160
|
+
constructHandlerOrConfig?: DefineNewableOrCallable | object,
|
|
157
161
|
config?: object
|
|
158
162
|
) {
|
|
159
163
|
// pass `result` itself as the stack-capture boundary (StackBoundary):
|
|
@@ -181,21 +185,21 @@ odp(
|
|
|
181
185
|
get (this: { subtypes: Map<string, object> }) {
|
|
182
186
|
const { subtypes } = this;
|
|
183
187
|
const result = function (
|
|
184
|
-
this:
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
+
this: StackBoundary,
|
|
189
|
+
TypeNameOrGetter: string | LazyTypeGetter,
|
|
190
|
+
getterOrConfig?: LazyTypeGetter | object,
|
|
191
|
+
namedFormConfig?: object
|
|
188
192
|
) {
|
|
189
193
|
let name: string | undefined;
|
|
190
194
|
let getter: LazyTypeGetter;
|
|
191
195
|
let config: object | undefined;
|
|
192
|
-
if (typeof
|
|
193
|
-
name =
|
|
194
|
-
getter =
|
|
195
|
-
config =
|
|
196
|
+
if (typeof TypeNameOrGetter === 'string') {
|
|
197
|
+
name = TypeNameOrGetter;
|
|
198
|
+
getter = getterOrConfig as LazyTypeGetter;
|
|
199
|
+
config = namedFormConfig;
|
|
196
200
|
} else {
|
|
197
|
-
getter =
|
|
198
|
-
config =
|
|
201
|
+
getter = TypeNameOrGetter as LazyTypeGetter;
|
|
202
|
+
config = getterOrConfig as object;
|
|
199
203
|
}
|
|
200
204
|
let lazyResult: TypeClass;
|
|
201
205
|
// same as in `define` above: pass `result` itself as the
|
|
@@ -231,7 +235,7 @@ odp(
|
|
|
231
235
|
get (this: TypesCollection) {
|
|
232
236
|
const self = this;
|
|
233
237
|
const result = function (config?: object) {
|
|
234
|
-
const decorator = function (cstr:
|
|
238
|
+
const decorator = function (cstr: DefineNewable) {
|
|
235
239
|
const { name } = cstr;
|
|
236
240
|
const defineResult = self.define(
|
|
237
241
|
name,
|
|
@@ -258,7 +262,10 @@ odp(
|
|
|
258
262
|
TypeNestedPath: string
|
|
259
263
|
) {
|
|
260
264
|
const lookupResult = lookup.call(
|
|
261
|
-
|
|
265
|
+
// a collection's subtypes map IS the runtime TypesMap (the
|
|
266
|
+
// MNEMOSYNE/SymbolParentType props are installed at
|
|
267
|
+
// construction), so the single cast only names that view
|
|
268
|
+
this.subtypes as TypesMap,
|
|
262
269
|
TypeNestedPath
|
|
263
270
|
);
|
|
264
271
|
return lookupResult;
|
|
@@ -314,7 +321,7 @@ odp(
|
|
|
314
321
|
'registerFlowChecker',
|
|
315
322
|
{
|
|
316
323
|
get (this: TypesCollection) {
|
|
317
|
-
const result = (flowCheckerCallback:
|
|
324
|
+
const result = (flowCheckerCallback: FlowChecker) => {
|
|
318
325
|
const checkerResult = registerFlowChecker.call(
|
|
319
326
|
typesCollections.get(this),
|
|
320
327
|
flowCheckerCallback
|
|
@@ -373,9 +380,29 @@ const typesCollectionProxyHandler = {
|
|
|
373
380
|
}
|
|
374
381
|
};
|
|
375
382
|
|
|
383
|
+
// every collection exports under exactly one name (lethe type.collection):
|
|
384
|
+
// the first collection — the default one — is 'defaultTypes'; later
|
|
385
|
+
// unnamed collections get 'collection_1', 'collection_2', … in creation
|
|
386
|
+
// order (the scheme tactica writes for mnemographica). No two collections
|
|
387
|
+
// may ever export the same name.
|
|
388
|
+
let defaultCollectionNamed = false;
|
|
389
|
+
let unnamedCollections = 0;
|
|
390
|
+
|
|
376
391
|
const createTypesCollection = (config: Record<string, unknown> = {}) => {
|
|
377
392
|
|
|
378
393
|
const typesCollection = new TypesCollection(config);
|
|
394
|
+
const collectionConfig = (typesCollection as { [ SymbolConfig ]?: Record<string, unknown> })[
|
|
395
|
+
SymbolConfig
|
|
396
|
+
];
|
|
397
|
+
if ( collectionConfig && !collectionConfig.name ) {
|
|
398
|
+
if ( !defaultCollectionNamed ) {
|
|
399
|
+
collectionConfig.name = 'defaultTypes';
|
|
400
|
+
defaultCollectionNamed = true;
|
|
401
|
+
} else {
|
|
402
|
+
unnamedCollections += 1;
|
|
403
|
+
collectionConfig.name = `collection_${unnamedCollections}`;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
379
406
|
const typesCollectionProxy = new Proxy(
|
|
380
407
|
typesCollection,
|
|
381
408
|
typesCollectionProxyHandler
|