@atscript/typescript 0.2.3 → 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 +233 -32
- package/dist/index.cjs +3 -3
- package/dist/index.d.ts +29 -1
- package/dist/index.mjs +2 -2
- package/dist/{plugin-DhesoKce.mjs → plugin-Bx7j-vWf.mjs} +77 -23
- package/dist/{plugin-oUSsb3Ri.cjs → plugin-JJ9MJ6AF.cjs} +80 -26
- package/dist/realm-runtime.js +1629 -385
- package/dist/test-utils.cjs +3 -3
- package/dist/test-utils.d.ts +355 -152
- package/dist/test-utils.mjs +2 -2
- package/dist/{unique-name-hg1CyPIs.mjs → user-text-BOf56N4d.mjs} +765 -192
- package/dist/{unique-name-xWd9uO-k.cjs → user-text-Dl8FNKLL.cjs} +848 -191
- package/dist/utils.cjs +858 -228
- package/dist/utils.d.ts +436 -32
- package/dist/utils.mjs +755 -137
- package/package.json +5 -5
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
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
|
-
import { DEFAULT_FORMAT, MERGE_TOO_COMPLEX, NAV_PROP_ANNOTATIONS, SemanticGroup, andExprs, 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 } from "@atscript/core";
|
|
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";
|
|
5
5
|
import { readFileSync } from "fs";
|
|
6
6
|
import { fileURLToPath } from "url";
|
|
@@ -154,7 +154,7 @@ var BaseRenderer = class extends CodePrinter {
|
|
|
154
154
|
renderType(node) {}
|
|
155
155
|
renderAnnotate(node) {}
|
|
156
156
|
transformFromPath(path) {
|
|
157
|
-
return `${path}.as`;
|
|
157
|
+
return `${withoutAsExtension(path)}.as`;
|
|
158
158
|
}
|
|
159
159
|
renderImport(node) {
|
|
160
160
|
const def = node.getDefinition();
|
|
@@ -352,7 +352,8 @@ const CHECKS_IMPORT_MARKER = "/*@@atscript-compiled-checks-import@@*/";
|
|
|
352
352
|
bundledSchemaOf: "$jbm",
|
|
353
353
|
$ps: "$ps",
|
|
354
354
|
$ak: "$ak",
|
|
355
|
-
$nr: "$nk"
|
|
355
|
+
$nr: "$nk",
|
|
356
|
+
$ut: "$ut"
|
|
356
357
|
};
|
|
357
358
|
const LITERAL_IDENTIFIERS = new Set([
|
|
358
359
|
"true",
|
|
@@ -406,7 +407,8 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
406
407
|
/**
|
|
407
408
|
* Relative imports get the configured `moduleExtension`; bare specifiers
|
|
408
409
|
* always keep `.as` — they resolve through the package's `exports` map.
|
|
409
|
-
*/ transformFromPath(
|
|
410
|
+
*/ transformFromPath(written) {
|
|
411
|
+
const path = withoutAsExtension(written);
|
|
410
412
|
return isBareSpecifier(path) ? `${path}.as` : `${path}${this.opts?.moduleExtension ?? ".as"}`;
|
|
411
413
|
}
|
|
412
414
|
/**
|
|
@@ -551,6 +553,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
551
553
|
const text = this.compiles ? this.spliceCompiledChecks(module.text, realm) : module.text;
|
|
552
554
|
if (this.bundledSchemas.length === 0) return text;
|
|
553
555
|
const views = /* @__PURE__ */ new Map();
|
|
556
|
+
const options = jsonSchemaOptionsArg(this.opts);
|
|
554
557
|
const viewOf = (id) => {
|
|
555
558
|
let view = views.get(id);
|
|
556
559
|
if (!view) {
|
|
@@ -558,12 +561,12 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
558
561
|
let accounted = [REALM_FAILED];
|
|
559
562
|
try {
|
|
560
563
|
const type = realm.classOf(this.doc, id);
|
|
561
|
-
schema = JSON.stringify(realm.rt.buildJsonSchema(type));
|
|
564
|
+
schema = JSON.stringify(realm.rt.buildJsonSchema(type, this.opts?.jsonSchemaOptions));
|
|
562
565
|
accounted = this.accountedFor(realm, type);
|
|
563
566
|
} catch {}
|
|
564
567
|
view = {
|
|
565
568
|
schema,
|
|
566
|
-
accounted: accounted.length > 0 ? `, ${JSON.stringify(accounted)}` : ""
|
|
569
|
+
accounted: accounted.length > 0 || options ? `, ${JSON.stringify(accounted)}${options ? `, ${options}` : ""}` : ""
|
|
567
570
|
};
|
|
568
571
|
views.set(id, view);
|
|
569
572
|
}
|
|
@@ -667,7 +670,8 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
667
670
|
*/ renderJsonSchemaMethod(_node) {
|
|
668
671
|
if (this.jsonSchemaMode === "lazy") {
|
|
669
672
|
this.writeln("static toJsonSchema() {");
|
|
670
|
-
|
|
673
|
+
const options = jsonSchemaOptionsArg(this.opts);
|
|
674
|
+
this.indent().writeln(`return ${this.util("lazyJsonSchema")}(this${options ? `, ${options}` : ""})`);
|
|
671
675
|
this.unindent();
|
|
672
676
|
this.writeln("}");
|
|
673
677
|
} else {
|
|
@@ -808,12 +812,16 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
808
812
|
if (dotIdx === -1) return `() => ${this.annotationTypeName(text, origin)}`;
|
|
809
813
|
return `{ type: () => ${this.annotationTypeName(text.slice(0, dotIdx), origin)}, field: "${escapeQuotes(text.slice(dotIdx + 1))}" }`;
|
|
810
814
|
}
|
|
811
|
-
emitArgValue(aSpec, argToken, origin) {
|
|
815
|
+
emitArgValue(aSpec, argToken, origin, mainToken) {
|
|
812
816
|
if (aSpec.type === "ref") return this.emitRefValue(argToken.text, origin);
|
|
813
817
|
if (aSpec.type === "query" && argToken.queryNode) return this.emitQueryTree(argToken.queryNode, origin);
|
|
814
818
|
if (aSpec.type === "expr" && argToken.exprNode) return this.emitExprNode(argToken.exprNode.expression, origin);
|
|
815
819
|
if (aSpec.type === "order" && argToken.orderNode) return `[${argToken.orderNode.items.map((item) => `{ ref: ${this.emitQueryFieldRef(item.ref, origin)}${item.desc ? ", desc: true" : ""} }`).join(", ")}]`;
|
|
816
|
-
return aSpec.type === "string" ? `"${escapeQuotes(argToken.text)}"` : argToken.text;
|
|
820
|
+
return aSpec.type === "string" || aSpec.type === "value" && (argToken.type === "text" || argToken.type === "number" && this.decimalField(mainToken, origin)) ? `"${escapeQuotes(argToken.text)}"` : argToken.text;
|
|
821
|
+
}
|
|
822
|
+
/** Whether annotation `mainToken` is written on a `decimal` field (not one that holds numbers). */ decimalField(mainToken, origin) {
|
|
823
|
+
const kinds = annotatedValueKinds(mainToken, origin ?? this.doc);
|
|
824
|
+
return !!kinds?.has("decimal") && !kinds.has("number") && !kinds.has("unknown");
|
|
817
825
|
}
|
|
818
826
|
emitExprNode(node, origin) {
|
|
819
827
|
switch (node.entity) {
|
|
@@ -893,13 +901,13 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
893
901
|
targetValue = "{ ";
|
|
894
902
|
let i = 0;
|
|
895
903
|
for (const aSpec of spec.arguments) {
|
|
896
|
-
if (an.args[i]) targetValue += `${wrapProp(aSpec.name)}: ${this.emitArgValue(aSpec, an.args[i], origin)}${i === length - 1 ? "" : ", "} `;
|
|
904
|
+
if (an.args[i]) targetValue += `${wrapProp(aSpec.name)}: ${this.emitArgValue(aSpec, an.args[i], origin, an.token)}${i === length - 1 ? "" : ", "} `;
|
|
897
905
|
i++;
|
|
898
906
|
}
|
|
899
907
|
targetValue += "}";
|
|
900
908
|
} else if (an.args[0]) {
|
|
901
909
|
const aSpec = spec.arguments[0];
|
|
902
|
-
targetValue = this.emitArgValue(aSpec, an.args[0], origin);
|
|
910
|
+
targetValue = this.emitArgValue(aSpec, an.args[0], origin, an.token);
|
|
903
911
|
}
|
|
904
912
|
}
|
|
905
913
|
} else {
|
|
@@ -1402,7 +1410,13 @@ var JsNextRenderer = class extends JsRenderer {
|
|
|
1402
1410
|
}
|
|
1403
1411
|
/** `$a(P.metadata, …)` writes of a class's own annotations (after its `$alias`). */ annotateCalls(name, node) {
|
|
1404
1412
|
let out = "";
|
|
1413
|
+
const resets = (node.annotations ?? []).filter((an) => an.name === ANNOTATE_RESET);
|
|
1414
|
+
if (resets.length > 0) {
|
|
1415
|
+
const data = this.blockAnnotationsData(node, resets);
|
|
1416
|
+
out += `\n${this.util("$blk")}(null, -1, ${name}, [[[${data.join(", ")}], []]])`;
|
|
1417
|
+
}
|
|
1405
1418
|
for (const an of node.annotations ?? []) {
|
|
1419
|
+
if (an.name === ANNOTATE_RESET) continue;
|
|
1406
1420
|
const { value, multiple } = this.computeAnnotationValue(node, an);
|
|
1407
1421
|
out += `\n${this.util("annotate")}(${name}.metadata, ${str(an.name)}, ${value}${multiple ? ", true" : ""})`;
|
|
1408
1422
|
}
|
|
@@ -2343,19 +2357,21 @@ var JsNextRenderer = class extends JsRenderer {
|
|
|
2343
2357
|
* The statement needs what the paths go through built (`$f` guards, needs).
|
|
2344
2358
|
*/ entriesData(b, reads) {
|
|
2345
2359
|
const entries = [];
|
|
2346
|
-
|
|
2347
|
-
const { value, multiple } = this.computeAnnotationValue(node, an);
|
|
2348
|
-
return `[${str(an.name)}, ${value}${multiple ? ", 1" : ""}]`;
|
|
2349
|
-
});
|
|
2350
|
-
if (b.rank && b.node.annotations?.length) entries.push(`[[${annotationsOf(b.node).join(", ")}], []]`);
|
|
2360
|
+
if (b.rank && b.node.annotations?.length) entries.push(`[[${this.blockAnnotationsData(b.node).join(", ")}], []]`);
|
|
2351
2361
|
for (const en of b.entries) {
|
|
2352
2362
|
if (en.error || en.paths.length === 0) continue;
|
|
2353
2363
|
const copy = reads !== b.target;
|
|
2354
2364
|
const paths = en.paths.map((p, i) => `[${stepsText(this.stepsData(reads, p, en.kinds[i], false, copy))}]`);
|
|
2355
|
-
entries.push(`[[${
|
|
2365
|
+
entries.push(`[[${this.blockAnnotationsData(en.ref).join(", ")}], ${paths.join(", ")}]`);
|
|
2356
2366
|
}
|
|
2357
2367
|
return entries.length > 0 ? `[\n ${entries.join(",\n ")},\n]` : "";
|
|
2358
2368
|
}
|
|
2369
|
+
/** Annotations `anns` of `node` as `$blk` entry data: `[name, value]`, `, 1` for a `multiple`. */ blockAnnotationsData(node, anns = node.annotations ?? []) {
|
|
2370
|
+
return anns.map((an) => {
|
|
2371
|
+
const { value, multiple } = this.computeAnnotationValue(node, an);
|
|
2372
|
+
return `[${str(an.name)}, ${value}${multiple ? ", 1" : ""}]`;
|
|
2373
|
+
});
|
|
2374
|
+
}
|
|
2359
2375
|
/**
|
|
2360
2376
|
* The runtime `[step, kind]`s of model path `p` of `reads` (an `as` copy's: its target's
|
|
2361
2377
|
* structure), and its needs: the classes it walks are built first (a shell def clones a built
|
|
@@ -2843,14 +2859,35 @@ var JsNextRenderer = class extends JsRenderer {
|
|
|
2843
2859
|
const rank = blocks[0].rank;
|
|
2844
2860
|
return `const ${R} = [${rank.depth}, ${str(rank.id)}]`;
|
|
2845
2861
|
}
|
|
2846
|
-
/**
|
|
2862
|
+
/**
|
|
2863
|
+
* The registries (`$ak`, `$nr`: how layers read those keys; `$ut`: the keys holding user-facing
|
|
2864
|
+
* text), before any statement.
|
|
2865
|
+
*/ registries() {
|
|
2847
2866
|
const { append, notReferred } = flagKeys(this.doc.config.annotations);
|
|
2848
2867
|
const out = [];
|
|
2849
2868
|
if (append.length > 0) out.push(`${this.util("$ak")}(${append.map(str).join(", ")})`);
|
|
2850
2869
|
if (notReferred.length > 0) out.push(`${this.util("$nr")}(${notReferred.map(str).join(", ")})`);
|
|
2870
|
+
const userText = this.moduleUserText();
|
|
2871
|
+
if (userText.length > 0) out.push(`${this.util("$ut")}(${userText.map(str).join(", ")})`);
|
|
2851
2872
|
return out;
|
|
2852
2873
|
}
|
|
2853
2874
|
/**
|
|
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`).
|
|
2878
|
+
*/ moduleUserText() {
|
|
2879
|
+
const out = /* @__PURE__ */ new Set();
|
|
2880
|
+
for (const name of new Set(this.doc.annotations.map((a) => a.name))) {
|
|
2881
|
+
if (CORE_USER_TEXT.has(name)) continue;
|
|
2882
|
+
const spec = this.doc.resolveAnnotation(name);
|
|
2883
|
+
const fields = spec && userTextFields(spec);
|
|
2884
|
+
if (fields === true) out.add(name);
|
|
2885
|
+
else if (fields === "expression") out.add(`${name}()`);
|
|
2886
|
+
else if (fields) for (const f of fields) out.add(`${name}:${f}`);
|
|
2887
|
+
}
|
|
2888
|
+
return [...out].sort();
|
|
2889
|
+
}
|
|
2890
|
+
/**
|
|
2854
2891
|
* The runtime steps of a model path: a `X | null` union the model passes through is a union
|
|
2855
2892
|
* node at runtime (its non-null item: `|i`, own when the union's holder is, else shelled).
|
|
2856
2893
|
*/ runtimeSteps(d, steps, kinds, { copy = false, items: withItems = false, pure = false } = {}) {
|
|
@@ -3630,11 +3667,14 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
|
|
|
3630
3667
|
const written = only?.prop.getDefinition();
|
|
3631
3668
|
return written && !(isGroup(only.def) && only.def !== written) ? written : only?.def;
|
|
3632
3669
|
}
|
|
3670
|
+
/** The TypeScript type of a final primitive type (`any`, the `unknown` primitive: `unknown`). */ function finalTsType(t) {
|
|
3671
|
+
return t === "void" ? "undefined" : t === "any" ? "unknown" : t;
|
|
3672
|
+
}
|
|
3633
3673
|
function renderPrimitiveTypeDef(def) {
|
|
3634
3674
|
if (!def) return "unknown";
|
|
3635
|
-
if (typeof def === "string") return def
|
|
3675
|
+
if (typeof def === "string") return finalTsType(def);
|
|
3636
3676
|
switch (def.kind) {
|
|
3637
|
-
case "final": return def.value
|
|
3677
|
+
case "final": return finalTsType(def.value);
|
|
3638
3678
|
case "union": return def.items.map(renderPrimitiveTypeDef).join(" | ");
|
|
3639
3679
|
case "intersection": return def.items.map((item) => typeof item === "object" && item.kind === "union" ? `(${renderPrimitiveTypeDef(item)})` : renderPrimitiveTypeDef(item)).join(" & ");
|
|
3640
3680
|
case "tuple": return `[${def.items.map(renderPrimitiveTypeDef).join(", ")}]`;
|
|
@@ -3670,6 +3710,17 @@ const JS_INTERPRETED_FORMAT = "js:interpreted";
|
|
|
3670
3710
|
if (opts?.jsonSchema !== void 0) return opts.jsonSchema;
|
|
3671
3711
|
return false;
|
|
3672
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
|
+
}
|
|
3673
3724
|
/** A generated JS module of `doc`, checked to load in a build (see `assertLoads`). */ function renderJs(doc, opts, mode, loadCheck) {
|
|
3674
3725
|
const content = rendererOf(doc, opts, mode).render();
|
|
3675
3726
|
assertLoads(doc, opts, mode, loadCheck);
|
|
@@ -3696,8 +3747,11 @@ const tsPlugin = (opts) => {
|
|
|
3696
3747
|
for (const [key, val] of Object.entries(annotations)) {
|
|
3697
3748
|
const multiple = val.multiple;
|
|
3698
3749
|
const typeLine = Array.from(val.types).map((t) => {
|
|
3699
|
-
if (t.type === "object" && "props" in t)
|
|
3700
|
-
|
|
3750
|
+
if (t.type === "object" && "props" in t) {
|
|
3751
|
+
const props = Object.entries(t.props);
|
|
3752
|
+
const object = `{ ${props.map(([k, v]) => `${wrapProp(k)}${v.optional ? "?" : ""}: ${v.type}`).join(", ")} }`;
|
|
3753
|
+
return props.length === 1 && props[0][1].optional ? `${object} | true` : object;
|
|
3754
|
+
} else return "optional" in t && t.optional ? `${t.type} | true` : t.type;
|
|
3701
3755
|
}).join(" | ");
|
|
3702
3756
|
rendered.push(`${wrapProp(key)}: ${multiple ? "(" : ""}${typeLine}${multiple ? ")[]" : ""}`);
|
|
3703
3757
|
}
|
|
@@ -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
|
|
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");
|
|
@@ -182,7 +182,7 @@ var BaseRenderer = class extends CodePrinter {
|
|
|
182
182
|
renderType(node) {}
|
|
183
183
|
renderAnnotate(node) {}
|
|
184
184
|
transformFromPath(path) {
|
|
185
|
-
return `${path}.as`;
|
|
185
|
+
return `${(0, _atscript_core.withoutAsExtension)(path)}.as`;
|
|
186
186
|
}
|
|
187
187
|
renderImport(node) {
|
|
188
188
|
const def = node.getDefinition();
|
|
@@ -380,7 +380,8 @@ const CHECKS_IMPORT_MARKER = "/*@@atscript-compiled-checks-import@@*/";
|
|
|
380
380
|
bundledSchemaOf: "$jbm",
|
|
381
381
|
$ps: "$ps",
|
|
382
382
|
$ak: "$ak",
|
|
383
|
-
$nr: "$nk"
|
|
383
|
+
$nr: "$nk",
|
|
384
|
+
$ut: "$ut"
|
|
384
385
|
};
|
|
385
386
|
const LITERAL_IDENTIFIERS = new Set([
|
|
386
387
|
"true",
|
|
@@ -434,7 +435,8 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
434
435
|
/**
|
|
435
436
|
* Relative imports get the configured `moduleExtension`; bare specifiers
|
|
436
437
|
* always keep `.as` — they resolve through the package's `exports` map.
|
|
437
|
-
*/ transformFromPath(
|
|
438
|
+
*/ transformFromPath(written) {
|
|
439
|
+
const path = (0, _atscript_core.withoutAsExtension)(written);
|
|
438
440
|
return (0, _atscript_core.isBareSpecifier)(path) ? `${path}.as` : `${path}${this.opts?.moduleExtension ?? ".as"}`;
|
|
439
441
|
}
|
|
440
442
|
/**
|
|
@@ -518,7 +520,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
518
520
|
};
|
|
519
521
|
}
|
|
520
522
|
/** `name`, suffixed (`_1`, `_2`, …) when `taken`; the result is taken from now on. */ allocAlias(name, taken) {
|
|
521
|
-
const alias =
|
|
523
|
+
const alias = require_user_text.uniqueName(name, (n) => taken.has(n), "_");
|
|
522
524
|
taken.add(alias);
|
|
523
525
|
return alias;
|
|
524
526
|
}
|
|
@@ -579,6 +581,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
579
581
|
const text = this.compiles ? this.spliceCompiledChecks(module.text, realm) : module.text;
|
|
580
582
|
if (this.bundledSchemas.length === 0) return text;
|
|
581
583
|
const views = /* @__PURE__ */ new Map();
|
|
584
|
+
const options = jsonSchemaOptionsArg(this.opts);
|
|
582
585
|
const viewOf = (id) => {
|
|
583
586
|
let view = views.get(id);
|
|
584
587
|
if (!view) {
|
|
@@ -586,12 +589,12 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
586
589
|
let accounted = [REALM_FAILED];
|
|
587
590
|
try {
|
|
588
591
|
const type = realm.classOf(this.doc, id);
|
|
589
|
-
schema = JSON.stringify(realm.rt.buildJsonSchema(type));
|
|
592
|
+
schema = JSON.stringify(realm.rt.buildJsonSchema(type, this.opts?.jsonSchemaOptions));
|
|
590
593
|
accounted = this.accountedFor(realm, type);
|
|
591
594
|
} catch {}
|
|
592
595
|
view = {
|
|
593
596
|
schema,
|
|
594
|
-
accounted: accounted.length > 0 ? `, ${JSON.stringify(accounted)}` : ""
|
|
597
|
+
accounted: accounted.length > 0 || options ? `, ${JSON.stringify(accounted)}${options ? `, ${options}` : ""}` : ""
|
|
595
598
|
};
|
|
596
599
|
views.set(id, view);
|
|
597
600
|
}
|
|
@@ -695,7 +698,8 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
695
698
|
*/ renderJsonSchemaMethod(_node) {
|
|
696
699
|
if (this.jsonSchemaMode === "lazy") {
|
|
697
700
|
this.writeln("static toJsonSchema() {");
|
|
698
|
-
|
|
701
|
+
const options = jsonSchemaOptionsArg(this.opts);
|
|
702
|
+
this.indent().writeln(`return ${this.util("lazyJsonSchema")}(this${options ? `, ${options}` : ""})`);
|
|
699
703
|
this.unindent();
|
|
700
704
|
this.writeln("}");
|
|
701
705
|
} else {
|
|
@@ -836,12 +840,16 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
836
840
|
if (dotIdx === -1) return `() => ${this.annotationTypeName(text, origin)}`;
|
|
837
841
|
return `{ type: () => ${this.annotationTypeName(text.slice(0, dotIdx), origin)}, field: "${escapeQuotes(text.slice(dotIdx + 1))}" }`;
|
|
838
842
|
}
|
|
839
|
-
emitArgValue(aSpec, argToken, origin) {
|
|
843
|
+
emitArgValue(aSpec, argToken, origin, mainToken) {
|
|
840
844
|
if (aSpec.type === "ref") return this.emitRefValue(argToken.text, origin);
|
|
841
845
|
if (aSpec.type === "query" && argToken.queryNode) return this.emitQueryTree(argToken.queryNode, origin);
|
|
842
846
|
if (aSpec.type === "expr" && argToken.exprNode) return this.emitExprNode(argToken.exprNode.expression, origin);
|
|
843
847
|
if (aSpec.type === "order" && argToken.orderNode) return `[${argToken.orderNode.items.map((item) => `{ ref: ${this.emitQueryFieldRef(item.ref, origin)}${item.desc ? ", desc: true" : ""} }`).join(", ")}]`;
|
|
844
|
-
return aSpec.type === "string" ? `"${escapeQuotes(argToken.text)}"` : argToken.text;
|
|
848
|
+
return aSpec.type === "string" || aSpec.type === "value" && (argToken.type === "text" || argToken.type === "number" && this.decimalField(mainToken, origin)) ? `"${escapeQuotes(argToken.text)}"` : argToken.text;
|
|
849
|
+
}
|
|
850
|
+
/** Whether annotation `mainToken` is written on a `decimal` field (not one that holds numbers). */ decimalField(mainToken, origin) {
|
|
851
|
+
const kinds = (0, _atscript_core.annotatedValueKinds)(mainToken, origin ?? this.doc);
|
|
852
|
+
return !!kinds?.has("decimal") && !kinds.has("number") && !kinds.has("unknown");
|
|
845
853
|
}
|
|
846
854
|
emitExprNode(node, origin) {
|
|
847
855
|
switch (node.entity) {
|
|
@@ -921,13 +929,13 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
921
929
|
targetValue = "{ ";
|
|
922
930
|
let i = 0;
|
|
923
931
|
for (const aSpec of spec.arguments) {
|
|
924
|
-
if (an.args[i]) targetValue += `${wrapProp(aSpec.name)}: ${this.emitArgValue(aSpec, an.args[i], origin)}${i === length - 1 ? "" : ", "} `;
|
|
932
|
+
if (an.args[i]) targetValue += `${wrapProp(aSpec.name)}: ${this.emitArgValue(aSpec, an.args[i], origin, an.token)}${i === length - 1 ? "" : ", "} `;
|
|
925
933
|
i++;
|
|
926
934
|
}
|
|
927
935
|
targetValue += "}";
|
|
928
936
|
} else if (an.args[0]) {
|
|
929
937
|
const aSpec = spec.arguments[0];
|
|
930
|
-
targetValue = this.emitArgValue(aSpec, an.args[0], origin);
|
|
938
|
+
targetValue = this.emitArgValue(aSpec, an.args[0], origin, an.token);
|
|
931
939
|
}
|
|
932
940
|
}
|
|
933
941
|
} else {
|
|
@@ -1282,7 +1290,7 @@ const flagKeysMemo = /* @__PURE__ */ new WeakMap();
|
|
|
1282
1290
|
* `let`, exported with an exported declaration (another module's merge may have its fields as
|
|
1283
1291
|
* contributors: a merge operand is flattened into its operands).
|
|
1284
1292
|
*/ function inPlaceName(d, steps) {
|
|
1285
|
-
return
|
|
1293
|
+
return require_user_text.uniqueName(`$i_${d.name}_${steps.map((s) => s.replace(/[^\w$]/g, (c) => c === "&" ? "" : "_")).join("_")}`, (n) => d.doc.registry.definitions.has(n) || d.doc.importedDefs.has(n), "_");
|
|
1286
1294
|
}
|
|
1287
1295
|
const hiddenExports = /* @__PURE__ */ new WeakMap();
|
|
1288
1296
|
/**
|
|
@@ -1319,7 +1327,7 @@ const hiddenExports = /* @__PURE__ */ new WeakMap();
|
|
|
1319
1327
|
def: def.base
|
|
1320
1328
|
});
|
|
1321
1329
|
if (def.kind === "merge" || def.kind === "umerge") {
|
|
1322
|
-
const name =
|
|
1330
|
+
const name = require_user_text.uniqueName(`$h_${[decl.name, ...steps.map((s) => s.replace(/[^\w$]/g, "_"))].join("_")}`, (n) => names.has(n) || doc.registry.definitions.has(n) || doc.importedDefs.has(n), "_");
|
|
1323
1331
|
names.add(name);
|
|
1324
1332
|
out.set(def.key, name);
|
|
1325
1333
|
}
|
|
@@ -1430,7 +1438,13 @@ var JsNextRenderer = class extends JsRenderer {
|
|
|
1430
1438
|
}
|
|
1431
1439
|
/** `$a(P.metadata, …)` writes of a class's own annotations (after its `$alias`). */ annotateCalls(name, node) {
|
|
1432
1440
|
let out = "";
|
|
1441
|
+
const resets = (node.annotations ?? []).filter((an) => an.name === _atscript_core.ANNOTATE_RESET);
|
|
1442
|
+
if (resets.length > 0) {
|
|
1443
|
+
const data = this.blockAnnotationsData(node, resets);
|
|
1444
|
+
out += `\n${this.util("$blk")}(null, -1, ${name}, [[[${data.join(", ")}], []]])`;
|
|
1445
|
+
}
|
|
1433
1446
|
for (const an of node.annotations ?? []) {
|
|
1447
|
+
if (an.name === _atscript_core.ANNOTATE_RESET) continue;
|
|
1434
1448
|
const { value, multiple } = this.computeAnnotationValue(node, an);
|
|
1435
1449
|
out += `\n${this.util("annotate")}(${name}.metadata, ${str(an.name)}, ${value}${multiple ? ", true" : ""})`;
|
|
1436
1450
|
}
|
|
@@ -2371,19 +2385,21 @@ var JsNextRenderer = class extends JsRenderer {
|
|
|
2371
2385
|
* The statement needs what the paths go through built (`$f` guards, needs).
|
|
2372
2386
|
*/ entriesData(b, reads) {
|
|
2373
2387
|
const entries = [];
|
|
2374
|
-
|
|
2375
|
-
const { value, multiple } = this.computeAnnotationValue(node, an);
|
|
2376
|
-
return `[${str(an.name)}, ${value}${multiple ? ", 1" : ""}]`;
|
|
2377
|
-
});
|
|
2378
|
-
if (b.rank && b.node.annotations?.length) entries.push(`[[${annotationsOf(b.node).join(", ")}], []]`);
|
|
2388
|
+
if (b.rank && b.node.annotations?.length) entries.push(`[[${this.blockAnnotationsData(b.node).join(", ")}], []]`);
|
|
2379
2389
|
for (const en of b.entries) {
|
|
2380
2390
|
if (en.error || en.paths.length === 0) continue;
|
|
2381
2391
|
const copy = reads !== b.target;
|
|
2382
2392
|
const paths = en.paths.map((p, i) => `[${stepsText(this.stepsData(reads, p, en.kinds[i], false, copy))}]`);
|
|
2383
|
-
entries.push(`[[${
|
|
2393
|
+
entries.push(`[[${this.blockAnnotationsData(en.ref).join(", ")}], ${paths.join(", ")}]`);
|
|
2384
2394
|
}
|
|
2385
2395
|
return entries.length > 0 ? `[\n ${entries.join(",\n ")},\n]` : "";
|
|
2386
2396
|
}
|
|
2397
|
+
/** Annotations `anns` of `node` as `$blk` entry data: `[name, value]`, `, 1` for a `multiple`. */ blockAnnotationsData(node, anns = node.annotations ?? []) {
|
|
2398
|
+
return anns.map((an) => {
|
|
2399
|
+
const { value, multiple } = this.computeAnnotationValue(node, an);
|
|
2400
|
+
return `[${str(an.name)}, ${value}${multiple ? ", 1" : ""}]`;
|
|
2401
|
+
});
|
|
2402
|
+
}
|
|
2387
2403
|
/**
|
|
2388
2404
|
* The runtime `[step, kind]`s of model path `p` of `reads` (an `as` copy's: its target's
|
|
2389
2405
|
* structure), and its needs: the classes it walks are built first (a shell def clones a built
|
|
@@ -2610,7 +2626,7 @@ var JsNextRenderer = class extends JsRenderer {
|
|
|
2610
2626
|
};
|
|
2611
2627
|
this.shellIndex.set(n, x);
|
|
2612
2628
|
const names = p.map(([s]) => s);
|
|
2613
|
-
for (const j of
|
|
2629
|
+
for (const j of require_user_text.stageEnds(p)) {
|
|
2614
2630
|
const kind = p[j][1];
|
|
2615
2631
|
const at = pathKey(names.slice(0, j + 1));
|
|
2616
2632
|
if (kind !== 0 && (j === p.length - 1 || !x.at.has(at))) {
|
|
@@ -2871,14 +2887,35 @@ var JsNextRenderer = class extends JsRenderer {
|
|
|
2871
2887
|
const rank = blocks[0].rank;
|
|
2872
2888
|
return `const ${R} = [${rank.depth}, ${str(rank.id)}]`;
|
|
2873
2889
|
}
|
|
2874
|
-
/**
|
|
2890
|
+
/**
|
|
2891
|
+
* The registries (`$ak`, `$nr`: how layers read those keys; `$ut`: the keys holding user-facing
|
|
2892
|
+
* text), before any statement.
|
|
2893
|
+
*/ registries() {
|
|
2875
2894
|
const { append, notReferred } = flagKeys(this.doc.config.annotations);
|
|
2876
2895
|
const out = [];
|
|
2877
2896
|
if (append.length > 0) out.push(`${this.util("$ak")}(${append.map(str).join(", ")})`);
|
|
2878
2897
|
if (notReferred.length > 0) out.push(`${this.util("$nr")}(${notReferred.map(str).join(", ")})`);
|
|
2898
|
+
const userText = this.moduleUserText();
|
|
2899
|
+
if (userText.length > 0) out.push(`${this.util("$ut")}(${userText.map(str).join(", ")})`);
|
|
2879
2900
|
return out;
|
|
2880
2901
|
}
|
|
2881
2902
|
/**
|
|
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`).
|
|
2906
|
+
*/ moduleUserText() {
|
|
2907
|
+
const out = /* @__PURE__ */ new Set();
|
|
2908
|
+
for (const name of new Set(this.doc.annotations.map((a) => a.name))) {
|
|
2909
|
+
if (require_user_text.CORE_USER_TEXT.has(name)) continue;
|
|
2910
|
+
const spec = this.doc.resolveAnnotation(name);
|
|
2911
|
+
const fields = spec && (0, _atscript_core.userTextFields)(spec);
|
|
2912
|
+
if (fields === true) out.add(name);
|
|
2913
|
+
else if (fields === "expression") out.add(`${name}()`);
|
|
2914
|
+
else if (fields) for (const f of fields) out.add(`${name}:${f}`);
|
|
2915
|
+
}
|
|
2916
|
+
return [...out].sort();
|
|
2917
|
+
}
|
|
2918
|
+
/**
|
|
2882
2919
|
* The runtime steps of a model path: a `X | null` union the model passes through is a union
|
|
2883
2920
|
* node at runtime (its non-null item: `|i`, own when the union's holder is, else shelled).
|
|
2884
2921
|
*/ runtimeSteps(d, steps, kinds, { copy = false, items: withItems = false, pure = false } = {}) {
|
|
@@ -3658,11 +3695,14 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
|
|
|
3658
3695
|
const written = only?.prop.getDefinition();
|
|
3659
3696
|
return written && !((0, _atscript_core.isGroup)(only.def) && only.def !== written) ? written : only?.def;
|
|
3660
3697
|
}
|
|
3698
|
+
/** The TypeScript type of a final primitive type (`any`, the `unknown` primitive: `unknown`). */ function finalTsType(t) {
|
|
3699
|
+
return t === "void" ? "undefined" : t === "any" ? "unknown" : t;
|
|
3700
|
+
}
|
|
3661
3701
|
function renderPrimitiveTypeDef(def) {
|
|
3662
3702
|
if (!def) return "unknown";
|
|
3663
|
-
if (typeof def === "string") return def
|
|
3703
|
+
if (typeof def === "string") return finalTsType(def);
|
|
3664
3704
|
switch (def.kind) {
|
|
3665
|
-
case "final": return def.value
|
|
3705
|
+
case "final": return finalTsType(def.value);
|
|
3666
3706
|
case "union": return def.items.map(renderPrimitiveTypeDef).join(" | ");
|
|
3667
3707
|
case "intersection": return def.items.map((item) => typeof item === "object" && item.kind === "union" ? `(${renderPrimitiveTypeDef(item)})` : renderPrimitiveTypeDef(item)).join(" & ");
|
|
3668
3708
|
case "tuple": return `[${def.items.map(renderPrimitiveTypeDef).join(", ")}]`;
|
|
@@ -3698,6 +3738,17 @@ const JS_INTERPRETED_FORMAT = "js:interpreted";
|
|
|
3698
3738
|
if (opts?.jsonSchema !== void 0) return opts.jsonSchema;
|
|
3699
3739
|
return false;
|
|
3700
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
|
+
}
|
|
3701
3752
|
/** A generated JS module of `doc`, checked to load in a build (see `assertLoads`). */ function renderJs(doc, opts, mode, loadCheck) {
|
|
3702
3753
|
const content = rendererOf(doc, opts, mode).render();
|
|
3703
3754
|
assertLoads(doc, opts, mode, loadCheck);
|
|
@@ -3724,8 +3775,11 @@ const tsPlugin = (opts) => {
|
|
|
3724
3775
|
for (const [key, val] of Object.entries(annotations)) {
|
|
3725
3776
|
const multiple = val.multiple;
|
|
3726
3777
|
const typeLine = Array.from(val.types).map((t) => {
|
|
3727
|
-
if (t.type === "object" && "props" in t)
|
|
3728
|
-
|
|
3778
|
+
if (t.type === "object" && "props" in t) {
|
|
3779
|
+
const props = Object.entries(t.props);
|
|
3780
|
+
const object = `{ ${props.map(([k, v]) => `${wrapProp(k)}${v.optional ? "?" : ""}: ${v.type}`).join(", ")} }`;
|
|
3781
|
+
return props.length === 1 && props[0][1].optional ? `${object} | true` : object;
|
|
3782
|
+
} else return "optional" in t && t.optional ? `${t.type} | true` : t.type;
|
|
3729
3783
|
}).join(" | ");
|
|
3730
3784
|
rendered.push(`${wrapProp(key)}: ${multiple ? "(" : ""}${typeLine}${multiple ? ")[]" : ""}`);
|
|
3731
3785
|
}
|