@skmtc/core 0.50.0 → 0.51.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.
@@ -37,14 +37,14 @@ export declare abstract class DefinitionBase<V extends GeneratedValue = Generate
37
37
  /** The generated value content */
38
38
  value: V;
39
39
  /**
40
- * The models this Definition's model reached while its projection was
41
- * being constructed that were pending then: the models further up the
42
- * stack it refers back to, directly or through the models it refers to.
43
- * A reference to this model is inside a cycle while one of them is still
44
- * pending. Set by the `ModelDriver`; `undefined` for a Definition it did
45
- * not construct.
40
+ * The model lowest on the pending stack that this Definition's model led
41
+ * back to while its projection was being constructed
42
+ * (`PendingDefinition.leadsTo`). A reference to this model is inside a
43
+ * cycle for as long as that cycle is open (`toOpenEnd`). Set by the
44
+ * `ModelDriver`; `undefined` for a model that led back to none, and for a
45
+ * Definition the Driver did not construct.
46
46
  */
47
- reaches: ReadonlySet<PendingDefinition> | undefined;
47
+ leadsTo: PendingDefinition | undefined;
48
48
  constructor({ context, identifier, value }: DefinitionBaseArgs<V>);
49
49
  /** Renders the definition's code. Implemented by the concrete subclass. */
50
50
  abstract toString(): string;
@@ -32,14 +32,14 @@ export class DefinitionBase extends SnippetBase {
32
32
  value: void 0
33
33
  });
34
34
  /**
35
- * The models this Definition's model reached while its projection was
36
- * being constructed that were pending then: the models further up the
37
- * stack it refers back to, directly or through the models it refers to.
38
- * A reference to this model is inside a cycle while one of them is still
39
- * pending. Set by the `ModelDriver`; `undefined` for a Definition it did
40
- * not construct.
35
+ * The model lowest on the pending stack that this Definition's model led
36
+ * back to while its projection was being constructed
37
+ * (`PendingDefinition.leadsTo`). A reference to this model is inside a
38
+ * cycle for as long as that cycle is open (`toOpenEnd`). Set by the
39
+ * `ModelDriver`; `undefined` for a model that led back to none, and for a
40
+ * Definition the Driver did not construct.
41
41
  */
42
- Object.defineProperty(this, "reaches", {
42
+ Object.defineProperty(this, "leadsTo", {
43
43
  enumerable: true,
44
44
  configurable: true,
45
45
  writable: true,
@@ -36,13 +36,29 @@ export declare class PendingDefinition {
36
36
  backReferences: number;
37
37
  /** True while the projection's constructor runs; false once it returned, however it returned. */
38
38
  pending: boolean;
39
+ /** The model's place on the pending stack, counted from the bottom. The Driver sets it when it pushes the model. */
40
+ depth: number;
39
41
  /**
40
- * The models this model's construction reached while they were under
41
- * construction: the targets of its references back, and what the models it
42
- * refers to reached. When the constructor returns, the Driver hands the set
43
- * to the Definition (`DefinitionBase.reaches`).
42
+ * The model lowest on the pending stack that this model's construction led
43
+ * back to, directly or through the models it refers to: itself when it led
44
+ * back to none. The models are constructed depth first, so this is the
45
+ * lowlink of Tarjan's algorithm, and a model that leads back to itself
46
+ * alone is the root of its cycle. When the constructor returns, the Driver
47
+ * hands it to the Definition (`DefinitionBase.leadsTo`).
44
48
  */
45
- reaches: Set<PendingDefinition>;
49
+ leadsTo: PendingDefinition;
46
50
  constructor({ identifier, exportPath, generatorKey, options, projection }: PendingDefinitionArgs);
51
+ /**
52
+ * Record that this model's construction led back to `other`, a model on
53
+ * the pending stack. The lowest one is kept.
54
+ */
55
+ leadBackTo(other: PendingDefinition): void;
56
+ /**
57
+ * The model under construction this model still leads back to: itself
58
+ * while its own constructor runs, and after that the one its cycle goes
59
+ * back to, for as long as that cycle is not complete. `undefined` once the
60
+ * root of its cycle has returned: the model is in no cycle that is open.
61
+ */
62
+ toOpenEnd(): PendingDefinition | undefined;
47
63
  }
48
64
  export {};
@@ -62,17 +62,26 @@ export class PendingDefinition {
62
62
  writable: true,
63
63
  value: true
64
64
  });
65
+ /** The model's place on the pending stack, counted from the bottom. The Driver sets it when it pushes the model. */
66
+ Object.defineProperty(this, "depth", {
67
+ enumerable: true,
68
+ configurable: true,
69
+ writable: true,
70
+ value: 0
71
+ });
65
72
  /**
66
- * The models this model's construction reached while they were under
67
- * construction: the targets of its references back, and what the models it
68
- * refers to reached. When the constructor returns, the Driver hands the set
69
- * to the Definition (`DefinitionBase.reaches`).
73
+ * The model lowest on the pending stack that this model's construction led
74
+ * back to, directly or through the models it refers to: itself when it led
75
+ * back to none. The models are constructed depth first, so this is the
76
+ * lowlink of Tarjan's algorithm, and a model that leads back to itself
77
+ * alone is the root of its cycle. When the constructor returns, the Driver
78
+ * hands it to the Definition (`DefinitionBase.leadsTo`).
70
79
  */
71
- Object.defineProperty(this, "reaches", {
80
+ Object.defineProperty(this, "leadsTo", {
72
81
  enumerable: true,
73
82
  configurable: true,
74
83
  writable: true,
75
- value: new Set()
84
+ value: this
76
85
  });
77
86
  this.identifier = identifier;
78
87
  this.exportPath = exportPath;
@@ -80,4 +89,25 @@ export class PendingDefinition {
80
89
  this.options = options;
81
90
  this.projection = projection;
82
91
  }
92
+ /**
93
+ * Record that this model's construction led back to `other`, a model on
94
+ * the pending stack. The lowest one is kept.
95
+ */
96
+ leadBackTo(other) {
97
+ if (other.depth < this.leadsTo.depth) {
98
+ this.leadsTo = other;
99
+ }
100
+ }
101
+ /**
102
+ * The model under construction this model still leads back to: itself
103
+ * while its own constructor runs, and after that the one its cycle goes
104
+ * back to, for as long as that cycle is not complete. `undefined` once the
105
+ * root of its cycle has returned: the model is in no cycle that is open.
106
+ */
107
+ toOpenEnd() {
108
+ if (this.pending) {
109
+ return this;
110
+ }
111
+ return this.leadsTo === this ? undefined : this.leadsTo.toOpenEnd();
112
+ }
83
113
  }
@@ -83,8 +83,10 @@ export type ModelRef<V extends GeneratedValue, EnrichmentType> = {
83
83
  settings: ContentSettings<EnrichmentType>;
84
84
  definition: GeneratedDefinition<V>;
85
85
  /**
86
- * The model reached a model that is still pending, so this reference is
87
- * inside a cycle. Every reference inside a cycle must be deferred where
86
+ * The model is in a cycle that is not complete yet: it leads back to a
87
+ * model that is still pending. The source is constructed inside that
88
+ * pending model, so this reference is inside the cycle, in whatever
89
+ * order the models are reached. Every reference inside a cycle must be deferred where
88
90
  * the target language reads the value when the module loads (zod's
89
91
  * `z.lazy`): any model of the cycle can be the first one loaded.
90
92
  */
@@ -167,7 +167,7 @@ export class ModelDriver {
167
167
  value,
168
168
  noExport: this.noExport
169
169
  });
170
- definition.reaches = marker.reaches;
170
+ definition.leadsTo = marker.leadsTo === marker ? undefined : marker.leadsTo;
171
171
  // Added once the constructor returned, so the Definition renders after
172
172
  // the models it refers to, and a constructor that throws adds no file.
173
173
  const taken = findDefinitionAt({ context: this.context, name: identifier.name, exportPath });
@@ -225,6 +225,7 @@ const assertSameOptions = ({ name, exportPath, cachedOptions, options }) => {
225
225
  };
226
226
  /** Run `construct` with `marker` on top of the context's pending stack, popped however `construct` ends. */
227
227
  const whilePending = ({ context, marker }, construct) => {
228
+ marker.depth = context.pendingStack.length;
228
229
  context.pendingStack.push(marker);
229
230
  try {
230
231
  return construct();
@@ -264,8 +265,6 @@ const stitchModelImport = ({ context, lang, identifier, exportPath, destinationP
264
265
  destinationPath: toImportFile({ context, lang, destination: importInto }).path
265
266
  });
266
267
  };
267
- /** The models `definition`'s construction reached that are still pending. */
268
- const toOpenReaches = (definition) => [...(definition.reaches ?? [])].filter(reached => reached.pending);
269
268
  /**
270
269
  * Whether anything referred back to the model of `settings` while its
271
270
  * projection was being constructed: the model is recursive, on its own or in
@@ -320,8 +319,8 @@ export const toModelRef = (args) => {
320
319
  projection
321
320
  });
322
321
  pending.backReferences++;
323
- // The model whose constructor is running reached a pending model.
324
- context.pendingStack.at(-1)?.reaches.add(pending);
322
+ // The model whose constructor is running led back to a pending model.
323
+ context.pendingStack.at(-1)?.leadBackTo(pending);
325
324
  stitchModelImport({
326
325
  context,
327
326
  lang: projection.lang,
@@ -332,17 +331,18 @@ export const toModelRef = (args) => {
332
331
  return { suspended: true, settings };
333
332
  }
334
333
  const driver = new ModelDriver(args, settings);
335
- // What the model reached that is still pending, the source reaches too:
336
- // through this reference. Constructed now or found in the cache, the
337
- // Definition carries what its construction reached.
338
- const open = toOpenReaches(driver.definition);
339
- const source = context.pendingStack.at(-1);
340
- open.forEach(reached => source?.reaches.add(reached));
334
+ // Constructed now or found in the cache, the Definition carries the model
335
+ // its construction led back to. While the cycle that model is in is open,
336
+ // this reference is inside it, and the source leads back there too.
337
+ const openEnd = driver.definition.leadsTo?.toOpenEnd();
338
+ if (openEnd !== undefined) {
339
+ context.pendingStack.at(-1)?.leadBackTo(openEnd);
340
+ }
341
341
  return {
342
342
  suspended: false,
343
343
  settings: driver.settings,
344
344
  definition: driver.definition,
345
- inCycle: open.length > 0
345
+ inCycle: openEnd !== undefined
346
346
  };
347
347
  };
348
348
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skmtc/core",
3
- "version": "0.50.0",
3
+ "version": "0.51.0",
4
4
  "description": "Skmtc is a declarative code generation framework",
5
5
  "keywords": [
6
6
  "openapi",
@@ -37,14 +37,14 @@ export declare abstract class DefinitionBase<V extends GeneratedValue = Generate
37
37
  /** The generated value content */
38
38
  value: V;
39
39
  /**
40
- * The models this Definition's model reached while its projection was
41
- * being constructed that were pending then: the models further up the
42
- * stack it refers back to, directly or through the models it refers to.
43
- * A reference to this model is inside a cycle while one of them is still
44
- * pending. Set by the `ModelDriver`; `undefined` for a Definition it did
45
- * not construct.
40
+ * The model lowest on the pending stack that this Definition's model led
41
+ * back to while its projection was being constructed
42
+ * (`PendingDefinition.leadsTo`). A reference to this model is inside a
43
+ * cycle for as long as that cycle is open (`toOpenEnd`). Set by the
44
+ * `ModelDriver`; `undefined` for a model that led back to none, and for a
45
+ * Definition the Driver did not construct.
46
46
  */
47
- reaches: ReadonlySet<PendingDefinition> | undefined;
47
+ leadsTo: PendingDefinition | undefined;
48
48
  constructor({ context, identifier, value }: DefinitionBaseArgs<V>);
49
49
  /** Renders the definition's code. Implemented by the concrete subclass. */
50
50
  abstract toString(): string;
@@ -35,14 +35,14 @@ class DefinitionBase extends SnippetBase_js_1.SnippetBase {
35
35
  value: void 0
36
36
  });
37
37
  /**
38
- * The models this Definition's model reached while its projection was
39
- * being constructed that were pending then: the models further up the
40
- * stack it refers back to, directly or through the models it refers to.
41
- * A reference to this model is inside a cycle while one of them is still
42
- * pending. Set by the `ModelDriver`; `undefined` for a Definition it did
43
- * not construct.
38
+ * The model lowest on the pending stack that this Definition's model led
39
+ * back to while its projection was being constructed
40
+ * (`PendingDefinition.leadsTo`). A reference to this model is inside a
41
+ * cycle for as long as that cycle is open (`toOpenEnd`). Set by the
42
+ * `ModelDriver`; `undefined` for a model that led back to none, and for a
43
+ * Definition the Driver did not construct.
44
44
  */
45
- Object.defineProperty(this, "reaches", {
45
+ Object.defineProperty(this, "leadsTo", {
46
46
  enumerable: true,
47
47
  configurable: true,
48
48
  writable: true,
@@ -36,13 +36,29 @@ export declare class PendingDefinition {
36
36
  backReferences: number;
37
37
  /** True while the projection's constructor runs; false once it returned, however it returned. */
38
38
  pending: boolean;
39
+ /** The model's place on the pending stack, counted from the bottom. The Driver sets it when it pushes the model. */
40
+ depth: number;
39
41
  /**
40
- * The models this model's construction reached while they were under
41
- * construction: the targets of its references back, and what the models it
42
- * refers to reached. When the constructor returns, the Driver hands the set
43
- * to the Definition (`DefinitionBase.reaches`).
42
+ * The model lowest on the pending stack that this model's construction led
43
+ * back to, directly or through the models it refers to: itself when it led
44
+ * back to none. The models are constructed depth first, so this is the
45
+ * lowlink of Tarjan's algorithm, and a model that leads back to itself
46
+ * alone is the root of its cycle. When the constructor returns, the Driver
47
+ * hands it to the Definition (`DefinitionBase.leadsTo`).
44
48
  */
45
- reaches: Set<PendingDefinition>;
49
+ leadsTo: PendingDefinition;
46
50
  constructor({ identifier, exportPath, generatorKey, options, projection }: PendingDefinitionArgs);
51
+ /**
52
+ * Record that this model's construction led back to `other`, a model on
53
+ * the pending stack. The lowest one is kept.
54
+ */
55
+ leadBackTo(other: PendingDefinition): void;
56
+ /**
57
+ * The model under construction this model still leads back to: itself
58
+ * while its own constructor runs, and after that the one its cycle goes
59
+ * back to, for as long as that cycle is not complete. `undefined` once the
60
+ * root of its cycle has returned: the model is in no cycle that is open.
61
+ */
62
+ toOpenEnd(): PendingDefinition | undefined;
47
63
  }
48
64
  export {};
@@ -65,17 +65,26 @@ class PendingDefinition {
65
65
  writable: true,
66
66
  value: true
67
67
  });
68
+ /** The model's place on the pending stack, counted from the bottom. The Driver sets it when it pushes the model. */
69
+ Object.defineProperty(this, "depth", {
70
+ enumerable: true,
71
+ configurable: true,
72
+ writable: true,
73
+ value: 0
74
+ });
68
75
  /**
69
- * The models this model's construction reached while they were under
70
- * construction: the targets of its references back, and what the models it
71
- * refers to reached. When the constructor returns, the Driver hands the set
72
- * to the Definition (`DefinitionBase.reaches`).
76
+ * The model lowest on the pending stack that this model's construction led
77
+ * back to, directly or through the models it refers to: itself when it led
78
+ * back to none. The models are constructed depth first, so this is the
79
+ * lowlink of Tarjan's algorithm, and a model that leads back to itself
80
+ * alone is the root of its cycle. When the constructor returns, the Driver
81
+ * hands it to the Definition (`DefinitionBase.leadsTo`).
73
82
  */
74
- Object.defineProperty(this, "reaches", {
83
+ Object.defineProperty(this, "leadsTo", {
75
84
  enumerable: true,
76
85
  configurable: true,
77
86
  writable: true,
78
- value: new Set()
87
+ value: this
79
88
  });
80
89
  this.identifier = identifier;
81
90
  this.exportPath = exportPath;
@@ -83,5 +92,26 @@ class PendingDefinition {
83
92
  this.options = options;
84
93
  this.projection = projection;
85
94
  }
95
+ /**
96
+ * Record that this model's construction led back to `other`, a model on
97
+ * the pending stack. The lowest one is kept.
98
+ */
99
+ leadBackTo(other) {
100
+ if (other.depth < this.leadsTo.depth) {
101
+ this.leadsTo = other;
102
+ }
103
+ }
104
+ /**
105
+ * The model under construction this model still leads back to: itself
106
+ * while its own constructor runs, and after that the one its cycle goes
107
+ * back to, for as long as that cycle is not complete. `undefined` once the
108
+ * root of its cycle has returned: the model is in no cycle that is open.
109
+ */
110
+ toOpenEnd() {
111
+ if (this.pending) {
112
+ return this;
113
+ }
114
+ return this.leadsTo === this ? undefined : this.leadsTo.toOpenEnd();
115
+ }
86
116
  }
87
117
  exports.PendingDefinition = PendingDefinition;
@@ -83,8 +83,10 @@ export type ModelRef<V extends GeneratedValue, EnrichmentType> = {
83
83
  settings: ContentSettings<EnrichmentType>;
84
84
  definition: GeneratedDefinition<V>;
85
85
  /**
86
- * The model reached a model that is still pending, so this reference is
87
- * inside a cycle. Every reference inside a cycle must be deferred where
86
+ * The model is in a cycle that is not complete yet: it leads back to a
87
+ * model that is still pending. The source is constructed inside that
88
+ * pending model, so this reference is inside the cycle, in whatever
89
+ * order the models are reached. Every reference inside a cycle must be deferred where
88
90
  * the target language reads the value when the module loads (zod's
89
91
  * `z.lazy`): any model of the cycle can be the first one loaded.
90
92
  */
@@ -173,7 +173,7 @@ class ModelDriver {
173
173
  value,
174
174
  noExport: this.noExport
175
175
  });
176
- definition.reaches = marker.reaches;
176
+ definition.leadsTo = marker.leadsTo === marker ? undefined : marker.leadsTo;
177
177
  // Added once the constructor returned, so the Definition renders after
178
178
  // the models it refers to, and a constructor that throws adds no file.
179
179
  const taken = (0, driverFiles_js_1.findDefinitionAt)({ context: this.context, name: identifier.name, exportPath });
@@ -232,6 +232,7 @@ const assertSameOptions = ({ name, exportPath, cachedOptions, options }) => {
232
232
  };
233
233
  /** Run `construct` with `marker` on top of the context's pending stack, popped however `construct` ends. */
234
234
  const whilePending = ({ context, marker }, construct) => {
235
+ marker.depth = context.pendingStack.length;
235
236
  context.pendingStack.push(marker);
236
237
  try {
237
238
  return construct();
@@ -271,8 +272,6 @@ const stitchModelImport = ({ context, lang, identifier, exportPath, destinationP
271
272
  destinationPath: (0, driverFiles_js_1.toImportFile)({ context, lang, destination: importInto }).path
272
273
  });
273
274
  };
274
- /** The models `definition`'s construction reached that are still pending. */
275
- const toOpenReaches = (definition) => [...(definition.reaches ?? [])].filter(reached => reached.pending);
276
275
  /**
277
276
  * Whether anything referred back to the model of `settings` while its
278
277
  * projection was being constructed: the model is recursive, on its own or in
@@ -328,8 +327,8 @@ const toModelRef = (args) => {
328
327
  projection
329
328
  });
330
329
  pending.backReferences++;
331
- // The model whose constructor is running reached a pending model.
332
- context.pendingStack.at(-1)?.reaches.add(pending);
330
+ // The model whose constructor is running led back to a pending model.
331
+ context.pendingStack.at(-1)?.leadBackTo(pending);
333
332
  stitchModelImport({
334
333
  context,
335
334
  lang: projection.lang,
@@ -340,17 +339,18 @@ const toModelRef = (args) => {
340
339
  return { suspended: true, settings };
341
340
  }
342
341
  const driver = new ModelDriver(args, settings);
343
- // What the model reached that is still pending, the source reaches too:
344
- // through this reference. Constructed now or found in the cache, the
345
- // Definition carries what its construction reached.
346
- const open = toOpenReaches(driver.definition);
347
- const source = context.pendingStack.at(-1);
348
- open.forEach(reached => source?.reaches.add(reached));
342
+ // Constructed now or found in the cache, the Definition carries the model
343
+ // its construction led back to. While the cycle that model is in is open,
344
+ // this reference is inside it, and the source leads back there too.
345
+ const openEnd = driver.definition.leadsTo?.toOpenEnd();
346
+ if (openEnd !== undefined) {
347
+ context.pendingStack.at(-1)?.leadBackTo(openEnd);
348
+ }
349
349
  return {
350
350
  suspended: false,
351
351
  settings: driver.settings,
352
352
  definition: driver.definition,
353
- inCycle: open.length > 0
353
+ inCycle: openEnd !== undefined
354
354
  };
355
355
  };
356
356
  exports.toModelRef = toModelRef;