@murky-web/typebuddy 2.1.0 → 3.0.0
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/README.md +184 -72
- package/dist/collections.js +1 -2
- package/dist/empty.js +34 -13
- package/dist/guards.js +33 -9
- package/dist/ids.js +6 -6
- package/dist/oxlint.js +7 -7
- package/dist/parse.js +39 -15
- package/dist/result.js +6 -6
- package/dist/rules/async_rule.js +39 -25
- package/dist/rules/deliberate_throw.js +5 -6
- package/dist/rules/maybe_promise_rule.js +99 -40
- package/dist/rules/maybe_rule.js +7 -5
- package/dist/rules/nullable_rule.js +7 -5
- package/dist/rules/optional_rule.js +8 -4
- package/dist/rules/own_subtree.js +1 -2
- package/dist/rules/test_file.js +63 -0
- package/dist/rules/typebuddy_import.js +242 -35
- package/dist/rules/use_server.js +1 -2
- package/globals.d.ts +49 -44
- package/jsr.json +3 -2
- package/oxlint/index.ts +7 -5
- package/package.json +92 -91
- package/rules/async_rule.ts +80 -64
- package/rules/deliberate_throw.ts +11 -11
- package/rules/maybe_promise_rule.ts +218 -91
- package/rules/maybe_rule.ts +74 -47
- package/rules/nullable_rule.ts +78 -50
- package/rules/optional_rule.ts +80 -51
- package/rules/own_subtree.ts +33 -33
- package/rules/test_file.ts +114 -0
- package/rules/typebuddy_import.ts +479 -87
- package/rules/use_server.ts +2 -1
- package/src/empty.ts +57 -19
- package/src/guards.ts +96 -30
- package/src/ids.ts +10 -4
- package/src/index.ts +1 -18
- package/src/parse.ts +104 -25
- package/src/public_api.ts +22 -0
- package/src/result.ts +5 -4
- package/src/types/globals.ts +11 -11
- package/src/types/index.ts +21 -21
- package/src/types/json.ts +48 -11
- package/src/types/maybe_promise.ts +17 -12
- package/src/types/nullable.ts +2 -2
- package/src/types/optional.ts +6 -6
package/src/types/index.ts
CHANGED
|
@@ -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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
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
|
-
|
|
4
|
-
|
|
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
|
-
|
|
9
|
-
|
|
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
|
|
17
|
-
* union or reach for `Awaited<...>` to undo a wrapper that is no longer
|
|
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
|
-
*
|
|
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`
|
|
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
|
|
package/src/types/nullable.ts
CHANGED
|
@@ -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
|
-
|
|
6
|
-
|
|
5
|
+
? Nullable<T>
|
|
6
|
+
: T;
|
|
7
7
|
|
|
8
8
|
export type { Nullable, ResolveNullable };
|
package/src/types/optional.ts
CHANGED
|
@@ -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
|
-
|
|
5
|
-
|
|
4
|
+
? Optional<T>
|
|
5
|
+
: T;
|
|
6
6
|
|
|
7
7
|
type ResolveBasicOptional<T, R extends Optional<T>> = R extends undefined
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
? Optional<T>
|
|
9
|
+
: T;
|
|
10
10
|
|
|
11
11
|
type ResolveOptional<T, R extends Optional<T>> = T extends unknown[]
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
? ResolveOptionalArray<T, R>
|
|
13
|
+
: ResolveBasicOptional<T, R>;
|
|
14
14
|
|
|
15
15
|
export type { Optional, ResolveOptional };
|