diff --git a/packages/query-codemods/src/v4/__testfixtures__/no-react-query-import.input.tsx b/packages/query-codemods/src/v4/__testfixtures__/no-react-query-import.input.tsx new file mode 100644 index 00000000000..0dccf158666 --- /dev/null +++ b/packages/query-codemods/src/v4/__testfixtures__/no-react-query-import.input.tsx @@ -0,0 +1,7 @@ +const title = 'No React Query usage' + +export function Example() { + const greeting = title.toUpperCase() + + return
{greeting}
+} diff --git a/packages/query-codemods/src/v4/__testfixtures__/no-react-query-import.output.tsx b/packages/query-codemods/src/v4/__testfixtures__/no-react-query-import.output.tsx new file mode 100644 index 00000000000..0dccf158666 --- /dev/null +++ b/packages/query-codemods/src/v4/__testfixtures__/no-react-query-import.output.tsx @@ -0,0 +1,7 @@ +const title = 'No React Query usage' + +export function Example() { + const greeting = title.toUpperCase() + + return
{greeting}
+} diff --git a/packages/query-codemods/src/v4/__tests__/key-transformation.test.cjs b/packages/query-codemods/src/v4/__tests__/key-transformation.test.cjs index 96fcf878a4d..18161dc3187 100644 --- a/packages/query-codemods/src/v4/__tests__/key-transformation.test.cjs +++ b/packages/query-codemods/src/v4/__tests__/key-transformation.test.cjs @@ -35,3 +35,7 @@ defineTest( defineTest(__dirname, 'key-transformation.cjs', null, 'type-arguments', { parser: 'tsx', }) + +defineTest(__dirname, 'key-transformation.cjs', null, 'no-react-query-import', { + parser: 'tsx', +}) diff --git a/packages/query-codemods/src/v4/key-transformation.cjs b/packages/query-codemods/src/v4/key-transformation.cjs index ad62c084a5f..6012a9b2fbc 100644 --- a/packages/query-codemods/src/v4/key-transformation.cjs +++ b/packages/query-codemods/src/v4/key-transformation.cjs @@ -145,7 +145,17 @@ module.exports = (file, api) => { const jscodeshift = api.jscodeshift const root = jscodeshift(file.source) - // TODO: Execute the transformers only when it contains a `react-query` import! + const hasReactQueryImport = root + .find(jscodeshift.ImportDeclaration, { + source: { + value: 'react-query', + }, + }) + .paths().length + + if (!hasReactQueryImport) { + return file.source + } const utils = createUtilsObject({ root, jscodeshift }) const filePath = file.path