-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add oxlint rule to catch thrown un-awaited redirect helpers #4222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /** | ||
| * oxlint custom rule: no-thrown-unawaited-redirect | ||
| * | ||
| * Catches `throw someRedirectHelper(...)` where the helper is an *async* function | ||
| * that returns a Promise<Response> (e.g. `redirectWithErrorMessage`). Throwing the | ||
| * un-awaited call throws a *pending Promise* instead of a Response, so Remix renders | ||
| * the route's error boundary instead of performing the redirect. | ||
| * | ||
| * Correct forms are: | ||
| * - `throw await redirectWithErrorMessage(...)` | ||
| * - `return redirectWithErrorMessage(...)` | ||
| * | ||
| * Note: the plain synchronous `redirect(...)` from `remix-typedjson` returns a | ||
| * `Response` directly, so `throw redirect(...)` is the intended Remix control-flow | ||
| * pattern and is intentionally NOT flagged. | ||
| */ | ||
|
|
||
| // Async redirect helpers that return a Promise. Extend this list as new async | ||
| // redirect helpers are added. | ||
| const ASYNC_REDIRECT_HELPERS = new Set([ | ||
| "redirectWithSuccessMessage", | ||
| "redirectWithErrorMessage", | ||
| "redirectBackWithErrorMessage", | ||
| "redirectBackWithSuccessMessage", | ||
| "redirectWithImpersonation", | ||
| ]); | ||
|
|
||
| const FUNCTION_TYPES = new Set([ | ||
| "ArrowFunctionExpression", | ||
| "FunctionDeclaration", | ||
| "FunctionExpression", | ||
| ]); | ||
|
|
||
| function isInsideAsyncFunction(node, sourceCode) { | ||
| const ancestors = sourceCode.getAncestors(node); | ||
|
|
||
| for (let index = ancestors.length - 1; index >= 0; index--) { | ||
| const ancestor = ancestors[index]; | ||
|
|
||
| if (FUNCTION_TYPES.has(ancestor.type)) { | ||
| return ancestor.async; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** @type {import("eslint").Rule.RuleModule} */ | ||
| const noThrownUnawaitedRedirect = { | ||
| meta: { | ||
| type: "problem", | ||
| docs: { | ||
| description: | ||
| "Disallow throwing an un-awaited async redirect helper (throws a pending Promise instead of a Response).", | ||
| }, | ||
| fixable: "code", | ||
| messages: { | ||
| unawaited: | ||
| 'Throwing an un-awaited "{{name}}()" throws a pending Promise (Remix renders the error boundary instead of redirecting). Use "throw await {{name}}()" or "return {{name}}()".', | ||
| }, | ||
| schema: [], | ||
| }, | ||
| create(context) { | ||
| return { | ||
| ThrowStatement(node) { | ||
| const argument = node.argument; | ||
|
|
||
| // Already awaited (`throw await helper()`) -> fine. | ||
| if (!argument || argument.type === "AwaitExpression") { | ||
| return; | ||
| } | ||
|
|
||
| // Only care about direct calls: `throw helper(...)`. | ||
| if (argument.type !== "CallExpression") { | ||
| return; | ||
| } | ||
|
|
||
| const callee = argument.callee; | ||
| if (callee.type !== "Identifier" || !ASYNC_REDIRECT_HELPERS.has(callee.name)) { | ||
| return; | ||
| } | ||
|
|
||
| const canAutofix = isInsideAsyncFunction(node, context.sourceCode); | ||
|
|
||
| context.report({ | ||
| node: argument, | ||
| messageId: "unawaited", | ||
| data: { name: callee.name }, | ||
| fix: canAutofix ? (fixer) => fixer.insertTextBefore(argument, "await ") : undefined, | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| /** @type {import("eslint").ESLint.Plugin} */ | ||
| const plugin = { | ||
| meta: { | ||
| name: "trigger", | ||
| }, | ||
| rules: { | ||
| "no-thrown-unawaited-redirect": noThrownUnawaitedRedirect, | ||
| }, | ||
| }; | ||
|
|
||
| export default plugin; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.