|
| 1 | +/** |
| 2 | + * oxlint custom rule: no-thrown-unawaited-redirect |
| 3 | + * |
| 4 | + * Catches `throw someRedirectHelper(...)` where the helper is an *async* function |
| 5 | + * that returns a Promise<Response> (e.g. `redirectWithErrorMessage`). Throwing the |
| 6 | + * un-awaited call throws a *pending Promise* instead of a Response, so Remix renders |
| 7 | + * the route's error boundary instead of performing the redirect. |
| 8 | + * |
| 9 | + * Correct forms are: |
| 10 | + * - `throw await redirectWithErrorMessage(...)` |
| 11 | + * - `return redirectWithErrorMessage(...)` |
| 12 | + * |
| 13 | + * Note: the plain synchronous `redirect(...)` from `remix-typedjson` returns a |
| 14 | + * `Response` directly, so `throw redirect(...)` is the intended Remix control-flow |
| 15 | + * pattern and is intentionally NOT flagged. |
| 16 | + */ |
| 17 | + |
| 18 | +// Async redirect helpers that return a Promise. Extend this list as new async |
| 19 | +// redirect helpers are added. |
| 20 | +const ASYNC_REDIRECT_HELPERS = new Set([ |
| 21 | + "redirectWithSuccessMessage", |
| 22 | + "redirectWithErrorMessage", |
| 23 | + "redirectBackWithErrorMessage", |
| 24 | + "redirectBackWithSuccessMessage", |
| 25 | + "redirectWithImpersonation", |
| 26 | +]); |
| 27 | + |
| 28 | +const FUNCTION_TYPES = new Set([ |
| 29 | + "ArrowFunctionExpression", |
| 30 | + "FunctionDeclaration", |
| 31 | + "FunctionExpression", |
| 32 | +]); |
| 33 | + |
| 34 | +function isInsideAsyncFunction(node, sourceCode) { |
| 35 | + const ancestors = sourceCode.getAncestors(node); |
| 36 | + |
| 37 | + for (let index = ancestors.length - 1; index >= 0; index--) { |
| 38 | + const ancestor = ancestors[index]; |
| 39 | + |
| 40 | + if (FUNCTION_TYPES.has(ancestor.type)) { |
| 41 | + return ancestor.async; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return false; |
| 46 | +} |
| 47 | + |
| 48 | +/** @type {import("eslint").Rule.RuleModule} */ |
| 49 | +const noThrownUnawaitedRedirect = { |
| 50 | + meta: { |
| 51 | + type: "problem", |
| 52 | + docs: { |
| 53 | + description: |
| 54 | + "Disallow throwing an un-awaited async redirect helper (throws a pending Promise instead of a Response).", |
| 55 | + }, |
| 56 | + fixable: "code", |
| 57 | + messages: { |
| 58 | + unawaited: |
| 59 | + 'Throwing an un-awaited "{{name}}()" throws a pending Promise (Remix renders the error boundary instead of redirecting). Use "throw await {{name}}()" or "return {{name}}()".', |
| 60 | + }, |
| 61 | + schema: [], |
| 62 | + }, |
| 63 | + create(context) { |
| 64 | + return { |
| 65 | + ThrowStatement(node) { |
| 66 | + const argument = node.argument; |
| 67 | + |
| 68 | + // Already awaited (`throw await helper()`) -> fine. |
| 69 | + if (!argument || argument.type === "AwaitExpression") { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // Only care about direct calls: `throw helper(...)`. |
| 74 | + if (argument.type !== "CallExpression") { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const callee = argument.callee; |
| 79 | + if (callee.type !== "Identifier" || !ASYNC_REDIRECT_HELPERS.has(callee.name)) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const canAutofix = isInsideAsyncFunction(node, context.sourceCode); |
| 84 | + |
| 85 | + context.report({ |
| 86 | + node: argument, |
| 87 | + messageId: "unawaited", |
| 88 | + data: { name: callee.name }, |
| 89 | + fix: canAutofix ? (fixer) => fixer.insertTextBefore(argument, "await ") : undefined, |
| 90 | + }); |
| 91 | + }, |
| 92 | + }; |
| 93 | + }, |
| 94 | +}; |
| 95 | + |
| 96 | +/** @type {import("eslint").ESLint.Plugin} */ |
| 97 | +const plugin = { |
| 98 | + meta: { |
| 99 | + name: "trigger", |
| 100 | + }, |
| 101 | + rules: { |
| 102 | + "no-thrown-unawaited-redirect": noThrownUnawaitedRedirect, |
| 103 | + }, |
| 104 | +}; |
| 105 | + |
| 106 | +export default plugin; |
0 commit comments