@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.
@@ -27,6 +27,7 @@ type RuleContext = {
27
27
  report(descriptor: {
28
28
  node: unknown;
29
29
  messageId: string;
30
+ data?: Record<string, string>;
30
31
  fix?(fixer: RuleFixer): unknown;
31
32
  suggest?: RuleSuggestion[];
32
33
  }): void;
@@ -279,6 +280,11 @@ const rule = {
279
280
  if (getTypeName(typeAnnotation) !== "Promise") return;
280
281
 
281
282
  const typeName = typeAnnotation["typeName"];
283
+ const awaited = getTypeArgument(typeAnnotation);
284
+ const data = {
285
+ promise: sourceCode.getText(typeAnnotation),
286
+ type: awaited ? sourceCode.getText(awaited) : "T"
287
+ };
282
288
  const rewrite = (fixer: RuleFixer) => {
283
289
  const typeImport = imports.fixes(
284
290
  typeName,
@@ -303,6 +309,7 @@ const rule = {
303
309
  context.report({
304
310
  node: typeName,
305
311
  messageId: "replaceWithMaybePromise",
312
+ data,
306
313
  fix(fixer) {
307
314
  return rewrite(fixer);
308
315
  }
@@ -313,6 +320,7 @@ const rule = {
313
320
  context.report({
314
321
  node: typeName,
315
322
  messageId: "replaceWithMaybePromise",
323
+ data,
316
324
  suggest: [
317
325
  {
318
326
  messageId: "suggestMaybePromise",
@@ -369,6 +377,7 @@ const rule = {
369
377
  context.report({
370
378
  node,
371
379
  messageId: "returnVoidPromise",
380
+ data: { code: sourceCode.getText(node) },
372
381
  fix(fixer) {
373
382
  return withHelperFixes(
374
383
  node,
@@ -411,6 +420,10 @@ const rule = {
411
420
  context.report({
412
421
  node: argument,
413
422
  messageId: "preferOkResult",
423
+ data: {
424
+ object: sourceCode.getText(argument),
425
+ value: sourceCode.getText(objectValue)
426
+ },
414
427
  fix(fixer) {
415
428
  const valueText = sourceCode.getText(objectValue);
416
429
  return withHelperFixes(
@@ -432,6 +445,7 @@ const rule = {
432
445
  context.report({
433
446
  node: argument,
434
447
  messageId: "preferErrResult",
448
+ data: { object: sourceCode.getText(argument) },
435
449
  fix(fixer) {
436
450
  return withHelperFixes(
437
451
  node,
@@ -456,6 +470,7 @@ const rule = {
456
470
  context.report({
457
471
  node: argument,
458
472
  messageId: "wrapReturn",
473
+ data: { argument: sourceCode.getText(argument) },
459
474
  suggest: suggestWithHelper(
460
475
  node,
461
476
  "suggestWrapReturn",
@@ -521,6 +536,7 @@ const rule = {
521
536
  context.report({
522
537
  node: statement,
523
538
  messageId: "returnFailedPromise",
539
+ data: { found: `\`${sourceCode.getText(statement)}\`` },
524
540
  fix(fixer) {
525
541
  return withHelperFixes(
526
542
  statement,
@@ -543,6 +559,7 @@ const rule = {
543
559
  context.report({
544
560
  node: argument,
545
561
  messageId: "preferErrResult",
562
+ data: { object: sourceCode.getText(argument) },
546
563
  fix(fixer) {
547
564
  return withHelperFixes(
548
565
  statement,
@@ -558,7 +575,8 @@ const rule = {
558
575
 
559
576
  context.report({
560
577
  node: statement,
561
- messageId: "returnFailedPromise"
578
+ messageId: "returnFailedPromise",
579
+ data: { found: `\`${sourceCode.getText(statement)}\`` }
562
580
  });
563
581
  }
564
582
 
@@ -639,6 +657,7 @@ const rule = {
639
657
  context.report({
640
658
  node: handler,
641
659
  messageId: "returnFailedPromise",
660
+ data: { found: "nothing (it has no `return`)" },
642
661
  fix(fixer) {
643
662
  const handlerBody = handler["body"];
644
663
  if (
@@ -695,18 +714,19 @@ const rule = {
695
714
  schema: [],
696
715
  messages: {
697
716
  replaceWithMaybePromise:
698
- "Use 'MaybePromise' instead of 'Promise' as the return type in async functions.",
717
+ '`{{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.',
699
718
  suggestMaybePromise: "Replace 'Promise' with 'MaybePromise'.",
700
- wrapReturn: "Wrap return value with ok(value).",
719
+ wrapReturn:
720
+ "`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`.",
701
721
  suggestWrapReturn: "Wrap the return value in ok(...).",
702
722
  preferOkResult:
703
- "Use ok(value) instead of an inline success result object.",
723
+ '`{{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).',
704
724
  preferErrResult:
705
- "Use err() instead of an inline error result object.",
725
+ '`{{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).',
706
726
  returnVoidPromise:
707
- "Return ok() for async functions with Promise<void> return type.",
727
+ '`{{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).',
708
728
  returnFailedPromise:
709
- "Return err() in the catch block of async functions."
729
+ '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`.'
710
730
  }
711
731
  }
712
732
  };
@@ -14,7 +14,7 @@ const rule = {
14
14
  report(descriptor: {
15
15
  node: unknown;
16
16
  messageId: string;
17
- data: { type: string };
17
+ data: { type: string; union: string };
18
18
  fix(fixer: RuleFixer): unknown;
19
19
  }): void;
20
20
  }) {
@@ -42,7 +42,10 @@ const rule = {
42
42
  context.report({
43
43
  node,
44
44
  messageId: "useMaybe",
45
- data: { type: typeText },
45
+ data: {
46
+ type: typeText,
47
+ union: sourceCode.getText(node)
48
+ },
46
49
  fix(fixer) {
47
50
  // `Maybe` has to be the package's type where the
48
51
  // fix puts it: imported, or already imported.
@@ -76,7 +79,8 @@ const rule = {
76
79
  fixable: "code",
77
80
  schema: [],
78
81
  messages: {
79
- useMaybe: "Use Maybe<{{type}}> instead of {{type}} | null"
82
+ useMaybe:
83
+ '`{{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).'
80
84
  }
81
85
  }
82
86
  };
@@ -14,7 +14,7 @@ const rule = {
14
14
  report(descriptor: {
15
15
  node: unknown;
16
16
  messageId: string;
17
- data: { type: string };
17
+ data: { type: string; union: string };
18
18
  fix(fixer: RuleFixer): unknown;
19
19
  }): void;
20
20
  }) {
@@ -43,7 +43,10 @@ const rule = {
43
43
  context.report({
44
44
  node,
45
45
  messageId: "useNullable",
46
- data: { type: typeText },
46
+ data: {
47
+ type: typeText,
48
+ union: sourceCode.getText(node)
49
+ },
47
50
  fix(fixer) {
48
51
  // See `prefer-maybe`: the fix only uses the
49
52
  // package's own `Nullable`, imported where it is
@@ -76,7 +79,7 @@ const rule = {
76
79
  schema: [],
77
80
  messages: {
78
81
  useNullable:
79
- "Use Nullable<{{type}}> instead of {{type}} | null | undefined"
82
+ '`{{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).'
80
83
  }
81
84
  }
82
85
  };
@@ -14,7 +14,7 @@ const rule = {
14
14
  report(descriptor: {
15
15
  node: unknown;
16
16
  messageId: string;
17
- data: { type: string };
17
+ data: { type: string; union: string };
18
18
  fix(fixer: RuleFixer): unknown;
19
19
  }): void;
20
20
  }) {
@@ -43,7 +43,10 @@ const rule = {
43
43
  context.report({
44
44
  node,
45
45
  messageId: "preferOptional",
46
- data: { type: typeText },
46
+ data: {
47
+ type: typeText,
48
+ union: sourceCode.getText(node)
49
+ },
47
50
  fix(fixer) {
48
51
  // See `prefer-maybe`: the fix only uses the
49
52
  // package's own `Optional`, imported where it is
@@ -76,7 +79,7 @@ const rule = {
76
79
  schema: [],
77
80
  messages: {
78
81
  preferOptional:
79
- "Use Optional<{{type}}> instead of {{type}} | undefined"
82
+ '`{{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).'
80
83
  }
81
84
  }
82
85
  };