Skip to content

Commit 5a3f389

Browse files
committed
feat: add autofix
1 parent e04a940 commit 5a3f389

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

Diff for: lib/rules/no-await-sync-queries.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TSESTree } from '@typescript-eslint/utils';
1+
import { ASTUtils, TSESTree } from '@typescript-eslint/utils';
22

33
import { createTestingLibraryRule } from '../create-testing-library-rule';
44
import { getDeepestIdentifierNode } from '../node-utils';
@@ -26,15 +26,20 @@ export default createTestingLibraryRule<Options, MessageIds>({
2626
'`{{ name }}` query is sync so it does not need to be awaited',
2727
},
2828
schema: [],
29+
fixable: 'code',
2930
},
3031
defaultOptions: [],
3132

3233
create(context, _, helpers) {
3334
return {
3435
'AwaitExpression > CallExpression'(node: TSESTree.CallExpression) {
36+
const awaitExpression = node.parent;
3537
const deepestIdentifierNode = getDeepestIdentifierNode(node);
3638

37-
if (!deepestIdentifierNode) {
39+
if (
40+
!ASTUtils.isAwaitExpression(awaitExpression) ||
41+
!deepestIdentifierNode
42+
) {
3843
return;
3944
}
4045

@@ -45,6 +50,15 @@ export default createTestingLibraryRule<Options, MessageIds>({
4550
data: {
4651
name: deepestIdentifierNode.name,
4752
},
53+
fix: (fixer) => {
54+
const awaitRangeStart = awaitExpression.range[0];
55+
const awaitRangeEnd = awaitExpression.range[0] + 'await '.length;
56+
57+
return fixer.replaceTextRange(
58+
[awaitRangeStart, awaitRangeEnd],
59+
''
60+
);
61+
},
4862
});
4963
}
5064
},

Diff for: tests/lib/rules/no-await-sync-queries.test.ts

+53
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ ruleTester.run(RULE_NAME, rule, {
142142
column: 31,
143143
},
144144
],
145+
output: `async () => {
146+
const element = ${query}('foo')
147+
}
148+
`,
145149
} as const)
146150
),
147151
// custom sync queries with await operator are not valid
@@ -152,6 +156,11 @@ ruleTester.run(RULE_NAME, rule, {
152156
}
153157
`,
154158
errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 31 }],
159+
output: `
160+
async () => {
161+
const element = getByIcon('search')
162+
}
163+
`,
155164
},
156165
{
157166
code: `
@@ -160,6 +169,11 @@ ruleTester.run(RULE_NAME, rule, {
160169
}
161170
`,
162171
errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 31 }],
172+
output: `
173+
async () => {
174+
const element = queryByIcon('search')
175+
}
176+
`,
163177
},
164178
{
165179
code: `
@@ -168,6 +182,11 @@ ruleTester.run(RULE_NAME, rule, {
168182
}
169183
`,
170184
errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 38 }],
185+
output: `
186+
async () => {
187+
const element = screen.getAllByIcon('search')
188+
}
189+
`,
171190
},
172191
{
173192
code: `
@@ -176,6 +195,11 @@ ruleTester.run(RULE_NAME, rule, {
176195
}
177196
`,
178197
errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 38 }],
198+
output: `
199+
async () => {
200+
const element = screen.queryAllByIcon('search')
201+
}
202+
`,
179203
},
180204
// sync queries with await operator inside assert are not valid
181205
...SYNC_QUERIES_COMBINATIONS.map(
@@ -192,6 +216,10 @@ ruleTester.run(RULE_NAME, rule, {
192216
column: 22,
193217
},
194218
],
219+
output: `async () => {
220+
expect(${query}('foo')).toBeEnabled()
221+
}
222+
`,
195223
} as const)
196224
),
197225

@@ -210,6 +238,10 @@ ruleTester.run(RULE_NAME, rule, {
210238
column: 38,
211239
},
212240
],
241+
output: `async () => {
242+
const element = screen.${query}('foo')
243+
}
244+
`,
213245
} as const)
214246
),
215247

@@ -228,6 +260,10 @@ ruleTester.run(RULE_NAME, rule, {
228260
column: 29,
229261
},
230262
],
263+
output: `async () => {
264+
expect(screen.${query}('foo')).toBeEnabled()
265+
}
266+
`,
231267
} as const)
232268
),
233269

@@ -244,6 +280,12 @@ ruleTester.run(RULE_NAME, rule, {
244280
}
245281
`,
246282
errors: [{ messageId: 'noAwaitSyncQuery', line: 4, column: 38 }],
283+
output: `
284+
import { screen } from '${testingFramework}'
285+
() => {
286+
const element = screen.getByRole('button')
287+
}
288+
`,
247289
} as const)
248290
),
249291
// sync query awaited and related to custom module is not valid
@@ -256,6 +298,12 @@ ruleTester.run(RULE_NAME, rule, {
256298
}
257299
`,
258300
errors: [{ messageId: 'noAwaitSyncQuery', line: 4, column: 38 }],
301+
output: `
302+
import { screen } from 'test-utils'
303+
() => {
304+
const element = screen.getByRole('button')
305+
}
306+
`,
259307
},
260308

261309
// awaited custom sync query matching custom-queries setting is invalid
@@ -269,6 +317,11 @@ ruleTester.run(RULE_NAME, rule, {
269317
})
270318
`,
271319
errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 31 }],
320+
output: `
321+
test('A valid example test', async () => {
322+
const element = queryByIcon('search')
323+
})
324+
`,
272325
},
273326
],
274327
});

0 commit comments

Comments
 (0)