Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

examples 30-36: non-exhaustive and wrong-arm rejections, regex, await in strings, ? operator and misuse, literal patterns

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9dcde1690dd617265b64861f019091be3e6ccec4
Parent 9ba9e6b Browse filesHistory

Changed files

14 files changed282 additions0 deletionsagainst 9ba9e6b

  1. Addedexamples/30-non-exhaustive-match/example.json+9−0
  2. Addedexamples/30-non-exhaustive-match/src/main.tsus+27−0
  3. Addedexamples/31-wrong-arm-type/example.json+9−0
  4. Addedexamples/31-wrong-arm-type/src/main.tsus+27−0
  5. Addedexamples/32-regex-literals/example.json+1−0
  6. Addedexamples/32-regex-literals/src/main.tsus+23−0
  7. Addedexamples/33-await-in-string/example.json+1−0
  8. Addedexamples/33-await-in-string/src/main.tsus+27−0
  9. Addedexamples/34-try-operator/example.json+16−0
  10. Addedexamples/34-try-operator/src/main.tsus+59−0
  11. Addedexamples/35-try-misuse/example.json+12−0
  12. Addedexamples/35-try-misuse/src/main.tsus+38−0
  13. Addedexamples/36-literal-patterns/example.json+1−0
  14. Addedexamples/36-literal-patterns/src/main.tsus+32−0
Addedexamples/30-non-exhaustive-match/example.json+9−0
@@ -0,0 +1,9 @@
+{
+ "expect": "check-fail",
+ "codes": ["TSUS201"],
+ "diagnostics": [
+ { "code": "TSUS201", "file": "src/main.tsus", "line": 7, "col": 17, "tsCode": 2345 },
+ { "code": "TSUS201", "file": "src/main.tsus", "line": 14, "col": 13, "tsCode": 2345 },
+ { "code": "TSUS201", "file": "src/main.tsus", "line": 22, "col": 3, "tsCode": 2554 }
+ ]
+}
View file
Addedexamples/30-non-exhaustive-match/src/main.tsus+27−0
@@ -0,0 +1,27 @@
+import { tag, type Tagged, type Result } from "@tsus/runtime";
+
+type Light = Tagged<"Red", {}> | Tagged<"Amber", {}> | Tagged<"Green", {}>;
+
+// Forgot Amber: a compile-time error, not a runtime TSUS100.
+const next = (light: Light): Light =>
+ match (light) {
+ Red => tag("Green", {}),
+ Green => tag("Amber", {}),
+ };
+
+// Forgot Err.
+const report = (r: Result<number, string>): string =>
+ match (r) {
+ Ok(n) => `got ${n}`,
+ };
+
+type Method = "GET" | "POST" | "DELETE";
+
+// Literal patterns over a finite union must cover it, or add a _ arm.
+const verb = (m: Method): string =>
+ match (m) {
+ "GET" => "read",
+ "POST" => "write",
+ };
+
+console.log(next(tag("Red", {})).tag, report, verb);
View file
Addedexamples/31-wrong-arm-type/example.json+9−0
@@ -0,0 +1,9 @@
+{
+ "expect": "check-fail",
+ "codes": ["TSUS200", "TSUS202"],
+ "diagnostics": [
+ { "code": "TSUS200", "file": "src/main.tsus", "line": 8, "tsCode": 2322 },
+ { "code": "TSUS200", "file": "src/main.tsus", "line": 17, "col": 23, "tsCode": 2339 },
+ { "code": "TSUS202", "file": "src/main.tsus", "line": 23, "col": 5 }
+ ]
+}
View file
Addedexamples/31-wrong-arm-type/src/main.tsus+27−0
@@ -0,0 +1,27 @@
+import { Ok, Err, type Result, type Option } from "@tsus/runtime";
+
+const parse = (raw: string): Result<number, number> =>
+ raw === "" ? Err(400) : Ok(raw.length);
+
+// An arm returns a number where the function promises a string.
+const label = (raw: string): string =>
+ match (parse(raw)) {
+ Ok(n) => n * 2,
+ Err(code) => `error ${code}`,
+ };
+
+// The Err payload is a number, so it has no toUpperCase.
+const shout = (raw: string): string =>
+ match (parse(raw)) {
+ Ok(n) => `${n}`,
+ Err(code) => code.toUpperCase(),
+ };
+
+// Some is misspelled: the subject has no variant called Sme.
+const first = (o: Option<string>): string =>
+ match (o) {
+ Sme(s) => s,
+ None => "none",
+ };
+
+console.log(label, shout, first);
View file
Addedexamples/32-regex-literals/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["a (had junk)", "plain (clean)", "empty", "true", "5"] }
View file
Addedexamples/32-regex-literals/src/main.tsus+23−0
@@ -0,0 +1,23 @@
+import { Some, None, type Option } from "@tsus/runtime";
+
+// Regex literals full of the characters a hand-written scanner trips on:
+// braces, quotes, a backtick, and text that looks like a match block.
+const braces = /[{}]+/g;
+const quotes = /["'`]/g;
+const decoy = /match \(x\) \{ Some\(y\) => y \}/;
+const half = 10 / 2 / 1;
+
+const clean = (raw: string): Option<string> =>
+ raw.length === 0 ? None : Some(raw.replace(braces, "").replace(quotes, ""));
+
+const show = (raw: string): string =>
+ match (clean(raw)) {
+ Some(s) => `${s} (${/[{"]/.test(raw) ? "had junk" : "clean"})`,
+ None => "empty",
+ };
+
+console.log(show(`{"a"}`));
+console.log(show("plain"));
+console.log(show(""));
+console.log(decoy.test("match (x) { Some(y) => y }"));
+console.log(half);
View file
Addedexamples/33-await-in-string/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["await me", "nothing to await", "slow 3", "await failed: boom"] }
View file
Addedexamples/33-await-in-string/src/main.tsus+27−0
@@ -0,0 +1,27 @@
+import { Some, None, Ok, Err, type Option, type Result } from "@tsus/runtime";
+
+// The word await appears only inside strings and comments, so these arms
+// stay synchronous and the match returns a plain string, not a Promise.
+const label = (o: Option<string>): string =>
+ match (o) {
+ Some(x) => `await ${x}`, // await in a template
+ None => "nothing to await",
+ };
+
+console.log(label(Some("me")));
+console.log(label(None));
+
+const slow = async (n: number): Promise<string> => {
+ await Promise.resolve();
+ return `slow ${n}`;
+};
+
+// A real await in one arm makes every arm async; the string one is untouched.
+const describe = async (r: Result<number, string>): Promise<string> =>
+ await match (r) {
+ Ok(n) => await slow(n),
+ Err(e) => "await failed: " + e,
+ };
+
+console.log(await describe(Ok(3)));
+console.log(await describe(Err("boom")));
View file
Addedexamples/34-try-operator/example.json+16−0
@@ -0,0 +1,16 @@
+{
+ "expect": "run",
+ "output": [
+ "ok privileged port 80",
+ "ok user port 8080",
+ "ok signed 443",
+ "err bad port: x",
+ "ok 3",
+ "err bad port: zz",
+ "3000",
+ "-1",
+ "ok 42",
+ "err bad port: no",
+ "EXAMPLE"
+ ]
+}
View file
Addedexamples/34-try-operator/src/main.tsus+59−0
@@ -0,0 +1,59 @@
+import { Ok, Err, Some, None, type Result, type Option } from "@tsus/runtime";
+
+const parsePort = (raw: string): Result<number, string> => {
+ const n = Number(raw);
+ return Number.isInteger(n) && n > 0 ? Ok(n) : Err(`bad port: ${raw}`);
+};
+
+// ? on a Result returns the Err early.
+const describePort = (raw: string): Result<string, string> => {
+ const port = parsePort(raw)?;
+ const kind = port < 1024 ? "privileged" : "user";
+ if (raw.startsWith("+")) return Ok(`signed ${parsePort(raw.slice(1))?}`);
+ return Ok(`${kind} port ${port}`);
+};
+
+// Several ? in one expression run left to right.
+const sum = (a: string, b: string): Result<number, string> => Ok((parsePort(a)?) + (parsePort(b)?));
+
+// ? on an Option returns None early.
+const lookup = (env: Map<string, string>, key: string): Option<string> =>
+ env.has(key) ? Some(env.get(key) ?? "") : None;
+
+const portFrom = (env: Map<string, string>): Option<number> => {
+ const raw = lookup(env, "PORT")?;
+ return parsePort(raw).ok();
+};
+
+// Async: ? applies to the awaited Result.
+const fetchPort = async (raw: string): Promise<Result<number, string>> => {
+ await Promise.resolve();
+ return parsePort(raw);
+};
+
+const doubled = async (raw: string): Promise<Result<number, string>> => {
+ const port = await fetchPort(raw)?;
+ return Ok(port * 2);
+};
+
+const show = (r: Result<string | number, string>): string =>
+ match (r) {
+ Ok(v) => `ok ${v}`,
+ Err(e) => `err ${e}`,
+ };
+
+console.log(show(describePort("80")));
+console.log(show(describePort("8080")));
+console.log(show(describePort("+443")));
+console.log(show(describePort("x")));
+console.log(show(sum("1", "2")));
+console.log(show(sum("1", "zz")));
+console.log(portFrom(new Map([["PORT", "3000"]])).unwrapOr(-1));
+console.log(portFrom(new Map()).unwrapOr(-1));
+console.log(show(await doubled("21")));
+console.log(show(await doubled("no")));
+
+// Optional chaining, nullish coalescing, and ternaries are not the ? operator.
+const env = new Map([["HOST", "example"]]);
+const host = env.get("HOST")?.toUpperCase() ?? "none";
+console.log(host.length > 3 ? host : "short");
View file
Addedexamples/35-try-misuse/example.json+12−0
@@ -0,0 +1,12 @@
+{
+ "expect": "check-fail",
+ "codes": ["TSUS300", "TSUS301", "TSUS302", "TSUS303"],
+ "diagnostics": [
+ { "code": "TSUS300", "file": "src/main.tsus", "line": 7, "col": 23 },
+ { "code": "TSUS300", "file": "src/main.tsus", "line": 12, "col": 25 },
+ { "code": "TSUS301", "file": "src/main.tsus", "line": 16, "col": 13, "tsCode": 2345 },
+ { "code": "TSUS302", "file": "src/main.tsus", "line": 22, "col": 23, "tsCode": 2322 },
+ { "code": "TSUS302", "file": "src/main.tsus", "line": 28, "col": 14, "tsCode": 2322 },
+ { "code": "TSUS303", "file": "src/main.tsus", "line": 34 }
+ ]
+}
View file
Addedexamples/35-try-misuse/src/main.tsus+38−0
@@ -0,0 +1,38 @@
+import { Ok, Err, Some, type Result, type Option } from "@tsus/runtime";
+
+const parse = (raw: string): Result<number, string> => (raw === "" ? Err("empty") : Ok(raw.length));
+
+// TSUS300: the function does not declare a Result or Option return type.
+const total = (raw: string): number => {
+ const n = parse(raw)?;
+ return n;
+};
+
+// TSUS300: no function to return from at all.
+const top = parse("abc")?;
+
+// TSUS301: ? needs a Result or Option, not a string.
+const name = (raw: string): Result<string, string> => {
+ const s = raw?;
+ return Ok(s);
+};
+
+// TSUS302: the Err carries a string but the function's error type is number.
+const coded = (raw: string): Result<number, number> => {
+ const n = parse(raw)?;
+ return Ok(n);
+};
+
+// TSUS302: None cannot propagate out of a function returning Result.
+const fromOption = (o: Option<number>): Result<number, string> => {
+ const n = o?;
+ return Ok(n);
+};
+
+// TSUS303: the right side of && may not run, so it cannot hold a ?.
+const guarded = (raw: string): Result<boolean, string> => {
+ const ok = raw.length > 0 && (parse(raw)?) > 1;
+ return Ok(ok);
+};
+
+console.log(total, top, name, coded, fromOption, guarded, Some);
View file
Addedexamples/36-literal-patterns/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["write negative off missing other"] }
View file
Addedexamples/36-literal-patterns/src/main.tsus+32−0
@@ -0,0 +1,32 @@
+type Method = "GET" | "POST" | "DELETE";
+
+// Literal arms that cover a finite union need no _ arm.
+const verb = (m: Method): string =>
+ match (m) {
+ "GET" => "read",
+ "POST" => "write",
+ "DELETE" => "remove",
+ };
+
+const sign = (n: -1 | 0 | 1): string =>
+ match (n) {
+ -1 => "negative",
+ 0 => "zero",
+ 1 => "positive",
+ };
+
+const toggle = (on: boolean): string =>
+ match (on) {
+ true => "on",
+ false => "off",
+ };
+
+// A plain number is not finite, so it needs _.
+const status = (code: number): string =>
+ match (code) {
+ 200 => "ok",
+ 404 => "missing",
+ _ => "other",
+ };
+
+console.log(verb("POST"), sign(-1), toggle(false), status(404), status(500));
View file

Checks

No checks reported for this commit.

Runs

No runs for this commit.