@atscript/core 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/index.cjs +399 -59
- package/dist/index.d.ts +86 -7
- package/dist/index.mjs +393 -59
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -664,6 +664,17 @@ const text$1 = (val) => new Token({
|
|
|
664
664
|
return specifier[0] !== "." && specifier[0] !== "/";
|
|
665
665
|
}
|
|
666
666
|
/**
|
|
667
|
+
* The import path of an `.as` import as atscript resolves it: without a trailing `.as` extension.
|
|
668
|
+
* `.as` imports may name a file with or without its extension (`'./user'`, `'./user.as'`,
|
|
669
|
+
* `'my-lib/models.as'`); both spellings resolve to the same module. A bare package name
|
|
670
|
+
* (`'my-lib'`, no subpath) is kept as written.
|
|
671
|
+
*/ function withoutAsExtension(specifier) {
|
|
672
|
+
if (!specifier.endsWith(".as")) return specifier;
|
|
673
|
+
const slash = specifier.lastIndexOf("/");
|
|
674
|
+
if (specifier.slice(slash + 1) === ".as" || isBareSpecifier(specifier) && parseBareSpecifier(specifier).subpath === ".") return specifier;
|
|
675
|
+
return specifier.slice(0, -3);
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
667
678
|
* Returns true if the document ID is a bare specifier placeholder.
|
|
668
679
|
*/ function isBareId(id) {
|
|
669
680
|
return id.startsWith("bare:");
|
|
@@ -770,6 +781,7 @@ const resolveCache = /* @__PURE__ */ new Map();
|
|
|
770
781
|
* @param fromDir Absolute directory of the importing file
|
|
771
782
|
* @returns Resolved absolute file path, or undefined if not found
|
|
772
783
|
*/ async function resolveBareSpecifier(specifier, fromDir) {
|
|
784
|
+
specifier = withoutAsExtension(specifier);
|
|
773
785
|
const cacheKey = `${fromDir}\0${specifier}`;
|
|
774
786
|
if (resolveCache.has(cacheKey)) return resolveCache.get(cacheKey);
|
|
775
787
|
const result = await _resolveBareSpecifier(specifier, fromDir);
|
|
@@ -815,16 +827,17 @@ async function _resolveBareSpecifier(specifier, fromDir) {
|
|
|
815
827
|
//#endregion
|
|
816
828
|
//#region packages/core/src/parser/utils.ts
|
|
817
829
|
/** {@link resolveAtscriptFromPath} by document id and import path (a pure function of both). */ const resolvedFromPaths = /* @__PURE__ */ new Map();
|
|
818
|
-
function resolveAtscriptFromPath(
|
|
830
|
+
function resolveAtscriptFromPath(written, id) {
|
|
819
831
|
let byFrom = resolvedFromPaths.get(id);
|
|
820
832
|
if (!byFrom) {
|
|
821
833
|
byFrom = /* @__PURE__ */ new Map();
|
|
822
834
|
resolvedFromPaths.set(id, byFrom);
|
|
823
835
|
}
|
|
824
|
-
let resolved = byFrom.get(
|
|
836
|
+
let resolved = byFrom.get(written);
|
|
825
837
|
if (resolved === void 0) {
|
|
838
|
+
const from = withoutAsExtension(written);
|
|
826
839
|
resolved = isBareSpecifier(from) ? `bare:${from}.as` : `file://${path.default.join(fileUriToPath(id).split("/").slice(0, -1).join("/"), from)}.as`;
|
|
827
|
-
byFrom.set(
|
|
840
|
+
byFrom.set(written, resolved);
|
|
828
841
|
}
|
|
829
842
|
return resolved;
|
|
830
843
|
}
|
|
@@ -1293,6 +1306,10 @@ function _define_property$9(obj, key, value) {
|
|
|
1293
1306
|
else obj[key] = value;
|
|
1294
1307
|
return obj;
|
|
1295
1308
|
}
|
|
1309
|
+
/**
|
|
1310
|
+
* The built-in annotation of `annotate` blocks that removes another annotation from the node
|
|
1311
|
+
* (`@annotate.reset 'expect.pattern'`): a directive, never written as metadata.
|
|
1312
|
+
*/ const ANNOTATE_RESET = "annotate.reset";
|
|
1296
1313
|
/** Whether an annotation argument type is written in backticks (`query`, `expr`, `order`). */ function isBacktickArgType(type) {
|
|
1297
1314
|
return type === "query" || type === "expr" || type === "order";
|
|
1298
1315
|
}
|
|
@@ -1311,7 +1328,7 @@ var AnnotationSpec = class {
|
|
|
1311
1328
|
return this.arguments.map((arg, index) => {
|
|
1312
1329
|
const placeholderIndex = index + 1;
|
|
1313
1330
|
const defaultValue = this.getDefaultValueForType(arg.name, arg.type);
|
|
1314
|
-
const quote = arg.type === "string" ? `'` : isBacktickArgType(arg.type) ? "`" : "";
|
|
1331
|
+
const quote = arg.type === "string" || arg.type === "value" ? `'` : isBacktickArgType(arg.type) ? "`" : "";
|
|
1315
1332
|
return `${quote}\${${placeholderIndex}:${defaultValue}}${quote}`;
|
|
1316
1333
|
}).join(", ");
|
|
1317
1334
|
}
|
|
@@ -1321,6 +1338,7 @@ var AnnotationSpec = class {
|
|
|
1321
1338
|
case "number": return tokenType === "number" ? void 0 : "number expected.";
|
|
1322
1339
|
case "boolean": return tokenType === "identifier" ? void 0 : "boolean expected.";
|
|
1323
1340
|
case "ref": return tokenType === "identifier" ? void 0 : "type reference expected.";
|
|
1341
|
+
case "value": return tokenType === "text" || tokenType === "number" || tokenType === "identifier" ? void 0 : "string, number or boolean expected.";
|
|
1324
1342
|
case "query":
|
|
1325
1343
|
case "expr":
|
|
1326
1344
|
case "order": return tokenType === "query" ? void 0 : BACKTICK_TYPE_MESSAGES[type];
|
|
@@ -1377,7 +1395,7 @@ var AnnotationSpec = class {
|
|
|
1377
1395
|
});
|
|
1378
1396
|
continue;
|
|
1379
1397
|
}
|
|
1380
|
-
const values = argSpec.type === "boolean" ? ["true", "false"] : argSpec.values;
|
|
1398
|
+
const values = argSpec.type === "boolean" || argSpec.type === "value" && tokenType === "identifier" ? ["true", "false"] : argSpec.values;
|
|
1381
1399
|
if (values && !values.includes(valueText)) messages.push({
|
|
1382
1400
|
severity: 1,
|
|
1383
1401
|
message: `${mainToken.text} at argument #${i + 1} ("${argSpec.name}") must be one of [${values.join(", ")}]`,
|
|
@@ -1426,10 +1444,33 @@ var AnnotationSpec = class {
|
|
|
1426
1444
|
messages.push(...this.validateTargetType(def, mainToken.range, defDoc) || []);
|
|
1427
1445
|
}
|
|
1428
1446
|
}
|
|
1447
|
+
messages.push(...this.validateValueArgs(mainToken, args, doc));
|
|
1429
1448
|
if (this.config.validate) messages.push(...this.config.validate(mainToken, args, doc) || []);
|
|
1430
1449
|
return messages.length > 0 ? messages : void 0;
|
|
1431
1450
|
}
|
|
1432
1451
|
/**
|
|
1452
|
+
* `value` arguments written as a number or a boolean must fit the annotated field's type: a
|
|
1453
|
+
* number literal a field that holds numbers, a boolean literal one that holds booleans (a
|
|
1454
|
+
* union: one of its members; `unknown`: any). A string is accepted for any field.
|
|
1455
|
+
*/ validateValueArgs(mainToken, args, doc) {
|
|
1456
|
+
const messages = [];
|
|
1457
|
+
const specArgs = this.arguments;
|
|
1458
|
+
let kinds = null;
|
|
1459
|
+
for (let i = 0; i < args.length && i < specArgs.length; i++) {
|
|
1460
|
+
const token = args[i];
|
|
1461
|
+
const kind = token.type === "number" ? "number" : token.type === "identifier" ? "boolean" : void 0;
|
|
1462
|
+
if (specArgs[i].type !== "value" || !kind) continue;
|
|
1463
|
+
if (kinds === null) kinds = annotatedValueKinds(mainToken, doc);
|
|
1464
|
+
if (!kinds) return messages;
|
|
1465
|
+
if (!literalFits(kind, kinds)) messages.push({
|
|
1466
|
+
severity: 1,
|
|
1467
|
+
message: `${mainToken.text} at argument #${i + 1} ("${specArgs[i].name}"): a ${kind} does not fit the field's type (${[...kinds].join(" | ") || "unknown"}).`,
|
|
1468
|
+
range: token.range
|
|
1469
|
+
});
|
|
1470
|
+
}
|
|
1471
|
+
return messages;
|
|
1472
|
+
}
|
|
1473
|
+
/**
|
|
1433
1474
|
* Checks an already-resolved target node against this annotation's `defType`
|
|
1434
1475
|
* guard (e.g. `@expect.minLength` requires `string | array`). Returns a
|
|
1435
1476
|
* diagnostic when the type doesn't match, or `undefined` when it matches or no
|
|
@@ -1444,11 +1485,12 @@ var AnnotationSpec = class {
|
|
|
1444
1485
|
*/ validateTargetType(def, range, doc) {
|
|
1445
1486
|
const defType = this.config.defType;
|
|
1446
1487
|
if (!defType?.length) return;
|
|
1447
|
-
|
|
1488
|
+
const literals = this.config.defTypeLiterals;
|
|
1489
|
+
if (matchesDefType(def, defType, literals)) return;
|
|
1448
1490
|
let got;
|
|
1449
1491
|
if (isGroup(def) && describeNode(def) === "union") {
|
|
1450
1492
|
const members = doc ? nonNullishMembers(def, doc) : void 0;
|
|
1451
|
-
if (members?.length && members.every((m) => matchesDefType(m, defType))) return;
|
|
1493
|
+
if (members?.length && members.every((m) => matchesDefType(m, defType, literals))) return;
|
|
1452
1494
|
got = `union (${describeUnionMembers(def, doc)})`;
|
|
1453
1495
|
} else got = `"${describeNode(def)}"`;
|
|
1454
1496
|
return [{
|
|
@@ -1462,9 +1504,9 @@ var AnnotationSpec = class {
|
|
|
1462
1504
|
const a = this.arguments[index];
|
|
1463
1505
|
if (a) {
|
|
1464
1506
|
const values = a.values ? `\n\nValues:\n${a.values.join(", ")}` : "";
|
|
1465
|
-
return `### \`${a.name}${a.optional ? "?" : ""}: ${a
|
|
1507
|
+
return `### \`${a.name}${a.optional ? "?" : ""}: ${argTypeLabel(a)}\`\n\n${a.description}${values}`;
|
|
1466
1508
|
}
|
|
1467
|
-
} else return `### ${index} ${this.arguments.map((a) => `\`${a.name}${a.optional ? "?" : ""}: ${a
|
|
1509
|
+
} else return `### ${index} ${this.arguments.map((a) => `\`${a.name}${a.optional ? "?" : ""}: ${argTypeLabel(a)}\``).join(", ")}\n\n${this.config.description}`;
|
|
1468
1510
|
}
|
|
1469
1511
|
getDefaultValueForType(name, type) {
|
|
1470
1512
|
switch (type) {
|
|
@@ -1475,6 +1517,7 @@ var AnnotationSpec = class {
|
|
|
1475
1517
|
case "query": return "field = value";
|
|
1476
1518
|
case "expr": return "field + 1";
|
|
1477
1519
|
case "order": return "field asc";
|
|
1520
|
+
case "value": return name;
|
|
1478
1521
|
default: return "";
|
|
1479
1522
|
}
|
|
1480
1523
|
}
|
|
@@ -1485,6 +1528,9 @@ var AnnotationSpec = class {
|
|
|
1485
1528
|
this.__is_annotation_spec = true;
|
|
1486
1529
|
}
|
|
1487
1530
|
};
|
|
1531
|
+
/** An argument's type as the docs show it (`value`: the literals it takes). */ function argTypeLabel(a) {
|
|
1532
|
+
return a.type === "value" ? "string | number | boolean" : a.type;
|
|
1533
|
+
}
|
|
1488
1534
|
function isAnnotationSpec(a) {
|
|
1489
1535
|
return Boolean(a) && a.__is_annotation_spec;
|
|
1490
1536
|
}
|
|
@@ -1508,6 +1554,30 @@ function isAnnotationSpec(a) {
|
|
|
1508
1554
|
};
|
|
1509
1555
|
walk(tree);
|
|
1510
1556
|
}
|
|
1557
|
+
/**
|
|
1558
|
+
* Where annotation `spec` holds user-facing text (arguments flagged `userText`): `true` — its
|
|
1559
|
+
* value itself (a spec with one argument, not a list: the value is the string; for a `multiple`
|
|
1560
|
+
* annotation, each value); `'expression'` — likewise, but the value is function source whose string
|
|
1561
|
+
* literals are the texts; a list — those fields of its value object (a spec with an argument
|
|
1562
|
+
* list), an expression field's name ending in `()`. `undefined`: no user-facing text.
|
|
1563
|
+
*/ function userTextFields(spec) {
|
|
1564
|
+
const arg = spec.config.argument;
|
|
1565
|
+
if (!arg) return;
|
|
1566
|
+
if (!Array.isArray(arg)) return arg.userText || void 0;
|
|
1567
|
+
const fields = arg.filter((a) => a.userText).map((a) => a.userText === "expression" ? `${a.name}()` : a.name);
|
|
1568
|
+
return fields.length > 0 ? fields : void 0;
|
|
1569
|
+
}
|
|
1570
|
+
/**
|
|
1571
|
+
* Every annotation of `tree` (core and plugins) holding user-facing text, by full name (see
|
|
1572
|
+
* {@link userTextFields}): what i18n tooling extracts and a translator translates.
|
|
1573
|
+
*/ function userTextAnnotations(tree) {
|
|
1574
|
+
const out = /* @__PURE__ */ new Map();
|
|
1575
|
+
forEachAnnotationSpec(tree, (name, spec) => {
|
|
1576
|
+
const fields = userTextFields(spec);
|
|
1577
|
+
if (fields) out.set(name, fields);
|
|
1578
|
+
});
|
|
1579
|
+
return out;
|
|
1580
|
+
}
|
|
1511
1581
|
function resolveAnnotation(name, annotationsTree) {
|
|
1512
1582
|
const parts = name.split(".");
|
|
1513
1583
|
let current = annotationsTree;
|
|
@@ -1522,15 +1592,28 @@ function resolveAnnotation(name, annotationsTree) {
|
|
|
1522
1592
|
/** `null` / `undefined` / `void` primitive. */ function isNullishPrimitive(node) {
|
|
1523
1593
|
return isPrimitive(node) && (node.type === "null" || node.type === "void");
|
|
1524
1594
|
}
|
|
1525
|
-
/**
|
|
1526
|
-
|
|
1595
|
+
/**
|
|
1596
|
+
* Whether a resolved node satisfies a `defType` guard (a union as a whole, not its members);
|
|
1597
|
+
* `literals`: a literal of an accepted type does too (see `defTypeLiterals`).
|
|
1598
|
+
*/ function matchesDefType(def, defType, literals) {
|
|
1599
|
+
if (defType.includes(describeNode(def))) return true;
|
|
1600
|
+
return !!literals && isConst(def) && defType.includes(literalType(def));
|
|
1601
|
+
}
|
|
1602
|
+
/**
|
|
1603
|
+
* The `defType` vocabulary name of a literal's value (`'a'` → `string`, `1` → `number`; atscript
|
|
1604
|
+
* literals are strings and numbers).
|
|
1605
|
+
*/ function literalType(def) {
|
|
1606
|
+
return def.token("identifier")?.type === "number" ? "number" : "string";
|
|
1527
1607
|
}
|
|
1528
1608
|
/** The `defType` vocabulary name of a resolved node (`string`, `object`, `union`, `array`, …). */ function describeNode(def) {
|
|
1529
|
-
if (isPrimitive(def)) return def.type || "unknown";
|
|
1609
|
+
if (isPrimitive(def)) return def.type === "any" ? "unknown" : def.type || "unknown";
|
|
1530
1610
|
if (isInterface(def) || isStructure(def)) return "object";
|
|
1531
1611
|
if (isGroup(def) && def.entity !== "tuple") return def.op === "&" ? "intersection" : "union";
|
|
1532
1612
|
return def?.entity || "unknown";
|
|
1533
1613
|
}
|
|
1614
|
+
/** A `|` group (not a tuple, an inline object `{ … }` or a `&` group). */ function isUnionGroup(node) {
|
|
1615
|
+
return node?.entity === "group" && node.op !== "&";
|
|
1616
|
+
}
|
|
1534
1617
|
/** Walks a `|` union (flattening nested unions and union aliases), resolving member refs. */ function walkUnionMembers(def, doc, cb, seen = /* @__PURE__ */ new Set()) {
|
|
1535
1618
|
if (seen.has(def)) return;
|
|
1536
1619
|
seen.add(def);
|
|
@@ -1542,7 +1625,7 @@ function resolveAnnotation(name, annotationsTree) {
|
|
|
1542
1625
|
member = unwound?.def;
|
|
1543
1626
|
memberDoc = unwound?.doc || doc;
|
|
1544
1627
|
}
|
|
1545
|
-
if (
|
|
1628
|
+
if (isUnionGroup(member)) walkUnionMembers(member, memberDoc, cb, seen);
|
|
1546
1629
|
else cb(member);
|
|
1547
1630
|
}
|
|
1548
1631
|
}
|
|
@@ -1569,7 +1652,7 @@ function describeUnionMembers(def, doc) {
|
|
|
1569
1652
|
* const ok = members ? members.length > 0 && members.every(isWanted) : isWanted(def)
|
|
1570
1653
|
* ```
|
|
1571
1654
|
*/ function nonNullishMembers(def, doc) {
|
|
1572
|
-
if (!
|
|
1655
|
+
if (!isUnionGroup(def)) return;
|
|
1573
1656
|
const members = [];
|
|
1574
1657
|
walkUnionMembers(def, doc, (m) => {
|
|
1575
1658
|
if (m && !isNullishPrimitive(m)) members.push(m);
|
|
@@ -1577,6 +1660,73 @@ function describeUnionMembers(def, doc) {
|
|
|
1577
1660
|
return members;
|
|
1578
1661
|
}
|
|
1579
1662
|
/**
|
|
1663
|
+
* The resolved type of the field (or type) an annotation is written on: an inline prop's, the
|
|
1664
|
+
* prop an `annotate` block entry names, an `annotate` block's target. `undefined` when it does
|
|
1665
|
+
* not resolve (reported elsewhere).
|
|
1666
|
+
*/ function annotatedFieldType(mainToken, doc) {
|
|
1667
|
+
const parent = mainToken.parentNode;
|
|
1668
|
+
if (!parent) return;
|
|
1669
|
+
const at = parent.entity === "annotate" ? doc.unwindType(parent.targetName) : doc.annotatedDefinition(parent);
|
|
1670
|
+
if (!at?.def) return;
|
|
1671
|
+
if (isRef(at.def)) {
|
|
1672
|
+
const unwound = at.doc.unwindRef(at.def);
|
|
1673
|
+
return unwound?.def ? {
|
|
1674
|
+
def: unwound.def,
|
|
1675
|
+
doc: unwound.doc
|
|
1676
|
+
} : void 0;
|
|
1677
|
+
}
|
|
1678
|
+
return {
|
|
1679
|
+
def: at.def,
|
|
1680
|
+
doc: at.doc
|
|
1681
|
+
};
|
|
1682
|
+
}
|
|
1683
|
+
/**
|
|
1684
|
+
* The scalar kinds a resolved type holds (`string`, `number`, `boolean`, `decimal`; `unknown`:
|
|
1685
|
+
* any value), through references and aliases: a union's members' (not `null` / `undefined`), a
|
|
1686
|
+
* scalar merge's kinds every member has (`unknown` takes no part: `unknown & number` is a
|
|
1687
|
+
* number), a literal's.
|
|
1688
|
+
*/ function valueKinds(def, doc, seen = /* @__PURE__ */ new Set()) {
|
|
1689
|
+
let node = def;
|
|
1690
|
+
let at = doc;
|
|
1691
|
+
while (node && isGroup(node) && !node.op && node.unwrap().length === 1) node = node.unwrap()[0];
|
|
1692
|
+
if (isRef(node)) {
|
|
1693
|
+
const unwound = at.unwindRef(node);
|
|
1694
|
+
node = unwound?.def;
|
|
1695
|
+
at = unwound?.doc ?? at;
|
|
1696
|
+
}
|
|
1697
|
+
if (!node || seen.has(node)) return /* @__PURE__ */ new Set();
|
|
1698
|
+
seen.add(node);
|
|
1699
|
+
if (node.entity === "const") return new Set([literalType(node)]);
|
|
1700
|
+
if (isGroup(node) && node.entity !== "tuple" && node.op === "&") {
|
|
1701
|
+
let out;
|
|
1702
|
+
for (const m of node.unwrap()) {
|
|
1703
|
+
const ks = valueKinds(m, at, seen);
|
|
1704
|
+
if (ks.size === 1 && ks.has("unknown")) continue;
|
|
1705
|
+
out = out ? new Set([...out].filter((k) => ks.has(k))) : ks;
|
|
1706
|
+
}
|
|
1707
|
+
return out ?? new Set(["unknown"]);
|
|
1708
|
+
}
|
|
1709
|
+
const members = nonNullishMembers(node, at);
|
|
1710
|
+
if (members) {
|
|
1711
|
+
const out = /* @__PURE__ */ new Set();
|
|
1712
|
+
for (const m of members) for (const k of valueKinds(m, at, seen)) out.add(k);
|
|
1713
|
+
return out;
|
|
1714
|
+
}
|
|
1715
|
+
return new Set([describeNode(node)]);
|
|
1716
|
+
}
|
|
1717
|
+
/**
|
|
1718
|
+
* The scalar kinds the field (or type) annotation `mainToken` is written on holds (see
|
|
1719
|
+
* `valueKinds`): `string`, `number`, `boolean`, `decimal`, `unknown` (any value), or another
|
|
1720
|
+
* kind name (`object`, `array`, …). `undefined` when the field does not resolve. Code generators
|
|
1721
|
+
* use it for `value` arguments: a number literal on a `decimal` field is written as its string.
|
|
1722
|
+
*/ function annotatedValueKinds(mainToken, doc) {
|
|
1723
|
+
const target = annotatedFieldType(mainToken, doc);
|
|
1724
|
+
return target ? valueKinds(target.def, target.doc) : void 0;
|
|
1725
|
+
}
|
|
1726
|
+
/** Whether a literal of `kind` (`number`, `boolean`) fits a field of `kinds` (see `valueKinds`). */ function literalFits(kind, kinds) {
|
|
1727
|
+
return kinds.has("unknown") || kinds.has(kind) || kind === "number" && kinds.has("decimal");
|
|
1728
|
+
}
|
|
1729
|
+
/**
|
|
1580
1730
|
* The node type an `annotate` block's top-level annotations apply to: its target's (`interface`
|
|
1581
1731
|
* or `type`; through an `annotate … as` alias, the alias's target's). `undefined` while the
|
|
1582
1732
|
* target does not resolve (reported elsewhere).
|
|
@@ -4200,6 +4350,11 @@ var TSeverity = /* @__PURE__ */ function(TSeverity) {
|
|
|
4200
4350
|
const UNION_IN_RECURSIVE_MERGE = "union members are not supported in recursive merges; split with named types";
|
|
4201
4351
|
const MERGE_TOO_COMPLEX = "merge too complex: split it with named types";
|
|
4202
4352
|
/** Why scalars (or an object and a scalar) merge to `never`. */ const NOTHING_IN_COMMON = "values with nothing in common";
|
|
4353
|
+
/**
|
|
4354
|
+
* Why a merge of `unknown` (any value) with an object, an array or `null` is refused: TypeScript
|
|
4355
|
+
* reads `unknown & X` as `X`, a merge of values would be `never` (a compile error, not a
|
|
4356
|
+
* validator rejecting what the `.d.ts` accepts).
|
|
4357
|
+
*/ const OPAQUE_MERGE = "\"unknown\" cannot be merged with an object, an array or null";
|
|
4203
4358
|
/** A type written as an operand of its own merge (`type X = A & X`). */ const SELF_OPERAND = "a type cannot be an operand of its own merge; recurse through a field instead";
|
|
4204
4359
|
/** A db entity whose flattening reaches a web of mutually recursive merges. */ const FLATTEN_WEB = "flattening this type reaches mutually recursive merges; split it with named types";
|
|
4205
4360
|
/**
|
|
@@ -4207,6 +4362,8 @@ const MERGE_TOO_COMPLEX = "merge too complex: split it with named types";
|
|
|
4207
4362
|
* itself splits (MODEL §15 SELF-SPLIT): where it lands depends on the positions it decides.
|
|
4208
4363
|
*/ const SELF_SPLIT = "A block's path can't cross the type path it splits.";
|
|
4209
4364
|
const isBranch = (s) => s.startsWith("|");
|
|
4365
|
+
/** A shape of the `unknown` primitive (any value). */ const isAnyShape = (s) => s.k === "scalar" && s.any === true;
|
|
4366
|
+
/** Merge refusals reported as errors, never dropped as a `never` branch. */ const refused = (reason) => reason === MERGE_TOO_COMPLEX || reason === OPAQUE_MERGE;
|
|
4210
4367
|
/** A step that is a prop's name (not a union branch, an in-place merge operand or an element). */ const isNameStep = (s) => !s.startsWith("|") && !s.startsWith("&") && s !== "[]";
|
|
4211
4368
|
/** A written `null`. */ const isNull = (x) => x.k === "scalar" && isPrimitive(x.node) && x.node.key === "null";
|
|
4212
4369
|
/** A shape that is, or has a branch that is, an array. */ const holdsArray = (s) => s?.k === "arr" || s?.k === "uni" && s.items.some(holdsArray);
|
|
@@ -6411,7 +6568,12 @@ function fieldLoc(s) {
|
|
|
6411
6568
|
}
|
|
6412
6569
|
shape0(t) {
|
|
6413
6570
|
switch (t.k) {
|
|
6414
|
-
case "scalar":
|
|
6571
|
+
case "scalar":
|
|
6572
|
+
if (!isPrimitive(t.node)) return { k: "scalar" };
|
|
6573
|
+
return t.node.key === "null" ? { k: "mixed" } : t.node.type === "any" ? {
|
|
6574
|
+
k: "scalar",
|
|
6575
|
+
any: true
|
|
6576
|
+
} : { k: "scalar" };
|
|
6415
6577
|
case "tup": {
|
|
6416
6578
|
const items = t.items.map((i) => this.shape(i));
|
|
6417
6579
|
if (!items.every((s) => s.k === "scalar" || s.k === "never")) return { k: "mixed" };
|
|
@@ -6423,6 +6585,10 @@ function fieldLoc(s) {
|
|
|
6423
6585
|
case "uni": {
|
|
6424
6586
|
if (t.struct) return this.shape(structExpr(t));
|
|
6425
6587
|
const ss = t.items.map((i) => this.shape(i));
|
|
6588
|
+
if (ss.some(isAnyShape)) return {
|
|
6589
|
+
k: "scalar",
|
|
6590
|
+
any: true
|
|
6591
|
+
};
|
|
6426
6592
|
if (ss.every((s) => s.k === "scalar" || s.k === "never")) return ss.every((s) => s.k === "never") ? {
|
|
6427
6593
|
k: "never",
|
|
6428
6594
|
reason: "union"
|
|
@@ -6490,13 +6656,17 @@ function fieldLoc(s) {
|
|
|
6490
6656
|
* algebra: disjoint scalars are `never`), a single union operand of objects distributes.
|
|
6491
6657
|
*/ combine(ss, ends) {
|
|
6492
6658
|
if (ss.length === 1) return ss[0];
|
|
6659
|
+
if (ss.some(isAnyShape) && !ss.every((s) => s.k === "scalar" || s.k === "never")) return {
|
|
6660
|
+
k: "never",
|
|
6661
|
+
reason: OPAQUE_MERGE
|
|
6662
|
+
};
|
|
6493
6663
|
const tuple = ends.length === ss.length ? this.tupleShape(ends) : void 0;
|
|
6494
6664
|
if (tuple) return tuple;
|
|
6495
6665
|
if (ss.some((s) => s.k === "mixed")) {
|
|
6496
6666
|
if (ends.length === ss.length && ends.some(isNull) && ends.every((e) => isNull(e) || this.nullable(e))) return { k: "scalar" };
|
|
6497
6667
|
const leaves = ends.length === ss.length ? this.mixedLeaves(ss, ends) : void 0;
|
|
6498
6668
|
const product = leaves && this.productShape(leaves.ss, leaves.ends);
|
|
6499
|
-
if (product?.k === "never" && product.reason
|
|
6669
|
+
if (product?.k === "never" && refused(product.reason)) return product;
|
|
6500
6670
|
return product?.k === "never" ? {
|
|
6501
6671
|
k: "never",
|
|
6502
6672
|
reason: NOTHING_IN_COMMON
|
|
@@ -6567,6 +6737,7 @@ function fieldLoc(s) {
|
|
|
6567
6737
|
}
|
|
6568
6738
|
const es = pick.map((p) => p.e);
|
|
6569
6739
|
const c = this.combine(pick.map((p) => p.s), es.every(Boolean) ? es : []);
|
|
6740
|
+
if (c.k === "never" && c.reason === OPAQUE_MERGE) return c;
|
|
6570
6741
|
if (c.k !== "never" && c.k !== "mixed") out.push(c);
|
|
6571
6742
|
}
|
|
6572
6743
|
if (out.length === 0) return {
|
|
@@ -8286,12 +8457,12 @@ var AtscriptDoc = class AtscriptDoc {
|
|
|
8286
8457
|
* them to its target's metadata: an `append` annotation adds its values after the existing
|
|
8287
8458
|
* ones, any other replaces them.
|
|
8288
8459
|
*/ overlayAnnotations(base, overlay) {
|
|
8289
|
-
const replaced = /* @__PURE__ */ new Set();
|
|
8460
|
+
const replaced = resetNames(overlay) ?? /* @__PURE__ */ new Set();
|
|
8290
8461
|
for (const a of overlay) {
|
|
8291
8462
|
const spec = this.resolveAnnotation(a.name);
|
|
8292
8463
|
if (!(spec?.config.multiple && spec.config.mergeStrategy === "append")) replaced.add(a.name);
|
|
8293
8464
|
}
|
|
8294
|
-
return [...(base ?? []).filter((a) => !replaced.has(a.name)), ...overlay];
|
|
8465
|
+
return [...(base ?? []).filter((a) => a.name === ANNOTATE_RESET || !replaced.has(a.name)), ...overlay];
|
|
8295
8466
|
}
|
|
8296
8467
|
/**
|
|
8297
8468
|
* What declaration `decl` (of this document) is defined as — an alias's, its target's (an
|
|
@@ -8660,7 +8831,7 @@ var AtscriptDoc = class AtscriptDoc {
|
|
|
8660
8831
|
*/ evalAnnotationsForNode(givenNode, own = givenNode.annotations, blocks = false) {
|
|
8661
8832
|
if (isAnnotate(givenNode) && !givenNode.isMutating) {
|
|
8662
8833
|
const target = this.getDeclarationOwnerNode(givenNode.targetName);
|
|
8663
|
-
return this.mergeNodesAnnotations(target?.node && target.node !== givenNode ? target.doc.evalAnnotationsForNode(target.node, annotationsOf(target.node, blocks), blocks) : void 0, own);
|
|
8834
|
+
return stripResets(this.mergeNodesAnnotations(target?.node && target.node !== givenNode ? target.doc.evalAnnotationsForNode(target.node, annotationsOf(target.node, blocks), blocks) : void 0, own, true));
|
|
8664
8835
|
}
|
|
8665
8836
|
return this.evalAnnotationsForDefinition(givenNode.getDefinition(), own, givenNode, blocks);
|
|
8666
8837
|
}
|
|
@@ -8689,24 +8860,24 @@ var AtscriptDoc = class AtscriptDoc {
|
|
|
8689
8860
|
for (const chainNode of chainNodes) {
|
|
8690
8861
|
if (chainNode === unwound?.def) continue;
|
|
8691
8862
|
const anns = chainNode === unwound?.node && fieldAnns ? fieldAnns : annotationsOf(chainNode, blocks);
|
|
8692
|
-
right = this.mergeNodesAnnotations(this.filterPassedWhenReferred(anns, viaProp), right);
|
|
8863
|
+
right = this.mergeNodesAnnotations(this.filterPassedWhenReferred(anns, viaProp), right, true);
|
|
8693
8864
|
}
|
|
8694
8865
|
def = unwound?.def || def;
|
|
8695
8866
|
}
|
|
8696
8867
|
if (def) {
|
|
8697
8868
|
const merged = this.mergeIntersection(def);
|
|
8698
8869
|
if (merged !== def && isRef(merged)) {
|
|
8699
|
-
right = this.mergeNodesAnnotations(merged.annotations, right);
|
|
8870
|
+
right = this.mergeNodesAnnotations(merged.annotations, right, true);
|
|
8700
8871
|
def = merged;
|
|
8701
8872
|
continue;
|
|
8702
8873
|
}
|
|
8703
8874
|
const written = unparenthesized(def);
|
|
8704
8875
|
const own = isIntersection(written) ? this.intersectionAnnotations(written, blocks).map((p) => p.an) : refDef ? annotationsOf(merged, blocks) : merged.annotations;
|
|
8705
|
-
right = this.mergeNodesAnnotations(refDef ? this.filterPassedWhenReferred(own, viaProp) : own, right);
|
|
8876
|
+
right = this.mergeNodesAnnotations(refDef ? this.filterPassedWhenReferred(own, viaProp) : own, right, true);
|
|
8706
8877
|
}
|
|
8707
8878
|
break;
|
|
8708
8879
|
}
|
|
8709
|
-
return right;
|
|
8880
|
+
return stripResets(right);
|
|
8710
8881
|
}
|
|
8711
8882
|
/**
|
|
8712
8883
|
* The annotations of the field chain reference `ref` names (its node `node`), with the blocks:
|
|
@@ -8916,7 +9087,7 @@ var AtscriptDoc = class AtscriptDoc {
|
|
|
8916
9087
|
const idToken = prop.token("identifier");
|
|
8917
9088
|
if (idToken) cloned.saveToken(idToken, "identifier");
|
|
8918
9089
|
if (newDef) cloned.define(newDef);
|
|
8919
|
-
cloned.annotations = group.own.length === 0 ? prop.annotations : this.mergeNodesAnnotations(prop.annotations, group.own);
|
|
9090
|
+
cloned.annotations = group.own.length === 0 ? prop.annotations : this.mergeNodesAnnotations(prop.annotations, group.own, true);
|
|
8920
9091
|
const optionalToken = prop.token("optional");
|
|
8921
9092
|
if (optionalToken) cloned.saveToken(optionalToken, "optional");
|
|
8922
9093
|
newProps.push(cloned);
|
|
@@ -9539,6 +9710,11 @@ var AtscriptDoc = class AtscriptDoc {
|
|
|
9539
9710
|
message: MERGE_TOO_COMPLEX,
|
|
9540
9711
|
range: token.range
|
|
9541
9712
|
});
|
|
9713
|
+
else if (token && reason === OPAQUE_MERGE) messages.push({
|
|
9714
|
+
severity: TSeverity.Error,
|
|
9715
|
+
message: `${OPAQUE_MERGE}${path.length > 0 ? ` at "${decl.name}.${path.join(".")}"` : ` in "${decl.name}"`}: write the other type alone`,
|
|
9716
|
+
range: token.range
|
|
9717
|
+
});
|
|
9542
9718
|
else if (token) messages.push({
|
|
9543
9719
|
severity: TSeverity.Warning,
|
|
9544
9720
|
message: `Intersection of ${reason} cannot be merged${path.length > 0 ? ` at "${decl.name}.${path.join(".")}"` : ` in "${decl.name}"`}; the type resolves to never`,
|
|
@@ -10114,14 +10290,29 @@ var AtscriptDoc = class AtscriptDoc {
|
|
|
10114
10290
|
add(group);
|
|
10115
10291
|
return out;
|
|
10116
10292
|
}
|
|
10117
|
-
|
|
10293
|
+
/**
|
|
10294
|
+
* Annotations `right` over `left`: right's win, `append` ones of both kept (left's after).
|
|
10295
|
+
*
|
|
10296
|
+
* `layered`: `right` is a node's and `left` what the node is layered over (the type it
|
|
10297
|
+
* references, an `as` copy's target) — right's `@annotate.reset 'x'` drop left's x, and the
|
|
10298
|
+
* resets of both stay, for the layers further below (see {@link stripResets}). Else they are
|
|
10299
|
+
* siblings (the members of a merge): a reset applies within its member's own layers only, and
|
|
10300
|
+
* is left out.
|
|
10301
|
+
*/ mergeNodesAnnotations(left, right, layered = false) {
|
|
10118
10302
|
const annotations = [];
|
|
10119
10303
|
const savedAnnotations = /* @__PURE__ */ new Set();
|
|
10304
|
+
const hidden = layered ? resetNames(right) : void 0;
|
|
10120
10305
|
for (const a of right || []) {
|
|
10306
|
+
if (a.name === ANNOTATE_RESET && !layered) continue;
|
|
10121
10307
|
annotations.push(a);
|
|
10122
10308
|
savedAnnotations.add(a.name);
|
|
10123
10309
|
}
|
|
10124
10310
|
for (const a of left || []) {
|
|
10311
|
+
if (a.name === ANNOTATE_RESET) {
|
|
10312
|
+
if (layered) annotations.push(a);
|
|
10313
|
+
continue;
|
|
10314
|
+
}
|
|
10315
|
+
if (hidden?.has(a.name)) continue;
|
|
10125
10316
|
const spec = this.resolveAnnotation(a.name);
|
|
10126
10317
|
if (spec && spec.config.multiple && spec.config.mergeStrategy === "append" || !savedAnnotations.has(a.name)) annotations.push(a);
|
|
10127
10318
|
}
|
|
@@ -10330,6 +10521,72 @@ const zeroRange = {
|
|
|
10330
10521
|
for (const t of decl.referredIdentifiers) out.add(t);
|
|
10331
10522
|
return out;
|
|
10332
10523
|
}
|
|
10524
|
+
/** The annotation names `@annotate.reset` entries of `anns` reset; `undefined`: none. */ function resetNames(anns) {
|
|
10525
|
+
let out;
|
|
10526
|
+
for (const a of anns ?? []) if (a.name === ANNOTATE_RESET && a.args[0]?.type === "text") (out ?? (out = /* @__PURE__ */ new Set())).add(a.args[0].text);
|
|
10527
|
+
return out;
|
|
10528
|
+
}
|
|
10529
|
+
/**
|
|
10530
|
+
* `anns` without their `@annotate.reset` entries: a node's annotations once the resets applied to
|
|
10531
|
+
* the layers below it (see `AtscriptDoc.mergeNodesAnnotations`).
|
|
10532
|
+
*/ function stripResets(anns) {
|
|
10533
|
+
return anns?.some((a) => a.name === ANNOTATE_RESET) ? anns.filter((a) => a.name !== ANNOTATE_RESET) : anns;
|
|
10534
|
+
}
|
|
10535
|
+
|
|
10536
|
+
//#endregion
|
|
10537
|
+
//#region packages/core/src/defaults/annotate-annotations.ts
|
|
10538
|
+
/** Whether `name` is in the `annotate` namespace: a directive of `annotate` blocks, never metadata. */ function isAnnotateDirective(name) {
|
|
10539
|
+
return name.startsWith("annotate.");
|
|
10540
|
+
}
|
|
10541
|
+
/** The annotations of the `annotate` namespace: directives of `annotate` blocks, not metadata. */ const annotateAnnotations = { reset: new AnnotationSpec({
|
|
10542
|
+
description: annotationExample("Removes an annotation the annotated field or type would otherwise carry — declared on it, inherited from the type it references (`email: Email`, a primitive like `string.email`), from the target of an `annotate … as` copy, or written by a lower-ranked `annotate` block. Allowed only in `annotate` blocks (mutating and `annotate … as`), on an entry or at the top of the block (the type itself). The block's own value of that annotation applies over the reset; any other value does not. Repeat it to reset several annotations.", "annotate User as PublicUser {", " @annotate.reset 'expect.pattern'", " @expect.pattern '^[a-z]+$'", " login", "", " @annotate.reset 'meta.sensitive'", " token", "}"),
|
|
10543
|
+
multiple: true,
|
|
10544
|
+
argument: {
|
|
10545
|
+
name: "annotation",
|
|
10546
|
+
type: "string",
|
|
10547
|
+
description: "The full name of the annotation to remove, without `@` (`expect.pattern`, `meta.label`).",
|
|
10548
|
+
valueScope: (_token, doc) => annotationNames(doc.config.annotations)
|
|
10549
|
+
},
|
|
10550
|
+
validate(token, args, doc) {
|
|
10551
|
+
const messages = [];
|
|
10552
|
+
const parent = token.parentNode;
|
|
10553
|
+
const id = parent && isRef(parent) ? parent.token("identifier") : void 0;
|
|
10554
|
+
if (!(!!parent && isAnnotate(parent) || !!id && !!doc.annotateBlockAt(id.range.start.line, id.range.start.character))) messages.push({
|
|
10555
|
+
severity: TSeverity.Error,
|
|
10556
|
+
message: "@annotate.reset is allowed only in annotate blocks",
|
|
10557
|
+
range: token.range
|
|
10558
|
+
});
|
|
10559
|
+
const name = args[0]?.type === "text" ? args[0].text : void 0;
|
|
10560
|
+
if (name === void 0) return messages;
|
|
10561
|
+
if (isAnnotateDirective(name) || name === "annotate") messages.push({
|
|
10562
|
+
severity: TSeverity.Error,
|
|
10563
|
+
message: `"${name}" is not metadata: it cannot be reset`,
|
|
10564
|
+
range: args[0].range
|
|
10565
|
+
});
|
|
10566
|
+
else if (!doc.resolveAnnotation(name)) {
|
|
10567
|
+
const unknown = doc.config.unknownAnnotation;
|
|
10568
|
+
if (unknown !== "allow") messages.push({
|
|
10569
|
+
severity: unknown === "warn" ? TSeverity.Warning : TSeverity.Error,
|
|
10570
|
+
message: `Unknown annotation "@${name}"`,
|
|
10571
|
+
range: args[0].range
|
|
10572
|
+
});
|
|
10573
|
+
}
|
|
10574
|
+
return messages;
|
|
10575
|
+
}
|
|
10576
|
+
}) };
|
|
10577
|
+
/** The full names of the metadata annotations of `tree` (completion of `@annotate.reset`). */ function annotationNames(tree) {
|
|
10578
|
+
const out = [];
|
|
10579
|
+
forEachAnnotationSpec(tree ?? {}, (name, spec) => {
|
|
10580
|
+
if (!isAnnotateDirective(name)) {
|
|
10581
|
+
const doc = spec.config.description;
|
|
10582
|
+
out.push({
|
|
10583
|
+
value: name,
|
|
10584
|
+
documentation: doc
|
|
10585
|
+
});
|
|
10586
|
+
}
|
|
10587
|
+
});
|
|
10588
|
+
return out;
|
|
10589
|
+
}
|
|
10333
10590
|
|
|
10334
10591
|
//#endregion
|
|
10335
10592
|
//#region packages/core/src/defaults/emit-annotations.ts
|
|
@@ -10367,6 +10624,13 @@ const emitAnnotations = {
|
|
|
10367
10624
|
//#endregion
|
|
10368
10625
|
//#region packages/core/src/defaults/expect-annotations.ts
|
|
10369
10626
|
const NULLABLE_NOTE = "Also allowed on a nullable field (`T | null`, `T | undefined`): `null` passes, any other value must satisfy the constraint.";
|
|
10627
|
+
/** The optional message argument of a constraint annotation. */ const MESSAGE_ARG = {
|
|
10628
|
+
name: "message",
|
|
10629
|
+
optional: true,
|
|
10630
|
+
type: "string",
|
|
10631
|
+
description: "Optional error message to display if the validation fails.",
|
|
10632
|
+
userText: true
|
|
10633
|
+
};
|
|
10370
10634
|
/** Whether `flags` are flags of a JS regular expression. */ function validFlags(flags) {
|
|
10371
10635
|
try {
|
|
10372
10636
|
new RegExp("", flags);
|
|
@@ -10395,12 +10659,40 @@ const NULLABLE_NOTE = "Also allowed on a nullable field (`T | null`, `T | undefi
|
|
|
10395
10659
|
name: argName,
|
|
10396
10660
|
type: "number",
|
|
10397
10661
|
description: argDescription
|
|
10398
|
-
},
|
|
10399
|
-
|
|
10400
|
-
|
|
10401
|
-
|
|
10402
|
-
|
|
10403
|
-
|
|
10662
|
+
}, MESSAGE_ARG]
|
|
10663
|
+
});
|
|
10664
|
+
}
|
|
10665
|
+
/** Whether `text` (a number argument) is a non-negative integer. */ function isCount(text) {
|
|
10666
|
+
return /^\d+$/.test(text);
|
|
10667
|
+
}
|
|
10668
|
+
/**
|
|
10669
|
+
* `@expect.minKeys` / `maxKeys`: a key count (a non-negative integer) plus an optional message.
|
|
10670
|
+
* `@expect.maxKeys` also checks it is not below the node's `@expect.minKeys`.
|
|
10671
|
+
*/ function keyCountSpec(bound, example, argDescription, min) {
|
|
10672
|
+
return new AnnotationSpec({
|
|
10673
|
+
description: annotationExample(`${`Validates that an object has ${bound} this many keys: the keys it keeps (with unknown properties ignored, every own key; else the declared and pattern-matched ones).`} ${NULLABLE_NOTE}`, ...example),
|
|
10674
|
+
defType: ["object"],
|
|
10675
|
+
argument: [{
|
|
10676
|
+
name: "count",
|
|
10677
|
+
type: "number",
|
|
10678
|
+
description: argDescription
|
|
10679
|
+
}, MESSAGE_ARG],
|
|
10680
|
+
validate(mainToken, args) {
|
|
10681
|
+
const count = args[0];
|
|
10682
|
+
if (!count || count.type !== "number") return [];
|
|
10683
|
+
if (!isCount(count.text)) return [{
|
|
10684
|
+
message: `${mainToken.text} expects a non-negative integer`,
|
|
10685
|
+
range: count.range,
|
|
10686
|
+
severity: 1
|
|
10687
|
+
}];
|
|
10688
|
+
const lower = min ? mainToken.parentNode?.annotations?.find((a) => a.name === min)?.args[0] : void 0;
|
|
10689
|
+
if (lower && isCount(lower.text) && Number(lower.text) > Number(count.text)) return [{
|
|
10690
|
+
message: `${mainToken.text} is below @${min} (${lower.text})`,
|
|
10691
|
+
range: count.range,
|
|
10692
|
+
severity: 1
|
|
10693
|
+
}];
|
|
10694
|
+
return [];
|
|
10695
|
+
}
|
|
10404
10696
|
});
|
|
10405
10697
|
}
|
|
10406
10698
|
const expectAnnotations = {
|
|
@@ -10411,12 +10703,7 @@ const expectAnnotations = {
|
|
|
10411
10703
|
name: "length",
|
|
10412
10704
|
type: "number",
|
|
10413
10705
|
description: "The minimum length of the string or array."
|
|
10414
|
-
},
|
|
10415
|
-
name: "message",
|
|
10416
|
-
optional: true,
|
|
10417
|
-
type: "string",
|
|
10418
|
-
description: "Optional error message to display if the validation fails."
|
|
10419
|
-
}]
|
|
10706
|
+
}, MESSAGE_ARG]
|
|
10420
10707
|
}),
|
|
10421
10708
|
maxLength: new AnnotationSpec({
|
|
10422
10709
|
description: annotationExample(`Validates that a string or array has a maximum length. ${NULLABLE_NOTE}`, "@expect.maxLength 5", "name: string"),
|
|
@@ -10425,12 +10712,7 @@ const expectAnnotations = {
|
|
|
10425
10712
|
name: "length",
|
|
10426
10713
|
type: "number",
|
|
10427
10714
|
description: "The maximum length of the string or array."
|
|
10428
|
-
},
|
|
10429
|
-
name: "message",
|
|
10430
|
-
optional: true,
|
|
10431
|
-
type: "string",
|
|
10432
|
-
description: "Optional error message to display if the validation fails."
|
|
10433
|
-
}]
|
|
10715
|
+
}, MESSAGE_ARG]
|
|
10434
10716
|
}),
|
|
10435
10717
|
min: numberBoundSpec("Validates that a number is greater than or equal to a minimum value.", ["@expect.min 18", "age: number"], "minValue", "The minimum value."),
|
|
10436
10718
|
max: numberBoundSpec("Validates that a number is less than or equal to a maximum value.", ["@expect.max 10", "count: number"], "maxValue", "The maximum value."),
|
|
@@ -10438,7 +10720,20 @@ const expectAnnotations = {
|
|
|
10438
10720
|
exclusiveMax: numberBoundSpec("Validates that a number is strictly less than a value (the value itself is rejected).", ["@expect.exclusiveMax 1", "ratio: number"], "exclusiveMaxValue", "The exclusive upper bound (the value must be less than this)."),
|
|
10439
10721
|
int: new AnnotationSpec({
|
|
10440
10722
|
description: annotationExample(`Validates that a number is an integer (no decimal places). ${NULLABLE_NOTE}`, "@expect.int", "age: number"),
|
|
10441
|
-
defType: ["number"]
|
|
10723
|
+
defType: ["number"],
|
|
10724
|
+
argument: [MESSAGE_ARG]
|
|
10725
|
+
}),
|
|
10726
|
+
minKeys: keyCountSpec("at least", ["@expect.minKeys 1", "labels: { [*]: string }"], "The minimum number of keys."),
|
|
10727
|
+
maxKeys: keyCountSpec("at most", ["@expect.maxKeys 20", "labels: { [*]: string }"], "The maximum number of keys.", "expect.minKeys"),
|
|
10728
|
+
message: new AnnotationSpec({
|
|
10729
|
+
description: annotationExample("Custom error message for a type mismatch: wrong type (a non-finite number, a malformed decimal), not the literal (or not one of a literal union's values), no union branch, unknown discriminator, wrong tuple length. Constraint failures keep their own messages (`@expect.minLength` …, `@meta.required`).", "@expect.message 'Choose a valid status'", "status: 'active' | 'blocked'"),
|
|
10730
|
+
nodeType: ["prop", "type"],
|
|
10731
|
+
argument: {
|
|
10732
|
+
name: "message",
|
|
10733
|
+
type: "string",
|
|
10734
|
+
description: "The error message.",
|
|
10735
|
+
userText: true
|
|
10736
|
+
}
|
|
10442
10737
|
}),
|
|
10443
10738
|
pattern: new AnnotationSpec({
|
|
10444
10739
|
description: annotationExample(`Validates that a string matches a specific pattern. ${NULLABLE_NOTE}`, "@expect.pattern \"[a-z]+\", \"u\"", "name: string"),
|
|
@@ -10523,12 +10818,7 @@ const expectAnnotations = {
|
|
|
10523
10818
|
],
|
|
10524
10819
|
description: "Optional flags for the regular expression."
|
|
10525
10820
|
},
|
|
10526
|
-
|
|
10527
|
-
name: "message",
|
|
10528
|
-
optional: true,
|
|
10529
|
-
type: "string",
|
|
10530
|
-
description: "Optional error message to display if the validation fails."
|
|
10531
|
-
}
|
|
10821
|
+
MESSAGE_ARG
|
|
10532
10822
|
],
|
|
10533
10823
|
validate(mainToken, args) {
|
|
10534
10824
|
if (args[0]) {
|
|
@@ -10557,7 +10847,8 @@ const expectAnnotations = {
|
|
|
10557
10847
|
name: "message",
|
|
10558
10848
|
optional: true,
|
|
10559
10849
|
type: "string",
|
|
10560
|
-
description: "Optional custom error message when duplicate items are found."
|
|
10850
|
+
description: "Optional custom error message when duplicate items are found.",
|
|
10851
|
+
userText: true
|
|
10561
10852
|
}],
|
|
10562
10853
|
validate(token, args, doc) {
|
|
10563
10854
|
const field = token.parentNode;
|
|
@@ -10588,7 +10879,8 @@ const expectAnnotations = {
|
|
|
10588
10879
|
name: "message",
|
|
10589
10880
|
optional: true,
|
|
10590
10881
|
type: "string",
|
|
10591
|
-
description: "Optional custom error message (used by @expect.array.uniqueItems when checking key-based uniqueness)."
|
|
10882
|
+
description: "Optional custom error message (used by @expect.array.uniqueItems when checking key-based uniqueness).",
|
|
10883
|
+
userText: true
|
|
10592
10884
|
}],
|
|
10593
10885
|
validate(token, args, doc) {
|
|
10594
10886
|
const field = token.parentNode;
|
|
@@ -10624,7 +10916,8 @@ const metaAnnotations = {
|
|
|
10624
10916
|
argument: {
|
|
10625
10917
|
name: "text",
|
|
10626
10918
|
type: "string",
|
|
10627
|
-
description: "The label to be used for this field or entity."
|
|
10919
|
+
description: "The label to be used for this field or entity.",
|
|
10920
|
+
userText: true
|
|
10628
10921
|
}
|
|
10629
10922
|
}),
|
|
10630
10923
|
id: new AnnotationSpec({
|
|
@@ -10637,7 +10930,8 @@ const metaAnnotations = {
|
|
|
10637
10930
|
argument: {
|
|
10638
10931
|
name: "text",
|
|
10639
10932
|
type: "string",
|
|
10640
|
-
description: "Detailed description for this field or entity."
|
|
10933
|
+
description: "Detailed description for this field or entity.",
|
|
10934
|
+
userText: true
|
|
10641
10935
|
}
|
|
10642
10936
|
}),
|
|
10643
10937
|
documentation: new AnnotationSpec({
|
|
@@ -10667,7 +10961,8 @@ const metaAnnotations = {
|
|
|
10667
10961
|
name: "message",
|
|
10668
10962
|
optional: true,
|
|
10669
10963
|
type: "string",
|
|
10670
|
-
description: "Optional error message to display if the validation fails."
|
|
10964
|
+
description: "Optional error message to display if the validation fails.",
|
|
10965
|
+
userText: true
|
|
10671
10966
|
}],
|
|
10672
10967
|
passedWhenReferred: false
|
|
10673
10968
|
}),
|
|
@@ -10692,6 +10987,33 @@ const metaAnnotations = {
|
|
|
10692
10987
|
})
|
|
10693
10988
|
};
|
|
10694
10989
|
|
|
10990
|
+
//#endregion
|
|
10991
|
+
//#region packages/core/src/defaults/normalize-annotations.ts
|
|
10992
|
+
const NOTE = "Normalizers change the value before validation: they run where the input is normalized (`normalizeForType()`, the Moost validator pipe), never inside the validator itself. Applies to strings: a `string` field, a union of strings and literals (with `null` / `undefined`), or a type alias of one (its fields and array items inherit it). Order: trim, then lowercase, then uppercase.";
|
|
10993
|
+
function normalizeSpec(summary, example, validate) {
|
|
10994
|
+
return new AnnotationSpec({
|
|
10995
|
+
description: annotationExample(`${summary} ${NOTE}`, ...example),
|
|
10996
|
+
nodeType: ["prop", "type"],
|
|
10997
|
+
defType: ["string"],
|
|
10998
|
+
defTypeLiterals: true,
|
|
10999
|
+
multiple: false,
|
|
11000
|
+
validate
|
|
11001
|
+
});
|
|
11002
|
+
}
|
|
11003
|
+
const normalizeAnnotations = {
|
|
11004
|
+
trim: normalizeSpec("Removes leading and trailing whitespace from the string.", ["@normalize.trim", "name: string"]),
|
|
11005
|
+
lowercase: normalizeSpec("Converts the string to lower case.", [
|
|
11006
|
+
"@normalize.trim",
|
|
11007
|
+
"@normalize.lowercase",
|
|
11008
|
+
"email: string.email"
|
|
11009
|
+
]),
|
|
11010
|
+
uppercase: normalizeSpec("Converts the string to upper case.", ["@normalize.uppercase", "countryCode: string"], (mainToken) => mainToken.parentNode?.annotations?.some((a) => a.name === "normalize.lowercase") ? [{
|
|
11011
|
+
message: "@normalize.lowercase has no effect beside @normalize.uppercase (uppercase runs last)",
|
|
11012
|
+
range: mainToken.range,
|
|
11013
|
+
severity: TSeverity.Warning
|
|
11014
|
+
}] : [])
|
|
11015
|
+
};
|
|
11016
|
+
|
|
10695
11017
|
//#endregion
|
|
10696
11018
|
//#region packages/core/src/defaults/primitives.ts
|
|
10697
11019
|
const IPV4_PATTERN = "^((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)$";
|
|
@@ -10788,6 +11110,10 @@ const primitives = {
|
|
|
10788
11110
|
required: {
|
|
10789
11111
|
documentation: "Non-empty string that contains at least one non-whitespace character.",
|
|
10790
11112
|
annotations: { "meta.required": true }
|
|
11113
|
+
},
|
|
11114
|
+
trimmed: {
|
|
11115
|
+
documentation: "String with leading and trailing whitespace removed before validation (`@normalize.trim`).",
|
|
11116
|
+
annotations: { "normalize.trim": true }
|
|
10791
11117
|
}
|
|
10792
11118
|
}
|
|
10793
11119
|
},
|
|
@@ -10932,6 +11258,10 @@ const primitives = {
|
|
|
10932
11258
|
type: "void",
|
|
10933
11259
|
documentation: "Represents no value."
|
|
10934
11260
|
},
|
|
11261
|
+
unknown: {
|
|
11262
|
+
type: "any",
|
|
11263
|
+
documentation: "Any value (an opaque value: JSON data of any shape). Not checked by the validator; JSON Schema `{}`; TypeScript `unknown`."
|
|
11264
|
+
},
|
|
10935
11265
|
phantom: {
|
|
10936
11266
|
type: "phantom",
|
|
10937
11267
|
documentation: "Phantom type. Does not affect the data type, validation, or schema. Discoverable via runtime type traversal."
|
|
@@ -10946,7 +11276,9 @@ function getDefaultAtscriptConfig() {
|
|
|
10946
11276
|
annotations: {
|
|
10947
11277
|
meta: { ...metaAnnotations },
|
|
10948
11278
|
expect: { ...expectAnnotations },
|
|
10949
|
-
|
|
11279
|
+
normalize: { ...normalizeAnnotations },
|
|
11280
|
+
emit: { ...emitAnnotations },
|
|
11281
|
+
annotate: { ...annotateAnnotations }
|
|
10950
11282
|
}
|
|
10951
11283
|
};
|
|
10952
11284
|
}
|
|
@@ -11108,12 +11440,14 @@ var AtscriptRepo = class {
|
|
|
11108
11440
|
ref: "import(\"@atscript/typescript/utils\").AtscriptRef",
|
|
11109
11441
|
query: "import(\"@atscript/typescript/utils\").AtscriptQueryNode",
|
|
11110
11442
|
expr: "import(\"@atscript/typescript/utils\").AtscriptExprNode",
|
|
11111
|
-
order: "import(\"@atscript/typescript/utils\").AtscriptOrderItem[]"
|
|
11443
|
+
order: "import(\"@atscript/typescript/utils\").AtscriptOrderItem[]",
|
|
11444
|
+
value: "string | number | boolean"
|
|
11112
11445
|
};
|
|
11113
11446
|
const mapArgType = (t) => ANNOTATION_TYPE_MAP[t] || t;
|
|
11114
11447
|
const manager = await this.getSharedPluginManager();
|
|
11115
11448
|
const annotations = {};
|
|
11116
11449
|
if (manager) await manager.loopInAnnotationsSpec((name, spec) => {
|
|
11450
|
+
if (isAnnotateDirective(name)) return;
|
|
11117
11451
|
const types = [];
|
|
11118
11452
|
const multiple = !!spec.config.multiple;
|
|
11119
11453
|
if (Array.isArray(spec.config.argument)) {
|
|
@@ -11141,7 +11475,7 @@ var AtscriptRepo = class {
|
|
|
11141
11475
|
for (const doc of Array.from(this.atscripts.values())) {
|
|
11142
11476
|
const awaited = await doc;
|
|
11143
11477
|
for (const { name, token, args } of awaited.annotations) {
|
|
11144
|
-
if (annotations[name]?.fromSpec) continue;
|
|
11478
|
+
if (annotations[name]?.fromSpec || isAnnotateDirective(name)) continue;
|
|
11145
11479
|
if (!annotations[name]) annotations[name] = {
|
|
11146
11480
|
multiple: false,
|
|
11147
11481
|
fromSpec: false,
|
|
@@ -11491,6 +11825,7 @@ let tmpSeq = 0;
|
|
|
11491
11825
|
|
|
11492
11826
|
//#endregion
|
|
11493
11827
|
exports.$n = $n;
|
|
11828
|
+
exports.ANNOTATE_RESET = ANNOTATE_RESET;
|
|
11494
11829
|
exports.AnnotationSpec = AnnotationSpec;
|
|
11495
11830
|
exports.AtscriptDoc = AtscriptDoc;
|
|
11496
11831
|
exports.AtscriptRepo = AtscriptRepo;
|
|
@@ -11503,6 +11838,7 @@ exports.MERGE_CAP = MERGE_CAP;
|
|
|
11503
11838
|
exports.MERGE_TOO_COMPLEX = MERGE_TOO_COMPLEX;
|
|
11504
11839
|
exports.NAV_PROP_ANNOTATIONS = NAV_PROP_ANNOTATIONS;
|
|
11505
11840
|
exports.NOTHING_IN_COMMON = NOTHING_IN_COMMON;
|
|
11841
|
+
exports.OPAQUE_MERGE = OPAQUE_MERGE;
|
|
11506
11842
|
exports.PRODUCT_CAP = PRODUCT_CAP;
|
|
11507
11843
|
exports.PluginManager = PluginManager;
|
|
11508
11844
|
exports.SELF_OPERAND = SELF_OPERAND;
|
|
@@ -11537,6 +11873,7 @@ exports.Token = Token;
|
|
|
11537
11873
|
exports.UNION_IN_RECURSIVE_MERGE = UNION_IN_RECURSIVE_MERGE;
|
|
11538
11874
|
exports.analyzeQueryCursorContext = analyzeQueryCursorContext;
|
|
11539
11875
|
exports.andExprs = andExprs;
|
|
11876
|
+
exports.annotatedValueKinds = annotatedValueKinds;
|
|
11540
11877
|
exports.annotationExample = annotationExample;
|
|
11541
11878
|
exports.baseSlot = baseSlot;
|
|
11542
11879
|
exports.blockDependencies = blockDependencies;
|
|
@@ -11605,4 +11942,7 @@ exports.resolveFieldRefAt = resolveFieldRefAt;
|
|
|
11605
11942
|
exports.resolveFieldRefsAt = resolveFieldRefsAt;
|
|
11606
11943
|
exports.resolveQueryFieldRef = resolveQueryFieldRef;
|
|
11607
11944
|
exports.unshell = unshell;
|
|
11608
|
-
exports.unwoundFields = unwoundFields;
|
|
11945
|
+
exports.unwoundFields = unwoundFields;
|
|
11946
|
+
exports.userTextAnnotations = userTextAnnotations;
|
|
11947
|
+
exports.userTextFields = userTextFields;
|
|
11948
|
+
exports.withoutAsExtension = withoutAsExtension;
|