@murky-web/typebuddy 2.0.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.
Files changed (45) 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 +9 -8
  9. package/dist/rules/async_rule.js +45 -28
  10. package/dist/rules/deliberate_throw.js +5 -6
  11. package/dist/rules/maybe_promise_rule.js +109 -47
  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 +63 -0
  19. package/globals.d.ts +49 -44
  20. package/jsr.json +3 -2
  21. package/oxlint/index.ts +7 -5
  22. package/package.json +92 -91
  23. package/rules/async_rule.ts +87 -67
  24. package/rules/deliberate_throw.ts +11 -11
  25. package/rules/maybe_promise_rule.ts +229 -98
  26. package/rules/maybe_rule.ts +74 -47
  27. package/rules/nullable_rule.ts +78 -50
  28. package/rules/optional_rule.ts +80 -51
  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 +117 -0
  33. package/src/empty.ts +57 -19
  34. package/src/guards.ts +96 -30
  35. package/src/ids.ts +10 -4
  36. package/src/index.ts +1 -18
  37. package/src/parse.ts +104 -25
  38. package/src/public_api.ts +22 -0
  39. package/src/result.ts +11 -9
  40. package/src/types/globals.ts +11 -11
  41. package/src/types/index.ts +21 -21
  42. package/src/types/json.ts +48 -11
  43. package/src/types/maybe_promise.ts +17 -12
  44. package/src/types/nullable.ts +2 -2
  45. 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.0.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.0",
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,9 @@
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";
6
+ import { createUseServerGuard } from "./use_server.js";
3
7
 
4
8
  type AstNode = {
5
9
  type: string;
@@ -10,26 +14,26 @@ type AstNode = {
10
14
  [key: string]: unknown;
11
15
  };
12
16
 
17
+ type RuleFixer = {
18
+ replaceText(node: unknown, text: string): unknown;
19
+ insertTextBeforeRange(range: [number, number], text: string): unknown;
20
+ };
21
+
13
22
  type RuleContext = {
14
- getSourceCode(): { getText(node: unknown): string };
23
+ getSourceCode(): {
24
+ getText(node: unknown): string;
25
+ getScope(node: unknown): unknown;
26
+ };
15
27
  report(descriptor: {
16
28
  node: unknown;
17
29
  messageId: string;
18
- fix(fixer: {
19
- replaceText(node: unknown, text: string): unknown;
20
- insertTextBeforeRange(
21
- range: [number, number],
22
- text: string,
23
- ): unknown;
24
- }): unknown;
30
+ suggest?: {
31
+ messageId: string;
32
+ fix(fixer: RuleFixer): unknown;
33
+ }[];
25
34
  }): void;
26
35
  };
27
36
 
28
- type RuleFixer = {
29
- replaceText(node: unknown, text: string): unknown;
30
- insertTextBeforeRange(range: [number, number], text: string): unknown;
31
- };
32
-
33
37
  function hasTryCatch(nodes: AstNode[]): boolean {
34
38
  return nodes.some((node) => {
35
39
  return node.type === "TryStatement";
@@ -54,46 +58,32 @@ function isCallArgumentCallback(node: AstNode): boolean {
54
58
  const rule = {
55
59
  create(context: RuleContext) {
56
60
  const sourceCode = context.getSourceCode();
57
- let scheduledErrImport = false;
58
-
59
- function ensureErrImportFixes(
60
- node: AstNode,
61
- fixer: RuleFixer,
62
- ): unknown[] {
63
- if (scheduledErrImport) {
64
- return [];
65
- }
66
-
67
- const insertion = getTypeBuddyImportInsertion(node, "err");
68
- if (!insertion) {
69
- return [];
70
- }
71
-
72
- scheduledErrImport = true;
73
-
74
- return [
75
- fixer.insertTextBeforeRange(insertion.range, insertion.text),
76
- ];
77
- }
61
+ const imports = createImportPlanner(sourceCode);
78
62
 
79
63
  // Two zero-width insertions leave the body's own text untouched, byte
80
64
  // for byte. Rebuilding it statement by statement used to strip leading
81
65
  // whitespace on every line — including lines inside template literals,
82
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.
83
71
  function wrapBlockFixes(
84
72
  bodyNode: AstNode,
85
73
  fixer: RuleFixer,
86
74
  ): unknown[] | null {
87
75
  const range = bodyNode.range;
88
- 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
+ ) {
89
82
  return null;
90
83
  }
91
84
 
92
85
  return [
93
- fixer.insertTextBeforeRange(
94
- [range[0] + 1, range[0] + 1],
95
- "\ntry {",
96
- ),
86
+ fixer.insertTextBeforeRange([tryStart, tryStart], "\ntry {"),
97
87
  fixer.insertTextBeforeRange(
98
88
  [range[1] - 1, range[1] - 1],
99
89
  "} catch {\nreturn err();\n}\n",
@@ -152,8 +142,50 @@ const rule = {
152
142
  ];
153
143
  }
154
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
+ },
180
+ ];
181
+ }
182
+
155
183
  function checkFunction(node: AstNode) {
156
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;
157
189
  if (isCallArgumentCallback(node)) return;
158
190
  const bodyNode = node.body;
159
191
  if (!bodyNode || Array.isArray(bodyNode)) return;
@@ -169,21 +201,9 @@ const rule = {
169
201
  context.report({
170
202
  node,
171
203
  messageId: "missingTryCatch",
172
- fix(fixer) {
173
- const wrapFixes = wrapExpressionBodyFixes(
174
- node,
175
- bodyNode,
176
- fixer,
177
- );
178
- if (!wrapFixes) {
179
- return null;
180
- }
181
-
182
- return [
183
- ...ensureErrImportFixes(node, fixer),
184
- ...wrapFixes,
185
- ];
186
- },
204
+ suggest: suggestWrap(node, (fixer) => {
205
+ return wrapExpressionBodyFixes(node, bodyNode, fixer);
206
+ }),
187
207
  });
188
208
  return;
189
209
  }
@@ -194,35 +214,35 @@ const rule = {
194
214
  context.report({
195
215
  node,
196
216
  messageId: "missingTryCatch",
197
- fix(fixer) {
198
- const wrapFixes = wrapBlockFixes(bodyNode, fixer);
199
- if (!wrapFixes) {
200
- return null;
201
- }
202
-
203
- return [...ensureErrImportFixes(node, fixer), ...wrapFixes];
204
- },
217
+ suggest: suggestWrap(bodyNode, (fixer) => {
218
+ return wrapBlockFixes(bodyNode, fixer);
219
+ }),
205
220
  });
206
221
  }
207
222
 
223
+ const { Program, guard } = createUseServerGuard();
224
+
208
225
  return {
209
- FunctionDeclaration: checkFunction,
210
- FunctionExpression: checkFunction,
211
- ArrowFunctionExpression: checkFunction,
226
+ Program,
227
+ FunctionDeclaration: guard(checkFunction),
228
+ FunctionExpression: guard(checkFunction),
229
+ ArrowFunctionExpression: guard(checkFunction),
212
230
  };
213
231
  },
214
232
  defaultOptions: [],
215
233
  meta: {
216
234
  type: "suggestion",
235
+ hasSuggestions: true,
217
236
  docs: {
218
237
  description:
219
238
  "Async functions should have a try-catch block returning an error result.",
220
239
  },
221
- fixable: "code",
222
240
  schema: [],
223
241
  messages: {
224
242
  missingTryCatch:
225
243
  "Async functions must have a try-catch block returning an error result.",
244
+ suggestTryCatch:
245
+ "Wrap the body in try/catch and return err() from the catch.",
226
246
  },
227
247
  },
228
248
  };
@@ -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 };