@murky-web/typebuddy 3.0.0 → 3.1.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 +23 -10
- package/dist/oxlint.js +2 -0
- package/dist/rules/async_rule.js +18 -1
- package/dist/rules/maybe_promise_rule.js +25 -7
- package/dist/rules/maybe_rule.js +5 -2
- package/dist/rules/nullable_rule.js +5 -2
- package/dist/rules/optional_rule.js +5 -2
- package/dist/rules/useless_undefined_rule.js +512 -0
- package/globals.d.ts +7 -7
- package/jsr.json +24 -24
- package/oxlint/index.ts +8 -4
- package/package.json +3 -3
- package/rules/async_rule.ts +47 -20
- package/rules/maybe_promise_rule.ts +60 -40
- package/rules/maybe_rule.ts +17 -13
- package/rules/nullable_rule.ts +16 -16
- package/rules/optional_rule.ts +15 -15
- package/rules/own_subtree.ts +2 -2
- package/rules/test_file.ts +4 -4
- package/rules/typebuddy_import.ts +9 -9
- package/rules/use_server.ts +3 -3
- package/rules/useless_undefined_rule.ts +982 -0
- package/src/collections.ts +1 -1
- package/src/empty.ts +1 -1
- package/src/guards.ts +5 -5
- package/src/parse.ts +4 -4
- package/src/public_api.ts +1 -1
- package/src/result.ts +3 -5
- package/src/types/globals.ts +1 -1
- package/src/types/index.ts +2 -2
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ import {
|
|
|
39
39
|
isString,
|
|
40
40
|
parseNumber,
|
|
41
41
|
isUuidString,
|
|
42
|
-
isEmptyObject
|
|
42
|
+
isEmptyObject
|
|
43
43
|
} from "@murky-web/typebuddy";
|
|
44
44
|
|
|
45
45
|
const value: unknown = "123";
|
|
@@ -103,8 +103,8 @@ If you want the `typebuddy` utility types globally, opt in once in your project:
|
|
|
103
103
|
// tsconfig.json
|
|
104
104
|
{
|
|
105
105
|
"compilerOptions": {
|
|
106
|
-
"types": ["@murky-web/typebuddy/globals"]
|
|
107
|
-
}
|
|
106
|
+
"types": ["@murky-web/typebuddy/globals"]
|
|
107
|
+
}
|
|
108
108
|
}
|
|
109
109
|
```
|
|
110
110
|
|
|
@@ -242,13 +242,26 @@ export async function flush(): MaybePromise<void> {
|
|
|
242
242
|
|
|
243
243
|
### Oxlint rules
|
|
244
244
|
|
|
245
|
-
`@murky-web/typebuddy/oxlint` has `
|
|
246
|
-
`prefer-nullable`, `prefer-optional` and
|
|
247
|
-
|
|
248
|
-
`tests/`, `test/` or
|
|
249
|
-
`
|
|
250
|
-
|
|
251
|
-
|
|
245
|
+
`@murky-web/typebuddy/oxlint` has `no-useless-undefined`, `prefer-maybe`,
|
|
246
|
+
`prefer-maybe-promise`, `prefer-nullable`, `prefer-optional` and
|
|
247
|
+
`require-try-catch`. All but `no-useless-undefined` skip test files: `*.test.*`,
|
|
248
|
+
`*.spec.*` and `*.bench.*`, anything under a `__tests__/`, `tests/`, `test/` or
|
|
249
|
+
`__mocks__/` directory, and `vitest.setup.*`, `vitest-setup.*` and
|
|
250
|
+
`setupTests.*`. Names like `latest.ts` or `contest.ts` are source files and stay
|
|
251
|
+
checked. Directories count relative to the directory oxlint runs in, so a
|
|
252
|
+
project checked out under `/tmp/test/` is not silenced.
|
|
253
|
+
|
|
254
|
+
`no-useless-undefined` is unicorn's rule of that name as oxlint ships it, with
|
|
255
|
+
the same options (`checkArguments`, `checkArrowFunctionBody`, both on), except
|
|
256
|
+
that a `let` whose type is `Optional<T>` or a union with `undefined` keeps its
|
|
257
|
+
`undefined` start value: `let x: Optional<T> = undefined;`, like
|
|
258
|
+
`let x: Maybe<T> = null;`, so every `let` can have an initializer
|
|
259
|
+
(`eslint/init-declarations`). It replaces `unicorn/no-useless-undefined` (turn
|
|
260
|
+
that one off) and, like it, lints test files too. An un-annotated
|
|
261
|
+
`let x = undefined` is reported without an autofix: the fix is a type or a real
|
|
262
|
+
default, not `let x;`. Trailing `undefined` arguments are reported without an
|
|
263
|
+
autofix too, because `arguments.length` and rest parameters see them (oxlint
|
|
264
|
+
only removes them under `--fix-dangerously`).
|
|
252
265
|
|
|
253
266
|
Autofixes only do what cannot change what the code means:
|
|
254
267
|
|
package/dist/oxlint.js
CHANGED
|
@@ -4,10 +4,12 @@ import { maybeRule as rule$2 } from "./rules/maybe_rule.js";
|
|
|
4
4
|
import { nullableRule as rule$3 } from "./rules/nullable_rule.js";
|
|
5
5
|
import { optionalRule as rule$4 } from "./rules/optional_rule.js";
|
|
6
6
|
import { withoutTestFiles } from "./rules/test_file.js";
|
|
7
|
+
import { uselessUndefinedRule as rule$5 } from "./rules/useless_undefined_rule.js";
|
|
7
8
|
// oxlint/index.ts
|
|
8
9
|
const plugin = {
|
|
9
10
|
meta: { name: "typebuddy" },
|
|
10
11
|
rules: {
|
|
12
|
+
"no-useless-undefined": rule$5,
|
|
11
13
|
"prefer-maybe": withoutTestFiles(rule$2),
|
|
12
14
|
"prefer-maybe-promise": withoutTestFiles(rule$1),
|
|
13
15
|
"prefer-nullable": withoutTestFiles(rule$3),
|
package/dist/rules/async_rule.js
CHANGED
|
@@ -7,6 +7,20 @@ function hasTryCatch(nodes) {
|
|
|
7
7
|
return node.type === "TryStatement";
|
|
8
8
|
});
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* What the message calls the function: its own name, the name it is bound to,
|
|
12
|
+
* or else the first line of its source.
|
|
13
|
+
*/
|
|
14
|
+
function functionLabel(node, getText) {
|
|
15
|
+
const candidates = [
|
|
16
|
+
node["id"],
|
|
17
|
+
node.parent?.["id"],
|
|
18
|
+
node.parent?.["key"]
|
|
19
|
+
];
|
|
20
|
+
for (const candidate of candidates) if (typeof candidate === "object" && candidate !== null && "type" in candidate && candidate.type === "Identifier" && "name" in candidate && typeof candidate.name === "string") return candidate.name;
|
|
21
|
+
const [firstLine = ""] = getText(node).split("\n");
|
|
22
|
+
return firstLine.trim();
|
|
23
|
+
}
|
|
10
24
|
function isCallArgumentCallback(node) {
|
|
11
25
|
const parent = node.parent;
|
|
12
26
|
if (!parent) return false;
|
|
@@ -17,6 +31,7 @@ const rule = {
|
|
|
17
31
|
create(context) {
|
|
18
32
|
const sourceCode = context.getSourceCode();
|
|
19
33
|
const imports = createImportPlanner(sourceCode);
|
|
34
|
+
const getText = (node) => sourceCode.getText(node);
|
|
20
35
|
function wrapBlockFixes(bodyNode, fixer) {
|
|
21
36
|
const range = bodyNode.range;
|
|
22
37
|
const tryStart = getBodyStartAfterDirectives(bodyNode);
|
|
@@ -75,6 +90,7 @@ const rule = {
|
|
|
75
90
|
context.report({
|
|
76
91
|
node,
|
|
77
92
|
messageId: "missingTryCatch",
|
|
93
|
+
data: { name: functionLabel(node, getText) },
|
|
78
94
|
suggest: suggestWrap(node, (fixer) => {
|
|
79
95
|
return wrapExpressionBodyFixes(node, bodyNode, fixer);
|
|
80
96
|
})
|
|
@@ -85,6 +101,7 @@ const rule = {
|
|
|
85
101
|
context.report({
|
|
86
102
|
node,
|
|
87
103
|
messageId: "missingTryCatch",
|
|
104
|
+
data: { name: functionLabel(node, getText) },
|
|
88
105
|
suggest: suggestWrap(bodyNode, (fixer) => {
|
|
89
106
|
return wrapBlockFixes(bodyNode, fixer);
|
|
90
107
|
})
|
|
@@ -105,7 +122,7 @@ const rule = {
|
|
|
105
122
|
docs: { description: "Async functions should have a try-catch block returning an error result." },
|
|
106
123
|
schema: [],
|
|
107
124
|
messages: {
|
|
108
|
-
missingTryCatch: "
|
|
125
|
+
missingTryCatch: "`{{name}}` is async without a `try`/`catch`, so a throw inside it rejects the promise and callers that check `isError` never see the failure. Fix: put the body in `try { … } catch { return err(); }` (after a directive such as `\"use cache\"`) with `import { err } from \"@murky-web/typebuddy\";` and `return ok(value)` in the `try`; if the failure is a bug rather than an outcome for the caller, `throw` instead, which the rule accepts. Do not wrap only part of the body, and do not return `ok(...)` from the `catch`.",
|
|
109
126
|
suggestTryCatch: "Wrap the body in try/catch and return err() from the catch."
|
|
110
127
|
}
|
|
111
128
|
}
|
|
@@ -141,6 +141,11 @@ const rule = {
|
|
|
141
141
|
if (!isTypeReference(typeAnnotation)) return;
|
|
142
142
|
if (getTypeName(typeAnnotation) !== "Promise") return;
|
|
143
143
|
const typeName = typeAnnotation["typeName"];
|
|
144
|
+
const awaited = getTypeArgument(typeAnnotation);
|
|
145
|
+
const data = {
|
|
146
|
+
promise: sourceCode.getText(typeAnnotation),
|
|
147
|
+
type: awaited ? sourceCode.getText(awaited) : "T"
|
|
148
|
+
};
|
|
144
149
|
const rewrite = (fixer) => {
|
|
145
150
|
const typeImport = imports.fixes(typeName, fixer, "MaybePromise", "type");
|
|
146
151
|
if (typeImport === null) return null;
|
|
@@ -150,6 +155,7 @@ const rule = {
|
|
|
150
155
|
context.report({
|
|
151
156
|
node: typeName,
|
|
152
157
|
messageId: "replaceWithMaybePromise",
|
|
158
|
+
data,
|
|
153
159
|
fix(fixer) {
|
|
154
160
|
return rewrite(fixer);
|
|
155
161
|
}
|
|
@@ -159,6 +165,7 @@ const rule = {
|
|
|
159
165
|
context.report({
|
|
160
166
|
node: typeName,
|
|
161
167
|
messageId: "replaceWithMaybePromise",
|
|
168
|
+
data,
|
|
162
169
|
suggest: [{
|
|
163
170
|
messageId: "suggestMaybePromise",
|
|
164
171
|
fix(fixer) {
|
|
@@ -200,6 +207,7 @@ const rule = {
|
|
|
200
207
|
if (returnType?.type === "TSVoidKeyword") context.report({
|
|
201
208
|
node,
|
|
202
209
|
messageId: "returnVoidPromise",
|
|
210
|
+
data: { code: sourceCode.getText(node) },
|
|
203
211
|
fix(fixer) {
|
|
204
212
|
return withHelperFixes(node, fixer, "ok", fixer.replaceText(node, "return ok();"));
|
|
205
213
|
}
|
|
@@ -214,6 +222,10 @@ const rule = {
|
|
|
214
222
|
context.report({
|
|
215
223
|
node: argument,
|
|
216
224
|
messageId: "preferOkResult",
|
|
225
|
+
data: {
|
|
226
|
+
object: sourceCode.getText(argument),
|
|
227
|
+
value: sourceCode.getText(objectValue)
|
|
228
|
+
},
|
|
217
229
|
fix(fixer) {
|
|
218
230
|
const valueText = sourceCode.getText(objectValue);
|
|
219
231
|
return withHelperFixes(node, fixer, "ok", fixer.replaceText(argument, `ok(${valueText})`));
|
|
@@ -225,6 +237,7 @@ const rule = {
|
|
|
225
237
|
context.report({
|
|
226
238
|
node: argument,
|
|
227
239
|
messageId: "preferErrResult",
|
|
240
|
+
data: { object: sourceCode.getText(argument) },
|
|
228
241
|
fix(fixer) {
|
|
229
242
|
return withHelperFixes(node, fixer, "err", fixer.replaceText(argument, "err()"));
|
|
230
243
|
}
|
|
@@ -234,6 +247,7 @@ const rule = {
|
|
|
234
247
|
if (!hasIsErrorFlag(argument, true) && !hasIsErrorFlag(argument, false)) context.report({
|
|
235
248
|
node: argument,
|
|
236
249
|
messageId: "wrapReturn",
|
|
250
|
+
data: { argument: sourceCode.getText(argument) },
|
|
237
251
|
suggest: suggestWithHelper(node, "suggestWrapReturn", "ok", (fixer) => {
|
|
238
252
|
const argumentText = sourceCode.getText(argument);
|
|
239
253
|
return fixer.replaceText(argument, `ok(${argumentText})`);
|
|
@@ -269,6 +283,7 @@ const rule = {
|
|
|
269
283
|
context.report({
|
|
270
284
|
node: statement,
|
|
271
285
|
messageId: "returnFailedPromise",
|
|
286
|
+
data: { found: `\`${sourceCode.getText(statement)}\`` },
|
|
272
287
|
fix(fixer) {
|
|
273
288
|
return withHelperFixes(statement, fixer, "err", fixer.replaceText(statement, "return err();"));
|
|
274
289
|
}
|
|
@@ -279,6 +294,7 @@ const rule = {
|
|
|
279
294
|
if (isNullLiteral(getObjectPropertyValue(argument, "value")) && hasOnlyResultProperties(argument)) context.report({
|
|
280
295
|
node: argument,
|
|
281
296
|
messageId: "preferErrResult",
|
|
297
|
+
data: { object: sourceCode.getText(argument) },
|
|
282
298
|
fix(fixer) {
|
|
283
299
|
return withHelperFixes(statement, fixer, "err", fixer.replaceText(argument, "err()"));
|
|
284
300
|
}
|
|
@@ -287,7 +303,8 @@ const rule = {
|
|
|
287
303
|
}
|
|
288
304
|
context.report({
|
|
289
305
|
node: statement,
|
|
290
|
-
messageId: "returnFailedPromise"
|
|
306
|
+
messageId: "returnFailedPromise",
|
|
307
|
+
data: { found: `\`${sourceCode.getText(statement)}\`` }
|
|
291
308
|
});
|
|
292
309
|
}
|
|
293
310
|
function processTryCatch(node) {
|
|
@@ -324,6 +341,7 @@ const rule = {
|
|
|
324
341
|
context.report({
|
|
325
342
|
node: handler,
|
|
326
343
|
messageId: "returnFailedPromise",
|
|
344
|
+
data: { found: "nothing (it has no `return`)" },
|
|
327
345
|
fix(fixer) {
|
|
328
346
|
const handlerBody = handler["body"];
|
|
329
347
|
if (!isNode(handlerBody) || !Array.isArray(handlerBody["range"])) return null;
|
|
@@ -351,14 +369,14 @@ const rule = {
|
|
|
351
369
|
hasSuggestions: true,
|
|
352
370
|
schema: [],
|
|
353
371
|
messages: {
|
|
354
|
-
replaceWithMaybePromise: "
|
|
372
|
+
replaceWithMaybePromise: "`{{promise}}` makes a failure a rejection the caller has to catch; typebuddy's async functions resolve to a result instead, which the caller checks with `isError`. Fix: `MaybePromise<{{type}}>` with `import type { MaybePromise } from \"@murky-web/typebuddy\";`, then `return ok(value)` from the `try` and `return err()` from the `catch`. Do not cast the returns with `as` to make them fit.",
|
|
355
373
|
suggestMaybePromise: "Replace 'Promise' with 'MaybePromise'.",
|
|
356
|
-
wrapReturn: "
|
|
374
|
+
wrapReturn: "`return {{argument}}` in the `try` of an async function resolves to a bare value, and a caller reading `.isError` gets `undefined`. Fix: `return ok({{argument}})`; if `{{argument}}` already is a `Result`, unwrap it: `const settled = {{argument}}; if (settled.isError) return err(); return ok(settled.value);`. Do not cast it with `as`.",
|
|
357
375
|
suggestWrapReturn: "Wrap the return value in ok(...).",
|
|
358
|
-
preferOkResult: "
|
|
359
|
-
preferErrResult: "
|
|
360
|
-
returnVoidPromise: "
|
|
361
|
-
returnFailedPromise: "
|
|
376
|
+
preferOkResult: "`{{object}}` builds the success result by hand; without a return type its `isError` widens to `boolean` and `isSuccess` stops narrowing it. Fix: `ok({{value}})` with `import { ok } from \"@murky-web/typebuddy\";` (`--fix` applies it).",
|
|
377
|
+
preferErrResult: "`{{object}}` builds the failed result by hand; without a return type its `isError` widens to `boolean` and `isSuccess` stops narrowing it. Fix: `err()` with `import { err } from \"@murky-web/typebuddy\";` (`--fix` applies it).",
|
|
378
|
+
returnVoidPromise: "`{{code}}` in the `try` of an async `void` function resolves to `undefined` instead of a result, so a caller reading `.isError` throws a `TypeError`. Fix: `return ok();` with `import { ok } from \"@murky-web/typebuddy\";` (`--fix` applies it).",
|
|
379
|
+
returnFailedPromise: "The `catch` of this async function settles on {{found}} instead of `err()`, so the caller gets no `Failed` result and `isError` misses the failure. Fix: end the `catch` with `return err();` and `import { err } from \"@murky-web/typebuddy\";`, renaming a `catch (err)` parameter first; a fallback value belongs in the `try` as `return ok(fallback)`. Do not return `ok(...)` from the `catch`."
|
|
362
380
|
}
|
|
363
381
|
}
|
|
364
382
|
};
|
package/dist/rules/maybe_rule.js
CHANGED
|
@@ -14,7 +14,10 @@ const rule = {
|
|
|
14
14
|
context.report({
|
|
15
15
|
node,
|
|
16
16
|
messageId: "useMaybe",
|
|
17
|
-
data: {
|
|
17
|
+
data: {
|
|
18
|
+
type: typeText,
|
|
19
|
+
union: sourceCode.getText(node)
|
|
20
|
+
},
|
|
18
21
|
fix(fixer) {
|
|
19
22
|
const importFixes = imports.fixes(node, fixer, "Maybe", "type");
|
|
20
23
|
if (importFixes === null) return null;
|
|
@@ -30,7 +33,7 @@ const rule = {
|
|
|
30
33
|
docs: { description: "Use Maybe<T> for T | null" },
|
|
31
34
|
fixable: "code",
|
|
32
35
|
schema: [],
|
|
33
|
-
messages: { useMaybe: "
|
|
36
|
+
messages: { useMaybe: "`{{union}}` is the type typebuddy names `Maybe<{{type}}>`. Both mean the same, and one spelling keeps `T | null` recognisable as the thing `isMaybe` narrows. Fix: `Maybe<{{type}}>` with `import type { Maybe } from \"@murky-web/typebuddy\";` (`--fix` applies it where `Maybe` is not already another binding)." }
|
|
34
37
|
}
|
|
35
38
|
};
|
|
36
39
|
export { rule as maybeRule };
|
|
@@ -15,7 +15,10 @@ const rule = {
|
|
|
15
15
|
context.report({
|
|
16
16
|
node,
|
|
17
17
|
messageId: "useNullable",
|
|
18
|
-
data: {
|
|
18
|
+
data: {
|
|
19
|
+
type: typeText,
|
|
20
|
+
union: sourceCode.getText(node)
|
|
21
|
+
},
|
|
19
22
|
fix(fixer) {
|
|
20
23
|
const importFixes = imports.fixes(node, fixer, "Nullable", "type");
|
|
21
24
|
if (importFixes === null) return null;
|
|
@@ -31,7 +34,7 @@ const rule = {
|
|
|
31
34
|
docs: { description: "Use Nullable<T> instead of T | null | undefined" },
|
|
32
35
|
fixable: "code",
|
|
33
36
|
schema: [],
|
|
34
|
-
messages: { useNullable: "
|
|
37
|
+
messages: { useNullable: "`{{union}}` is the type typebuddy names `Nullable<{{type}}>`. Both mean the same, and one spelling keeps `T | null | undefined` recognisable as the thing `isNullable` narrows. Fix: `Nullable<{{type}}>` with `import type { Nullable } from \"@murky-web/typebuddy\";` (`--fix` applies it where `Nullable` is not already another binding)." }
|
|
35
38
|
}
|
|
36
39
|
};
|
|
37
40
|
export { rule as nullableRule };
|
|
@@ -13,7 +13,10 @@ const rule = {
|
|
|
13
13
|
context.report({
|
|
14
14
|
node,
|
|
15
15
|
messageId: "preferOptional",
|
|
16
|
-
data: {
|
|
16
|
+
data: {
|
|
17
|
+
type: typeText,
|
|
18
|
+
union: sourceCode.getText(node)
|
|
19
|
+
},
|
|
17
20
|
fix(fixer) {
|
|
18
21
|
const importFixes = imports.fixes(node, fixer, "Optional", "type");
|
|
19
22
|
if (importFixes === null) return null;
|
|
@@ -29,7 +32,7 @@ const rule = {
|
|
|
29
32
|
docs: { description: "Enforce using Optional<T> instead of T | undefined" },
|
|
30
33
|
fixable: "code",
|
|
31
34
|
schema: [],
|
|
32
|
-
messages: { preferOptional: "
|
|
35
|
+
messages: { preferOptional: "`{{union}}` is the type typebuddy names `Optional<{{type}}>`. Both mean the same, and one spelling keeps `T | undefined` recognisable as the thing `isOptional` narrows. Fix: `Optional<{{type}}>` with `import type { Optional } from \"@murky-web/typebuddy\";` (`--fix` applies it where `Optional` is not already another binding)." }
|
|
33
36
|
}
|
|
34
37
|
};
|
|
35
38
|
export { rule as optionalRule };
|