@atscript/core 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1485,11 +1485,12 @@ var AnnotationSpec = class {
1485
1485
  */ validateTargetType(def, range, doc) {
1486
1486
  const defType = this.config.defType;
1487
1487
  if (!defType?.length) return;
1488
- if (matchesDefType(def, defType)) return;
1488
+ const literals = this.config.defTypeLiterals;
1489
+ if (matchesDefType(def, defType, literals)) return;
1489
1490
  let got;
1490
1491
  if (isGroup(def) && describeNode(def) === "union") {
1491
1492
  const members = doc ? nonNullishMembers(def, doc) : void 0;
1492
- if (members?.length && members.every((m) => matchesDefType(m, defType))) return;
1493
+ if (members?.length && members.every((m) => matchesDefType(m, defType, literals))) return;
1493
1494
  got = `union (${describeUnionMembers(def, doc)})`;
1494
1495
  } else got = `"${describeNode(def)}"`;
1495
1496
  return [{
@@ -1556,13 +1557,14 @@ function isAnnotationSpec(a) {
1556
1557
  /**
1557
1558
  * Where annotation `spec` holds user-facing text (arguments flagged `userText`): `true` — its
1558
1559
  * value itself (a spec with one argument, not a list: the value is the string; for a `multiple`
1559
- * annotation, each value); a list — those fields of its value object (a spec with an argument
1560
- * list). `undefined`: no user-facing text.
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.
1561
1563
  */ function userTextFields(spec) {
1562
1564
  const arg = spec.config.argument;
1563
1565
  if (!arg) return;
1564
- if (!Array.isArray(arg)) return arg.userText ? true : void 0;
1565
- const fields = arg.filter((a) => a.userText).map((a) => a.name);
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);
1566
1568
  return fields.length > 0 ? fields : void 0;
1567
1569
  }
1568
1570
  /**
@@ -1590,8 +1592,18 @@ function resolveAnnotation(name, annotationsTree) {
1590
1592
  /** `null` / `undefined` / `void` primitive. */ function isNullishPrimitive(node) {
1591
1593
  return isPrimitive(node) && (node.type === "null" || node.type === "void");
1592
1594
  }
1593
- /** Whether a resolved node satisfies a `defType` guard (a union as a whole, not its members). */ function matchesDefType(def, defType) {
1594
- return defType.includes(describeNode(def));
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";
1595
1607
  }
1596
1608
  /** The `defType` vocabulary name of a resolved node (`string`, `object`, `union`, `array`, …). */ function describeNode(def) {
1597
1609
  if (isPrimitive(def)) return def.type === "any" ? "unknown" : def.type || "unknown";
@@ -1684,7 +1696,7 @@ function describeUnionMembers(def, doc) {
1684
1696
  }
1685
1697
  if (!node || seen.has(node)) return /* @__PURE__ */ new Set();
1686
1698
  seen.add(node);
1687
- if (node.entity === "const") return new Set([node.token("identifier")?.type === "number" ? "number" : "string"]);
1699
+ if (node.entity === "const") return new Set([literalType(node)]);
1688
1700
  if (isGroup(node) && node.entity !== "tuple" && node.op === "&") {
1689
1701
  let out;
1690
1702
  for (const m of node.unwrap()) {
@@ -10975,6 +10987,33 @@ const metaAnnotations = {
10975
10987
  })
10976
10988
  };
10977
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
+
10978
11017
  //#endregion
10979
11018
  //#region packages/core/src/defaults/primitives.ts
10980
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)$";
@@ -11071,6 +11110,10 @@ const primitives = {
11071
11110
  required: {
11072
11111
  documentation: "Non-empty string that contains at least one non-whitespace character.",
11073
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 }
11074
11117
  }
11075
11118
  }
11076
11119
  },
@@ -11233,6 +11276,7 @@ function getDefaultAtscriptConfig() {
11233
11276
  annotations: {
11234
11277
  meta: { ...metaAnnotations },
11235
11278
  expect: { ...expectAnnotations },
11279
+ normalize: { ...normalizeAnnotations },
11236
11280
  emit: { ...emitAnnotations },
11237
11281
  annotate: { ...annotateAnnotations }
11238
11282
  }
package/dist/index.d.ts CHANGED
@@ -2667,9 +2667,11 @@ interface TAnnotationArgument {
2667
2667
  * The argument is user-facing text (a label, a description, an error message): i18n tooling
2668
2668
  * and translators read the annotations holding such text through {@link userTextAnnotations}
2669
2669
  * (build time) and `userTextOf` / `userTextKeys` of `@atscript/typescript/utils` (runtime).
2670
- * A `string` argument only.
2670
+ * `'expression'`: the argument is the source of a function the client evaluates (a computed
2671
+ * label, a custom validator returning its message); the texts it writes are the string literals
2672
+ * in that source, which a translator extracts and serves next to it. A `string` argument only.
2671
2673
  */
2672
- userText?: boolean;
2674
+ userText?: boolean | 'expression';
2673
2675
  }
2674
2676
  interface TAnnotationSpecConfig {
2675
2677
  multiple?: boolean;
@@ -2687,6 +2689,11 @@ interface TAnnotationSpecConfig {
2687
2689
  description?: string;
2688
2690
  nodeType?: TNodeEntity[];
2689
2691
  defType?: Array<SemanticPrimitiveNode['type']>;
2692
+ /**
2693
+ * With `defType`: a literal of an accepted type matches too (`'a'` for `string`, `1` for
2694
+ * `number`), alone or as a union member (`'a' | 'b' | null`).
2695
+ */
2696
+ defTypeLiterals?: boolean;
2690
2697
  argument?: TAnnotationArgument[] | TAnnotationArgument;
2691
2698
  validate?: (mainToken: Token, args: Token[], doc: AtscriptDoc) => TMessages | undefined;
2692
2699
  modify?: (mainToken: Token, args: Token[], doc: AtscriptDoc) => void;
@@ -2745,15 +2752,16 @@ declare function forEachAnnotationSpec(tree: TAnnotationsTree, cb: (name: string
2745
2752
  /**
2746
2753
  * Where annotation `spec` holds user-facing text (arguments flagged `userText`): `true` — its
2747
2754
  * value itself (a spec with one argument, not a list: the value is the string; for a `multiple`
2748
- * annotation, each value); a list — those fields of its value object (a spec with an argument
2749
- * list). `undefined`: no user-facing text.
2755
+ * annotation, each value); `'expression'` — likewise, but the value is function source whose string
2756
+ * literals are the texts; a list — those fields of its value object (a spec with an argument
2757
+ * list), an expression field's name ending in `()`. `undefined`: no user-facing text.
2750
2758
  */
2751
- declare function userTextFields(spec: AnnotationSpec): true | string[] | undefined;
2759
+ declare function userTextFields(spec: AnnotationSpec): true | 'expression' | string[] | undefined;
2752
2760
  /**
2753
2761
  * Every annotation of `tree` (core and plugins) holding user-facing text, by full name (see
2754
2762
  * {@link userTextFields}): what i18n tooling extracts and a translator translates.
2755
2763
  */
2756
- declare function userTextAnnotations(tree: TAnnotationsTree): Map<string, true | string[]>;
2764
+ declare function userTextAnnotations(tree: TAnnotationsTree): Map<string, true | 'expression' | string[]>;
2757
2765
  declare function resolveAnnotation(name: string, annotationsTree?: TAnnotationsTree): AnnotationSpec | undefined;
2758
2766
  /**
2759
2767
  * Resolved members of a union other than `null` / `undefined`, or `undefined`
package/dist/index.mjs CHANGED
@@ -1454,11 +1454,12 @@ var AnnotationSpec = class {
1454
1454
  */ validateTargetType(def, range, doc) {
1455
1455
  const defType = this.config.defType;
1456
1456
  if (!defType?.length) return;
1457
- if (matchesDefType(def, defType)) return;
1457
+ const literals = this.config.defTypeLiterals;
1458
+ if (matchesDefType(def, defType, literals)) return;
1458
1459
  let got;
1459
1460
  if (isGroup(def) && describeNode(def) === "union") {
1460
1461
  const members = doc ? nonNullishMembers(def, doc) : void 0;
1461
- if (members?.length && members.every((m) => matchesDefType(m, defType))) return;
1462
+ if (members?.length && members.every((m) => matchesDefType(m, defType, literals))) return;
1462
1463
  got = `union (${describeUnionMembers(def, doc)})`;
1463
1464
  } else got = `"${describeNode(def)}"`;
1464
1465
  return [{
@@ -1525,13 +1526,14 @@ function isAnnotationSpec(a) {
1525
1526
  /**
1526
1527
  * Where annotation `spec` holds user-facing text (arguments flagged `userText`): `true` — its
1527
1528
  * value itself (a spec with one argument, not a list: the value is the string; for a `multiple`
1528
- * annotation, each value); a list — those fields of its value object (a spec with an argument
1529
- * list). `undefined`: no user-facing text.
1529
+ * annotation, each value); `'expression'` — likewise, but the value is function source whose string
1530
+ * literals are the texts; a list — those fields of its value object (a spec with an argument
1531
+ * list), an expression field's name ending in `()`. `undefined`: no user-facing text.
1530
1532
  */ function userTextFields(spec) {
1531
1533
  const arg = spec.config.argument;
1532
1534
  if (!arg) return;
1533
- if (!Array.isArray(arg)) return arg.userText ? true : void 0;
1534
- const fields = arg.filter((a) => a.userText).map((a) => a.name);
1535
+ if (!Array.isArray(arg)) return arg.userText || void 0;
1536
+ const fields = arg.filter((a) => a.userText).map((a) => a.userText === "expression" ? `${a.name}()` : a.name);
1535
1537
  return fields.length > 0 ? fields : void 0;
1536
1538
  }
1537
1539
  /**
@@ -1559,8 +1561,18 @@ function resolveAnnotation(name, annotationsTree) {
1559
1561
  /** `null` / `undefined` / `void` primitive. */ function isNullishPrimitive(node) {
1560
1562
  return isPrimitive(node) && (node.type === "null" || node.type === "void");
1561
1563
  }
1562
- /** Whether a resolved node satisfies a `defType` guard (a union as a whole, not its members). */ function matchesDefType(def, defType) {
1563
- return defType.includes(describeNode(def));
1564
+ /**
1565
+ * Whether a resolved node satisfies a `defType` guard (a union as a whole, not its members);
1566
+ * `literals`: a literal of an accepted type does too (see `defTypeLiterals`).
1567
+ */ function matchesDefType(def, defType, literals) {
1568
+ if (defType.includes(describeNode(def))) return true;
1569
+ return !!literals && isConst(def) && defType.includes(literalType(def));
1570
+ }
1571
+ /**
1572
+ * The `defType` vocabulary name of a literal's value (`'a'` → `string`, `1` → `number`; atscript
1573
+ * literals are strings and numbers).
1574
+ */ function literalType(def) {
1575
+ return def.token("identifier")?.type === "number" ? "number" : "string";
1564
1576
  }
1565
1577
  /** The `defType` vocabulary name of a resolved node (`string`, `object`, `union`, `array`, …). */ function describeNode(def) {
1566
1578
  if (isPrimitive(def)) return def.type === "any" ? "unknown" : def.type || "unknown";
@@ -1653,7 +1665,7 @@ function describeUnionMembers(def, doc) {
1653
1665
  }
1654
1666
  if (!node || seen.has(node)) return /* @__PURE__ */ new Set();
1655
1667
  seen.add(node);
1656
- if (node.entity === "const") return new Set([node.token("identifier")?.type === "number" ? "number" : "string"]);
1668
+ if (node.entity === "const") return new Set([literalType(node)]);
1657
1669
  if (isGroup(node) && node.entity !== "tuple" && node.op === "&") {
1658
1670
  let out;
1659
1671
  for (const m of node.unwrap()) {
@@ -10944,6 +10956,33 @@ const metaAnnotations = {
10944
10956
  })
10945
10957
  };
10946
10958
 
10959
+ //#endregion
10960
+ //#region packages/core/src/defaults/normalize-annotations.ts
10961
+ 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.";
10962
+ function normalizeSpec(summary, example, validate) {
10963
+ return new AnnotationSpec({
10964
+ description: annotationExample(`${summary} ${NOTE}`, ...example),
10965
+ nodeType: ["prop", "type"],
10966
+ defType: ["string"],
10967
+ defTypeLiterals: true,
10968
+ multiple: false,
10969
+ validate
10970
+ });
10971
+ }
10972
+ const normalizeAnnotations = {
10973
+ trim: normalizeSpec("Removes leading and trailing whitespace from the string.", ["@normalize.trim", "name: string"]),
10974
+ lowercase: normalizeSpec("Converts the string to lower case.", [
10975
+ "@normalize.trim",
10976
+ "@normalize.lowercase",
10977
+ "email: string.email"
10978
+ ]),
10979
+ uppercase: normalizeSpec("Converts the string to upper case.", ["@normalize.uppercase", "countryCode: string"], (mainToken) => mainToken.parentNode?.annotations?.some((a) => a.name === "normalize.lowercase") ? [{
10980
+ message: "@normalize.lowercase has no effect beside @normalize.uppercase (uppercase runs last)",
10981
+ range: mainToken.range,
10982
+ severity: TSeverity.Warning
10983
+ }] : [])
10984
+ };
10985
+
10947
10986
  //#endregion
10948
10987
  //#region packages/core/src/defaults/primitives.ts
10949
10988
  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)$";
@@ -11040,6 +11079,10 @@ const primitives = {
11040
11079
  required: {
11041
11080
  documentation: "Non-empty string that contains at least one non-whitespace character.",
11042
11081
  annotations: { "meta.required": true }
11082
+ },
11083
+ trimmed: {
11084
+ documentation: "String with leading and trailing whitespace removed before validation (`@normalize.trim`).",
11085
+ annotations: { "normalize.trim": true }
11043
11086
  }
11044
11087
  }
11045
11088
  },
@@ -11202,6 +11245,7 @@ function getDefaultAtscriptConfig() {
11202
11245
  annotations: {
11203
11246
  meta: { ...metaAnnotations },
11204
11247
  expect: { ...expectAnnotations },
11248
+ normalize: { ...normalizeAnnotations },
11205
11249
  emit: { ...emitAnnotations },
11206
11250
  annotate: { ...annotateAnnotations }
11207
11251
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/core",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "Core library for Atscript parsing and file generation.",
5
5
  "keywords": [
6
6
  "annotations",