@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/package.json CHANGED
@@ -1,98 +1,99 @@
1
1
  {
2
- "name": "@murky-web/typebuddy",
3
- "version": "2.1.0",
4
- "description": "Your new best friend for simple typescript guards every project needs.",
5
- "private": false,
6
- "main": "./dist/index.js",
7
- "module": "./dist/index.js",
8
- "types": "./src/index.ts",
9
- "exports": {
10
- ".": {
11
- "types": "./src/index.ts",
12
- "import": "./dist/index.js",
13
- "default": "./dist/index.js"
2
+ "name": "@murky-web/typebuddy",
3
+ "version": "3.0.1",
4
+ "private": false,
5
+ "description": "Your new best friend for simple typescript guards every project needs.",
6
+ "homepage": "https://github.com/MurkyTheMurloc/web_dev_tools/tree/main/packages/typebuddy",
7
+ "bugs": {
8
+ "url": "https://github.com/MurkyTheMurloc/web_dev_tools/issues"
14
9
  },
15
- "./guards": {
16
- "types": "./src/guards.ts",
17
- "import": "./dist/guards.js",
18
- "default": "./dist/guards.js"
10
+ "license": "ISC",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/MurkyTheMurloc/web_dev_tools.git",
14
+ "directory": "packages/typebuddy"
19
15
  },
20
- "./result": {
21
- "types": "./src/result.ts",
22
- "import": "./dist/result.js",
23
- "default": "./dist/result.js"
16
+ "files": [
17
+ "dist",
18
+ "globals.d.ts",
19
+ "jsr.json",
20
+ "oxlint/index.ts",
21
+ "rules",
22
+ "src"
23
+ ],
24
+ "type": "module",
25
+ "sideEffects": false,
26
+ "main": "./dist/index.js",
27
+ "module": "./dist/index.js",
28
+ "types": "./src/index.ts",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./src/index.ts",
32
+ "import": "./dist/index.js",
33
+ "default": "./dist/index.js"
34
+ },
35
+ "./guards": {
36
+ "types": "./src/guards.ts",
37
+ "import": "./dist/guards.js",
38
+ "default": "./dist/guards.js"
39
+ },
40
+ "./result": {
41
+ "types": "./src/result.ts",
42
+ "import": "./dist/result.js",
43
+ "default": "./dist/result.js"
44
+ },
45
+ "./parse": {
46
+ "types": "./src/parse.ts",
47
+ "import": "./dist/parse.js",
48
+ "default": "./dist/parse.js"
49
+ },
50
+ "./empty": {
51
+ "types": "./src/empty.ts",
52
+ "import": "./dist/empty.js",
53
+ "default": "./dist/empty.js"
54
+ },
55
+ "./ids": {
56
+ "types": "./src/ids.ts",
57
+ "import": "./dist/ids.js",
58
+ "default": "./dist/ids.js"
59
+ },
60
+ "./collections": {
61
+ "types": "./src/collections.ts",
62
+ "import": "./dist/collections.js",
63
+ "default": "./dist/collections.js"
64
+ },
65
+ "./globals": {
66
+ "types": "./globals.d.ts"
67
+ },
68
+ "./oxlint": {
69
+ "types": "./oxlint/index.ts",
70
+ "import": "./dist/oxlint.js",
71
+ "default": "./dist/oxlint.js"
72
+ },
73
+ "./package.json": "./package.json"
24
74
  },
25
- "./parse": {
26
- "types": "./src/parse.ts",
27
- "import": "./dist/parse.js",
28
- "default": "./dist/parse.js"
75
+ "publishConfig": {
76
+ "access": "public",
77
+ "provenance": true
29
78
  },
30
- "./empty": {
31
- "types": "./src/empty.ts",
32
- "import": "./dist/empty.js",
33
- "default": "./dist/empty.js"
79
+ "scripts": {
80
+ "test": "vitest run",
81
+ "build:local": "bun run build",
82
+ "build": "bun run clean && tsdown",
83
+ "clean": "rm -rf dist",
84
+ "lint": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.lint.json --type-aware src tsdown.config.ts vitest.config.ts",
85
+ "lint:fix": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.lint.json --type-aware --fix src tsdown.config.ts vitest.config.ts",
86
+ "format": "oxfmt -c ../../config/oxc/.oxfmtrc.jsonc src rules tests tsdown.config.ts vitest.config.ts",
87
+ "format:check": "oxfmt -c ../../config/oxc/.oxfmtrc.jsonc --check src rules tests tsdown.config.ts vitest.config.ts",
88
+ "smoke:globals": "bun run build && tsc --project ./smoke/globals/tsconfig.json --noEmit && mkdir -p ./smoke/globals/node_modules/@murky-web && ln -sfn ../../../.. ./smoke/globals/node_modules/@murky-web/typebuddy && tsc --project ./smoke/globals/tsconfig.types.json --noEmit",
89
+ "smoke:oxlint": "bun run build && bun ./smoke/oxlint/run-smoke.ts",
90
+ "smoke:oxlint:fix": "bun run build && bun ./smoke/oxlint/run-fix-smoke.ts",
91
+ "smoke:subpath": "bun run build && bun ./smoke/subpath/run.ts",
92
+ "smoke:treeshake": "bun run build && bun ./smoke/treeshake/run.ts",
93
+ "typecheck": "tsc --project ./tsconfig.json --noEmit && tsc --project ./tsconfig.vitest.json --noEmit"
34
94
  },
35
- "./ids": {
36
- "types": "./src/ids.ts",
37
- "import": "./dist/ids.js",
38
- "default": "./dist/ids.js"
39
- },
40
- "./collections": {
41
- "types": "./src/collections.ts",
42
- "import": "./dist/collections.js",
43
- "default": "./dist/collections.js"
44
- },
45
- "./globals": {
46
- "types": "./globals.d.ts"
47
- },
48
- "./oxlint": {
49
- "types": "./oxlint/index.ts",
50
- "import": "./dist/oxlint.js",
51
- "default": "./dist/oxlint.js"
52
- },
53
- "./package.json": "./package.json"
54
- },
55
- "scripts": {
56
- "test": "vitest run",
57
- "build:local": "bun run build",
58
- "build": "bun run clean && tsdown",
59
- "clean": "rm -rf dist",
60
- "lint": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.lint.json --type-aware src tsdown.config.ts vitest.config.ts",
61
- "lint:fix": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.lint.json --type-aware --fix src tsdown.config.ts vitest.config.ts",
62
- "format": "oxfmt -c ../../config/oxc/.oxfmtrc.jsonc src rules tests tsdown.config.ts vitest.config.ts",
63
- "format:check": "oxfmt -c ../../config/oxc/.oxfmtrc.jsonc --check src rules tests tsdown.config.ts vitest.config.ts",
64
- "smoke:globals": "bun run build && tsc --project ./smoke/globals/tsconfig.json --noEmit && mkdir -p ./smoke/globals/node_modules/@murky-web && ln -sfn ../../../.. ./smoke/globals/node_modules/@murky-web/typebuddy && tsc --project ./smoke/globals/tsconfig.types.json --noEmit",
65
- "smoke:oxlint": "bun run build && bun ./smoke/oxlint/run-smoke.ts",
66
- "smoke:oxlint:fix": "bun run build && bun ./smoke/oxlint/run-fix-smoke.ts",
67
- "smoke:subpath": "bun run build && bun ./smoke/subpath/run.ts",
68
- "smoke:treeshake": "bun run build && bun ./smoke/treeshake/run.ts",
69
- "typecheck": "tsc --project ./tsconfig.json --noEmit && tsc --project ./tsconfig.vitest.json --noEmit"
70
- },
71
- "files": [
72
- "dist",
73
- "globals.d.ts",
74
- "jsr.json",
75
- "oxlint/index.ts",
76
- "rules",
77
- "src"
78
- ],
79
- "repository": {
80
- "type": "git",
81
- "url": "git+https://github.com/MurkyTheMurloc/web_dev_tools.git",
82
- "directory": "packages/typebuddy"
83
- },
84
- "homepage": "https://github.com/MurkyTheMurloc/web_dev_tools/tree/main/packages/typebuddy",
85
- "bugs": {
86
- "url": "https://github.com/MurkyTheMurloc/web_dev_tools/issues"
87
- },
88
- "license": "ISC",
89
- "type": "module",
90
- "sideEffects": false,
91
- "publishConfig": {
92
- "access": "public",
93
- "provenance": true
94
- },
95
- "devDependencies": {
96
- "@types/node": "^25.5.0"
97
- }
95
+ "devDependencies": {
96
+ "@types/node": "^24.19.2",
97
+ "oxlint": "^1.94.0"
98
+ }
98
99
  }
@@ -1,5 +1,8 @@
1
1
  import { throwsDeliberately } from "./deliberate_throw.js";
2
- import { getTypeBuddyImportInsertion } from "./typebuddy_import.js";
2
+ import {
3
+ createImportPlanner,
4
+ getBodyStartAfterDirectives
5
+ } from "./typebuddy_import.js";
3
6
  import { createUseServerGuard } from "./use_server.js";
4
7
 
5
8
  type AstNode = {
@@ -11,26 +14,26 @@ type AstNode = {
11
14
  [key: string]: unknown;
12
15
  };
13
16
 
17
+ type RuleFixer = {
18
+ replaceText(node: unknown, text: string): unknown;
19
+ insertTextBeforeRange(range: [number, number], text: string): unknown;
20
+ };
21
+
14
22
  type RuleContext = {
15
- getSourceCode(): { getText(node: unknown): string };
23
+ getSourceCode(): {
24
+ getText(node: unknown): string;
25
+ getScope(node: unknown): unknown;
26
+ };
16
27
  report(descriptor: {
17
28
  node: unknown;
18
29
  messageId: string;
19
- fix(fixer: {
20
- replaceText(node: unknown, text: string): unknown;
21
- insertTextBeforeRange(
22
- range: [number, number],
23
- text: string,
24
- ): unknown;
25
- }): unknown;
30
+ suggest?: {
31
+ messageId: string;
32
+ fix(fixer: RuleFixer): unknown;
33
+ }[];
26
34
  }): void;
27
35
  };
28
36
 
29
- type RuleFixer = {
30
- replaceText(node: unknown, text: string): unknown;
31
- insertTextBeforeRange(range: [number, number], text: string): unknown;
32
- };
33
-
34
37
  function hasTryCatch(nodes: AstNode[]): boolean {
35
38
  return nodes.some((node) => {
36
39
  return node.type === "TryStatement";
@@ -55,50 +58,36 @@ function isCallArgumentCallback(node: AstNode): boolean {
55
58
  const rule = {
56
59
  create(context: RuleContext) {
57
60
  const sourceCode = context.getSourceCode();
58
- let scheduledErrImport = false;
59
-
60
- function ensureErrImportFixes(
61
- node: AstNode,
62
- fixer: RuleFixer,
63
- ): unknown[] {
64
- if (scheduledErrImport) {
65
- return [];
66
- }
67
-
68
- const insertion = getTypeBuddyImportInsertion(node, "err");
69
- if (!insertion) {
70
- return [];
71
- }
72
-
73
- scheduledErrImport = true;
74
-
75
- return [
76
- fixer.insertTextBeforeRange(insertion.range, insertion.text),
77
- ];
78
- }
61
+ const imports = createImportPlanner(sourceCode);
79
62
 
80
63
  // Two zero-width insertions leave the body's own text untouched, byte
81
64
  // for byte. Rebuilding it statement by statement used to strip leading
82
65
  // whitespace on every line — including lines inside template literals,
83
66
  // which silently changed string values.
67
+ //
68
+ // The `try {` goes after the directive prologue. `"use cache"` and
69
+ // `"use server"` only mean something as the first statements of the
70
+ // body; inside a `try` they are plain expression statements.
84
71
  function wrapBlockFixes(
85
72
  bodyNode: AstNode,
86
- fixer: RuleFixer,
73
+ fixer: RuleFixer
87
74
  ): unknown[] | null {
88
75
  const range = bodyNode.range;
89
- if (!Array.isArray(range) || range.length < 2) {
76
+ const tryStart = getBodyStartAfterDirectives(bodyNode);
77
+ if (
78
+ !Array.isArray(range) ||
79
+ range.length < 2 ||
80
+ tryStart === null
81
+ ) {
90
82
  return null;
91
83
  }
92
84
 
93
85
  return [
94
- fixer.insertTextBeforeRange(
95
- [range[0] + 1, range[0] + 1],
96
- "\ntry {",
97
- ),
86
+ fixer.insertTextBeforeRange([tryStart, tryStart], "\ntry {"),
98
87
  fixer.insertTextBeforeRange(
99
88
  [range[1] - 1, range[1] - 1],
100
- "} catch {\nreturn err();\n}\n",
101
- ),
89
+ "} catch {\nreturn err();\n}\n"
90
+ )
102
91
  ];
103
92
  }
104
93
 
@@ -114,7 +103,7 @@ const rule = {
114
103
  function wrapExpressionBodyFixes(
115
104
  node: AstNode,
116
105
  bodyNode: AstNode,
117
- fixer: RuleFixer,
106
+ fixer: RuleFixer
118
107
  ): unknown[] | null {
119
108
  const arrowRange = node.range;
120
109
  const bodyRange = bodyNode.range;
@@ -144,17 +133,59 @@ const rule = {
144
133
  return [
145
134
  fixer.insertTextBeforeRange(
146
135
  [start, start],
147
- "{\ntry {\nreturn ",
136
+ "{\ntry {\nreturn "
148
137
  ),
149
138
  fixer.insertTextBeforeRange(
150
139
  [end, end],
151
- ";\n} catch {\nreturn err();\n}\n}",
152
- ),
140
+ ";\n} catch {\nreturn err();\n}\n}"
141
+ )
142
+ ];
143
+ }
144
+
145
+ /**
146
+ * The wrap is a suggestion, never a fix. It turns every exception of
147
+ * the function into `err()`, so a failure nobody handled becomes a
148
+ * value somebody has to remember to check, and `catch { return err();
149
+ * }` hides what went wrong. Whether that is wanted is the author's
150
+ * call. `err` has to be free at that spot: a parameter or local called
151
+ * `err` would make `err()` call something else, and then there is no
152
+ * suggestion.
153
+ */
154
+ function suggestWrap(
155
+ scopeNode: AstNode,
156
+ wrap: (fixer: RuleFixer) => unknown[] | null
157
+ ) {
158
+ return [
159
+ {
160
+ messageId: "suggestTryCatch",
161
+ fix(fixer: RuleFixer) {
162
+ const wrapFixes = wrap(fixer);
163
+ if (!wrapFixes) {
164
+ return null;
165
+ }
166
+
167
+ const importFixes = imports.fixes(
168
+ scopeNode,
169
+ fixer,
170
+ "err",
171
+ "value"
172
+ );
173
+ if (importFixes === null) {
174
+ return null;
175
+ }
176
+
177
+ return [...importFixes, ...wrapFixes];
178
+ }
179
+ }
153
180
  ];
154
181
  }
155
182
 
156
183
  function checkFunction(node: AstNode) {
157
184
  if (node.async !== true) return;
185
+ // An async generator yields its values; there is no single result
186
+ // to wrap, and a `catch` that returns would end the iteration and
187
+ // swallow the error.
188
+ if (node["generator"] === true) return;
158
189
  if (isCallArgumentCallback(node)) return;
159
190
  const bodyNode = node.body;
160
191
  if (!bodyNode || Array.isArray(bodyNode)) return;
@@ -170,21 +201,9 @@ const rule = {
170
201
  context.report({
171
202
  node,
172
203
  messageId: "missingTryCatch",
173
- fix(fixer) {
174
- const wrapFixes = wrapExpressionBodyFixes(
175
- node,
176
- bodyNode,
177
- fixer,
178
- );
179
- if (!wrapFixes) {
180
- return null;
181
- }
182
-
183
- return [
184
- ...ensureErrImportFixes(node, fixer),
185
- ...wrapFixes,
186
- ];
187
- },
204
+ suggest: suggestWrap(node, (fixer) => {
205
+ return wrapExpressionBodyFixes(node, bodyNode, fixer);
206
+ })
188
207
  });
189
208
  return;
190
209
  }
@@ -195,14 +214,9 @@ const rule = {
195
214
  context.report({
196
215
  node,
197
216
  messageId: "missingTryCatch",
198
- fix(fixer) {
199
- const wrapFixes = wrapBlockFixes(bodyNode, fixer);
200
- if (!wrapFixes) {
201
- return null;
202
- }
203
-
204
- return [...ensureErrImportFixes(node, fixer), ...wrapFixes];
205
- },
217
+ suggest: suggestWrap(bodyNode, (fixer) => {
218
+ return wrapBlockFixes(bodyNode, fixer);
219
+ })
206
220
  });
207
221
  }
208
222
 
@@ -212,23 +226,25 @@ const rule = {
212
226
  Program,
213
227
  FunctionDeclaration: guard(checkFunction),
214
228
  FunctionExpression: guard(checkFunction),
215
- ArrowFunctionExpression: guard(checkFunction),
229
+ ArrowFunctionExpression: guard(checkFunction)
216
230
  };
217
231
  },
218
232
  defaultOptions: [],
219
233
  meta: {
220
234
  type: "suggestion",
235
+ hasSuggestions: true,
221
236
  docs: {
222
237
  description:
223
- "Async functions should have a try-catch block returning an error result.",
238
+ "Async functions should have a try-catch block returning an error result."
224
239
  },
225
- fixable: "code",
226
240
  schema: [],
227
241
  messages: {
228
242
  missingTryCatch:
229
243
  "Async functions must have a try-catch block returning an error result.",
230
- },
231
- },
244
+ suggestTryCatch:
245
+ "Wrap the body in try/catch and return err() from the catch."
246
+ }
247
+ }
232
248
  };
233
249
 
234
250
  export { rule as requireTryCatchAsyncRule };
@@ -5,12 +5,12 @@ import { walkOwnSubtree } from "./own_subtree.js";
5
5
  *
6
6
  * A `throw` in a function's own body is the author saying this failure is not a
7
7
  * value to hand back. A missing manifest for a route the app is navigating to,
8
- * an unreachable branch, a broken invariant — those should be loud. Turning them
9
- * into `err()` converts a bug into a silently handled outcome, and the caller
10
- * degrades politely instead of surfacing that something is wrong.
8
+ * an unreachable branch, a broken invariant — those should be loud. Turning
9
+ * them into `err()` converts a bug into a silently handled outcome, and the
10
+ * caller degrades politely instead of surfacing that something is wrong.
11
11
  *
12
12
  * So both async rules leave such a function alone. The rules exist to push
13
- * *recoverable* failures into the return type, not to abolish throwing.
13
+ * _recoverable_ failures into the return type, not to abolish throwing.
14
14
  *
15
15
  * Two boundaries matter. A throw inside a nested function belongs to that
16
16
  * function, so the walk does not descend into one. A throw inside `catch` still
@@ -21,15 +21,15 @@ import { walkOwnSubtree } from "./own_subtree.js";
21
21
  * @returns {boolean} True when the body throws outside any nested function.
22
22
  */
23
23
  function throwsDeliberately(body: unknown): boolean {
24
- let found = false;
24
+ let found = false;
25
25
 
26
- walkOwnSubtree(body, (node) => {
27
- if (node.type === "ThrowStatement") {
28
- found = true;
29
- }
30
- });
26
+ walkOwnSubtree(body, (node) => {
27
+ if (node.type === "ThrowStatement") {
28
+ found = true;
29
+ }
30
+ });
31
31
 
32
- return found;
32
+ return found;
33
33
  }
34
34
 
35
35
  export { throwsDeliberately };