mnemonica 1.3.5 → 1.3.7
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 +6 -1
- package/README.md +1 -1
- package/SKILL.md +11 -1
- package/build/api/types/InstanceCreator.js +21 -3
- package/build/api/types/index.js +1 -1
- package/build/index.d.ts +4 -4
- package/build/index.js +1 -1
- package/build/types/index.d.ts +30 -16
- package/build/types/index.js +1 -1
- package/build/utils/deepParse.d.ts +2 -0
- package/build/utils/deepParse.js +21 -0
- package/build/utils/index.js +3 -1
- package/build/utils/parent.d.ts +1 -1
- package/build/utils/parent.js +9 -4
- package/build/utils/parse.d.ts +3 -2
- package/build/utils/parse.js +19 -6
- package/build/utils/toJSON.js +21 -18
- package/docs/UTILS.md +46 -8
- package/docs/typed-lookup.md +101 -0
- package/package.json +135 -134
- package/src/api/types/InstanceCreator.ts +25 -2
- package/src/api/types/index.ts +3 -3
- package/src/index.ts +16 -4
- package/src/types/index.ts +75 -20
- package/src/utils/deepParse.ts +28 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/parent.ts +20 -5
- package/src/utils/parse.ts +42 -6
- package/src/utils/toJSON.ts +35 -36
package/build/utils/toJSON.js
CHANGED
|
@@ -2,31 +2,34 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.toJSON = void 0;
|
|
4
4
|
const extract_1 = require("./extract");
|
|
5
|
+
const descriptionOf = (error) => {
|
|
6
|
+
const { stack, message } = error;
|
|
7
|
+
const description = {
|
|
8
|
+
description: 'This value type is not supported by JSON.stringify',
|
|
9
|
+
stack,
|
|
10
|
+
message
|
|
11
|
+
};
|
|
12
|
+
return description;
|
|
13
|
+
};
|
|
5
14
|
const toJSON = (instance) => {
|
|
6
15
|
const extracted = (0, extract_1.extract)(instance);
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
if (
|
|
10
|
-
return
|
|
16
|
+
const collected = {};
|
|
17
|
+
Object.entries(extracted).forEach(([name, value]) => {
|
|
18
|
+
if (value === null || value === undefined) {
|
|
19
|
+
return;
|
|
11
20
|
}
|
|
12
|
-
let
|
|
21
|
+
let checked;
|
|
13
22
|
try {
|
|
14
|
-
|
|
23
|
+
const probe = JSON.stringify(value);
|
|
24
|
+
checked = probe === undefined ? descriptionOf(value) : value;
|
|
15
25
|
}
|
|
16
26
|
catch (error) {
|
|
17
|
-
|
|
18
|
-
const { stack, message } = error;
|
|
19
|
-
value = JSON.stringify({
|
|
20
|
-
description,
|
|
21
|
-
stack,
|
|
22
|
-
message
|
|
23
|
-
});
|
|
27
|
+
checked = descriptionOf(error);
|
|
24
28
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
.replace(/,$/, '}');
|
|
29
|
+
collected[name] = checked;
|
|
30
|
+
});
|
|
31
|
+
const result = JSON.stringify(collected);
|
|
29
32
|
return result;
|
|
30
33
|
};
|
|
31
34
|
exports.toJSON = toJSON;
|
|
32
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
35
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidG9KU09OLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3V0aWxzL3RvSlNPTi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxZQUFZLENBQUM7OztBQUViLHVDQUFvQztBQUVwQyxNQUFNLGFBQWEsR0FBRyxDQUFFLEtBQWMsRUFBRyxFQUFFO0lBQzFDLE1BQU0sRUFDTCxLQUFLLEVBQ0wsT0FBTyxFQUNQLEdBQUcsS0FBYyxDQUFDO0lBQ25CLE1BQU0sV0FBVyxHQUFHO1FBQ25CLFdBQVcsRUFBRyxvREFBb0Q7UUFDbEUsS0FBSztRQUNMLE9BQU87S0FDUCxDQUFDO0lBQ0YsT0FBTyxXQUFXLENBQUM7QUFDcEIsQ0FBQyxDQUFDO0FB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ZXQgY2hlY2tlZDogdW5rbm93bjtcblx0XHR0cnkge1xuXHRcdFx0Y29uc3QgcHJvYmUgPSBKU09OLnN0cmluZ2lmeSggdmFsdWUgKTtcblx0XHRcdC8vIHN0cmluZ2lmeSByZXR1cm5zIHVuZGVmaW5lZCAobm8gdGhyb3cpIGZvciB2YWx1ZXMgSlNPTiBoYXMgbm9cblx0XHRcdC8vIHJlcHJlc2VudGF0aW9uIG9mIOKAlCBmdW5jdGlvbnMsIHN5bWJvbHM6IHVuc3RyaW5naWZpYWJsZSB0b29cblx0XHRcdGNoZWNrZWQgPSBwcm9iZSA9PT0gdW5kZWZpbmVkID8gZGVzY3JpcHRpb25PZiggdmFsdWUgKSA6IHZhbHVlO1xuXHRcdH0gY2F0Y2ggKCBlcnJvcjogdW5rbm93biApIHtcblx0XHRcdGNoZWNrZWQgPSBkZXNjcmlwdGlvbk9mKCBlcnJvciApO1xuXHRcdH1cblx0XHRjb2xsZWN0ZWRbIG5hbWUgXSA9IGNoZWNrZWQ7XG5cdH0gKTtcblxuXHRjb25zdCByZXN1bHQgPSBKU09OLnN0cmluZ2lmeSggY29sbGVjdGVkICk7XG5cdHJldHVybiByZXN1bHQ7XG5cbn07XG4iXX0=
|
package/docs/UTILS.md
CHANGED
|
@@ -105,16 +105,17 @@ export interface SiblingAccessor {
|
|
|
105
105
|
| `utils.pick(instance, 'a', 'b')` | `{ a: T['a'], b: T['b'] } & {}` | Literal keys → typed subset. Dynamic `string[]` → `Record<string, unknown>`. |
|
|
106
106
|
| `utils.clone(instance)` | `T` | Same instance type. |
|
|
107
107
|
| `utils.fork(instance)` | `(this: object, ...args: unknown[]) => T` | Returns a fork constructor. |
|
|
108
|
-
| `utils.parent(instance, path?)` | `object \| undefined` | Structural only; nominal path typing needs `TypeRegistry`. |
|
|
108
|
+
| `utils.parent(instance, path?)` | `object \| null` (no path); `object \| undefined` (path: searched, not found) | Structural only; nominal path typing needs `TypeRegistry`. |
|
|
109
109
|
| `utils.sibling(instance)` | `SiblingAccessor` | Look up sibling constructors by string name or property access. |
|
|
110
110
|
| `utils.merge(A, B, ...args)` | `InstanceResult<Merge<B, A>>` | `A` wins; `B` fills non-overlapping keys. |
|
|
111
111
|
| `utils.parse(instance)` | `Parsed<T>` | One-level prototype-chain snapshot. |
|
|
112
|
+
| `utils.deepParse(instance)` | `Parsed<T>[]` | Ordered parse() levels from the instance (index 0) to its root. |
|
|
112
113
|
| `utils.toJSON(instance)` | `string` | Generic so the instance type is captured at the call site. |
|
|
113
114
|
| `utils.collectConstructors(instance, asSequence?)` | `string[]` when `asSequence: true`, otherwise a `{ [name]: true }` lookup object | Prototype-chain constructor names, up to `Mnemonica`. |
|
|
114
115
|
| `new utils.exception(instance, error, ...args)` | `Error` | **Must be called with `new`.** Error instance of the instance's type; data via `getProps()`. |
|
|
115
116
|
|
|
116
117
|
The complete `utils` collection is: `extract`, `pick`, `parent`, `sibling`,
|
|
117
|
-
`exception`, `fork`, `clone`, `toJSON`, `parse`, `merge`,
|
|
118
|
+
`exception`, `fork`, `clone`, `toJSON`, `parse`, `deepParse`, `merge`,
|
|
118
119
|
`collectConstructors`.
|
|
119
120
|
|
|
120
121
|
---
|
|
@@ -156,15 +157,24 @@ utils.extract(merged); // OK
|
|
|
156
157
|
|
|
157
158
|
## `utils.parse(instance)` in detail
|
|
158
159
|
|
|
159
|
-
`parse` returns a one-level snapshot of the instance's prototype chain
|
|
160
|
+
`parse` returns a one-level snapshot of the instance's prototype chain.
|
|
161
|
+
`parse(null)` is the one non-object input it accepts: the empty shape
|
|
162
|
+
(`EmptyParsed` — empty `props`/`joint`, `self: null`, `parent: undefined`),
|
|
163
|
+
"nothing was given". `undefined` and every other non-object throw
|
|
164
|
+
`WRONG_MODIFICATION_PATTERN`, same as `utils.parent(null)`.
|
|
160
165
|
|
|
161
166
|
- `name` — constructor name of the immediate prototype.
|
|
162
167
|
- `props` — `Extracted<T>` from the instance itself.
|
|
163
168
|
- `self` — the original instance reference (`T`).
|
|
164
169
|
- `proto` — `Object.getPrototypeOf(instance)`.
|
|
165
170
|
- `joint` — enumerable properties copied from `proto`.
|
|
166
|
-
- `parent` —
|
|
167
|
-
|
|
171
|
+
- `parent` — the parent INSTANCE: the same object `utils.parent(instance)`
|
|
172
|
+
returns (the `__parent__` of the construction props), not a prototype
|
|
173
|
+
layer. `null` for a root instance — a root's `__parent__` points at
|
|
174
|
+
mnemonica's internal root sentinel, which `parse` reports as no parent,
|
|
175
|
+
and no parent is `null` (the end of the chain).
|
|
176
|
+
Not recursively parsed — walking the whole lineage level by level is
|
|
177
|
+
`utils.deepParse`'s job (below).
|
|
168
178
|
|
|
169
179
|
```typescript
|
|
170
180
|
const parsed = utils.parse(user);
|
|
@@ -176,6 +186,28 @@ const parent: object | undefined = parsed.parent;
|
|
|
176
186
|
|
|
177
187
|
---
|
|
178
188
|
|
|
189
|
+
## `utils.deepParse(instance)` in detail
|
|
190
|
+
|
|
191
|
+
The lineage walker: from the instance to its root through the REAL parent
|
|
192
|
+
instances — `parse().parent` hops directly from instance to instance, so
|
|
193
|
+
the per-type prototype layers never appear. Returns the ordered list of
|
|
194
|
+
parsed levels: index 0 is the instance itself, the last level is its root
|
|
195
|
+
(`parent: null`).
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
const levels = utils.deepParse(embed);
|
|
199
|
+
// [Parsed<Embed>, Parsed<Chunk>, Parsed<Doc>] — leaf first, root last
|
|
200
|
+
levels[0].self === embed; // true
|
|
201
|
+
levels[1].self === utils.parent(embed); // true — the real parent instance
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
A `strictChain: false` lineage may repeat a type name at several levels —
|
|
205
|
+
each occurrence is its own level. `utils.fork()` produces a sibling: the
|
|
206
|
+
forked instance's chain has the same shape, and its parent level IS the
|
|
207
|
+
same instance the forked-from chain shares.
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
179
211
|
## `utils.exception(instance, error, ...args)` in detail
|
|
180
212
|
|
|
181
213
|
`exception` wraps a caught `Error` in an error instance of the instance's own
|
|
@@ -267,7 +299,13 @@ mnemonica throws `WRONG_MODIFICATION_PATTERN`
|
|
|
267
299
|
type without a user-augmented `TypeRegistry`. This is the same limitation as
|
|
268
300
|
`lookup()`: TypeScript cannot retroactively learn the type graph created
|
|
269
301
|
by `define()` calls. See [`./typed-lookup.md`](./typed-lookup.md).
|
|
270
|
-
- `utils.parse()` returns `parent` as `object |
|
|
271
|
-
|
|
272
|
-
|
|
302
|
+
- `utils.parse()` returns `parent` as `object | null` — the parent instance,
|
|
303
|
+
`null` for a root (see `utils.parent` above); deep recursive parsing is
|
|
304
|
+
`utils.deepParse`.
|
|
305
|
+
- `utils.toJSON()` always returns `string` — always VALID JSON: the fields
|
|
306
|
+
are collected into a plain object and serialized ONCE, so keys are escaped
|
|
307
|
+
by the serializer itself and an empty field set is `'{}'`.
|
|
308
|
+
`null`/`undefined` fields are omitted; a field value JSON cannot represent
|
|
309
|
+
(a circular structure, a function) is replaced by the description object
|
|
310
|
+
`{ description, stack, message }`. The generic parameter only preserves
|
|
273
311
|
the instance type at the call site for consistency with the other utilities.
|
package/docs/typed-lookup.md
CHANGED
|
@@ -94,6 +94,16 @@ const user = new User({ name: 'Ada' });
|
|
|
94
94
|
const admin = new user.Admin({ role: 'root' });
|
|
95
95
|
```
|
|
96
96
|
|
|
97
|
+
The types that make this work are a small deliberate public vocabulary —
|
|
98
|
+
the only names a declaration file (`.d.ts`) built on mnemonica ever needs:
|
|
99
|
+
|
|
100
|
+
- `RegistryEntry<Shape, Path>` — what the builder stores per defined path
|
|
101
|
+
and what typed lookups read back;
|
|
102
|
+
- `LookedUpConstructor<Registry, Path>` — the type of a `lookup()` result:
|
|
103
|
+
a constructor whose instances carry their registered subtypes, plus a
|
|
104
|
+
`lookup()` scoped to its own path;
|
|
105
|
+
- `LookedUpInstance<Registry, Path>` — the instance side of the same idea.
|
|
106
|
+
|
|
97
107
|
### Custom collections
|
|
98
108
|
|
|
99
109
|
`createTypesCollection()` works the same way and is fully isolated from the default collection.
|
|
@@ -170,6 +180,97 @@ Direct `new` on a constructor returned by `.define()` works for **root types** o
|
|
|
170
180
|
|
|
171
181
|
---
|
|
172
182
|
|
|
183
|
+
## Declaration emit on TypeScript 6 (the TS2883 symptom)
|
|
184
|
+
|
|
185
|
+
Builder mode compiles cleanly with `declaration: true` on TypeScript 6:
|
|
186
|
+
the exported builder value, `lookup()` results, and instances built
|
|
187
|
+
through them emit declarations that name only the public vocabulary
|
|
188
|
+
above. This is checked by the `test:ts:consumers` suite in the mnemonica
|
|
189
|
+
repository.
|
|
190
|
+
|
|
191
|
+
The compiler error below is **not** a missing export, and adding one is
|
|
192
|
+
never the fix:
|
|
193
|
+
|
|
194
|
+
```
|
|
195
|
+
error TS2883: The inferred type of 'Widget' cannot be named without a reference
|
|
196
|
+
to 'GlobalRegistry' from '…/mnemonica/build/types'. This is likely not portable.
|
|
197
|
+
A type annotation is necessary.
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### What the error really means
|
|
201
|
+
|
|
202
|
+
The project exports the result of the free `define()` (or of
|
|
203
|
+
`Type.define()`) and uses it as its API:
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
export const Widget = define('Widget', function (this: WidgetInstance) { … });
|
|
207
|
+
export const Gadget = Widget.define('Gadget', function (this: GadgetInstance) { … });
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
That path has no `TypeRegistry` augmentation, so everything types through
|
|
211
|
+
the catch-all half of the internal `GlobalRegistry`
|
|
212
|
+
(`TypeRegistry & Record<string, TypeConstructorBase>`). The internal
|
|
213
|
+
registry names stay private on purpose: under TypeScript 6 a project that
|
|
214
|
+
emits declarations cannot name them, and the build fails. That failure
|
|
215
|
+
is the guard rail. Exporting the internals would make the wrong pattern
|
|
216
|
+
compile everywhere, and every project after yours copies it.
|
|
217
|
+
|
|
218
|
+
### Why this is proven impossible, not just discouraged
|
|
219
|
+
|
|
220
|
+
A free `define()` + free `lookup()` pair cannot be typed without
|
|
221
|
+
declaration merging and without casts — in TypeScript as a language, not
|
|
222
|
+
only in mnemonica. A type never depends on which calls ran: a free
|
|
223
|
+
function's return type depends only on its arguments' types, so
|
|
224
|
+
`lookup('Widget')` sees the literal `'Widget'` and nothing else. The
|
|
225
|
+
only global string→type map TypeScript has is an interface you merge
|
|
226
|
+
into — `TypeRegistry`.
|
|
227
|
+
|
|
228
|
+
Checked against mnemonica (TypeScript 6, no augmentation; `Gadget`
|
|
229
|
+
defined in a separate statement from `Widget`, as projects write it):
|
|
230
|
+
|
|
231
|
+
| form | typed without casts? |
|
|
232
|
+
|---|---|
|
|
233
|
+
| `new Widget(…)`, `new Gadget(…)` — the define() return values | yes |
|
|
234
|
+
| `new widget.Gadget(…)` — construction from a parent instance | **no** — `Property 'Gadget' does not exist on type 'InstanceResult<…>'` |
|
|
235
|
+
| `Widget.lookup('Gadget')` → instance fields | **no** — the instance is an unresolved internal type |
|
|
236
|
+
| free `lookup('Widget')` → instance fields | **no** — the instance is `object` |
|
|
237
|
+
|
|
238
|
+
So the "working" naive path can only use the define() return values — it
|
|
239
|
+
throws away construction from a parent instance, which is what mnemonica
|
|
240
|
+
exists for — or it casts (`as unknown as`), which the mnemonica agent
|
|
241
|
+
guidelines forbid.
|
|
242
|
+
|
|
243
|
+
Checked in plain TypeScript, no mnemonica — three ways to connect a free
|
|
244
|
+
`define()` and `lookup()`:
|
|
245
|
+
|
|
246
|
+
| mechanism | lookup free? | typed without casts? |
|
|
247
|
+
|---|---|---|
|
|
248
|
+
| truly free functions over an implicit registry | yes | **no** — the lookup result is `unknown` |
|
|
249
|
+
| assertion function narrowing an explicit registry (`asserts reg is R & {…}`) | no — the registry is an argument | only after the define call, in the same control flow; `unknown` anywhere else |
|
|
250
|
+
| chained values, each define returning a grown registry type (the builder shape) | no — the value is threaded | yes |
|
|
251
|
+
|
|
252
|
+
Every cast-free option gives up "free". That is why the three typing
|
|
253
|
+
paths exist, and why tactica exists for the free lineage: it generates
|
|
254
|
+
the declaration merge the language requires, derived from the handlers
|
|
255
|
+
so it cannot drift.
|
|
256
|
+
|
|
257
|
+
### The fixes that ARE right
|
|
258
|
+
|
|
259
|
+
- **Tactica:** run `npx tactica`, include `.tactica/` in the tsconfig, and
|
|
260
|
+
export `lookup('Widget')` results — never the define() results.
|
|
261
|
+
- **Hand-written augmentation:** merge the entries into `TypeRegistry`
|
|
262
|
+
yourself (`test-ts/typed-utils-registry.ts` in the mnemonica repository
|
|
263
|
+
is the pattern) and export `lookup()` results. Works, but the
|
|
264
|
+
hand-written types can drift from the handlers.
|
|
265
|
+
- **Builder mode:** chain `.define()` on `mnemonica`; the exported
|
|
266
|
+
builder value IS the API (registry threading), and declaration emit
|
|
267
|
+
works on TypeScript 6.
|
|
268
|
+
|
|
269
|
+
If none of these fits, ask before changing anything. Never fix this
|
|
270
|
+
symptom with casts, re-exports, or type annotations on the exports.
|
|
271
|
+
|
|
272
|
+
---
|
|
273
|
+
|
|
173
274
|
## Multi-file usage: registry threading
|
|
174
275
|
|
|
175
276
|
**Yes — an exported builder value works across files.** Two reasons:
|
package/package.json
CHANGED
|
@@ -1,136 +1,137 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
2
|
+
"name": "mnemonica",
|
|
3
|
+
"version": "1.3.7",
|
|
4
|
+
"description": "AI-agent ready instance inheritance system for JavaScript/TypeScript via prototype chains",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "./build/index.js",
|
|
7
|
+
"types": "./build/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./build/index.d.ts",
|
|
11
|
+
"default": "./build/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./module": {
|
|
14
|
+
"types": "./build/index.d.ts",
|
|
15
|
+
"import": "./module/index.js",
|
|
16
|
+
"browser": "./module/index.js",
|
|
17
|
+
"require": "./build/index.js",
|
|
18
|
+
"default": "./build/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"README.md",
|
|
24
|
+
"SKILL.md",
|
|
25
|
+
"FOR_HUMANS.md",
|
|
26
|
+
"LICENSE",
|
|
27
|
+
"docs",
|
|
28
|
+
"src",
|
|
29
|
+
"module",
|
|
30
|
+
"build"
|
|
31
|
+
],
|
|
32
|
+
"directories": {
|
|
33
|
+
"build": "build/",
|
|
34
|
+
"module": "module/"
|
|
35
|
+
},
|
|
36
|
+
"contributors": [
|
|
37
|
+
"laplandin <laplandin.denis@gmail.com>",
|
|
38
|
+
"petrgrishin <iam@petrgrishin.com>",
|
|
39
|
+
"Mobydack <devzetsubou@gmail.com>"
|
|
40
|
+
],
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "rm -rf ./build && npx tsc --pretty --project tsconfig.json",
|
|
43
|
+
"build:all": "npm run build && npm run decorate:ts",
|
|
44
|
+
"verify": "npm run build && npm run lint:check",
|
|
45
|
+
"example": "npm run build && node ./test/example.js",
|
|
46
|
+
"example:async": "npm run build && node ./examples/AsyncNewTest.js",
|
|
47
|
+
"example:rename": "npm run build && node ./examples/ClassReName.js",
|
|
48
|
+
"example:v8bug": "npm run build && node ./examples/v8bug.js",
|
|
49
|
+
"example:ts": "npx tsc --ignoreConfig --noCheck --experimentalDecorators --target ES2021 --module CommonJS ./test-ts/test-decorate.ts ./test-ts/test-decorate-typings.ts ./test-ts/test-example.ts ./test-ts/test-no-types.ts ./test-ts/typed-collections.ts ./test-ts/typed-utils.ts ./test-ts/typed-utils-registry.ts && npx eslint --fix ./test-ts/*.js && node ./test-ts/test-no-types.js && node ./test-ts/test-decorate-typings.js",
|
|
50
|
+
"test:ts:strict": "npx tsc -p ./test-ts/tsconfig.strict.json",
|
|
51
|
+
"test:ts:consumers": "node scripts/check-consumers.js",
|
|
52
|
+
"deopt": "node --trace-deopt --trace-opt ./node_modules/mocha/bin/mocha.js ./test/index.js",
|
|
53
|
+
"decorate:ts": "npx tsc --ignoreConfig --target ES2021 --moduleResolution NodeNext --module NodeNext --sourceMap ./test/decorate.ts ./test/decorate-builder.ts",
|
|
54
|
+
"lint:md": "node scripts/check-published-links.js && find . -name '*.md' -not -path '*/node_modules/*' | xargs markdown-link-check -q --config .markdown-link-check.json",
|
|
55
|
+
"lint": "npx eslint",
|
|
56
|
+
"lint:check": "npx eslint ./src ./test",
|
|
57
|
+
"lint:src": "npx eslint --fix ./src",
|
|
58
|
+
"lint:test": "npx eslint --fix ./test",
|
|
59
|
+
"lint:fix": "npm run lint:src && npm run lint:test",
|
|
60
|
+
"pretest": "npm run decorate:ts",
|
|
61
|
+
"test": "mocha --allow-uncaught --enable-source-maps test/index.js",
|
|
62
|
+
"test:cov": "npm run build:all && nyc --reporter=text-summary --reporter=lcov mocha --allow-uncaught --enable-source-maps --ui bdd --reporter spec ./test/index.js",
|
|
63
|
+
"debug": "npx tsc --pretty --project tsconfig.json && npx mocha --allow-uncaught --enable-source-maps --reporter spec --inspect-brk test/index",
|
|
64
|
+
"test:jest": "npx jest",
|
|
65
|
+
"test:async_init": "mocha --allow-uncaught test_async/index.js",
|
|
66
|
+
"test:yields": "mocha --allow-uncaught test_yields/index.js",
|
|
67
|
+
"test:jest:cov": "npx jest --collectCoverage",
|
|
68
|
+
"test:jest:debug": "npx --node-options=--inspect-brk jest",
|
|
69
|
+
"test:mjs": "npm run build && node --experimental-modules ./test/test.mjs",
|
|
70
|
+
"watch": "npx tsc --pretty --project tsconfig.json -w",
|
|
71
|
+
"prepare": "husky"
|
|
72
|
+
},
|
|
73
|
+
"repository": {
|
|
74
|
+
"type": "git",
|
|
75
|
+
"url": "git+https://github.com/wentout/mnemonica.git"
|
|
76
|
+
},
|
|
77
|
+
"keywords": [
|
|
78
|
+
"ETL",
|
|
79
|
+
"DFD",
|
|
80
|
+
"inheritance",
|
|
81
|
+
"instance-inheritance",
|
|
82
|
+
"prototype-inheritance",
|
|
83
|
+
"prototype-chain",
|
|
84
|
+
"typescript",
|
|
85
|
+
"nodejs",
|
|
86
|
+
"decorator",
|
|
87
|
+
"data-flow",
|
|
88
|
+
"mnemonica",
|
|
89
|
+
"ai-agents",
|
|
90
|
+
"agent-ready"
|
|
91
|
+
],
|
|
92
|
+
"author": "went.out@gmail.com",
|
|
93
|
+
"license": "MIT",
|
|
94
|
+
"bugs": {
|
|
95
|
+
"url": "https://github.com/wentout/mnemonica/issues"
|
|
96
|
+
},
|
|
97
|
+
"homepage": "https://github.com/wentout/mnemonica#readme",
|
|
98
|
+
"devDependencies": {
|
|
99
|
+
"@eslint/eslintrc": "^3.3.7",
|
|
100
|
+
"@eslint/js": "^9.21.0",
|
|
101
|
+
"@mnemonica/tactica": "^0.4.4",
|
|
102
|
+
"@types/jest": "^29.5.14",
|
|
103
|
+
"@types/node": "^22.20.4",
|
|
104
|
+
"@typescript-eslint/eslint-plugin": "^8.70.1",
|
|
105
|
+
"@typescript-eslint/parser": "^8.70.1",
|
|
106
|
+
"chai": "^5.3.3",
|
|
107
|
+
"eslint": "^9.39.5",
|
|
108
|
+
"eslint-plugin-import": "^2.32.0",
|
|
109
|
+
"eslint-plugin-mocha": "^10.5.0",
|
|
110
|
+
"eslint-plugin-no-arrow-this": "^1.3.0",
|
|
111
|
+
"globals": "^15.15.0",
|
|
112
|
+
"husky": "^9.1.7",
|
|
113
|
+
"jest": "^29.7.0",
|
|
114
|
+
"json5": "^2.2.3",
|
|
115
|
+
"lint-staged": "^15.5.2",
|
|
116
|
+
"markdown-link-check": "^3.15.0",
|
|
117
|
+
"mocha": "^11.8.0",
|
|
118
|
+
"mocha-lcov-reporter": "^1.3.0",
|
|
119
|
+
"nyc": "^17.1.0",
|
|
120
|
+
"oxlint": "^0.16.12",
|
|
121
|
+
"ts-jest": "^29.4.14",
|
|
122
|
+
"typeomatica": "^0.3.63",
|
|
123
|
+
"typescript": "^6.0.3"
|
|
124
|
+
},
|
|
125
|
+
"engines": {
|
|
126
|
+
"node": ">=18 <26"
|
|
127
|
+
},
|
|
128
|
+
"overrides": {
|
|
129
|
+
"serialize-javascript": "^7.0.5",
|
|
130
|
+
"uuid": "^11.1.1",
|
|
131
|
+
"diff": "^9.0.0"
|
|
132
|
+
},
|
|
133
|
+
"lint-staged": {
|
|
134
|
+
"src/**/*.ts": "eslint --fix",
|
|
135
|
+
"test/**/*.js": "eslint --fix"
|
|
136
|
+
}
|
|
136
137
|
}
|
|
@@ -194,8 +194,20 @@ const postProcessing = function ( this: InstanceCreatorContext, continuationOf?:
|
|
|
194
194
|
}
|
|
195
195
|
|
|
196
196
|
const prev = parent(self.inheritedInstance) as object | null;
|
|
197
|
-
if (!prev
|
|
198
|
-
|
|
197
|
+
if (!prev) {
|
|
198
|
+
// no-parent-is-null: prev is null for a ROOT inherited instance
|
|
199
|
+
// and for a broken chain. The raw __parent__ record tells them
|
|
200
|
+
// apart — a root's is the internal sentinel (has a constructor:
|
|
201
|
+
// the lineage is wrong, name the expected parent); a broken
|
|
202
|
+
// chain's is constructor-less or unreachable (reading it throws:
|
|
203
|
+
// "some instance")
|
|
204
|
+
const madeOnName = prevParentChainHeadName(self.inheritedInstance);
|
|
205
|
+
let msg: string;
|
|
206
|
+
if ( madeOnName === undefined ) {
|
|
207
|
+
msg = 'should inherit from some instance';
|
|
208
|
+
} else {
|
|
209
|
+
msg = `should inherit from ${self.type.parentType!.TypeName} but made on ${inheritedConstructor.name}`;
|
|
210
|
+
}
|
|
199
211
|
self.throwModificationError( new WRONG_MODIFICATION_PATTERN(
|
|
200
212
|
msg,
|
|
201
213
|
stack
|
|
@@ -219,6 +231,17 @@ const postProcessing = function ( this: InstanceCreatorContext, continuationOf?:
|
|
|
219
231
|
|
|
220
232
|
};
|
|
221
233
|
|
|
234
|
+
const prevParentChainHeadName = function ( inherited: object ): string | undefined {
|
|
235
|
+
try {
|
|
236
|
+
const inheritedProps = _getProps( inherited ) as Props;
|
|
237
|
+
const chainHead = inheritedProps.__parent__ as { constructor: { name: string } };
|
|
238
|
+
const chainHeadConstructorName = chainHead.constructor.name;
|
|
239
|
+
return chainHeadConstructorName;
|
|
240
|
+
} catch {
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
222
245
|
const addThen = function ( this: InstanceCreatorContext, then: ThenSpec ) {
|
|
223
246
|
|
|
224
247
|
|
package/src/api/types/index.ts
CHANGED
|
@@ -265,9 +265,9 @@ TypeDescriptor.prototype.lookup = function (
|
|
|
265
265
|
// collection, e.g. AdminType.lookup('UserType.AdminType'),
|
|
266
266
|
// which is what the TypeLookup<Registry> type contract promises
|
|
267
267
|
const collection = this.collection as CollectionDef;
|
|
268
|
-
// the
|
|
269
|
-
//
|
|
270
|
-
const rootResult = collection.lookup(TypeNestedPath)
|
|
268
|
+
// CollectionDef.lookup is the runtime contract (CollectionLookup), so a
|
|
269
|
+
// plain string path yields TypeClass | undefined with no cast
|
|
270
|
+
const rootResult = collection.lookup(TypeNestedPath);
|
|
271
271
|
return rootResult;
|
|
272
272
|
};
|
|
273
273
|
|