mnemonica 1.3.8 → 1.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.
Files changed (45) hide show
  1. package/FOR_HUMANS.md +101 -31
  2. package/README.md +2 -2
  3. package/build/api/errors/index.d.ts +3 -7
  4. package/build/api/errors/index.js +3 -5
  5. package/build/api/errors/throwModificationError.js +2 -4
  6. package/build/api/hooks/HookInvocation.d.ts +2 -2
  7. package/build/api/hooks/HookInvocation.js +1 -1
  8. package/build/api/hooks/flowCheckers.d.ts +3 -3
  9. package/build/api/hooks/flowCheckers.js +1 -1
  10. package/build/api/index.d.ts +3 -3
  11. package/build/api/types/index.d.ts +3 -3
  12. package/build/api/types/index.js +18 -26
  13. package/build/api/utils/index.d.ts +2 -2
  14. package/build/api/utils/index.js +31 -18
  15. package/build/descriptors/errors/index.js +1 -1
  16. package/build/descriptors/types/index.js +8 -8
  17. package/build/index.d.ts +10 -13
  18. package/build/index.js +13 -16
  19. package/build/types/index.d.ts +89 -25
  20. package/build/types/index.js +1 -1
  21. package/build/utils/collectConstructors.js +1 -1
  22. package/build/utils/fork.d.ts +2 -1
  23. package/build/utils/fork.js +1 -1
  24. package/build/utils/index.js +10 -8
  25. package/build/utils/parse.js +5 -2
  26. package/docs/UTILS.md +3 -2
  27. package/docs/async-constructors.md +5 -3
  28. package/docs/empathy-in-ai.md +2 -2
  29. package/docs/prototype-chain.md +1 -1
  30. package/docs/typed-lookup.md +3 -1
  31. package/package.json +12 -12
  32. package/src/api/errors/index.ts +12 -15
  33. package/src/api/errors/throwModificationError.ts +3 -4
  34. package/src/api/hooks/HookInvocation.ts +6 -5
  35. package/src/api/hooks/flowCheckers.ts +6 -3
  36. package/src/api/types/index.ts +45 -44
  37. package/src/api/utils/index.ts +60 -24
  38. package/src/descriptors/errors/index.ts +4 -5
  39. package/src/descriptors/types/index.ts +20 -16
  40. package/src/index.ts +55 -47
  41. package/src/types/index.ts +252 -39
  42. package/src/utils/collectConstructors.ts +0 -9
  43. package/src/utils/fork.ts +2 -1
  44. package/src/utils/index.ts +15 -14
  45. package/src/utils/parse.ts +9 -1
@@ -1,6 +1,9 @@
1
1
  'use strict';
2
2
 
3
- import type { StackBoundary } from '../../types';
3
+ import type {
4
+ StackBoundary,
5
+ MnemonicaErrorConstructor
6
+ } from '../../types';
4
7
 
5
8
  import { constants } from '../../constants';
6
9
 
@@ -76,7 +79,7 @@ export const getStack = function (
76
79
 
77
80
  export class BASE_MNEMONICA_ERROR extends Error {
78
81
 
79
- constructor ( message = BASE_ERROR_MESSAGE, additionalStack: string[] ) {
82
+ constructor ( message = BASE_ERROR_MESSAGE, additionalStack?: string[] ) {
80
83
 
81
84
  super( message );
82
85
  const BaseStack: string = this.stack as string;
@@ -119,9 +122,9 @@ Object.defineProperty(
119
122
  );
120
123
 
121
124
 
122
- export const constructError = ( name: string, message: string ) => {
125
+ export const constructError = ( name: string, message: string ): MnemonicaErrorConstructor => {
123
126
  const NamedErrorConstructor = class extends BASE_MNEMONICA_ERROR {
124
- constructor ( addition: string, stack: string[] ) {
127
+ constructor ( addition?: string, stack?: string[] ) {
125
128
  const saying = addition ? `${message} : ${addition}` : `${message}`;
126
129
  super(
127
130
  saying,
@@ -130,23 +133,17 @@ export const constructError = ( name: string, message: string ) => {
130
133
  }
131
134
  };
132
135
 
133
- const reNamer = {} as {
134
- [ key: string ]: {
135
- prototype: {
136
- constructor: CallableFunction
137
- }
138
- },
139
- };
140
- reNamer[ name ] = NamedErrorConstructor;
136
+ // the class's prototype.constructor IS the class, so the name getter is
137
+ // defined on the class itself (same effect as BASE_MNEMONICA_ERROR's above)
141
138
  Object.defineProperty(
142
- reNamer[ name ].prototype.constructor,
139
+ NamedErrorConstructor,
143
140
  'name',
144
141
  {
145
142
  get () {
146
143
  const result = new String( name );
147
144
  return result;
148
145
  }
149
- }
146
+ }
150
147
  );
151
- return reNamer[ name ];
148
+ return NamedErrorConstructor;
152
149
  };
@@ -11,11 +11,10 @@ const {
11
11
  } = constants;
12
12
 
13
13
 
14
- import { ErrorsTypes } from '../../descriptors/errors';
15
- const { BASE_MNEMONICA_ERROR } = ErrorsTypes;
16
-
14
+ // the base class itself (ErrorsTypes holds this same class): instanceof
15
+ // against it narrows to the class's own instance type
17
16
  import {
18
- cleanupStack, getStack
17
+ BASE_MNEMONICA_ERROR, cleanupStack, getStack
19
18
  } from './';
20
19
 
21
20
  import TypesUtils from '../utils';
@@ -1,10 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  import type {
4
- hooksOpts, TypeDef
4
+ ThrowModificationError,
5
+ deliveredHookOpts, TypeDef
5
6
  } from '../../types';
6
7
 
7
- // hooksOpts is used in build() return type
8
+ // deliveredHookOpts is used in build() return type
8
9
 
9
10
  /**
10
11
  * Builder for hook invocation arguments.
@@ -21,7 +22,7 @@ export class HookInvocation {
21
22
  private readonly existentInstance: object;
22
23
  private readonly args: unknown[];
23
24
  private inheritedInstance?: object;
24
- private _throwModificationError?: (error: Error) => void;
25
+ private _throwModificationError?: ThrowModificationError;
25
26
 
26
27
  constructor (
27
28
  type: TypeDef,
@@ -46,8 +47,8 @@ export class HookInvocation {
46
47
  return this;
47
48
  }
48
49
 
49
- build (): hooksOpts {
50
- const result: hooksOpts = {
50
+ build (): deliveredHookOpts {
51
+ const result: deliveredHookOpts = {
51
52
  type : this.type,
52
53
  TypeName : this.TypeName,
53
54
  existentInstance : this.existentInstance,
@@ -1,6 +1,9 @@
1
1
  'use strict';
2
2
 
3
- import type { Hookable } from '../../types';
3
+ import type {
4
+ Hookable,
5
+ FlowChecker
6
+ } from '../../types';
4
7
 
5
8
  import { ErrorsTypes } from '../../descriptors/errors';
6
9
  const {
@@ -10,8 +13,8 @@ const {
10
13
 
11
14
  // the stored checker receives the hook invocation record — same contract
12
15
  // Hookable.registerFlowChecker declares for its callback
13
- export const flowCheckers = new WeakMap<Hookable, (opts: object) => unknown>();
14
- export const registerFlowChecker = function (this: Hookable, cb: () => unknown ) {
16
+ export const flowCheckers = new WeakMap<Hookable, FlowChecker>();
17
+ export const registerFlowChecker = function (this: Hookable, cb: FlowChecker ) {
15
18
 
16
19
  if ( typeof cb !== 'function' ) {
17
20
  throw new MISSING_CALLBACK_ARGUMENT;
@@ -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.
@@ -213,8 +216,8 @@ Object.assign(
213
216
 
214
217
  TypeDescriptor.prototype.define = function (
215
218
  this: TypeDescriptorInstance,
216
- TypeOrTypeName: string | CallableFunction,
217
- constructHandlerOrConfig?: CallableFunction | object,
219
+ TypeOrTypeName: string | DefineNewableOrCallable,
220
+ constructHandlerOrConfig?: DefineNewableOrCallable | object,
218
221
  config?: object
219
222
  ) {
220
223
  const result = define.call(
@@ -229,20 +232,20 @@ TypeDescriptor.prototype.define = function (
229
232
 
230
233
  TypeDescriptor.prototype.lazy = function (
231
234
  this: TypeDescriptorInstance,
232
- arg1: string | CallableFunction,
233
- arg2?: CallableFunction | object,
234
- arg3?: object
235
+ TypeNameOrGetter: string | LazyDef<object>,
236
+ getterOrConfig?: LazyDef<object> | object,
237
+ namedFormConfig?: object
235
238
  ) {
236
239
  let name: string | undefined;
237
240
  let getter: LazyTypeGetter;
238
241
  let config: object | undefined;
239
- if (typeof arg1 === 'string') {
240
- name = arg1;
241
- getter = arg2 as LazyTypeGetter;
242
- config = arg3;
242
+ if (typeof TypeNameOrGetter === 'string') {
243
+ name = TypeNameOrGetter;
244
+ getter = getterOrConfig as LazyTypeGetter;
245
+ config = namedFormConfig;
243
246
  } else {
244
- getter = arg1 as LazyTypeGetter;
245
- config = arg2 as object;
247
+ getter = TypeNameOrGetter as LazyTypeGetter;
248
+ config = getterOrConfig as object;
246
249
  }
247
250
  let result: TypeClass;
248
251
  if (name) {
@@ -297,7 +300,7 @@ odp(
297
300
  get (this: TypeDescriptorInstance) {
298
301
  const self = this;
299
302
  const result = function (options?: object) {
300
- const decorator = function (cstr: CallableFunction) {
303
+ const decorator = function (cstr: DefineNewable) {
301
304
  const { name } = cstr;
302
305
  // define() returns the registered constructor itself —
303
306
  // the full TypeClass is a richer honest type than any cast
@@ -500,36 +503,34 @@ const createFromLazyGetter = function (
500
503
  (typeof type.prototype === 'object')
501
504
  ) ? type.prototype : getDefaultPrototype();
502
505
 
506
+ // the getter's raw function cannot construct directly: it lacks the
507
+ // compiled modificator body — the type's name on prototype.constructor,
508
+ // the CreationHandler call, the Reflect.construct with new.target —
509
+ // which is why named lazy with an anonymous or differently named
510
+ // handler failed to construct, and async lazy handlers broke. Build
511
+ // through the SAME wrapper as the direct path, per construction: the
512
+ // getter may return a NEW function each time, so neither the shape
513
+ // check nor the wrapper can be cached. This is what makes lazy slower
514
+ // than a direct define
515
+ const modificatorBody = compileNewModificatorFunctionBody(
516
+ TypeName,
517
+ asClass
518
+ );
519
+
503
520
  const makeConstructHandler = () => {
504
521
  const constructHandler = getter();
505
522
 
506
- // and on EVERY construction: the getter may return a new function
507
- // each time, so the shape check cannot be cached from define time.
508
- // This is what makes lazy slower than a direct define
509
523
  classifyConstructHandler(
510
524
  TypeName,
511
525
  constructHandler as ConstructHandler
512
526
  );
513
527
 
514
- odp(
515
- constructHandler,
516
- SymbolConstructorName, {
517
- get () {
518
- return TypeName;
519
- }
520
- }
528
+ const wrappedBody = modificatorBody(
529
+ constructHandler as ConstructHandler,
530
+ CreationHandler,
531
+ SymbolConstructorName
521
532
  );
522
-
523
- const protoDesc = Object
524
- .getOwnPropertyDescriptor(
525
- constructHandler,
526
- 'prototype'
527
- ) as PropertyDescriptor | undefined;
528
- if (protoDesc && protoDesc.writable) {
529
- constructHandler.prototype = getDefaultPrototype();
530
- }
531
-
532
- const handlerResult = constructHandler as MnemonicaConstructorFactory;
533
+ const handlerResult = wrappedBody();
533
534
  return handlerResult;
534
535
  };
535
536
 
@@ -569,8 +570,8 @@ const createFromLazyGetter = function (
569
570
  export const define = function (
570
571
  this: unknown,
571
572
  subtypes: TypesMap,
572
- TypeOrTypeName: string | CallableFunction,
573
- constructHandlerOrConfig?: CallableFunction | object,
573
+ TypeOrTypeName: string | DefineNewableOrCallable,
574
+ constructHandlerOrConfig?: DefineNewableOrCallable | object,
574
575
  config?: object
575
576
  ): TypeClass {
576
577
 
@@ -638,21 +639,21 @@ export const define = function (
638
639
  export const lazy = function (
639
640
  this: unknown,
640
641
  subtypes: TypesMap,
641
- arg1: string | LazyTypeGetter | undefined,
642
- arg2?: LazyTypeGetter | object,
643
- arg3?: object
642
+ TypeNameOrGetter: string | LazyTypeGetter | undefined,
643
+ getterOrConfig?: LazyTypeGetter | object,
644
+ namedFormConfig?: object
644
645
  ): TypeClass {
645
646
 
646
647
  let name: string | undefined;
647
648
  let getter: LazyTypeGetter;
648
649
  let config: object | undefined;
649
- if (typeof arg1 === 'string') {
650
- name = arg1;
651
- getter = arg2 as LazyTypeGetter;
652
- config = arg3;
650
+ if (typeof TypeNameOrGetter === 'string') {
651
+ name = TypeNameOrGetter;
652
+ getter = getterOrConfig as LazyTypeGetter;
653
+ config = namedFormConfig;
653
654
  } else {
654
- getter = arg1 as LazyTypeGetter;
655
- config = arg2 as object;
655
+ getter = TypeNameOrGetter as LazyTypeGetter;
656
+ config = getterOrConfig as object;
656
657
  }
657
658
 
658
659
  if (typeof getter !== 'function') {
@@ -16,13 +16,19 @@ const { WRONG_TYPE_DEFINITION, } = ErrorsTypes;
16
16
  import {
17
17
  _getProps, Props
18
18
  } from '../types/Props';
19
- import type { MnemonicaConstructor } from '../../types';
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 (this: object & { constructor: NewableFunction }, constructionAnswer: unknown) {
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
- // TODO: .valueOf(), .toString() ???
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
- odp(
333
+ reflectPrimitive(
284
334
  thisArg,
285
- Symbol.toPrimitive,
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
- odp(
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
- Symbol.toPrimitive,
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
 
@@ -10,10 +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
- // BASE_MNEMONICA_ERROR is a class: its construct signature matches the
14
- // interface, only the (unused) call signature is missing — the map is
15
- // only ever `new`-ed, so the bridge is safe
16
- BASE_MNEMONICA_ERROR : BASE_MNEMONICA_ERROR as unknown as MnemonicaErrorConstructor
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
17
16
  };
18
17
 
19
18
  Object.entries( ErrorMessages ).forEach( entry => {
@@ -23,7 +22,7 @@ Object.entries( ErrorMessages ).forEach( entry => {
23
22
  ErrorConstructorName,
24
23
  message
25
24
  );
26
- ErrorsTypes[ ErrorConstructorName ] = ErrorCtor as MnemonicaErrorConstructor;
25
+ ErrorsTypes[ ErrorConstructorName ] = ErrorCtor;
27
26
  } );
28
27
 
29
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: CallableFunction,
155
- TypeOrTypeName: string | CallableFunction,
156
- constructHandlerOrConfig?: CallableFunction | object,
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: CallableFunction,
185
- arg1: string | CallableFunction,
186
- arg2?: CallableFunction | object,
187
- arg3?: object
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 arg1 === 'string') {
193
- name = arg1;
194
- getter = arg2 as LazyTypeGetter;
195
- config = arg3;
196
+ if (typeof TypeNameOrGetter === 'string') {
197
+ name = TypeNameOrGetter;
198
+ getter = getterOrConfig as LazyTypeGetter;
199
+ config = namedFormConfig;
196
200
  } else {
197
- getter = arg1 as LazyTypeGetter;
198
- config = arg2 as object;
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: CallableFunction) {
238
+ const decorator = function (cstr: DefineNewable) {
235
239
  const { name } = cstr;
236
240
  const defineResult = self.define(
237
241
  name,
@@ -317,7 +321,7 @@ odp(
317
321
  'registerFlowChecker',
318
322
  {
319
323
  get (this: TypesCollection) {
320
- const result = (flowCheckerCallback: () => unknown) => {
324
+ const result = (flowCheckerCallback: FlowChecker) => {
321
325
  const checkerResult = registerFlowChecker.call(
322
326
  typesCollections.get(this),
323
327
  flowCheckerCallback