Skip to content

Commit

Permalink
feat(no-focused-tests): make fixable
Browse files Browse the repository at this point in the history
  • Loading branch information
alagane committed Mar 8, 2021
1 parent 9725597 commit db65c75
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
19 changes: 19 additions & 0 deletions src/rules/__tests__/no-focused-tests.test.ts
Expand Up @@ -31,78 +31,97 @@ ruleTester.run('no-focused-tests', rule, {
invalid: [
{
code: 'describe.only()',
output: 'describe()',
errors: [{ messageId: 'focusedTest', column: 10, line: 1 }],
},
{
code: 'describe.only.each()',
output: 'describe.each()',
errors: [{ messageId: 'focusedTest', column: 10, line: 1 }],
},
{
code: 'describe.only.each`table`()',
output: 'describe.each`table`()',
errors: [{ messageId: 'focusedTest', column: 10, line: 1 }],
},
{
code: 'describe["only"]()',
output: 'describe()',
errors: [{ messageId: 'focusedTest', column: 10, line: 1 }],
},
{
code: 'it.only()',
output: 'it()',
errors: [{ messageId: 'focusedTest', column: 4, line: 1 }],
},
{
code: 'it.concurrent.only()',
output: 'it.concurrent()',
errors: [{ messageId: 'focusedTest', column: 4, line: 1 }],
},
{
code: 'it.only.each()',
output: 'it.each()',
errors: [{ messageId: 'focusedTest', column: 4, line: 1 }],
},
{
code: 'it.only.each`table`()',
output: 'it.each`table`()',
errors: [{ messageId: 'focusedTest', column: 4, line: 1 }],
},
{
code: 'it["only"]()',
output: 'it()',
errors: [{ messageId: 'focusedTest', column: 4, line: 1 }],
},
{
code: 'test.only()',
output: 'test()',
errors: [{ messageId: 'focusedTest', column: 6, line: 1 }],
},
{
code: 'test.concurrent.only()',
output: 'test.concurrent()',
errors: [{ messageId: 'focusedTest', column: 6, line: 1 }],
},
{
code: 'test.only.each()',
output: 'test.each()',
errors: [{ messageId: 'focusedTest', column: 6, line: 1 }],
},
{
code: 'test.only.each`table`()',
output: 'test.each`table`()',
errors: [{ messageId: 'focusedTest', column: 6, line: 1 }],
},
{
code: 'test["only"]()',
output: 'test()',
errors: [{ messageId: 'focusedTest', column: 6, line: 1 }],
},
{
code: 'fdescribe()',
output: 'describe()',
errors: [{ messageId: 'focusedTest', column: 1, line: 1 }],
},
{
code: 'fit()',
output: 'it()',
errors: [{ messageId: 'focusedTest', column: 1, line: 1 }],
},
{
code: 'fit.each()',
output: 'it.each()',
errors: [{ messageId: 'focusedTest', column: 1, line: 1 }],
},
{
code: 'fit.each`table`()',
output: 'it.each`table`()',
errors: [{ messageId: 'focusedTest', column: 1, line: 1 }],
},
{
code: 'ftest.each`table`()',
output: 'test.each`table`()',
errors: [{ messageId: 'focusedTest', column: 1, line: 1 }],
},
],
Expand Down
45 changes: 41 additions & 4 deletions src/rules/no-focused-tests.ts
Expand Up @@ -71,7 +71,18 @@ export default createRule({
callee.object.type === AST_NODE_TYPES.Identifier &&
isCallToFocusedTestFunction(callee.object)
) {
context.report({ messageId: 'focusedTest', node: callee.object });
context.report({
messageId: 'focusedTest',
node: callee.object,
fix(fixer) {
return [
fixer.removeRange([
callee.object.range[0],
callee.object.range[0] + 1,
]),
];
},
});

return;
}
Expand All @@ -80,16 +91,36 @@ export default createRule({
callee.object.type === AST_NODE_TYPES.MemberExpression &&
isCallToTestOnlyFunction(callee.object)
) {
const calleeObject: TSESTree.MemberExpression = callee.object;

context.report({
messageId: 'focusedTest',
node: callee.object.property,
node: calleeObject.property,
fix(fixer) {
return [
fixer.removeRange(
calleeObject.property.type === AST_NODE_TYPES.Identifier &&
calleeObject.property.name === 'only'
? [calleeObject.object.range[1], calleeObject.range[1]]
: [calleeObject.range[1], callee.range[1]],
),
];
},
});

return;
}

if (isCallToTestOnlyFunction(callee)) {
context.report({ messageId: 'focusedTest', node: callee.property });
context.report({
messageId: 'focusedTest',
node: callee.property,
fix(fixer) {
return [
fixer.removeRange([callee.object.range[1], callee.range[1]]),
];
},
});

return;
}
Expand All @@ -99,7 +130,13 @@ export default createRule({
callee.type === AST_NODE_TYPES.Identifier &&
isCallToFocusedTestFunction(callee)
) {
context.report({ messageId: 'focusedTest', node: callee });
context.report({
messageId: 'focusedTest',
node: callee,
fix(fixer) {
return [fixer.removeRange([callee.range[0], callee.range[0] + 1])];
},
});
}
},
}),
Expand Down

0 comments on commit db65c75

Please sign in to comment.