@atscript/typescript 0.2.4 → 0.2.5

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/dist/cli.cjs CHANGED
@@ -1079,6 +1079,7 @@ var JsRenderer = class extends BaseRenderer {
1079
1079
  const text = this.compiles ? this.spliceCompiledChecks(module.text, realm) : module.text;
1080
1080
  if (this.bundledSchemas.length === 0) return text;
1081
1081
  const views = /* @__PURE__ */ new Map();
1082
+ const options = jsonSchemaOptionsArg(this.opts);
1082
1083
  const viewOf = (id) => {
1083
1084
  let view = views.get(id);
1084
1085
  if (!view) {
@@ -1086,12 +1087,12 @@ var JsRenderer = class extends BaseRenderer {
1086
1087
  let accounted = [REALM_FAILED];
1087
1088
  try {
1088
1089
  const type = realm.classOf(this.doc, id);
1089
- schema = JSON.stringify(realm.rt.buildJsonSchema(type));
1090
+ schema = JSON.stringify(realm.rt.buildJsonSchema(type, this.opts?.jsonSchemaOptions));
1090
1091
  accounted = this.accountedFor(realm, type);
1091
1092
  } catch {}
1092
1093
  view = {
1093
1094
  schema,
1094
- accounted: accounted.length > 0 ? `, ${JSON.stringify(accounted)}` : ""
1095
+ accounted: accounted.length > 0 || options ? `, ${JSON.stringify(accounted)}${options ? `, ${options}` : ""}` : ""
1095
1096
  };
1096
1097
  views.set(id, view);
1097
1098
  }
@@ -1195,7 +1196,8 @@ var JsRenderer = class extends BaseRenderer {
1195
1196
  */ renderJsonSchemaMethod(_node) {
1196
1197
  if (this.jsonSchemaMode === "lazy") {
1197
1198
  this.writeln("static toJsonSchema() {");
1198
- this.indent().writeln(`return ${this.util("lazyJsonSchema")}(this)`);
1199
+ const options = jsonSchemaOptionsArg(this.opts);
1200
+ this.indent().writeln(`return ${this.util("lazyJsonSchema")}(this${options ? `, ${options}` : ""})`);
1199
1201
  this.unindent();
1200
1202
  this.writeln("}");
1201
1203
  } else {
@@ -1477,6 +1479,17 @@ const JS_INTERPRETED_FORMAT = "js:interpreted";
1477
1479
  if (opts?.jsonSchema !== void 0) return opts.jsonSchema;
1478
1480
  return false;
1479
1481
  }
1482
+ /**
1483
+ * The `jsonSchemaOptions` argument the generated `toJsonSchema()` passes to the runtime: a JS
1484
+ * literal of the options that differ from the defaults, `undefined` when none do.
1485
+ */ function jsonSchemaOptionsArg(opts) {
1486
+ const o = opts?.jsonSchemaOptions;
1487
+ const set = {};
1488
+ if (o?.closed === true) set.closed = true;
1489
+ if (o?.annotations === false) set.annotations = false;
1490
+ if (o?.enums === false) set.enums = false;
1491
+ return Object.keys(set).length > 0 ? JSON.stringify(set) : void 0;
1492
+ }
1480
1493
  /** A generated JS module of `doc`, checked to load in a build (see `assertLoads`). */ function renderJs(doc, opts, mode, loadCheck) {
1481
1494
  const content = rendererOf(doc, opts, mode).render();
1482
1495
  assertLoads(doc, opts, mode, loadCheck);
@@ -2138,7 +2151,11 @@ var _globalThis$1, _Symbol_for$1;
2138
2151
  * their specs flag `userText`. Core's are known from the start ({@link CORE_USER_TEXT}); a plugin's
2139
2152
  * are registered by the generated modules that write them (`$ut`), or by the plugin itself
2140
2153
  * ({@link registerUserText}), so a translator finds them at runtime without the specs.
2141
- */ /** `true`: the value itself is the text; a list: those fields of the value object are. */ /**
2154
+ */ /**
2155
+ * `true`: the value itself is the text; `'expression'`: the value is the source of a function the
2156
+ * client evaluates, its string literals the texts; a list: those fields of the value object are
2157
+ * (a name ending in `()`: that field is an expression).
2158
+ */ /**
2142
2159
  * The user-facing text of `@atscript/core`'s annotations (its specs' `userText` arguments; a test
2143
2160
  * keeps them equal).
2144
2161
  */ const CORE_USER_TEXT = new Map([
@@ -4128,8 +4145,9 @@ var JsNextRenderer = class extends JsRenderer {
4128
4145
  return out;
4129
4146
  }
4130
4147
  /**
4131
- * The plugin annotations holding user-facing text this module writes (`$ut` entries: `key`, or
4132
- * `key:field`); core's are known to the runtime from the start (`CORE_USER_TEXT`).
4148
+ * The plugin annotations holding user-facing text this module writes (`$ut` entries: `key`,
4149
+ * `key()` for an expression, `key:field`, `key:field()`); core's are known to the runtime from
4150
+ * the start (`CORE_USER_TEXT`).
4133
4151
  */ moduleUserText() {
4134
4152
  const out = /* @__PURE__ */ new Set();
4135
4153
  for (const name of new Set(this.doc.annotations.map((a) => a.name))) {
@@ -4137,6 +4155,7 @@ var JsNextRenderer = class extends JsRenderer {
4137
4155
  const spec = this.doc.resolveAnnotation(name);
4138
4156
  const fields = spec && (0, _atscript_core.userTextFields)(spec);
4139
4157
  if (fields === true) out.add(name);
4158
+ else if (fields === "expression") out.add(`${name}()`);
4140
4159
  else if (fields) for (const f of fields) out.add(`${name}:${f}`);
4141
4160
  }
4142
4161
  return [...out].sort();
package/dist/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
2
- const require_plugin = require('./plugin-Cqao46Gt.cjs');
3
- const require_user_text = require('./user-text-BddKk8EU.cjs');
2
+ const require_plugin = require('./plugin-JJ9MJ6AF.cjs');
3
+ const require_user_text = require('./user-text-Dl8FNKLL.cjs');
4
4
 
5
5
  exports.JS_COMPILED_FORMAT = require_plugin.JS_COMPILED_FORMAT;
6
6
  exports.JS_INTERPRETED_FORMAT = require_plugin.JS_INTERPRETED_FORMAT;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,25 @@
1
1
  import { TAtscriptPlugin, AtscriptDoc } from '@atscript/core';
2
2
 
3
+ /** Options of {@link buildJsonSchema} (and of the `jsonSchemaOptions` plugin option). */
4
+ interface TJsonSchemaOptions {
5
+ /**
6
+ * `additionalProperties: false` on every object schema: the schema rejects keys the type does
7
+ * not declare, as the validator does with its default `unknownProps: 'error'`.
8
+ * *(default: `false`)*
9
+ */
10
+ closed?: boolean;
11
+ /**
12
+ * `@meta.description` → `description` and `@meta.label` → `title`, on the schema of the
13
+ * type or field that carries them. *(default: `true`)*
14
+ */
15
+ annotations?: boolean;
16
+ /**
17
+ * A union of literals (`'a' | 'b'`, also with `null`) → `{ type, enum }` instead of an
18
+ * `anyOf` of `const` schemas. *(default: `true`)*
19
+ */
20
+ enums?: boolean;
21
+ }
22
+
3
23
  interface TTsPluginOptions {
4
24
  /**
5
25
  * JSON Schema support mode:
@@ -11,6 +31,14 @@ interface TTsPluginOptions {
11
31
  * to force build-time embedding regardless of this setting.
12
32
  */
13
33
  jsonSchema?: false | 'lazy' | 'bundle';
34
+ /**
35
+ * Options of the generated `toJsonSchema()` (`'lazy'`, `'bundle'` and `@emit.jsonSchema`
36
+ * alike): the same schema `buildJsonSchema(type, jsonSchemaOptions)` builds at runtime.
37
+ * `closed` (`additionalProperties: false`, default `false`), `annotations` (`description` /
38
+ * `title` from `@meta.description` / `@meta.label`, default `true`), `enums` (a union of
39
+ * literals as `enum`, default `true`).
40
+ */
41
+ jsonSchemaOptions?: TJsonSchemaOptions;
14
42
  /**
15
43
  * Example data support:
16
44
  * - `false` — No support. `toExampleData` is not rendered. *(default)*
@@ -102,4 +130,4 @@ declare function withLoadCheck<T>(fn: () => T): T;
102
130
  declare function importCycle(doc: AtscriptDoc): boolean;
103
131
 
104
132
  export { JS_COMPILED_FORMAT, JS_INTERPRETED_FORMAT, tsPlugin as default, importCycle, invalidateCompiledValidators, tsPlugin, withLoadCheck };
105
- export type { TTsPluginOptions };
133
+ export type { TJsonSchemaOptions, TTsPluginOptions };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as withLoadCheck, i as importCycle, n as JS_INTERPRETED_FORMAT, r as tsPlugin, t as JS_COMPILED_FORMAT } from "./plugin-Ct9Ko5JG.mjs";
2
- import { q as invalidateCompiledValidators } from "./user-text-qL59indq.mjs";
1
+ import { a as withLoadCheck, i as importCycle, n as JS_INTERPRETED_FORMAT, r as tsPlugin, t as JS_COMPILED_FORMAT } from "./plugin-Bx7j-vWf.mjs";
2
+ import { X as invalidateCompiledValidators } from "./user-text-BOf56N4d.mjs";
3
3
 
4
4
  export { JS_COMPILED_FORMAT, JS_INTERPRETED_FORMAT, tsPlugin as default, tsPlugin, importCycle, invalidateCompiledValidators, withLoadCheck };
@@ -1,4 +1,4 @@
1
- import { n as CORE_USER_TEXT, o as uniqueName, v as stageEnds } from "./user-text-qL59indq.mjs";
1
+ import { n as CORE_USER_TEXT, o as uniqueName, v as stageEnds } from "./user-text-BOf56N4d.mjs";
2
2
  import path from "path";
3
3
  import { ANNOTATE_RESET, DEFAULT_FORMAT, MERGE_TOO_COMPLEX, NAV_PROP_ANNOTATIONS, SemanticGroup, andExprs, annotatedValueKinds, componentOf, components, defPosition, documentsEpoch, entryParts, flatDefinitions, forEachAnnotationSpec, getRelPath, hasMutatingAnnotates, hasNavPropAnnotation, isAnnotate, isArray, isAsView, isBareId, isBareSpecifier, isConst, isDbEntityNode, isGroup, isInterface, isNull, isPhantomNode, isPrimitive, isQueryLogical, isRef, isStructure, isTuple, moduleId, pastViews, resolveAtscriptFromPath, unshell, userTextFields, withoutAsExtension } from "@atscript/core";
4
4
  import { AsyncLocalStorage } from "node:async_hooks";
@@ -553,6 +553,7 @@ var JsRenderer = class extends BaseRenderer {
553
553
  const text = this.compiles ? this.spliceCompiledChecks(module.text, realm) : module.text;
554
554
  if (this.bundledSchemas.length === 0) return text;
555
555
  const views = /* @__PURE__ */ new Map();
556
+ const options = jsonSchemaOptionsArg(this.opts);
556
557
  const viewOf = (id) => {
557
558
  let view = views.get(id);
558
559
  if (!view) {
@@ -560,12 +561,12 @@ var JsRenderer = class extends BaseRenderer {
560
561
  let accounted = [REALM_FAILED];
561
562
  try {
562
563
  const type = realm.classOf(this.doc, id);
563
- schema = JSON.stringify(realm.rt.buildJsonSchema(type));
564
+ schema = JSON.stringify(realm.rt.buildJsonSchema(type, this.opts?.jsonSchemaOptions));
564
565
  accounted = this.accountedFor(realm, type);
565
566
  } catch {}
566
567
  view = {
567
568
  schema,
568
- accounted: accounted.length > 0 ? `, ${JSON.stringify(accounted)}` : ""
569
+ accounted: accounted.length > 0 || options ? `, ${JSON.stringify(accounted)}${options ? `, ${options}` : ""}` : ""
569
570
  };
570
571
  views.set(id, view);
571
572
  }
@@ -669,7 +670,8 @@ var JsRenderer = class extends BaseRenderer {
669
670
  */ renderJsonSchemaMethod(_node) {
670
671
  if (this.jsonSchemaMode === "lazy") {
671
672
  this.writeln("static toJsonSchema() {");
672
- this.indent().writeln(`return ${this.util("lazyJsonSchema")}(this)`);
673
+ const options = jsonSchemaOptionsArg(this.opts);
674
+ this.indent().writeln(`return ${this.util("lazyJsonSchema")}(this${options ? `, ${options}` : ""})`);
673
675
  this.unindent();
674
676
  this.writeln("}");
675
677
  } else {
@@ -2870,8 +2872,9 @@ var JsNextRenderer = class extends JsRenderer {
2870
2872
  return out;
2871
2873
  }
2872
2874
  /**
2873
- * The plugin annotations holding user-facing text this module writes (`$ut` entries: `key`, or
2874
- * `key:field`); core's are known to the runtime from the start (`CORE_USER_TEXT`).
2875
+ * The plugin annotations holding user-facing text this module writes (`$ut` entries: `key`,
2876
+ * `key()` for an expression, `key:field`, `key:field()`); core's are known to the runtime from
2877
+ * the start (`CORE_USER_TEXT`).
2875
2878
  */ moduleUserText() {
2876
2879
  const out = /* @__PURE__ */ new Set();
2877
2880
  for (const name of new Set(this.doc.annotations.map((a) => a.name))) {
@@ -2879,6 +2882,7 @@ var JsNextRenderer = class extends JsRenderer {
2879
2882
  const spec = this.doc.resolveAnnotation(name);
2880
2883
  const fields = spec && userTextFields(spec);
2881
2884
  if (fields === true) out.add(name);
2885
+ else if (fields === "expression") out.add(`${name}()`);
2882
2886
  else if (fields) for (const f of fields) out.add(`${name}:${f}`);
2883
2887
  }
2884
2888
  return [...out].sort();
@@ -3706,6 +3710,17 @@ const JS_INTERPRETED_FORMAT = "js:interpreted";
3706
3710
  if (opts?.jsonSchema !== void 0) return opts.jsonSchema;
3707
3711
  return false;
3708
3712
  }
3713
+ /**
3714
+ * The `jsonSchemaOptions` argument the generated `toJsonSchema()` passes to the runtime: a JS
3715
+ * literal of the options that differ from the defaults, `undefined` when none do.
3716
+ */ function jsonSchemaOptionsArg(opts) {
3717
+ const o = opts?.jsonSchemaOptions;
3718
+ const set = {};
3719
+ if (o?.closed === true) set.closed = true;
3720
+ if (o?.annotations === false) set.annotations = false;
3721
+ if (o?.enums === false) set.enums = false;
3722
+ return Object.keys(set).length > 0 ? JSON.stringify(set) : void 0;
3723
+ }
3709
3724
  /** A generated JS module of `doc`, checked to load in a build (see `assertLoads`). */ function renderJs(doc, opts, mode, loadCheck) {
3710
3725
  const content = rendererOf(doc, opts, mode).render();
3711
3726
  assertLoads(doc, opts, mode, loadCheck);
@@ -25,7 +25,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
25
25
  }) : target, mod));
26
26
 
27
27
  //#endregion
28
- const require_user_text = require('./user-text-BddKk8EU.cjs');
28
+ const require_user_text = require('./user-text-Dl8FNKLL.cjs');
29
29
  let path = require("path");
30
30
  path = __toESM(path);
31
31
  let _atscript_core = require("@atscript/core");
@@ -581,6 +581,7 @@ var JsRenderer = class extends BaseRenderer {
581
581
  const text = this.compiles ? this.spliceCompiledChecks(module.text, realm) : module.text;
582
582
  if (this.bundledSchemas.length === 0) return text;
583
583
  const views = /* @__PURE__ */ new Map();
584
+ const options = jsonSchemaOptionsArg(this.opts);
584
585
  const viewOf = (id) => {
585
586
  let view = views.get(id);
586
587
  if (!view) {
@@ -588,12 +589,12 @@ var JsRenderer = class extends BaseRenderer {
588
589
  let accounted = [REALM_FAILED];
589
590
  try {
590
591
  const type = realm.classOf(this.doc, id);
591
- schema = JSON.stringify(realm.rt.buildJsonSchema(type));
592
+ schema = JSON.stringify(realm.rt.buildJsonSchema(type, this.opts?.jsonSchemaOptions));
592
593
  accounted = this.accountedFor(realm, type);
593
594
  } catch {}
594
595
  view = {
595
596
  schema,
596
- accounted: accounted.length > 0 ? `, ${JSON.stringify(accounted)}` : ""
597
+ accounted: accounted.length > 0 || options ? `, ${JSON.stringify(accounted)}${options ? `, ${options}` : ""}` : ""
597
598
  };
598
599
  views.set(id, view);
599
600
  }
@@ -697,7 +698,8 @@ var JsRenderer = class extends BaseRenderer {
697
698
  */ renderJsonSchemaMethod(_node) {
698
699
  if (this.jsonSchemaMode === "lazy") {
699
700
  this.writeln("static toJsonSchema() {");
700
- this.indent().writeln(`return ${this.util("lazyJsonSchema")}(this)`);
701
+ const options = jsonSchemaOptionsArg(this.opts);
702
+ this.indent().writeln(`return ${this.util("lazyJsonSchema")}(this${options ? `, ${options}` : ""})`);
701
703
  this.unindent();
702
704
  this.writeln("}");
703
705
  } else {
@@ -2898,8 +2900,9 @@ var JsNextRenderer = class extends JsRenderer {
2898
2900
  return out;
2899
2901
  }
2900
2902
  /**
2901
- * The plugin annotations holding user-facing text this module writes (`$ut` entries: `key`, or
2902
- * `key:field`); core's are known to the runtime from the start (`CORE_USER_TEXT`).
2903
+ * The plugin annotations holding user-facing text this module writes (`$ut` entries: `key`,
2904
+ * `key()` for an expression, `key:field`, `key:field()`); core's are known to the runtime from
2905
+ * the start (`CORE_USER_TEXT`).
2903
2906
  */ moduleUserText() {
2904
2907
  const out = /* @__PURE__ */ new Set();
2905
2908
  for (const name of new Set(this.doc.annotations.map((a) => a.name))) {
@@ -2907,6 +2910,7 @@ var JsNextRenderer = class extends JsRenderer {
2907
2910
  const spec = this.doc.resolveAnnotation(name);
2908
2911
  const fields = spec && (0, _atscript_core.userTextFields)(spec);
2909
2912
  if (fields === true) out.add(name);
2913
+ else if (fields === "expression") out.add(`${name}()`);
2910
2914
  else if (fields) for (const f of fields) out.add(`${name}:${f}`);
2911
2915
  }
2912
2916
  return [...out].sort();
@@ -3734,6 +3738,17 @@ const JS_INTERPRETED_FORMAT = "js:interpreted";
3734
3738
  if (opts?.jsonSchema !== void 0) return opts.jsonSchema;
3735
3739
  return false;
3736
3740
  }
3741
+ /**
3742
+ * The `jsonSchemaOptions` argument the generated `toJsonSchema()` passes to the runtime: a JS
3743
+ * literal of the options that differ from the defaults, `undefined` when none do.
3744
+ */ function jsonSchemaOptionsArg(opts) {
3745
+ const o = opts?.jsonSchemaOptions;
3746
+ const set = {};
3747
+ if (o?.closed === true) set.closed = true;
3748
+ if (o?.annotations === false) set.annotations = false;
3749
+ if (o?.enums === false) set.enums = false;
3750
+ return Object.keys(set).length > 0 ? JSON.stringify(set) : void 0;
3751
+ }
3737
3752
  /** A generated JS module of `doc`, checked to load in a build (see `assertLoads`). */ function renderJs(doc, opts, mode, loadCheck) {
3738
3753
  const content = rendererOf(doc, opts, mode).render();
3739
3754
  assertLoads(doc, opts, mode, loadCheck);