@skmtc/core 0.49.1 → 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.
Files changed (35) hide show
  1. package/esm/context/CoreContext.js +80 -3
  2. package/esm/context/GenerateContext.d.ts +6 -1
  3. package/esm/context/GenerateContext.js +149 -81
  4. package/esm/context/LogLinesHandler.d.ts +1 -1
  5. package/esm/context/LogLinesHandler.js +3 -0
  6. package/esm/context/ParseContext.d.ts +3 -0
  7. package/esm/context/ParseContext.js +36 -14
  8. package/esm/dsl/Definition.d.ts +7 -7
  9. package/esm/dsl/Definition.js +7 -7
  10. package/esm/dsl/PendingDefinition.d.ts +21 -5
  11. package/esm/dsl/PendingDefinition.js +36 -6
  12. package/esm/dsl/model/ModelDriver.d.ts +4 -2
  13. package/esm/dsl/model/ModelDriver.js +12 -12
  14. package/esm/helpers/toCountPhrase.d.ts +11 -0
  15. package/esm/helpers/toCountPhrase.js +11 -0
  16. package/esm/types/LogLine.d.ts +40 -1
  17. package/esm/types/LogLine.js +25 -0
  18. package/package.json +1 -1
  19. package/script/context/CoreContext.js +80 -3
  20. package/script/context/GenerateContext.d.ts +6 -1
  21. package/script/context/GenerateContext.js +149 -81
  22. package/script/context/LogLinesHandler.d.ts +1 -1
  23. package/script/context/LogLinesHandler.js +3 -0
  24. package/script/context/ParseContext.d.ts +3 -0
  25. package/script/context/ParseContext.js +36 -14
  26. package/script/dsl/Definition.d.ts +7 -7
  27. package/script/dsl/Definition.js +7 -7
  28. package/script/dsl/PendingDefinition.d.ts +21 -5
  29. package/script/dsl/PendingDefinition.js +36 -6
  30. package/script/dsl/model/ModelDriver.d.ts +4 -2
  31. package/script/dsl/model/ModelDriver.js +12 -12
  32. package/script/helpers/toCountPhrase.d.ts +11 -0
  33. package/script/helpers/toCountPhrase.js +15 -0
  34. package/script/types/LogLine.d.ts +40 -1
  35. package/script/types/LogLine.js +27 -1
@@ -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;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * `count` and the noun for it, for a log line: `1 file`, `2 files`.
3
+ *
4
+ * @example
5
+ * ```typescript
6
+ * toCountPhrase(1, 'warning') // '1 warning'
7
+ * toCountPhrase(0, 'error') // '0 errors'
8
+ * toCountPhrase(2, 'debug', 'debug') // '2 debug'
9
+ * ```
10
+ */
11
+ export declare const toCountPhrase: (count: number, singular: string, plural?: string) => string;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toCountPhrase = void 0;
4
+ /**
5
+ * `count` and the noun for it, for a log line: `1 file`, `2 files`.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * toCountPhrase(1, 'warning') // '1 warning'
10
+ * toCountPhrase(0, 'error') // '0 errors'
11
+ * toCountPhrase(2, 'debug', 'debug') // '2 debug'
12
+ * ```
13
+ */
14
+ const toCountPhrase = (count, singular, plural = `${singular}s`) => `${count} ${count === 1 ? singular : plural}`;
15
+ exports.toCountPhrase = toCountPhrase;
@@ -13,15 +13,48 @@ import { type SerializedError } from './SerializedError.js';
13
13
  export type LogLineLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR';
14
14
  /** Valibot schema for {@link LogLineLevel}. */
15
15
  export declare const logLineLevel: v.PicklistSchema<["DEBUG", "INFO", "WARN", "ERROR"], undefined>;
16
+ /** The fields of the parse issue a {@link LogLine} reports. */
17
+ export type LogLineIssue = {
18
+ /**
19
+ * The issue's type. A string, not the closed list of types: a reader on
20
+ * an older core must still read a line with a type added since.
21
+ */
22
+ type: string;
23
+ level: 'error' | 'warning' | 'debug';
24
+ /** The issue's `location`, as `manifest.parseIssues` has it. */
25
+ location: string;
26
+ /** The object around the issue, as JSON. */
27
+ parent?: string;
28
+ };
29
+ /** Valibot schema for {@link LogLineIssue}. */
30
+ export declare const logLineIssue: v.ObjectSchema<{
31
+ readonly type: v.StringSchema<undefined>;
32
+ readonly level: v.PicklistSchema<["error", "warning", "debug"], undefined>;
33
+ readonly location: v.StringSchema<undefined>;
34
+ readonly parent: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
35
+ }, undefined>;
36
+ /**
37
+ * A log argument that gives its line an `issue` field. Parse logs one with
38
+ * each issue it records.
39
+ */
40
+ export declare class LogLineIssueArg {
41
+ issue: LogLineIssue;
42
+ constructor(issue: LogLineIssue);
43
+ }
16
44
  export type LogLine = {
17
45
  /** Position of the line in the run, from 0. */
18
46
  seq: number;
19
47
  level: LogLineLevel;
20
- /** The subject's stack trail. Absent outside a subject (parse, render). */
48
+ /**
49
+ * The subject's stack trail, or the generator's on the line a generator
50
+ * logs after it ran. Absent outside a generator (parse, render).
51
+ */
21
52
  trail?: string;
22
53
  message: string;
23
54
  /** The first `Error` the line was logged with. */
24
55
  error?: SerializedError;
56
+ /** The parse issue the line reports. */
57
+ issue?: LogLineIssue;
25
58
  /** When the line was logged, in milliseconds since the epoch. */
26
59
  time: number;
27
60
  };
@@ -32,5 +65,11 @@ export declare const logLine: v.ObjectSchema<{
32
65
  readonly trail: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
33
66
  readonly message: v.StringSchema<undefined>;
34
67
  readonly error: v.OptionalSchema<v.GenericSchema<SerializedError>, undefined>;
68
+ readonly issue: v.OptionalSchema<v.ObjectSchema<{
69
+ readonly type: v.StringSchema<undefined>;
70
+ readonly level: v.PicklistSchema<["error", "warning", "debug"], undefined>;
71
+ readonly location: v.StringSchema<undefined>;
72
+ readonly parent: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
73
+ }, undefined>, undefined>;
35
74
  readonly time: v.NumberSchema<undefined>;
36
75
  }, undefined>;
@@ -42,13 +42,38 @@ var __importStar = (this && this.__importStar) || (function () {
42
42
  };
43
43
  })();
44
44
  Object.defineProperty(exports, "__esModule", { value: true });
45
- exports.logLine = exports.logLineLevel = void 0;
45
+ exports.logLine = exports.LogLineIssueArg = exports.logLineIssue = exports.logLineLevel = void 0;
46
46
  const v = __importStar(require("valibot"));
47
47
  const SerializedError_js_1 = require("./SerializedError.js");
48
48
  /** Valibot schema for {@link LogLineLevel}. */
49
49
  exports.logLineLevel = v.picklist(['DEBUG', 'INFO', 'WARN', 'ERROR']);
50
50
  const _logLineLevelDriftCheck = exports.logLineLevel;
51
51
  void _logLineLevelDriftCheck;
52
+ /** Valibot schema for {@link LogLineIssue}. */
53
+ exports.logLineIssue = v.object({
54
+ type: v.string(),
55
+ level: v.picklist(['error', 'warning', 'debug']),
56
+ location: v.string(),
57
+ parent: v.optional(v.string())
58
+ });
59
+ const _logLineIssueDriftCheck = exports.logLineIssue;
60
+ void _logLineIssueDriftCheck;
61
+ /**
62
+ * A log argument that gives its line an `issue` field. Parse logs one with
63
+ * each issue it records.
64
+ */
65
+ class LogLineIssueArg {
66
+ constructor(issue) {
67
+ Object.defineProperty(this, "issue", {
68
+ enumerable: true,
69
+ configurable: true,
70
+ writable: true,
71
+ value: void 0
72
+ });
73
+ this.issue = issue;
74
+ }
75
+ }
76
+ exports.LogLineIssueArg = LogLineIssueArg;
52
77
  /** Valibot schema for {@link LogLine}. */
53
78
  exports.logLine = v.object({
54
79
  seq: v.number(),
@@ -56,6 +81,7 @@ exports.logLine = v.object({
56
81
  trail: v.optional(v.string()),
57
82
  message: v.string(),
58
83
  error: v.optional(SerializedError_js_1.serializedError),
84
+ issue: v.optional(exports.logLineIssue),
59
85
  time: v.number()
60
86
  });
61
87
  const _logLineDriftCheck = exports.logLine;