@atscript/typescript 0.2.4 → 0.2.6

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.
@@ -72,10 +72,11 @@
72
72
  const mapSet = Map.prototype.set;
73
73
  /** Global mutation epoch: compiled checks bound in an older epoch re-bind before use. */ let epoch = 0;
74
74
  /**
75
- * Writes of metadata maps so far (any map, any key: `annotate()`, the `Map` methods, a node's map
76
- * replaced, a layer flag or intrinsic mark registered): a lazy JSON schema or a layered map's
77
- * flattened view built before the last one is built again (see `lazyJsonSchema`, `LayerMap`).
78
- */ let metadataWrites = 0;
75
+ * Writes of type trees so far, watched or not: of metadata (any map, any key: `annotate()`, the
76
+ * `Map` methods, a node's map replaced, a layer flag or intrinsic mark registered) and of shape
77
+ * (every {@link touched} mutation, `invalidateCompiledValidators()`). What is derived from a type
78
+ * tree before the last write is derived again (`lazyJsonSchema`, `LayerMap`, `normalizeForType`).
79
+ */ let typeWrites = 0;
79
80
  /** Some bind has watched an object (until then no mutation can affect a compiled check). */ let tracking = false;
80
81
  /** Objects (nodes, type defs, metadata / props maps) read by some compiled-check bind. */ const watched = /* @__PURE__ */ new WeakSet();
81
82
  /** The current mutation epoch. */ function currentEpoch() {
@@ -85,7 +86,11 @@ const mapSet = Map.prototype.set;
85
86
  tracking = true;
86
87
  watched.add(obj);
87
88
  }
88
- /** Records a tracked mutation of `obj` (a no-op unless some bind has read it). */ function touched(obj) {
89
+ /**
90
+ * Records a tracked mutation of `obj`: a type tree write (see `typeWrites`); compiled checks
91
+ * re-bind when some bind has read it.
92
+ */ function touched(obj) {
93
+ typeWrites++;
89
94
  if (tracking && watched.has(obj)) epoch++;
90
95
  }
91
96
  /**
@@ -111,13 +116,13 @@ const mapSet = Map.prototype.set;
111
116
  * avoid mutating validated types per request.
112
117
  */ function invalidateCompiledValidators() {
113
118
  epoch++;
114
- metadataWrites++;
119
+ typeWrites++;
115
120
  }
116
- /** The number of metadata writes so far (see `metadataWrites`). */ function metadataWriteCount() {
117
- return metadataWrites;
121
+ /** The number of type tree writes so far (see `typeWrites`). */ function typeWriteCount() {
122
+ return typeWrites;
118
123
  }
119
124
  /** Records a write of a node's metadata the maps do not see: the node's map replaced. */ function metadataReplaced() {
120
- metadataWrites++;
125
+ typeWrites++;
121
126
  }
122
127
  /**
123
128
  * `annotate(m, key, value, true)` where map `m` holds `own` as its own value of `key`: appended in
@@ -126,7 +131,7 @@ const mapSet = Map.prototype.set;
126
131
  if (Array.isArray(own)) {
127
132
  own.push(value);
128
133
  if (VALIDATOR_KEY_SET.has(key)) touched(m);
129
- metadataWrites++;
134
+ else typeWrites++;
130
135
  } else m.set(key, [own, value]);
131
136
  }
132
137
  /**
@@ -136,21 +141,18 @@ const mapSet = Map.prototype.set;
136
141
  set(key, value) {
137
142
  super.set(key, value);
138
143
  if (tracking && VALIDATOR_KEY_SET.has(key)) touched(this);
139
- metadataWrites++;
144
+ else typeWrites++;
140
145
  return this;
141
146
  }
142
147
  delete(key) {
143
148
  const deleted = super.delete(key);
144
- if (deleted) {
145
- if (tracking && VALIDATOR_KEY_SET.has(key)) touched(this);
146
- metadataWrites++;
147
- }
149
+ if (deleted) if (tracking && VALIDATOR_KEY_SET.has(key)) touched(this);
150
+ else typeWrites++;
148
151
  return deleted;
149
152
  }
150
153
  clear() {
151
154
  super.clear();
152
155
  touched(this);
153
- metadataWrites++;
154
156
  }
155
157
  /** A new map holding `entries`: no write (a new map is in no compiled check nor schema). */ constructor(entries) {
156
158
  super();
@@ -190,7 +192,7 @@ const mapSet = Map.prototype.set;
190
192
  * a base shows through every layer over it.
191
193
  *
192
194
  * Reads go through a flattened view of the effective entries, rebuilt on the first read after any
193
- * metadata write (one global write counter: `metadataWriteCount`). The view is used only while
195
+ * type tree write (one global write counter: `typeWriteCount`). The view is used only while
194
196
  * every map below is tracked (an `AnnotationMap` or a `LayerMap`, whose writes the counter
195
197
  * sees); otherwise the reads are live. A raw write the counter does not see (a node's map swapped,
196
198
  * `node.metadata = X`, or edited in place) must be followed by `invalidateCompiledValidators()`,
@@ -260,7 +262,6 @@ let hiding = false;
260
262
  s.add(key);
261
263
  hiding = true;
262
264
  touched(m);
263
- metadataReplaced();
264
265
  }
265
266
  /** The keys map `m` hides from its bases (see {@link hideKey}), for a map replacing it. */ function moveHidden(from, to) {
266
267
  const s = HIDDEN.get(from);
@@ -281,7 +282,7 @@ let _Symbol_iterator = Symbol.iterator;
281
282
  * The flattened view (built again after a metadata write), `undefined`: read live. Internal:
282
283
  * this module's readers and the validators read it (`readable`, `filledMeta`), never write.
283
284
  */ view() {
284
- return this.w === metadataWriteCount() ? this.f : this.flatten();
285
+ return this.w === typeWriteCount() ? this.f : this.flatten();
285
286
  }
286
287
  flatten() {
287
288
  this.w = -1;
@@ -291,7 +292,7 @@ let _Symbol_iterator = Symbol.iterator;
291
292
  f = keys.length === 0 ? NO_ENTRIES : new AnnotationMap(keys.map((k) => [k, this.liveGet(k)]));
292
293
  }
293
294
  this.f = f;
294
- this.w = metadataWriteCount();
295
+ this.w = typeWriteCount();
295
296
  return f;
296
297
  }
297
298
  /** The value of `key` (not concatenated), or `MISS`; `nr`: a `passedWhenReferred:false` key. */ lookup(key, nr) {
@@ -319,7 +320,7 @@ let _Symbol_iterator = Symbol.iterator;
319
320
  return MISS;
320
321
  }
321
322
  get(key) {
322
- const view = this.w === metadataWriteCount() ? this.f : this.flatten();
323
+ const view = this.w === typeWriteCount() ? this.f : this.flatten();
323
324
  return view === void 0 ? this.liveGet(key) : mapGet.call(view, key);
324
325
  }
325
326
  /** `get` read through the layers (see the file comment). */ liveGet(key) {
@@ -380,7 +381,6 @@ let _Symbol_iterator = Symbol.iterator;
380
381
  const deleted = super.delete(key);
381
382
  if ((hiding ? HIDDEN.get(this) : void 0)?.delete(key)) {
382
383
  touched(this);
383
- metadataReplaced();
384
384
  return true;
385
385
  }
386
386
  return deleted;
@@ -422,7 +422,7 @@ Object.defineProperty(LayerMap.prototype, "size", {
422
422
  });
423
423
  /**
424
424
  * Every map of `m`'s layers is tracked (an `AnnotationMap` or a `LayerMap`, whose writes count in
425
- * `metadataWrites`), so a flattened view of `m` stays right until the next write.
425
+ * `typeWrites`), so a flattened view of `m` stays right until the next write.
426
426
  */ function tracked(m, seen) {
427
427
  const p = Object.getPrototypeOf(m);
428
428
  if (p !== AnnotationMap.prototype && p !== LayerMap.prototype) return false;
@@ -990,7 +990,7 @@ function caseVariants(ch, unicode) {
990
990
  let result = null;
991
991
  for (const candidate of candidates) {
992
992
  const values = /* @__PURE__ */ new Set();
993
- const indexMapping = {};
993
+ const indexMapping = Object.create(null);
994
994
  let valid = true;
995
995
  for (let i = 0; i < items.length; i++) {
996
996
  const prop = items[i].type.props.get(candidate);
@@ -3159,8 +3159,8 @@ function _define_property(obj, key, value) {
3159
3159
  return typeof annotation === "object" && annotation !== null ? annotation.message || void 0 : void 0;
3160
3160
  }
3161
3161
  /**
3162
- * Decided discriminators, by union `items` (reported errors only: a union's discriminator is
3163
- * looked for once its value failed). An entry is reused while the items, their type defs and the
3162
+ * Decided discriminators, by union `items` (the validator's reported errors, the branch
3163
+ * `normalizeForType` takes). An entry is reused while the items, their type defs and the
3164
3164
  * discriminator props are the same objects.
3165
3165
  */ const discCache = /* @__PURE__ */ new WeakMap();
3166
3166
  /**
@@ -3956,7 +3956,11 @@ const queue = [];
3956
3956
  * their specs flag `userText`. Core's are known from the start ({@link CORE_USER_TEXT}); a plugin's
3957
3957
  * are registered by the generated modules that write them (`$ut`), or by the plugin itself
3958
3958
  * ({@link registerUserText}), so a translator finds them at runtime without the specs.
3959
- */ /** `true`: the value itself is the text; a list: those fields of the value object are. */ /**
3959
+ */ /**
3960
+ * `true`: the value itself is the text; `'expression'`: the value is the source of a function the
3961
+ * client evaluates, its string literals the texts; a list: those fields of the value object are
3962
+ * (a name ending in `()`: that field is an expression).
3963
+ */ /**
3960
3964
  * The user-facing text of `@atscript/core`'s annotations (its specs' `userText` arguments; a test
3961
3965
  * keeps them equal).
3962
3966
  */ const CORE_USER_TEXT = new Map([
@@ -3979,33 +3983,36 @@ const queue = [];
3979
3983
  ]);
3980
3984
  const USER_TEXT = new Map(CORE_USER_TEXT);
3981
3985
  /**
3982
- * Registers metadata key `key` as holding user-facing text: `true` (default) — its value is the
3983
- * text; a list of field names — those fields of its value object are. Fields add up across calls.
3986
+ * Registers metadata key `key` as holding user-facing text (see {@link TUserTextFields}; `true` by
3987
+ * default). Fields add up across calls; a whole-value kind replaces a field list, and an
3988
+ * expression wins over text (`name()` over `name`), whatever the order of the calls.
3984
3989
  * For plugins that write such metadata at runtime; generated modules register what they write.
3985
3990
  */ function registerUserText(key, fields = true) {
3986
3991
  const known = USER_TEXT.get(key);
3987
- if (known === true || fields === true) {
3988
- USER_TEXT.set(key, true);
3992
+ if (known === "expression" || known === true && typeof fields === "object") return;
3993
+ if (typeof fields !== "object") {
3994
+ USER_TEXT.set(key, fields);
3989
3995
  return;
3990
3996
  }
3991
- const added = fields.filter((f) => !known?.includes(f));
3992
- if (added.length > 0 || !known) USER_TEXT.set(key, [...known ?? [], ...added]);
3997
+ const all = new Set([...typeof known === "object" ? known : [], ...fields]);
3998
+ USER_TEXT.set(key, [...all].filter((f) => !all.has(`${f}()`)));
3993
3999
  }
3994
4000
  /**
3995
4001
  * Registers user-text keys written by a generated module (see {@link registerUserText}): `key` —
3996
- * the value itself; `key:field` — that field of the value object.
4002
+ * the value itself; `key()` — the value is an expression; `key:field` — that field of the value
4003
+ * object; `key:field()` — that field is an expression.
3997
4004
  */ function $ut(...entries) {
3998
4005
  for (const entry of entries) {
3999
4006
  const at = entry.indexOf(":");
4000
- if (at === -1) registerUserText(entry);
4001
- else registerUserText(entry.slice(0, at), [entry.slice(at + 1)]);
4007
+ if (at !== -1) registerUserText(entry.slice(0, at), [entry.slice(at + 1)]);
4008
+ else if (entry.endsWith("()")) registerUserText(entry.slice(0, -2), "expression");
4009
+ else registerUserText(entry);
4002
4010
  }
4003
4011
  }
4004
4012
  /**
4005
- * Where metadata key `key` holds user-facing text: `true` — its value is the text (a string; for
4006
- * a `multiple` annotation, each item of the list); a list of field names — those fields of its
4007
- * value object (each item's, for a list); `undefined` — no user-facing text. Core keys are known
4008
- * from the start; a plugin's once a module writing them is loaded (or the plugin registered them).
4013
+ * Where metadata key `key` holds user-facing text (see {@link TUserTextFields}; for a `multiple`
4014
+ * annotation, each item of its list), `undefined` when it holds none. Core keys are known from the
4015
+ * start; a plugin's once a module writing them is loaded (or the plugin registered them).
4009
4016
  */ function userTextOf(key) {
4010
4017
  return USER_TEXT.get(key);
4011
4018
  }
@@ -4014,4 +4021,4 @@ const USER_TEXT = new Map(CORE_USER_TEXT);
4014
4021
  }
4015
4022
 
4016
4023
  //#endregion
4017
- export { boundOf as $, propAtChain as A, jsonSchemaPattern as B, createAnnotatedTypeNode as C, isAnnotatedType as D, defineAnnotatedType as E, formatValidatorMessage as F, metaEmpty as G, $ak as H, localizeErrors as I, metadataWriteCount as J, watchLayer as K, messagesTranslator as L, ValidatorError as M, VALIDATOR_ERROR_CODES as N, isAnnotatedTypeOfPrimitive as O, VALIDATOR_MESSAGES as P, arrayKeyProps as Q, registerCompiledCheck as R, annotate as S, createObjectTypeDef as T, $nr as U, statelessRegExp as V, LayerMap as W, DECIMAL_RE as X, touched as Y, VALIDATOR_KEYS as Z, pendingStatements as _, userTextOf as a, cutKey as b, $blk as c, $fc as d, $ft as f, appliedBlockModules as g, $sh as h, userTextKeys as i, Validator as j, isPhantomType as k, $end as l, $q as m, CORE_USER_TEXT as n, uniqueName as o, $ps as p, invalidateCompiledValidators as q, registerUserText as r, $alias as s, $ut as t, $f as u, stageEnds as v, createMetadataMap as w, enterCut as x, cutAs as y, detectDiscriminator as z };
4024
+ export { VALIDATOR_KEYS as $, propAtChain as A, registerCompiledCheck as B, createAnnotatedTypeNode as C, isAnnotatedType as D, defineAnnotatedType as E, VALIDATOR_MESSAGES as F, $nr as G, jsonSchemaPattern as H, formatCodeOf as I, watchLayer as J, LayerMap as K, formatValidatorMessage as L, ValidatorError as M, unionDiscriminator as N, isAnnotatedTypeOfPrimitive as O, VALIDATOR_ERROR_CODES as P, DECIMAL_RE as Q, localizeErrors as R, annotate as S, createObjectTypeDef as T, statelessRegExp as U, detectDiscriminator as V, $ak as W, touched as X, invalidateCompiledValidators as Y, typeWriteCount as Z, pendingStatements as _, userTextOf as a, cutKey as b, $blk as c, $fc as d, arrayKeyProps as et, $ft as f, appliedBlockModules as g, $sh as h, userTextKeys as i, Validator as j, isPhantomType as k, $end as l, $q as m, CORE_USER_TEXT as n, uniqueName as o, $ps as p, metaEmpty as q, registerUserText as r, $alias as s, $ut as t, boundOf as tt, $f as u, stageEnds as v, createMetadataMap as w, enterCut as x, cutAs as y, messagesTranslator as z };