From 7787f627544da6578fd5252f9fb6c74e82513574 Mon Sep 17 00:00:00 2001 From: Marat Dyatko Date: Wed, 8 Sep 2021 15:52:45 +0200 Subject: [PATCH 1/2] fix(prefer-find-by): stop prefer-find-by fixer to break code when no spaces before bracket --- lib/rules/prefer-find-by.ts | 6 ++++-- tests/lib/rules/prefer-find-by.test.ts | 29 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/rules/prefer-find-by.ts b/lib/rules/prefer-find-by.ts index 97c99300..cf13b621 100644 --- a/lib/rules/prefer-find-by.ts +++ b/lib/rules/prefer-find-by.ts @@ -464,10 +464,12 @@ export default createTestingLibraryRule({ const textDestructuring = sourceCode.getText( allVariableDeclarations ); + const hasSpaceBeforeBracket = textDestructuring.endsWith(' }'); + const symbolsToRemove = hasSpaceBeforeBracket ? 2 : 1; const text = `${textDestructuring.substring( 0, - textDestructuring.length - 2 - )}, ${findByMethod} }`; + textDestructuring.length - symbolsToRemove + )}, ${findByMethod}${hasSpaceBeforeBracket ? ' ' : ''}}`; allFixes.push(fixer.replaceText(allVariableDeclarations, text)); } diff --git a/tests/lib/rules/prefer-find-by.test.ts b/tests/lib/rules/prefer-find-by.test.ts index 7975a023..aaafdaef 100644 --- a/tests/lib/rules/prefer-find-by.test.ts +++ b/tests/lib/rules/prefer-find-by.test.ts @@ -457,6 +457,35 @@ ruleTester.run(RULE_NAME, rule, { }) `, })), + ...createScenario((waitMethod: string, queryMethod: string) => ({ + code: ` + import {${waitMethod}} from '@testing-library/foo'; + it('tests', async () => { + const {${queryMethod}} = render() + const submitButton = await ${waitMethod}(() => expect(${queryMethod}('foo', { name: 'baz' })).not.toBeNull()) + }) + `, + errors: [ + { + messageId: 'preferFindBy', + data: { + queryVariant: getFindByQueryVariant(queryMethod), + queryMethod: queryMethod.split('By')[1], + prevQuery: queryMethod, + waitForMethodName: waitMethod, + }, + }, + ], + output: ` + import {${waitMethod}} from '@testing-library/foo'; + it('tests', async () => { + const {${queryMethod}, ${buildFindByMethod(queryMethod)}} = render() + const submitButton = await ${buildFindByMethod( + queryMethod + )}('foo', { name: 'baz' }) + }) + `, + })), ...createScenario((waitMethod: string, queryMethod: string) => ({ code: ` import {${waitMethod}} from '@testing-library/foo'; From 41a826401953a50174151db1d44c0ae9331df489 Mon Sep 17 00:00:00 2001 From: Marat Dyatko Date: Wed, 8 Sep 2021 17:22:27 +0200 Subject: [PATCH 2/2] fix(prefer-find-by): use regex to add findByMethod --- lib/rules/prefer-find-by.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/rules/prefer-find-by.ts b/lib/rules/prefer-find-by.ts index cf13b621..40bb865a 100644 --- a/lib/rules/prefer-find-by.ts +++ b/lib/rules/prefer-find-by.ts @@ -464,12 +464,10 @@ export default createTestingLibraryRule({ const textDestructuring = sourceCode.getText( allVariableDeclarations ); - const hasSpaceBeforeBracket = textDestructuring.endsWith(' }'); - const symbolsToRemove = hasSpaceBeforeBracket ? 2 : 1; - const text = `${textDestructuring.substring( - 0, - textDestructuring.length - symbolsToRemove - )}, ${findByMethod}${hasSpaceBeforeBracket ? ' ' : ''}}`; + const text = textDestructuring.replace( + /(\s*})$/, + `, ${findByMethod}$1` + ); allFixes.push(fixer.replaceText(allVariableDeclarations, text)); }