From db65c75c0b47a2266eaf83fbc79fa0754dc7774e Mon Sep 17 00:00:00 2001 From: Alexandre Lagane Date: Mon, 8 Mar 2021 14:19:19 +0100 Subject: [PATCH] feat(no-focused-tests): make fixable --- src/rules/__tests__/no-focused-tests.test.ts | 19 +++++++++ src/rules/no-focused-tests.ts | 45 ++++++++++++++++++-- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/rules/__tests__/no-focused-tests.test.ts b/src/rules/__tests__/no-focused-tests.test.ts index decf128b1..51d8840be 100644 --- a/src/rules/__tests__/no-focused-tests.test.ts +++ b/src/rules/__tests__/no-focused-tests.test.ts @@ -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 }], }, ], diff --git a/src/rules/no-focused-tests.ts b/src/rules/no-focused-tests.ts index d0af58d81..a4945dedf 100644 --- a/src/rules/no-focused-tests.ts +++ b/src/rules/no-focused-tests.ts @@ -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; } @@ -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; } @@ -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])]; + }, + }); } }, }),