@atscript/typescript 0.1.99 → 0.1.101
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 +85 -48
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{json-schema-Cka8ETxe.mjs → json-schema-BogNt-9w.mjs} +4 -0
- package/dist/{json-schema-CZo482nY.cjs → json-schema-CtQ_rzJn.cjs} +4 -0
- package/dist/{plugin-BG_encHp.cjs → plugin-Cu6Nez-H.cjs} +82 -49
- package/dist/{plugin-CVvPRB8q.mjs → plugin-DG3jlbSN.mjs} +82 -49
- package/dist/test-utils.cjs +1 -1
- package/dist/test-utils.mjs +1 -1
- package/dist/utils.cjs +228 -47
- package/dist/utils.d.ts +18 -0
- package/dist/utils.mjs +228 -47
- package/package.json +2 -2
package/dist/cli.cjs
CHANGED
|
@@ -993,6 +993,10 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
993
993
|
this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
|
|
994
994
|
return false;
|
|
995
995
|
}
|
|
996
|
+
if (!Number.isFinite(value)) {
|
|
997
|
+
this.error(`Expected finite number, got ${value}`);
|
|
998
|
+
return false;
|
|
999
|
+
}
|
|
996
1000
|
return this.validateNumber(def, value);
|
|
997
1001
|
case "boolean":
|
|
998
1002
|
if (typeOfValue !== def.type.designType) {
|
|
@@ -1603,6 +1607,7 @@ const QUERY_OP_MAP = {
|
|
|
1603
1607
|
"exists": "$exists",
|
|
1604
1608
|
"not exists": "$exists"
|
|
1605
1609
|
};
|
|
1610
|
+
const SYNTH_IMPORTS_MARKER = "/*@@atscript-synth-imports@@*/";
|
|
1606
1611
|
var JsRenderer = class extends BaseRenderer {
|
|
1607
1612
|
/**
|
|
1608
1613
|
* Relative imports get the configured `moduleExtension`; bare specifiers
|
|
@@ -1660,32 +1665,56 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1660
1665
|
this._synthComputed = true;
|
|
1661
1666
|
const refToOriginDoc = this.getExtendsRefs();
|
|
1662
1667
|
if (refToOriginDoc.size === 0) return;
|
|
1663
|
-
const taken = new Set(this.doc.registry.definitions.keys());
|
|
1664
|
-
const allocated = /* @__PURE__ */ new Map();
|
|
1665
|
-
const synthPathCache = /* @__PURE__ */ new Map();
|
|
1666
1668
|
for (const [refNode, originDoc] of refToOriginDoc) {
|
|
1667
1669
|
const refId = refNode.id;
|
|
1668
1670
|
if (!refId) continue;
|
|
1669
1671
|
const ownerInfo = this.resolveOwner(originDoc, refId);
|
|
1670
1672
|
if (!ownerInfo?.doc || ownerInfo.doc === this.doc) continue;
|
|
1671
1673
|
if (ownerInfo.node && (0, _atscript_core.isPrimitive)(ownerInfo.node)) continue;
|
|
1672
|
-
const
|
|
1673
|
-
|
|
1674
|
-
if (synthPathCache.has(ownerDoc)) synthPath = synthPathCache.get(ownerDoc);
|
|
1675
|
-
else {
|
|
1676
|
-
synthPath = this.computeSynthPath(ownerDoc);
|
|
1677
|
-
synthPathCache.set(ownerDoc, synthPath);
|
|
1678
|
-
}
|
|
1679
|
-
if (!synthPath) continue;
|
|
1680
|
-
const allocKey = `${synthPath}::${refId}`;
|
|
1681
|
-
let info = allocated.get(allocKey);
|
|
1682
|
-
if (!info) {
|
|
1683
|
-
info = this.allocateSynthInfo(refId, synthPath, ownerInfo, taken);
|
|
1684
|
-
allocated.set(allocKey, info);
|
|
1685
|
-
}
|
|
1674
|
+
const info = this.synthBinding(refId, ownerInfo);
|
|
1675
|
+
if (!info) continue;
|
|
1686
1676
|
this._refSynthInfo.set(refNode, info);
|
|
1687
1677
|
}
|
|
1688
1678
|
}
|
|
1679
|
+
getSynthTaken() {
|
|
1680
|
+
if (!this._synthTaken) {
|
|
1681
|
+
this._synthTaken = new Set(this.doc.registry.definitions.keys());
|
|
1682
|
+
for (const name of this.doc.importedDefs.keys()) this._synthTaken.add(name);
|
|
1683
|
+
}
|
|
1684
|
+
return this._synthTaken;
|
|
1685
|
+
}
|
|
1686
|
+
getSynthPath(ownerDoc) {
|
|
1687
|
+
if (!this._synthPathCache.has(ownerDoc)) this._synthPathCache.set(ownerDoc, this.computeSynthPath(ownerDoc));
|
|
1688
|
+
return this._synthPathCache.get(ownerDoc);
|
|
1689
|
+
}
|
|
1690
|
+
synthBinding(name, ownerInfo) {
|
|
1691
|
+
const synthPath = this.getSynthPath(ownerInfo.doc);
|
|
1692
|
+
if (!synthPath) return;
|
|
1693
|
+
const allocKey = `${synthPath}::${name}`;
|
|
1694
|
+
let info = this._synthAllocated.get(allocKey);
|
|
1695
|
+
if (!info) {
|
|
1696
|
+
info = this.allocateSynthInfo(name, synthPath, ownerInfo, this.getSynthTaken());
|
|
1697
|
+
this._synthAllocated.set(allocKey, info);
|
|
1698
|
+
}
|
|
1699
|
+
return info;
|
|
1700
|
+
}
|
|
1701
|
+
/**
|
|
1702
|
+
* Binding for a type name used by an annotation argument (`ref` / qualified
|
|
1703
|
+
* query field refs). An annotation inherited from another document (chain ref
|
|
1704
|
+
* or `extends`) carries names that only that document imports, so when the
|
|
1705
|
+
* name does not resolve here to the same declaration, a synthesized import is
|
|
1706
|
+
* allocated (lazily, spliced into the header by `render()`).
|
|
1707
|
+
*/ annotationTypeName(typeName, origin) {
|
|
1708
|
+
if (!origin || origin === this.doc) return typeName;
|
|
1709
|
+
const theirs = this.resolveOwner(origin, typeName);
|
|
1710
|
+
if (!theirs?.doc || !theirs.node || theirs.doc === this.doc || (0, _atscript_core.isPrimitive)(theirs.node)) return typeName;
|
|
1711
|
+
const mine = this.resolveOwner(this.doc, typeName);
|
|
1712
|
+
if (mine?.doc === theirs.doc && mine.node === theirs.node) return typeName;
|
|
1713
|
+
return this.synthBinding(typeName, {
|
|
1714
|
+
doc: theirs.doc,
|
|
1715
|
+
node: theirs.node
|
|
1716
|
+
})?.alias ?? typeName;
|
|
1717
|
+
}
|
|
1689
1718
|
resolveOwner(fromDoc, refId) {
|
|
1690
1719
|
const key = `${fromDoc.id}::${refId}`;
|
|
1691
1720
|
let v = this._ownerCache.get(key);
|
|
@@ -1759,10 +1788,17 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1759
1788
|
if (jsonSchemaMode === false) imports.push("throwFeatureDisabled as $d");
|
|
1760
1789
|
this.writeln(`import { ${imports.join(", ")} } from "@atscript/typescript/utils"`);
|
|
1761
1790
|
this.computeSynthesizedImports();
|
|
1791
|
+
this.writeln(SYNTH_IMPORTS_MARKER);
|
|
1792
|
+
}
|
|
1793
|
+
render() {
|
|
1794
|
+
const out = super.render();
|
|
1795
|
+
const lines = [];
|
|
1762
1796
|
for (const [synthPath, names] of this._synthImports) {
|
|
1763
1797
|
const list = names.map((n) => n.alias === n.name ? n.name : `${n.name} as ${n.alias}`).join(", ");
|
|
1764
|
-
|
|
1798
|
+
lines.push(`import { ${list} } from "${this.transformFromPath(synthPath)}"`);
|
|
1765
1799
|
}
|
|
1800
|
+
const marker = `${SYNTH_IMPORTS_MARKER}\n`;
|
|
1801
|
+
return out.replace(marker, () => lines.length > 0 ? `${lines.join("\n")}\n` : "");
|
|
1766
1802
|
}
|
|
1767
1803
|
buildAdHocMap(annotateNodes) {
|
|
1768
1804
|
const map = /* @__PURE__ */ new Map();
|
|
@@ -2234,53 +2270,53 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2234
2270
|
if (multiple) this.writeln(`.annotate("${escapeQuotes(an.name)}", ${value}, true)`);
|
|
2235
2271
|
else this.writeln(`.annotate("${escapeQuotes(an.name)}", ${value})`);
|
|
2236
2272
|
}
|
|
2237
|
-
emitRefValue(text) {
|
|
2273
|
+
emitRefValue(text, origin) {
|
|
2238
2274
|
const dotIdx = text.indexOf(".");
|
|
2239
|
-
if (dotIdx === -1) return `() => ${text}`;
|
|
2240
|
-
return `{ type: () => ${text.slice(0, dotIdx)}, field: "${escapeQuotes(text.slice(dotIdx + 1))}" }`;
|
|
2241
|
-
}
|
|
2242
|
-
emitArgValue(aSpec, argToken) {
|
|
2243
|
-
if (aSpec.type === "ref") return this.emitRefValue(argToken.text);
|
|
2244
|
-
if (aSpec.type === "query" && argToken.queryNode) return this.emitQueryTree(argToken.queryNode);
|
|
2245
|
-
if (aSpec.type === "expr" && argToken.exprNode) return this.emitExprNode(argToken.exprNode.expression);
|
|
2246
|
-
if (aSpec.type === "order" && argToken.orderNode) return `[${argToken.orderNode.items.map((item) => `{ ref: ${this.emitQueryFieldRef(item.ref)}${item.desc ? ", desc: true" : ""} }`).join(", ")}]`;
|
|
2275
|
+
if (dotIdx === -1) return `() => ${this.annotationTypeName(text, origin)}`;
|
|
2276
|
+
return `{ type: () => ${this.annotationTypeName(text.slice(0, dotIdx), origin)}, field: "${escapeQuotes(text.slice(dotIdx + 1))}" }`;
|
|
2277
|
+
}
|
|
2278
|
+
emitArgValue(aSpec, argToken, origin) {
|
|
2279
|
+
if (aSpec.type === "ref") return this.emitRefValue(argToken.text, origin);
|
|
2280
|
+
if (aSpec.type === "query" && argToken.queryNode) return this.emitQueryTree(argToken.queryNode, origin);
|
|
2281
|
+
if (aSpec.type === "expr" && argToken.exprNode) return this.emitExprNode(argToken.exprNode.expression, origin);
|
|
2282
|
+
if (aSpec.type === "order" && argToken.orderNode) return `[${argToken.orderNode.items.map((item) => `{ ref: ${this.emitQueryFieldRef(item.ref, origin)}${item.desc ? ", desc: true" : ""} }`).join(", ")}]`;
|
|
2247
2283
|
return aSpec.type === "string" ? `"${escapeQuotes(argToken.text)}"` : argToken.text;
|
|
2248
2284
|
}
|
|
2249
|
-
emitExprNode(node) {
|
|
2285
|
+
emitExprNode(node, origin) {
|
|
2250
2286
|
switch (node.entity) {
|
|
2251
2287
|
case "query-expr-number": return String(node.value);
|
|
2252
2288
|
case "query-expr-binary": {
|
|
2253
2289
|
const { op, left, right } = node;
|
|
2254
|
-
return `{ op: "${op}", args: [${this.emitExprNode(left)}, ${this.emitExprNode(right)}] }`;
|
|
2290
|
+
return `{ op: "${op}", args: [${this.emitExprNode(left, origin)}, ${this.emitExprNode(right, origin)}] }`;
|
|
2255
2291
|
}
|
|
2256
2292
|
case "query-expr-unary": {
|
|
2257
2293
|
const { op, operand } = node;
|
|
2258
|
-
return `{ op: "${op}", args: [${this.emitExprNode(operand)}] }`;
|
|
2294
|
+
return `{ op: "${op}", args: [${this.emitExprNode(operand, origin)}] }`;
|
|
2259
2295
|
}
|
|
2260
2296
|
case "query-expr-call": {
|
|
2261
2297
|
const { fn, args } = node;
|
|
2262
|
-
return `{ op: "${fn}", args: [${args.map((a) => this.emitExprNode(a)).join(", ")}] }`;
|
|
2298
|
+
return `{ op: "${fn}", args: [${args.map((a) => this.emitExprNode(a, origin)).join(", ")}] }`;
|
|
2263
2299
|
}
|
|
2264
|
-
default: return this.emitQueryFieldRef(node);
|
|
2300
|
+
default: return this.emitQueryFieldRef(node, origin);
|
|
2265
2301
|
}
|
|
2266
2302
|
}
|
|
2267
|
-
emitQueryTree(queryNode) {
|
|
2268
|
-
return this.emitQueryExpr(queryNode.expression);
|
|
2303
|
+
emitQueryTree(queryNode, origin) {
|
|
2304
|
+
return this.emitQueryExpr(queryNode.expression, origin);
|
|
2269
2305
|
}
|
|
2270
|
-
emitQueryExpr(node) {
|
|
2271
|
-
if ((0, _atscript_core.isQueryLogical)(node)) return this.emitQueryLogical(node);
|
|
2272
|
-
return this.emitQueryComparison(node);
|
|
2306
|
+
emitQueryExpr(node, origin) {
|
|
2307
|
+
if ((0, _atscript_core.isQueryLogical)(node)) return this.emitQueryLogical(node, origin);
|
|
2308
|
+
return this.emitQueryComparison(node, origin);
|
|
2273
2309
|
}
|
|
2274
|
-
emitQueryLogical(node) {
|
|
2275
|
-
if (node.operator === "not") return `{ "$not": ${this.emitQueryExpr(node.operands[0])} }`;
|
|
2276
|
-
return `{ "${node.operator === "and" ? "$and" : "$or"}": [${node.operands.map((op) => this.emitQueryExpr(op)).join(", ")}] }`;
|
|
2310
|
+
emitQueryLogical(node, origin) {
|
|
2311
|
+
if (node.operator === "not") return `{ "$not": ${this.emitQueryExpr(node.operands[0], origin)} }`;
|
|
2312
|
+
return `{ "${node.operator === "and" ? "$and" : "$or"}": [${node.operands.map((op) => this.emitQueryExpr(op, origin)).join(", ")}] }`;
|
|
2277
2313
|
}
|
|
2278
|
-
emitQueryComparison(node) {
|
|
2279
|
-
const left = this.emitQueryFieldRef(node.left);
|
|
2314
|
+
emitQueryComparison(node, origin) {
|
|
2315
|
+
const left = this.emitQueryFieldRef(node.left, origin);
|
|
2280
2316
|
const mappedOp = QUERY_OP_MAP[node.operator] || node.operator;
|
|
2281
2317
|
const parts = [`left: ${left}`, `op: "${mappedOp}"`];
|
|
2282
2318
|
if (node.right) {
|
|
2283
|
-
if ("fieldRef" in node.right && node.right.fieldRef) parts.push(`right: ${this.emitQueryFieldRef(node.right)}`);
|
|
2319
|
+
if ("fieldRef" in node.right && node.right.fieldRef) parts.push(`right: ${this.emitQueryFieldRef(node.right, origin)}`);
|
|
2284
2320
|
else if ("values" in node.right && node.right.values) {
|
|
2285
2321
|
const values = node.right.values.map((v) => this.emitQueryLiteral(v)).join(", ");
|
|
2286
2322
|
parts.push(`right: [${values}]`);
|
|
@@ -2289,9 +2325,9 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2289
2325
|
else if (node.operator === "not exists") parts.push("right: false");
|
|
2290
2326
|
return `{ ${parts.join(", ")} }`;
|
|
2291
2327
|
}
|
|
2292
|
-
emitQueryFieldRef(node) {
|
|
2328
|
+
emitQueryFieldRef(node, origin) {
|
|
2293
2329
|
const parts = [];
|
|
2294
|
-
if (node.typeRef) parts.push(`type: () => ${node.typeRef.text}`);
|
|
2330
|
+
if (node.typeRef) parts.push(`type: () => ${this.annotationTypeName(node.typeRef.text, origin)}`);
|
|
2295
2331
|
parts.push(`field: "${escapeQuotes(node.fieldRef.text)}"`);
|
|
2296
2332
|
return `{ ${parts.join(", ")} }`;
|
|
2297
2333
|
}
|
|
@@ -2312,6 +2348,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2312
2348
|
}
|
|
2313
2349
|
}
|
|
2314
2350
|
computeAnnotationValue(node, an) {
|
|
2351
|
+
const origin = this.doc.annotationOrigin(an);
|
|
2315
2352
|
const spec = this.doc.resolveAnnotation(an.name);
|
|
2316
2353
|
let targetValue = "true";
|
|
2317
2354
|
let multiple = false;
|
|
@@ -2322,13 +2359,13 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2322
2359
|
targetValue = "{ ";
|
|
2323
2360
|
let i = 0;
|
|
2324
2361
|
for (const aSpec of spec.arguments) {
|
|
2325
|
-
if (an.args[i]) targetValue += `${wrapProp(aSpec.name)}: ${this.emitArgValue(aSpec, an.args[i])}${i === length - 1 ? "" : ", "} `;
|
|
2362
|
+
if (an.args[i]) targetValue += `${wrapProp(aSpec.name)}: ${this.emitArgValue(aSpec, an.args[i], origin)}${i === length - 1 ? "" : ", "} `;
|
|
2326
2363
|
i++;
|
|
2327
2364
|
}
|
|
2328
2365
|
targetValue += "}";
|
|
2329
2366
|
} else {
|
|
2330
2367
|
const aSpec = spec.arguments[0];
|
|
2331
|
-
targetValue = an.args[0] ? this.emitArgValue(aSpec, an.args[0]) : "true";
|
|
2368
|
+
targetValue = an.args[0] ? this.emitArgValue(aSpec, an.args[0], origin) : "true";
|
|
2332
2369
|
}
|
|
2333
2370
|
} else {
|
|
2334
2371
|
multiple = node.countAnnotations(an.name) > 1 || an.args.length > 1;
|
|
@@ -2466,7 +2503,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2466
2503
|
return [];
|
|
2467
2504
|
}
|
|
2468
2505
|
constructor(doc, opts) {
|
|
2469
|
-
super(doc), _define_property$3(this, "opts", void 0), _define_property$3(this, "postAnnotate", void 0), _define_property$3(this, "_adHocAnnotations", void 0), _define_property$3(this, "_propPath", void 0), _define_property$3(this, "typeIds", void 0), _define_property$3(this, "_unusedJs", void 0), _define_property$3(this, "_extendsRefs", void 0), _define_property$3(this, "_refSynthInfo", void 0), _define_property$3(this, "_synthImports", void 0), _define_property$3(this, "_synthComputed", void 0), _define_property$3(this, "_ownerCache", void 0), this.opts = opts, this.postAnnotate = [], this._adHocAnnotations = null, this._propPath = [], this.typeIds = /* @__PURE__ */ new Map(), this._refSynthInfo = /* @__PURE__ */ new Map(), this._synthImports = /* @__PURE__ */ new Map(), this._synthComputed = false, this._ownerCache = /* @__PURE__ */ new Map();
|
|
2506
|
+
super(doc), _define_property$3(this, "opts", void 0), _define_property$3(this, "postAnnotate", void 0), _define_property$3(this, "_adHocAnnotations", void 0), _define_property$3(this, "_propPath", void 0), _define_property$3(this, "typeIds", void 0), _define_property$3(this, "_unusedJs", void 0), _define_property$3(this, "_extendsRefs", void 0), _define_property$3(this, "_refSynthInfo", void 0), _define_property$3(this, "_synthImports", void 0), _define_property$3(this, "_synthComputed", void 0), _define_property$3(this, "_synthTaken", void 0), _define_property$3(this, "_synthAllocated", void 0), _define_property$3(this, "_synthPathCache", void 0), _define_property$3(this, "_ownerCache", void 0), this.opts = opts, this.postAnnotate = [], this._adHocAnnotations = null, this._propPath = [], this.typeIds = /* @__PURE__ */ new Map(), this._refSynthInfo = /* @__PURE__ */ new Map(), this._synthImports = /* @__PURE__ */ new Map(), this._synthComputed = false, this._synthAllocated = /* @__PURE__ */ new Map(), this._synthPathCache = /* @__PURE__ */ new Map(), this._ownerCache = /* @__PURE__ */ new Map();
|
|
2470
2507
|
}
|
|
2471
2508
|
};
|
|
2472
2509
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
2
|
-
const require_plugin = require('./plugin-
|
|
2
|
+
const require_plugin = require('./plugin-Cu6Nez-H.cjs');
|
|
3
3
|
|
|
4
4
|
exports.default = require_plugin.tsPlugin;
|
|
5
5
|
exports.tsPlugin = require_plugin.tsPlugin;
|
package/dist/index.mjs
CHANGED
|
@@ -324,6 +324,10 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
324
324
|
this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
|
|
325
325
|
return false;
|
|
326
326
|
}
|
|
327
|
+
if (!Number.isFinite(value)) {
|
|
328
|
+
this.error(`Expected finite number, got ${value}`);
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
327
331
|
return this.validateNumber(def, value);
|
|
328
332
|
case "boolean":
|
|
329
333
|
if (typeOfValue !== def.type.designType) {
|
|
@@ -325,6 +325,10 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
325
325
|
this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
|
|
326
326
|
return false;
|
|
327
327
|
}
|
|
328
|
+
if (!Number.isFinite(value)) {
|
|
329
|
+
this.error(`Expected finite number, got ${value}`);
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
328
332
|
return this.validateNumber(def, value);
|
|
329
333
|
case "boolean":
|
|
330
334
|
if (typeOfValue !== def.type.designType) {
|
|
@@ -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_json_schema = require('./json-schema-
|
|
28
|
+
const require_json_schema = require('./json-schema-CtQ_rzJn.cjs');
|
|
29
29
|
let path = require("path");
|
|
30
30
|
path = __toESM(path);
|
|
31
31
|
let _atscript_core = require("@atscript/core");
|
|
@@ -686,6 +686,7 @@ const QUERY_OP_MAP = {
|
|
|
686
686
|
"exists": "$exists",
|
|
687
687
|
"not exists": "$exists"
|
|
688
688
|
};
|
|
689
|
+
const SYNTH_IMPORTS_MARKER = "/*@@atscript-synth-imports@@*/";
|
|
689
690
|
var JsRenderer = class extends BaseRenderer {
|
|
690
691
|
/**
|
|
691
692
|
* Relative imports get the configured `moduleExtension`; bare specifiers
|
|
@@ -743,32 +744,56 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
743
744
|
this._synthComputed = true;
|
|
744
745
|
const refToOriginDoc = this.getExtendsRefs();
|
|
745
746
|
if (refToOriginDoc.size === 0) return;
|
|
746
|
-
const taken = new Set(this.doc.registry.definitions.keys());
|
|
747
|
-
const allocated = /* @__PURE__ */ new Map();
|
|
748
|
-
const synthPathCache = /* @__PURE__ */ new Map();
|
|
749
747
|
for (const [refNode, originDoc] of refToOriginDoc) {
|
|
750
748
|
const refId = refNode.id;
|
|
751
749
|
if (!refId) continue;
|
|
752
750
|
const ownerInfo = this.resolveOwner(originDoc, refId);
|
|
753
751
|
if (!ownerInfo?.doc || ownerInfo.doc === this.doc) continue;
|
|
754
752
|
if (ownerInfo.node && (0, _atscript_core.isPrimitive)(ownerInfo.node)) continue;
|
|
755
|
-
const
|
|
756
|
-
|
|
757
|
-
if (synthPathCache.has(ownerDoc)) synthPath = synthPathCache.get(ownerDoc);
|
|
758
|
-
else {
|
|
759
|
-
synthPath = this.computeSynthPath(ownerDoc);
|
|
760
|
-
synthPathCache.set(ownerDoc, synthPath);
|
|
761
|
-
}
|
|
762
|
-
if (!synthPath) continue;
|
|
763
|
-
const allocKey = `${synthPath}::${refId}`;
|
|
764
|
-
let info = allocated.get(allocKey);
|
|
765
|
-
if (!info) {
|
|
766
|
-
info = this.allocateSynthInfo(refId, synthPath, ownerInfo, taken);
|
|
767
|
-
allocated.set(allocKey, info);
|
|
768
|
-
}
|
|
753
|
+
const info = this.synthBinding(refId, ownerInfo);
|
|
754
|
+
if (!info) continue;
|
|
769
755
|
this._refSynthInfo.set(refNode, info);
|
|
770
756
|
}
|
|
771
757
|
}
|
|
758
|
+
getSynthTaken() {
|
|
759
|
+
if (!this._synthTaken) {
|
|
760
|
+
this._synthTaken = new Set(this.doc.registry.definitions.keys());
|
|
761
|
+
for (const name of this.doc.importedDefs.keys()) this._synthTaken.add(name);
|
|
762
|
+
}
|
|
763
|
+
return this._synthTaken;
|
|
764
|
+
}
|
|
765
|
+
getSynthPath(ownerDoc) {
|
|
766
|
+
if (!this._synthPathCache.has(ownerDoc)) this._synthPathCache.set(ownerDoc, this.computeSynthPath(ownerDoc));
|
|
767
|
+
return this._synthPathCache.get(ownerDoc);
|
|
768
|
+
}
|
|
769
|
+
synthBinding(name, ownerInfo) {
|
|
770
|
+
const synthPath = this.getSynthPath(ownerInfo.doc);
|
|
771
|
+
if (!synthPath) return;
|
|
772
|
+
const allocKey = `${synthPath}::${name}`;
|
|
773
|
+
let info = this._synthAllocated.get(allocKey);
|
|
774
|
+
if (!info) {
|
|
775
|
+
info = this.allocateSynthInfo(name, synthPath, ownerInfo, this.getSynthTaken());
|
|
776
|
+
this._synthAllocated.set(allocKey, info);
|
|
777
|
+
}
|
|
778
|
+
return info;
|
|
779
|
+
}
|
|
780
|
+
/**
|
|
781
|
+
* Binding for a type name used by an annotation argument (`ref` / qualified
|
|
782
|
+
* query field refs). An annotation inherited from another document (chain ref
|
|
783
|
+
* or `extends`) carries names that only that document imports, so when the
|
|
784
|
+
* name does not resolve here to the same declaration, a synthesized import is
|
|
785
|
+
* allocated (lazily, spliced into the header by `render()`).
|
|
786
|
+
*/ annotationTypeName(typeName, origin) {
|
|
787
|
+
if (!origin || origin === this.doc) return typeName;
|
|
788
|
+
const theirs = this.resolveOwner(origin, typeName);
|
|
789
|
+
if (!theirs?.doc || !theirs.node || theirs.doc === this.doc || (0, _atscript_core.isPrimitive)(theirs.node)) return typeName;
|
|
790
|
+
const mine = this.resolveOwner(this.doc, typeName);
|
|
791
|
+
if (mine?.doc === theirs.doc && mine.node === theirs.node) return typeName;
|
|
792
|
+
return this.synthBinding(typeName, {
|
|
793
|
+
doc: theirs.doc,
|
|
794
|
+
node: theirs.node
|
|
795
|
+
})?.alias ?? typeName;
|
|
796
|
+
}
|
|
772
797
|
resolveOwner(fromDoc, refId) {
|
|
773
798
|
const key = `${fromDoc.id}::${refId}`;
|
|
774
799
|
let v = this._ownerCache.get(key);
|
|
@@ -842,10 +867,17 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
842
867
|
if (jsonSchemaMode === false) imports.push("throwFeatureDisabled as $d");
|
|
843
868
|
this.writeln(`import { ${imports.join(", ")} } from "@atscript/typescript/utils"`);
|
|
844
869
|
this.computeSynthesizedImports();
|
|
870
|
+
this.writeln(SYNTH_IMPORTS_MARKER);
|
|
871
|
+
}
|
|
872
|
+
render() {
|
|
873
|
+
const out = super.render();
|
|
874
|
+
const lines = [];
|
|
845
875
|
for (const [synthPath, names] of this._synthImports) {
|
|
846
876
|
const list = names.map((n) => n.alias === n.name ? n.name : `${n.name} as ${n.alias}`).join(", ");
|
|
847
|
-
|
|
877
|
+
lines.push(`import { ${list} } from "${this.transformFromPath(synthPath)}"`);
|
|
848
878
|
}
|
|
879
|
+
const marker = `${SYNTH_IMPORTS_MARKER}\n`;
|
|
880
|
+
return out.replace(marker, () => lines.length > 0 ? `${lines.join("\n")}\n` : "");
|
|
849
881
|
}
|
|
850
882
|
buildAdHocMap(annotateNodes) {
|
|
851
883
|
const map = /* @__PURE__ */ new Map();
|
|
@@ -1317,53 +1349,53 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1317
1349
|
if (multiple) this.writeln(`.annotate("${escapeQuotes(an.name)}", ${value}, true)`);
|
|
1318
1350
|
else this.writeln(`.annotate("${escapeQuotes(an.name)}", ${value})`);
|
|
1319
1351
|
}
|
|
1320
|
-
emitRefValue(text) {
|
|
1352
|
+
emitRefValue(text, origin) {
|
|
1321
1353
|
const dotIdx = text.indexOf(".");
|
|
1322
|
-
if (dotIdx === -1) return `() => ${text}`;
|
|
1323
|
-
return `{ type: () => ${text.slice(0, dotIdx)}, field: "${escapeQuotes(text.slice(dotIdx + 1))}" }`;
|
|
1324
|
-
}
|
|
1325
|
-
emitArgValue(aSpec, argToken) {
|
|
1326
|
-
if (aSpec.type === "ref") return this.emitRefValue(argToken.text);
|
|
1327
|
-
if (aSpec.type === "query" && argToken.queryNode) return this.emitQueryTree(argToken.queryNode);
|
|
1328
|
-
if (aSpec.type === "expr" && argToken.exprNode) return this.emitExprNode(argToken.exprNode.expression);
|
|
1329
|
-
if (aSpec.type === "order" && argToken.orderNode) return `[${argToken.orderNode.items.map((item) => `{ ref: ${this.emitQueryFieldRef(item.ref)}${item.desc ? ", desc: true" : ""} }`).join(", ")}]`;
|
|
1354
|
+
if (dotIdx === -1) return `() => ${this.annotationTypeName(text, origin)}`;
|
|
1355
|
+
return `{ type: () => ${this.annotationTypeName(text.slice(0, dotIdx), origin)}, field: "${escapeQuotes(text.slice(dotIdx + 1))}" }`;
|
|
1356
|
+
}
|
|
1357
|
+
emitArgValue(aSpec, argToken, origin) {
|
|
1358
|
+
if (aSpec.type === "ref") return this.emitRefValue(argToken.text, origin);
|
|
1359
|
+
if (aSpec.type === "query" && argToken.queryNode) return this.emitQueryTree(argToken.queryNode, origin);
|
|
1360
|
+
if (aSpec.type === "expr" && argToken.exprNode) return this.emitExprNode(argToken.exprNode.expression, origin);
|
|
1361
|
+
if (aSpec.type === "order" && argToken.orderNode) return `[${argToken.orderNode.items.map((item) => `{ ref: ${this.emitQueryFieldRef(item.ref, origin)}${item.desc ? ", desc: true" : ""} }`).join(", ")}]`;
|
|
1330
1362
|
return aSpec.type === "string" ? `"${escapeQuotes(argToken.text)}"` : argToken.text;
|
|
1331
1363
|
}
|
|
1332
|
-
emitExprNode(node) {
|
|
1364
|
+
emitExprNode(node, origin) {
|
|
1333
1365
|
switch (node.entity) {
|
|
1334
1366
|
case "query-expr-number": return String(node.value);
|
|
1335
1367
|
case "query-expr-binary": {
|
|
1336
1368
|
const { op, left, right } = node;
|
|
1337
|
-
return `{ op: "${op}", args: [${this.emitExprNode(left)}, ${this.emitExprNode(right)}] }`;
|
|
1369
|
+
return `{ op: "${op}", args: [${this.emitExprNode(left, origin)}, ${this.emitExprNode(right, origin)}] }`;
|
|
1338
1370
|
}
|
|
1339
1371
|
case "query-expr-unary": {
|
|
1340
1372
|
const { op, operand } = node;
|
|
1341
|
-
return `{ op: "${op}", args: [${this.emitExprNode(operand)}] }`;
|
|
1373
|
+
return `{ op: "${op}", args: [${this.emitExprNode(operand, origin)}] }`;
|
|
1342
1374
|
}
|
|
1343
1375
|
case "query-expr-call": {
|
|
1344
1376
|
const { fn, args } = node;
|
|
1345
|
-
return `{ op: "${fn}", args: [${args.map((a) => this.emitExprNode(a)).join(", ")}] }`;
|
|
1377
|
+
return `{ op: "${fn}", args: [${args.map((a) => this.emitExprNode(a, origin)).join(", ")}] }`;
|
|
1346
1378
|
}
|
|
1347
|
-
default: return this.emitQueryFieldRef(node);
|
|
1379
|
+
default: return this.emitQueryFieldRef(node, origin);
|
|
1348
1380
|
}
|
|
1349
1381
|
}
|
|
1350
|
-
emitQueryTree(queryNode) {
|
|
1351
|
-
return this.emitQueryExpr(queryNode.expression);
|
|
1382
|
+
emitQueryTree(queryNode, origin) {
|
|
1383
|
+
return this.emitQueryExpr(queryNode.expression, origin);
|
|
1352
1384
|
}
|
|
1353
|
-
emitQueryExpr(node) {
|
|
1354
|
-
if ((0, _atscript_core.isQueryLogical)(node)) return this.emitQueryLogical(node);
|
|
1355
|
-
return this.emitQueryComparison(node);
|
|
1385
|
+
emitQueryExpr(node, origin) {
|
|
1386
|
+
if ((0, _atscript_core.isQueryLogical)(node)) return this.emitQueryLogical(node, origin);
|
|
1387
|
+
return this.emitQueryComparison(node, origin);
|
|
1356
1388
|
}
|
|
1357
|
-
emitQueryLogical(node) {
|
|
1358
|
-
if (node.operator === "not") return `{ "$not": ${this.emitQueryExpr(node.operands[0])} }`;
|
|
1359
|
-
return `{ "${node.operator === "and" ? "$and" : "$or"}": [${node.operands.map((op) => this.emitQueryExpr(op)).join(", ")}] }`;
|
|
1389
|
+
emitQueryLogical(node, origin) {
|
|
1390
|
+
if (node.operator === "not") return `{ "$not": ${this.emitQueryExpr(node.operands[0], origin)} }`;
|
|
1391
|
+
return `{ "${node.operator === "and" ? "$and" : "$or"}": [${node.operands.map((op) => this.emitQueryExpr(op, origin)).join(", ")}] }`;
|
|
1360
1392
|
}
|
|
1361
|
-
emitQueryComparison(node) {
|
|
1362
|
-
const left = this.emitQueryFieldRef(node.left);
|
|
1393
|
+
emitQueryComparison(node, origin) {
|
|
1394
|
+
const left = this.emitQueryFieldRef(node.left, origin);
|
|
1363
1395
|
const mappedOp = QUERY_OP_MAP[node.operator] || node.operator;
|
|
1364
1396
|
const parts = [`left: ${left}`, `op: "${mappedOp}"`];
|
|
1365
1397
|
if (node.right) {
|
|
1366
|
-
if ("fieldRef" in node.right && node.right.fieldRef) parts.push(`right: ${this.emitQueryFieldRef(node.right)}`);
|
|
1398
|
+
if ("fieldRef" in node.right && node.right.fieldRef) parts.push(`right: ${this.emitQueryFieldRef(node.right, origin)}`);
|
|
1367
1399
|
else if ("values" in node.right && node.right.values) {
|
|
1368
1400
|
const values = node.right.values.map((v) => this.emitQueryLiteral(v)).join(", ");
|
|
1369
1401
|
parts.push(`right: [${values}]`);
|
|
@@ -1372,9 +1404,9 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1372
1404
|
else if (node.operator === "not exists") parts.push("right: false");
|
|
1373
1405
|
return `{ ${parts.join(", ")} }`;
|
|
1374
1406
|
}
|
|
1375
|
-
emitQueryFieldRef(node) {
|
|
1407
|
+
emitQueryFieldRef(node, origin) {
|
|
1376
1408
|
const parts = [];
|
|
1377
|
-
if (node.typeRef) parts.push(`type: () => ${node.typeRef.text}`);
|
|
1409
|
+
if (node.typeRef) parts.push(`type: () => ${this.annotationTypeName(node.typeRef.text, origin)}`);
|
|
1378
1410
|
parts.push(`field: "${escapeQuotes(node.fieldRef.text)}"`);
|
|
1379
1411
|
return `{ ${parts.join(", ")} }`;
|
|
1380
1412
|
}
|
|
@@ -1395,6 +1427,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1395
1427
|
}
|
|
1396
1428
|
}
|
|
1397
1429
|
computeAnnotationValue(node, an) {
|
|
1430
|
+
const origin = this.doc.annotationOrigin(an);
|
|
1398
1431
|
const spec = this.doc.resolveAnnotation(an.name);
|
|
1399
1432
|
let targetValue = "true";
|
|
1400
1433
|
let multiple = false;
|
|
@@ -1405,13 +1438,13 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1405
1438
|
targetValue = "{ ";
|
|
1406
1439
|
let i = 0;
|
|
1407
1440
|
for (const aSpec of spec.arguments) {
|
|
1408
|
-
if (an.args[i]) targetValue += `${wrapProp(aSpec.name)}: ${this.emitArgValue(aSpec, an.args[i])}${i === length - 1 ? "" : ", "} `;
|
|
1441
|
+
if (an.args[i]) targetValue += `${wrapProp(aSpec.name)}: ${this.emitArgValue(aSpec, an.args[i], origin)}${i === length - 1 ? "" : ", "} `;
|
|
1409
1442
|
i++;
|
|
1410
1443
|
}
|
|
1411
1444
|
targetValue += "}";
|
|
1412
1445
|
} else {
|
|
1413
1446
|
const aSpec = spec.arguments[0];
|
|
1414
|
-
targetValue = an.args[0] ? this.emitArgValue(aSpec, an.args[0]) : "true";
|
|
1447
|
+
targetValue = an.args[0] ? this.emitArgValue(aSpec, an.args[0], origin) : "true";
|
|
1415
1448
|
}
|
|
1416
1449
|
} else {
|
|
1417
1450
|
multiple = node.countAnnotations(an.name) > 1 || an.args.length > 1;
|
|
@@ -1549,7 +1582,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1549
1582
|
return [];
|
|
1550
1583
|
}
|
|
1551
1584
|
constructor(doc, opts) {
|
|
1552
|
-
super(doc), _define_property(this, "opts", void 0), _define_property(this, "postAnnotate", void 0), _define_property(this, "_adHocAnnotations", void 0), _define_property(this, "_propPath", void 0), _define_property(this, "typeIds", void 0), _define_property(this, "_unusedJs", void 0), _define_property(this, "_extendsRefs", void 0), _define_property(this, "_refSynthInfo", void 0), _define_property(this, "_synthImports", void 0), _define_property(this, "_synthComputed", void 0), _define_property(this, "_ownerCache", void 0), this.opts = opts, this.postAnnotate = [], this._adHocAnnotations = null, this._propPath = [], this.typeIds = /* @__PURE__ */ new Map(), this._refSynthInfo = /* @__PURE__ */ new Map(), this._synthImports = /* @__PURE__ */ new Map(), this._synthComputed = false, this._ownerCache = /* @__PURE__ */ new Map();
|
|
1585
|
+
super(doc), _define_property(this, "opts", void 0), _define_property(this, "postAnnotate", void 0), _define_property(this, "_adHocAnnotations", void 0), _define_property(this, "_propPath", void 0), _define_property(this, "typeIds", void 0), _define_property(this, "_unusedJs", void 0), _define_property(this, "_extendsRefs", void 0), _define_property(this, "_refSynthInfo", void 0), _define_property(this, "_synthImports", void 0), _define_property(this, "_synthComputed", void 0), _define_property(this, "_synthTaken", void 0), _define_property(this, "_synthAllocated", void 0), _define_property(this, "_synthPathCache", void 0), _define_property(this, "_ownerCache", void 0), this.opts = opts, this.postAnnotate = [], this._adHocAnnotations = null, this._propPath = [], this.typeIds = /* @__PURE__ */ new Map(), this._refSynthInfo = /* @__PURE__ */ new Map(), this._synthImports = /* @__PURE__ */ new Map(), this._synthComputed = false, this._synthAllocated = /* @__PURE__ */ new Map(), this._synthPathCache = /* @__PURE__ */ new Map(), this._ownerCache = /* @__PURE__ */ new Map();
|
|
1553
1586
|
}
|
|
1554
1587
|
};
|
|
1555
1588
|
|