@murky-web/typebuddy 3.0.1 → 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 CHANGED
@@ -242,13 +242,26 @@ export async function flush(): MaybePromise<void> {
242
242
 
243
243
  ### Oxlint rules
244
244
 
245
- `@murky-web/typebuddy/oxlint` has `prefer-maybe`, `prefer-maybe-promise`,
246
- `prefer-nullable`, `prefer-optional` and `require-try-catch`. They skip test
247
- files: `*.test.*`, `*.spec.*` and `*.bench.*`, anything under a `__tests__/`,
248
- `tests/`, `test/` or `__mocks__/` directory, and `vitest.setup.*`,
249
- `vitest-setup.*` and `setupTests.*`. Names like `latest.ts` or `contest.ts` are
250
- source files and stay checked. Directories count relative to the directory
251
- oxlint runs in, so a project checked out under `/tmp/test/` is not silenced.
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),
@@ -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: "Async functions must have a try-catch block returning an error result.",
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: "Use 'MaybePromise' instead of 'Promise' as the return type in async functions.",
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: "Wrap return value with ok(value).",
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: "Use ok(value) instead of an inline success result object.",
359
- preferErrResult: "Use err() instead of an inline error result object.",
360
- returnVoidPromise: "Return ok() for async functions with Promise<void> return type.",
361
- returnFailedPromise: "Return err() in the catch block of async functions."
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
  };
@@ -14,7 +14,10 @@ const rule = {
14
14
  context.report({
15
15
  node,
16
16
  messageId: "useMaybe",
17
- data: { type: typeText },
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: "Use Maybe<{{type}}> instead of {{type}} | null" }
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: { type: typeText },
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: "Use Nullable<{{type}}> instead of {{type}} | null | undefined" }
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: { type: typeText },
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: "Use Optional<{{type}}> instead of {{type}} | undefined" }
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 };