@murky-web/typebuddy 2.1.0 → 3.0.1

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.
Files changed (46) hide show
  1. package/README.md +184 -72
  2. package/dist/collections.js +1 -2
  3. package/dist/empty.js +34 -13
  4. package/dist/guards.js +33 -9
  5. package/dist/ids.js +6 -6
  6. package/dist/oxlint.js +7 -7
  7. package/dist/parse.js +39 -15
  8. package/dist/result.js +6 -6
  9. package/dist/rules/async_rule.js +39 -25
  10. package/dist/rules/deliberate_throw.js +5 -6
  11. package/dist/rules/maybe_promise_rule.js +99 -40
  12. package/dist/rules/maybe_rule.js +7 -5
  13. package/dist/rules/nullable_rule.js +7 -5
  14. package/dist/rules/optional_rule.js +8 -4
  15. package/dist/rules/own_subtree.js +1 -2
  16. package/dist/rules/test_file.js +63 -0
  17. package/dist/rules/typebuddy_import.js +242 -35
  18. package/dist/rules/use_server.js +1 -2
  19. package/globals.d.ts +49 -44
  20. package/jsr.json +24 -23
  21. package/oxlint/index.ts +9 -7
  22. package/package.json +92 -91
  23. package/rules/async_rule.ts +91 -75
  24. package/rules/deliberate_throw.ts +11 -11
  25. package/rules/maybe_promise_rule.ts +238 -111
  26. package/rules/maybe_rule.ts +76 -49
  27. package/rules/nullable_rule.ts +77 -52
  28. package/rules/optional_rule.ts +79 -53
  29. package/rules/own_subtree.ts +33 -33
  30. package/rules/test_file.ts +114 -0
  31. package/rules/typebuddy_import.ts +479 -87
  32. package/rules/use_server.ts +5 -4
  33. package/src/collections.ts +1 -1
  34. package/src/empty.ts +58 -20
  35. package/src/guards.ts +100 -34
  36. package/src/ids.ts +10 -4
  37. package/src/index.ts +1 -18
  38. package/src/parse.ts +108 -29
  39. package/src/public_api.ts +22 -0
  40. package/src/result.ts +8 -7
  41. package/src/types/globals.ts +11 -11
  42. package/src/types/index.ts +21 -21
  43. package/src/types/json.ts +48 -11
  44. package/src/types/maybe_promise.ts +17 -12
  45. package/src/types/nullable.ts +2 -2
  46. package/src/types/optional.ts +6 -6
package/src/result.ts CHANGED
@@ -13,20 +13,21 @@ import type { Failed, Result, Success } from "./types/maybe_promise.js";
13
13
  * ```typescript
14
14
  * const result = await someAsyncFunction();
15
15
  * if (isSuccess(result)) {
16
- * return result.value; // TypeScript knows `value` is of type `T`
16
+ * return result.value; // TypeScript knows `value` is of type `T`
17
17
  * }
18
18
  * ```
19
19
  *
20
20
  * @template R The result type passed in. The guard narrows it by its `isError`
21
- * flag, so `value` keeps the success type without an extra `| null`.
22
- * @param {R} result The result to check, which can be either a `Success<T>`
23
- * or `Failed`.
21
+ * flag, so `value` keeps the success type without an extra `| null`. A
22
+ * `Result<T | null>`, a generic `Result<T>` and a `Result<any>` all work.
23
+ * @param {R} result The result to check, which can be either a `Success<T>` or
24
+ * `Failed`.
24
25
  * @returns {boolean} `true` if the result is a `Success<T>`, `false` if it is a
25
26
  * `Failed`. This function acts as a type guard, narrowing `result` to its
26
27
  * `Success` member when the condition is met.
27
28
  */
28
29
  function isSuccess<R extends Readonly<Result<unknown>>>(
29
- result: R,
30
+ result: R
30
31
  ): result is Extract<R, { isError: false }> {
31
32
  return !result.isError;
32
33
  }
@@ -39,7 +40,7 @@ function ok<T>(...args: readonly [T] | []): Success<T | void> {
39
40
  const [value] = args;
40
41
  return {
41
42
  isError: false,
42
- value,
43
+ value
43
44
  };
44
45
  }
45
46
 
@@ -51,7 +52,7 @@ function err<T extends null>(...args: readonly [T] | []): Failed {
51
52
  const [value] = args;
52
53
  return {
53
54
  isError: true,
54
- value: value ?? null,
55
+ value: value ?? null
55
56
  };
56
57
  }
57
58
 
@@ -1,13 +1,13 @@
1
1
  export type {
2
- Failed,
3
- JsonifiedValue,
4
- Maybe,
5
- MaybePromise,
6
- Nullable,
7
- Optional,
8
- ResolveMaybe,
9
- ResolveNullable,
10
- ResolveOptional,
11
- Stringified,
12
- Success,
2
+ Failed,
3
+ JsonifiedValue,
4
+ Maybe,
5
+ MaybePromise,
6
+ Nullable,
7
+ Optional,
8
+ ResolveMaybe,
9
+ ResolveNullable,
10
+ ResolveOptional,
11
+ Stringified,
12
+ Success
13
13
  } from "./index.js";
@@ -1,28 +1,28 @@
1
- import type {
2
- AsyncResult,
3
- Failed,
4
- MaybePromise,
5
- Result,
6
- Success,
7
- } from "./maybe_promise.js";
8
1
  import type { JsonifiedObject, JsonifiedValue, Stringified } from "./json.js";
9
2
  import type { Maybe, ResolveMaybe } from "./maybe.js";
3
+ import type {
4
+ AsyncResult,
5
+ Failed,
6
+ MaybePromise,
7
+ Result,
8
+ Success
9
+ } from "./maybe_promise.js";
10
10
  import type { Nullable, ResolveNullable } from "./nullable.js";
11
11
  import type { Optional, ResolveOptional } from "./optional.js";
12
12
 
13
13
  export type {
14
- AsyncResult,
15
- Failed,
16
- JsonifiedObject,
17
- JsonifiedValue,
18
- Maybe,
19
- MaybePromise,
20
- Nullable,
21
- Optional,
22
- ResolveMaybe,
23
- ResolveNullable,
24
- ResolveOptional,
25
- Result,
26
- Stringified,
27
- Success,
14
+ AsyncResult,
15
+ Failed,
16
+ JsonifiedObject,
17
+ JsonifiedValue,
18
+ Maybe,
19
+ MaybePromise,
20
+ Nullable,
21
+ Optional,
22
+ ResolveMaybe,
23
+ ResolveNullable,
24
+ ResolveOptional,
25
+ Result,
26
+ Stringified,
27
+ Success
28
28
  };
package/src/types/json.ts CHANGED
@@ -1,19 +1,56 @@
1
1
  type UnknownFunction = (...args: readonly unknown[]) => unknown;
2
2
 
3
+ /** What `JSON.stringify` serialises to `{}`: it has no enumerable own data. */
4
+ type EmptyJsonObject = Record<string, never>;
5
+
6
+ /**
7
+ * Array and tuple elements JSON cannot hold become `null` instead of vanishing
8
+ * (`JSON.stringify([undefined])` is `"[null]"`), so the element type keeps a
9
+ * `null` where an object property would be dropped.
10
+ */
11
+ type JsonifiedElement<T> = T extends undefined | symbol | UnknownFunction
12
+ ? null
13
+ : JsonifiedValue<T>;
14
+
15
+ /**
16
+ * Arrays and tuples are mapped element by element with a plain homomorphic
17
+ * mapped type, which keeps them arrays and tuples. `JsonifiedObject` cannot do
18
+ * this: its key remapping turns an array into an object type without `map`. The
19
+ * result is mutable, because `JSON.parse` builds fresh arrays.
20
+ */
21
+ type JsonifiedArray<T extends readonly unknown[]> = {
22
+ -readonly [Index in keyof T]: JsonifiedElement<T[Index]>;
23
+ };
24
+
25
+ /**
26
+ * The type `JSON.parse` gives back for a value that went through
27
+ * `JSON.stringify`.
28
+ *
29
+ * `number` stays `number`: the type system has no way to say "not NaN or
30
+ * Infinity", and `JSON.stringify` turns those two into `null` at runtime.
31
+ */
3
32
  type JsonifiedValue<T> = T extends string | number | null | boolean
4
- ? T
5
- : T extends { toJSON: () => infer R }
6
- ? R
7
- : T extends undefined | UnknownFunction
8
- ? never
9
- : T extends object
10
- ? JsonifiedObject<T>
11
- : never;
33
+ ? T
34
+ : T extends { toJSON: () => infer R }
35
+ ? R
36
+ : T extends undefined | symbol | UnknownFunction
37
+ ? never
38
+ : T extends readonly unknown[]
39
+ ? JsonifiedArray<T>
40
+ : T extends
41
+ | ReadonlyMap<unknown, unknown>
42
+ | ReadonlySet<unknown>
43
+ | WeakMap<WeakKey, unknown>
44
+ | WeakSet<WeakKey>
45
+ ? EmptyJsonObject
46
+ : T extends object
47
+ ? JsonifiedObject<T>
48
+ : never;
12
49
 
13
50
  type JsonifiedObject<T> = {
14
- [Key in keyof T as [JsonifiedValue<T[Key]>] extends [never]
15
- ? never
16
- : Key]: JsonifiedValue<T[Key]>;
51
+ [
52
+ Key in keyof T as [JsonifiedValue<T[Key]>] extends [never] ? never : Key
53
+ ]: JsonifiedValue<T[Key]>;
17
54
  };
18
55
 
19
56
  // Stringified type for JSON.stringify to return as a string with a source type
@@ -1,21 +1,26 @@
1
- type ErrorState<T> = T extends null ? true : false;
1
+ /**
2
+ * A successful outcome. `isError` is always `false`, whatever `value` is: a
3
+ * success may carry `null` (`ok(null)`), and the flag must not flip with the
4
+ * payload.
5
+ */
2
6
  type Success<T> = {
3
- isError: ErrorState<T>;
4
- value: T;
7
+ isError: false;
8
+ value: T;
5
9
  };
6
10
 
11
+ /** A failed outcome. `isError` is always `true` and `value` is `null`. */
7
12
  type Failed<T extends null = null> = {
8
- isError: ErrorState<T>;
9
- value: T;
13
+ isError: true;
14
+ value: T;
10
15
  };
11
16
 
12
17
  /**
13
18
  * A settled outcome: the value, or the failure.
14
19
  *
15
20
  * This is what a `MaybePromise` resolves to, and it needed a name of its own.
16
- * Without one, anything that holds an already-awaited outcome has to restate the
17
- * union or reach for `Awaited<...>` to undo a wrapper that is no longer there —
18
- * which describes the route taken rather than the thing being held.
21
+ * Without one, anything that holds an already-awaited outcome has to restate
22
+ * the union or reach for `Awaited<...>` to undo a wrapper that is no longer
23
+ * there — which describes the route taken rather than the thing being held.
19
24
  */
20
25
  type Result<T> = Success<T> | Failed;
21
26
 
@@ -24,12 +29,12 @@ type MaybePromise<T> = Promise<Result<T>>;
24
29
  /**
25
30
  * `MaybePromise` under a name that says what it is.
26
31
  *
27
- * Elsewhere in the ecosystem `MaybePromise<T>` almost always means
28
- * `T | Promise<T>` — a value that may or may not be a promise. Here it means a
32
+ * Elsewhere in the ecosystem `MaybePromise<T>` almost always means `T |
33
+ * Promise<T>` — a value that may or may not be a promise. Here it means a
29
34
  * promise of a `Result`, which is a different thing entirely, so a reader who
30
35
  * knows the other convention reads this one wrong. Prefer `AsyncResult` in new
31
- * code. `MaybePromise` keeps working and is what the `prefer-maybe-promise` rule
32
- * still suggests.
36
+ * code. `MaybePromise` keeps working and is what the `prefer-maybe-promise`
37
+ * rule still suggests.
33
38
  */
34
39
  type AsyncResult<T> = MaybePromise<T>;
35
40
 
@@ -2,7 +2,7 @@ import type { Maybe } from "./maybe.js";
2
2
  import type { Optional } from "./optional.js";
3
3
  type Nullable<N> = Optional<N> | Maybe<N>;
4
4
  type ResolveNullable<T, R extends Nullable<T>> = R extends null | undefined
5
- ? Nullable<T>
6
- : T;
5
+ ? Nullable<T>
6
+ : T;
7
7
 
8
8
  export type { Nullable, ResolveNullable };
@@ -1,15 +1,15 @@
1
1
  type Optional<T> = T | undefined;
2
2
 
3
3
  type ResolveOptionalArray<T, R extends Optional<T>> = R extends undefined
4
- ? Optional<T>
5
- : T;
4
+ ? Optional<T>
5
+ : T;
6
6
 
7
7
  type ResolveBasicOptional<T, R extends Optional<T>> = R extends undefined
8
- ? Optional<T>
9
- : T;
8
+ ? Optional<T>
9
+ : T;
10
10
 
11
11
  type ResolveOptional<T, R extends Optional<T>> = T extends unknown[]
12
- ? ResolveOptionalArray<T, R>
13
- : ResolveBasicOptional<T, R>;
12
+ ? ResolveOptionalArray<T, R>
13
+ : ResolveBasicOptional<T, R>;
14
14
 
15
15
  export type { Optional, ResolveOptional };