Skip to content

Commit 84abd02

Browse files
committed
feat: add auto-fix for o-wait-for-side-effects
1 parent 2d24966 commit 84abd02

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lib/rules/no-wait-for-side-effects.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isAwaitExpression } from '@typescript-eslint/utils/ast-utils';
2+
13
import { createTestingLibraryRule } from '../create-testing-library-rule';
24
import {
35
getPropertyIdentifierNode,
@@ -8,6 +10,7 @@ import {
810
isSequenceExpression,
911
hasThenProperty,
1012
} from '../node-utils';
13+
import { getSourceCode } from '../utils';
1114

1215
import type { TSESTree } from '@typescript-eslint/utils';
1316

@@ -35,6 +38,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
3538
'Avoid using side effects within `waitFor` callback',
3639
},
3740
schema: [],
41+
fixable: 'code',
3842
},
3943
defaultOptions: [],
4044
create(context, _, helpers) {
@@ -209,10 +213,15 @@ export default createTestingLibraryRule<Options, MessageIds>({
209213
}
210214

211215
function reportImplicitReturnSideEffect(
212-
node:
216+
node: (
213217
| TSESTree.AssignmentExpression
214218
| TSESTree.CallExpression
215219
| TSESTree.SequenceExpression
220+
) & {
221+
parent: TSESTree.ArrowFunctionExpression & {
222+
parent: TSESTree.CallExpression;
223+
};
224+
}
216225
) {
217226
if (!isCallerWaitFor(node)) {
218227
return;
@@ -242,6 +251,14 @@ export default createTestingLibraryRule<Options, MessageIds>({
242251
context.report({
243252
node,
244253
messageId: 'noSideEffectsWaitFor',
254+
fix: (fixer) => {
255+
const { parent: callExpressionNode } = node.parent;
256+
const targetNode = isAwaitExpression(callExpressionNode.parent)
257+
? callExpressionNode.parent
258+
: callExpressionNode;
259+
const sourceCode = getSourceCode(context);
260+
return fixer.replaceText(targetNode, sourceCode.getText(node));
261+
},
245262
});
246263
}
247264

0 commit comments

Comments
 (0)