Skip to content

Commit db65c75

Browse files
committed
feat(no-focused-tests): make fixable
1 parent 9725597 commit db65c75

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

src/rules/__tests__/no-focused-tests.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -31,78 +31,97 @@ ruleTester.run('no-focused-tests', rule, {
3131
invalid: [
3232
{
3333
code: 'describe.only()',
34+
output: 'describe()',
3435
errors: [{ messageId: 'focusedTest', column: 10, line: 1 }],
3536
},
3637
{
3738
code: 'describe.only.each()',
39+
output: 'describe.each()',
3840
errors: [{ messageId: 'focusedTest', column: 10, line: 1 }],
3941
},
4042
{
4143
code: 'describe.only.each`table`()',
44+
output: 'describe.each`table`()',
4245
errors: [{ messageId: 'focusedTest', column: 10, line: 1 }],
4346
},
4447
{
4548
code: 'describe["only"]()',
49+
output: 'describe()',
4650
errors: [{ messageId: 'focusedTest', column: 10, line: 1 }],
4751
},
4852
{
4953
code: 'it.only()',
54+
output: 'it()',
5055
errors: [{ messageId: 'focusedTest', column: 4, line: 1 }],
5156
},
5257
{
5358
code: 'it.concurrent.only()',
59+
output: 'it.concurrent()',
5460
errors: [{ messageId: 'focusedTest', column: 4, line: 1 }],
5561
},
5662
{
5763
code: 'it.only.each()',
64+
output: 'it.each()',
5865
errors: [{ messageId: 'focusedTest', column: 4, line: 1 }],
5966
},
6067
{
6168
code: 'it.only.each`table`()',
69+
output: 'it.each`table`()',
6270
errors: [{ messageId: 'focusedTest', column: 4, line: 1 }],
6371
},
6472
{
6573
code: 'it["only"]()',
74+
output: 'it()',
6675
errors: [{ messageId: 'focusedTest', column: 4, line: 1 }],
6776
},
6877
{
6978
code: 'test.only()',
79+
output: 'test()',
7080
errors: [{ messageId: 'focusedTest', column: 6, line: 1 }],
7181
},
7282
{
7383
code: 'test.concurrent.only()',
84+
output: 'test.concurrent()',
7485
errors: [{ messageId: 'focusedTest', column: 6, line: 1 }],
7586
},
7687
{
7788
code: 'test.only.each()',
89+
output: 'test.each()',
7890
errors: [{ messageId: 'focusedTest', column: 6, line: 1 }],
7991
},
8092
{
8193
code: 'test.only.each`table`()',
94+
output: 'test.each`table`()',
8295
errors: [{ messageId: 'focusedTest', column: 6, line: 1 }],
8396
},
8497
{
8598
code: 'test["only"]()',
99+
output: 'test()',
86100
errors: [{ messageId: 'focusedTest', column: 6, line: 1 }],
87101
},
88102
{
89103
code: 'fdescribe()',
104+
output: 'describe()',
90105
errors: [{ messageId: 'focusedTest', column: 1, line: 1 }],
91106
},
92107
{
93108
code: 'fit()',
109+
output: 'it()',
94110
errors: [{ messageId: 'focusedTest', column: 1, line: 1 }],
95111
},
96112
{
97113
code: 'fit.each()',
114+
output: 'it.each()',
98115
errors: [{ messageId: 'focusedTest', column: 1, line: 1 }],
99116
},
100117
{
101118
code: 'fit.each`table`()',
119+
output: 'it.each`table`()',
102120
errors: [{ messageId: 'focusedTest', column: 1, line: 1 }],
103121
},
104122
{
105123
code: 'ftest.each`table`()',
124+
output: 'test.each`table`()',
106125
errors: [{ messageId: 'focusedTest', column: 1, line: 1 }],
107126
},
108127
],

src/rules/no-focused-tests.ts

+41-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,18 @@ export default createRule({
7171
callee.object.type === AST_NODE_TYPES.Identifier &&
7272
isCallToFocusedTestFunction(callee.object)
7373
) {
74-
context.report({ messageId: 'focusedTest', node: callee.object });
74+
context.report({
75+
messageId: 'focusedTest',
76+
node: callee.object,
77+
fix(fixer) {
78+
return [
79+
fixer.removeRange([
80+
callee.object.range[0],
81+
callee.object.range[0] + 1,
82+
]),
83+
];
84+
},
85+
});
7586

7687
return;
7788
}
@@ -80,16 +91,36 @@ export default createRule({
8091
callee.object.type === AST_NODE_TYPES.MemberExpression &&
8192
isCallToTestOnlyFunction(callee.object)
8293
) {
94+
const calleeObject: TSESTree.MemberExpression = callee.object;
95+
8396
context.report({
8497
messageId: 'focusedTest',
85-
node: callee.object.property,
98+
node: calleeObject.property,
99+
fix(fixer) {
100+
return [
101+
fixer.removeRange(
102+
calleeObject.property.type === AST_NODE_TYPES.Identifier &&
103+
calleeObject.property.name === 'only'
104+
? [calleeObject.object.range[1], calleeObject.range[1]]
105+
: [calleeObject.range[1], callee.range[1]],
106+
),
107+
];
108+
},
86109
});
87110

88111
return;
89112
}
90113

91114
if (isCallToTestOnlyFunction(callee)) {
92-
context.report({ messageId: 'focusedTest', node: callee.property });
115+
context.report({
116+
messageId: 'focusedTest',
117+
node: callee.property,
118+
fix(fixer) {
119+
return [
120+
fixer.removeRange([callee.object.range[1], callee.range[1]]),
121+
];
122+
},
123+
});
93124

94125
return;
95126
}
@@ -99,7 +130,13 @@ export default createRule({
99130
callee.type === AST_NODE_TYPES.Identifier &&
100131
isCallToFocusedTestFunction(callee)
101132
) {
102-
context.report({ messageId: 'focusedTest', node: callee });
133+
context.report({
134+
messageId: 'focusedTest',
135+
node: callee,
136+
fix(fixer) {
137+
return [fixer.removeRange([callee.range[0], callee.range[0] + 1])];
138+
},
139+
});
103140
}
104141
},
105142
}),

0 commit comments

Comments
 (0)