Skip to content

Commit c3ebd91

Browse files
committed
fix(lint): make redirect autofix safe
1 parent 96ac097 commit c3ebd91

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

.oxlintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "./node_modules/oxlint/configuration_schema.json",
33
"plugins": ["typescript", "import", "react"],
4-
"jsPlugins": ["./oxlint-plugins/no-thrown-unawaited-redirect.js"],
4+
"jsPlugins": ["./oxlint-plugins/no-thrown-unawaited-redirect.mjs"],
55
"ignorePatterns": [
66
"**/dist/**",
77
"**/build/**",

oxlint-plugins/no-thrown-unawaited-redirect.js renamed to oxlint-plugins/no-thrown-unawaited-redirect.mjs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ const ASYNC_REDIRECT_HELPERS = new Set([
2525
"redirectWithImpersonation",
2626
]);
2727

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+
2848
/** @type {import("eslint").Rule.RuleModule} */
2949
const noThrownUnawaitedRedirect = {
3050
meta: {
@@ -60,13 +80,13 @@ const noThrownUnawaitedRedirect = {
6080
return;
6181
}
6282

83+
const canAutofix = isInsideAsyncFunction(node, context.sourceCode);
84+
6385
context.report({
6486
node: argument,
6587
messageId: "unawaited",
6688
data: { name: callee.name },
67-
fix(fixer) {
68-
return fixer.insertTextBefore(argument, "await ");
69-
},
89+
fix: canAutofix ? (fixer) => fixer.insertTextBefore(argument, "await ") : undefined,
7090
});
7191
},
7292
};

0 commit comments

Comments
 (0)