lemmascript 0.6.12 → 0.6.14

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.
@@ -8,6 +8,19 @@
8
8
  export function isBigInt(ty) {
9
9
  return (ty.kind === "int" || ty.kind === "nat") && !!ty.big;
10
10
  }
11
+ /** Which absent sentinel an `optional` type models (`"undefined"` by default). */
12
+ export function optAbsent(ty) {
13
+ return ty.absent ?? "undefined";
14
+ }
15
+ /** True for any nullable storage type (`Option<T>` or `Nullish<T>`). */
16
+ export function isNullableTy(ty) {
17
+ return ty.kind === "optional" || ty.kind === "nullish";
18
+ }
19
+ /** True when a value's runtime storage is `Nullish<T>` (either unnarrowed
20
+ * `nullish` or partially narrowed `optional` with `fromNullish: true`). */
21
+ export function isNullishStorageTy(ty) {
22
+ return ty.kind === "nullish" || (ty.kind === "optional" && !!ty.fromNullish);
23
+ }
11
24
  /** Structural equality on Ty. Used to decide whether a tuple type is homogeneous
12
25
  * (all elements equal ⇒ lower to `seq`) vs heterogeneous (⇒ keep as `tuple`). */
13
26
  export function tyEqual(a, b) {
@@ -24,7 +37,11 @@ export function tyEqual(a, b) {
24
37
  const bm = b;
25
38
  return tyEqual(a.key, bm.key) && tyEqual(a.value, bm.value);
26
39
  }
27
- case "optional": return tyEqual(a.inner, b.inner);
40
+ case "optional": {
41
+ const bo = b;
42
+ return tyEqual(a.inner, bo.inner) && optAbsent(a) === optAbsent(bo) && !!a.fromNullish === !!bo.fromNullish;
43
+ }
44
+ case "nullish": return tyEqual(a.inner, b.inner);
28
45
  case "user": return a.name === b.name;
29
46
  case "fn": {
30
47
  const bf = b;
@@ -41,6 +58,7 @@ export function tyEqual(a, b) {
41
58
  case "bool":
42
59
  case "real":
43
60
  case "void":
61
+ case "null":
44
62
  case "unknown": return true; // no payload
45
63
  }
46
64
  }
@@ -41,9 +41,38 @@ function tyFromTypeNode(tn) {
41
41
  if (Node.isUnionTypeNode(tn)) {
42
42
  const arms = tn.getTypeNodes();
43
43
  const isBoolLit = (a) => Node.isLiteralTypeNode(a) && (a.getLiteral().getKind() === SyntaxKind.TrueKeyword || a.getLiteral().getKind() === SyntaxKind.FalseKeyword);
44
- const isNullish = (a) => a.getKind() === SyntaxKind.NullKeyword ||
45
- a.getKind() === SyntaxKind.UndefinedKeyword ||
44
+ const isNullNode = (a) => a.getKind() === SyntaxKind.NullKeyword ||
46
45
  (Node.isLiteralTypeNode(a) && a.getLiteral().getKind() === SyntaxKind.NullKeyword);
46
+ const isUndefNode = (a) => a.getKind() === SyntaxKind.UndefinedKeyword;
47
+ const isNullish = (a) => isNullNode(a) || isUndefNode(a);
48
+ const hasNull = arms.some(isNullNode);
49
+ const hasUndef = arms.some(isUndefNode);
50
+ const wrapNullability = (inner) => {
51
+ let payload = inner;
52
+ let mergedNull = hasNull;
53
+ let mergedUndef = hasUndef;
54
+ while (payload.kind === "optional" || payload.kind === "nullish") {
55
+ if (payload.kind === "nullish") {
56
+ mergedNull = true;
57
+ mergedUndef = true;
58
+ payload = payload.inner;
59
+ }
60
+ else {
61
+ if (payload.absent === "null")
62
+ mergedNull = true;
63
+ else
64
+ mergedUndef = true;
65
+ payload = payload.inner;
66
+ }
67
+ }
68
+ if (mergedNull && mergedUndef)
69
+ return { kind: "nullish", inner: payload };
70
+ if (mergedNull)
71
+ return { kind: "optional", inner: payload, absent: "null" };
72
+ if (mergedUndef)
73
+ return { kind: "optional", inner: payload };
74
+ return payload;
75
+ };
47
76
  // Collapse expanded boolean (`true | false`) into a single bool slot, so
48
77
  // `boolean | undefined` (which TS expands to `false | true | undefined`)
49
78
  // still reads as `optional<bool>` rather than falling through to user.
@@ -56,6 +85,13 @@ function tyFromTypeNode(tn) {
56
85
  if (normalized.length === 1 && "syntheticBool" in normalized[0])
57
86
  return { kind: "bool" };
58
87
  const nonNullish = normalized.filter(a => "syntheticBool" in a || !isNullish(a.node));
88
+ if (nonNullish.length === 0) {
89
+ if (hasNull && hasUndef)
90
+ return { kind: "nullish", inner: { kind: "void" } };
91
+ if (hasNull)
92
+ return { kind: "null" };
93
+ return { kind: "void" };
94
+ }
59
95
  // Inline string-literal union (`"a" | "b"`, not a //@ declare-type): no datatype
60
96
  // to resolve against, so lower to plain string (the arms are strings; == holds).
61
97
  const isStrLit = (a) => !("syntheticBool" in a) && Node.isLiteralTypeNode(a.node) && a.node.getLiteral().getKind() === SyntaxKind.StringLiteral;
@@ -66,9 +102,7 @@ function tyFromTypeNode(tn) {
66
102
  const inner = Node.isLiteralTypeNode(lit) ? lit.getLiteral() : lit;
67
103
  return Node.isStringLiteral(inner) ? inner.getLiteralValue() : inner.getText();
68
104
  });
69
- return normalized.some(a => !("syntheticBool" in a) && isNullish(a.node))
70
- ? { kind: "optional", inner: { kind: "string", values } }
71
- : { kind: "string", values };
105
+ return wrapNullability({ kind: "string", values });
72
106
  }
73
107
  // Numeric literal unions have the same integer model as number, including
74
108
  // in inline annotations and optional types with several numeric arms.
@@ -78,24 +112,21 @@ function tyFromTypeNode(tn) {
78
112
  const ty = tyFromTypeNode(a.node);
79
113
  return ty.kind === "int" && !ty.big;
80
114
  })) {
81
- const inner = { kind: "int" };
82
- return normalized.some(a => !("syntheticBool" in a) && isNullish(a.node))
83
- ? { kind: "optional", inner } : inner;
115
+ return wrapNullability({ kind: "int" });
84
116
  }
85
117
  if (nonNullish.length === 1 && normalized.length >= 2) {
86
118
  const sole = nonNullish[0];
87
119
  const inner = "syntheticBool" in sole ? { kind: "bool" } : tyFromTypeNode(sole.node);
88
- return { kind: "optional", inner };
120
+ return wrapNullability(inner);
89
121
  }
90
122
  // Multi-member union with a nullish arm (`A | B | undefined`): the
91
123
  // structured part is opaque to us, but the optionality isn't. Expose it as
92
- // optional<user "A | B"> so downstream (e.g. the let-type merge in resolve)
93
- // can keep the nullability while taking structure from a more precise
94
- // source. Without this the whole thing collapses to one opaque user blob.
95
- const hadNullish = normalized.some(a => !("syntheticBool" in a) && isNullish(a.node));
96
- if (hadNullish && nonNullish.length >= 2) {
124
+ // optional/nullish<user "A | B"> so downstream (e.g. the let-type merge in
125
+ // resolve) can keep the nullability while taking structure from a more
126
+ // precise source. Without this the whole thing collapses to one opaque blob.
127
+ if ((hasNull || hasUndef) && nonNullish.length >= 2) {
97
128
  const innerName = nonNullish.map(a => ("syntheticBool" in a ? "boolean" : a.node.getText())).join(" | ");
98
- return { kind: "optional", inner: { kind: "user", name: innerName } };
129
+ return wrapNullability({ kind: "user", name: innerName });
99
130
  }
100
131
  // Other unions: leave as a user type spelled how the source wrote it.
101
132
  return { kind: "user", name: tn.getText() };
@@ -123,6 +154,8 @@ function tyFromTypeNode(tn) {
123
154
  if (Node.isLiteralTypeNode(tn)) {
124
155
  const literal = tn.getLiteral();
125
156
  const lk = literal.getKind();
157
+ if (lk === SyntaxKind.NullKeyword)
158
+ return { kind: "null" };
126
159
  if (lk === SyntaxKind.TrueKeyword || lk === SyntaxKind.FalseKeyword)
127
160
  return { kind: "bool" };
128
161
  if (Node.isNumericLiteral(literal) ||
@@ -138,6 +171,8 @@ function tyFromTypeNode(tn) {
138
171
  return { kind: "bool" };
139
172
  case SyntaxKind.StringKeyword:
140
173
  return { kind: "string" };
174
+ case SyntaxKind.NullKeyword:
175
+ return { kind: "null" };
141
176
  case SyntaxKind.VoidKeyword:
142
177
  case SyntaxKind.UndefinedKeyword:
143
178
  return { kind: "void" };
@@ -177,12 +212,14 @@ export function tyToCanonical(ty) {
177
212
  case "real": return "real";
178
213
  case "string": return "string";
179
214
  case "void": return "void";
215
+ case "null": return "null";
180
216
  case "unknown": return "unknown";
181
217
  case "array": return `seq<${tyToCanonical(ty.elem)}>`;
182
218
  case "tuple": return `(${ty.elems.map(tyToCanonical).join(", ")})`;
183
219
  case "map": return `map<${tyToCanonical(ty.key)}, ${tyToCanonical(ty.value)}>`;
184
220
  case "set": return `set<${tyToCanonical(ty.elem)}>`;
185
221
  case "optional": return `Option<${tyToCanonical(ty.inner)}>`;
222
+ case "nullish": return `Nullish<${tyToCanonical(ty.inner)}>`;
186
223
  case "user": return ty.name;
187
224
  case "fn": return `(${ty.params.map(tyToCanonical).join(", ")}) -> ${tyToCanonical(ty.result)}`;
188
225
  }