Skip to content

Commit 6de56f6

Browse files
committed
feat: implement wrapWithFunctionExpressionFix function
1 parent a330a56 commit 6de56f6

File tree

3 files changed

+29
-34
lines changed

3 files changed

+29
-34
lines changed

lib/rules/await-async-queries.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
isMemberExpression,
1212
isPromiseHandled,
1313
} from '../node-utils';
14+
import { wrapWithFunctionExpressionFix } from '../utils/wrap-function-expression-fix';
1415

1516
import type { TSESTree } from '@typescript-eslint/utils';
1617

@@ -164,20 +165,11 @@ export default createTestingLibraryRule<Options, MessageIds>({
164165
);
165166
}
166167

167-
const ruleFixes = [IdentifierNodeFixer];
168-
if (!functionExpression.async) {
169-
/**
170-
* Mutate the actual node so if other nodes exist in this
171-
* function expression body they don't also try to fix it.
172-
*/
173-
functionExpression.async = true;
174-
175-
ruleFixes.push(
176-
fixer.insertTextBefore(functionExpression, 'async ')
177-
);
178-
}
179-
180-
return ruleFixes;
168+
return wrapWithFunctionExpressionFix(
169+
fixer,
170+
IdentifierNodeFixer,
171+
functionExpression
172+
);
181173
},
182174
});
183175
}

lib/rules/await-async-utils.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
isPromiseHandled,
1515
isProperty,
1616
} from '../node-utils';
17+
import { wrapWithFunctionExpressionFix } from '../utils/wrap-function-expression-fix';
1718

1819
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
1920

@@ -93,26 +94,6 @@ export default createTestingLibraryRule<Options, MessageIds>({
9394
}
9495
}
9596
}
96-
function wrapWithFunctionExpressionFix(
97-
fixer: TSESLint.RuleFixer,
98-
ruleFix: TSESLint.RuleFix,
99-
functionExpression:
100-
| TSESTree.ArrowFunctionExpression
101-
| TSESTree.FunctionDeclaration
102-
| TSESTree.FunctionExpression
103-
| null
104-
) {
105-
if (functionExpression && !functionExpression.async) {
106-
/**
107-
* Mutate the actual node so if other nodes exist in this
108-
* function expression body they don't also try to fix it.
109-
*/
110-
functionExpression.async = true;
111-
112-
return [ruleFix, fixer.insertTextBefore(functionExpression, 'async ')];
113-
}
114-
return ruleFix;
115-
}
11697

11798
function insertAwaitBeforeNode(
11899
fixer: TSESLint.RuleFixer,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
2+
3+
export const wrapWithFunctionExpressionFix = (
4+
fixer: TSESLint.RuleFixer,
5+
ruleFix: TSESLint.RuleFix,
6+
functionExpression:
7+
| TSESTree.ArrowFunctionExpression
8+
| TSESTree.FunctionDeclaration
9+
| TSESTree.FunctionExpression
10+
| null
11+
) => {
12+
if (functionExpression && !functionExpression.async) {
13+
/**
14+
* Mutate the actual node so if other nodes exist in this
15+
* function expression body they don't also try to fix it.
16+
*/
17+
functionExpression.async = true;
18+
19+
return [ruleFix, fixer.insertTextBefore(functionExpression, 'async ')];
20+
}
21+
return ruleFix;
22+
};

0 commit comments

Comments
 (0)