From d87188922e8d4da53bbaa3955bc48bdaf118aab4 Mon Sep 17 00:00:00 2001 From: balazsmatepetro Date: Tue, 31 Oct 2023 18:56:02 +0100 Subject: [PATCH 1/8] chore(codemod): add named examples to the `keep-previous-data` codemod, because these were missing --- .../__testfixtures__/named.input.tsx | 90 ++++++++++++++++++ .../__testfixtures__/named.output.tsx | 94 +++++++++++++++++++ 2 files changed, 184 insertions(+) diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx index e69de29bb2..4a44d23f24 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx @@ -0,0 +1,90 @@ +import * as React from 'react' +import axios from 'axios' +import { useQueries as useRenamedUseQueries, useQuery as useRenamedUseQuery } from '@tanstack/react-query' + +type Post = { + id: number + title: string + body: string +} + +const queryFn = async (): Promise> => { + const { data } = await axios.get( + 'https://jsonplaceholder.typicode.com/posts', + ) + return data +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform + * this usage. + */ +export const Example1 = () => { + const { data } = useRenamedUseQuery({ + queryKey: ['posts'], + queryFn: queryFn, + keepPreviousData: true, + }) + + return
{JSON.stringify(data)}
+} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example2 = () => { + const { data } = useRenamedUseQuery({ + queryKey: ['posts'], + queryFn: queryFn, + keepPreviousData: true, + placeholderData: () => previousData + }) + + return
{JSON.stringify(data)}
+} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example3 = () => { + const { data } = useRenamedUseQueries({ + queryKey: ['posts'], + queryFn: queryFn, + keepPreviousData: true, + placeholderData: "somePlaceholderData" + }) + + return
{JSON.stringify(data)}
+} + +/** + * The object expression has a `keepPreviousData` property with value `false`. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example4 = () => { + const { data } = useRenamedUseQueries({ + queryKey: ['posts'], + queryFn: queryFn, + keepPreviousData: false, + }) + + return
{JSON.stringify(data)}
+} + +/** + * The object expression has a `keepPreviousData` property with which is an identifier. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example5 = () => { + const keepPreviousDataIdentifier = false + const { data } = useRenamedUseQueries({ + queryKey: ['posts'], + queryFn: queryFn, + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) + + return
{JSON.stringify(data)}
+} diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx index e69de29bb2..d1235e04b6 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx @@ -0,0 +1,94 @@ +import * as React from 'react' +import axios from 'axios' +import { + keepPreviousData, + useQueries as useRenamedUseQueries, + useQuery as useRenamedUseQuery, +} from '@tanstack/react-query'; + +type Post = { + id: number + title: string + body: string +} + +const queryFn = async (): Promise> => { + const { data } = await axios.get( + 'https://jsonplaceholder.typicode.com/posts', + ) + return data +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform + * this usage. + */ +export const Example1 = () => { + const { data } = useRenamedUseQuery({ + queryKey: ['posts'], + queryFn: queryFn, + placeholderData: keepPreviousData + }) + + return
{JSON.stringify(data)}
+} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example2 = () => { + const { data } = useRenamedUseQuery({ + queryKey: ['posts'], + queryFn: queryFn, + keepPreviousData: true, + placeholderData: () => previousData + }) + + return
{JSON.stringify(data)}
+} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example3 = () => { + const { data } = useRenamedUseQueries({ + queryKey: ['posts'], + queryFn: queryFn, + keepPreviousData: true, + placeholderData: "somePlaceholderData" + }) + + return
{JSON.stringify(data)}
+} + +/** + * The object expression has a `keepPreviousData` property with value `false`. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example4 = () => { + const { data } = useRenamedUseQueries({ + queryKey: ['posts'], + queryFn: queryFn, + keepPreviousData: false, + }) + + return
{JSON.stringify(data)}
+} + +/** + * The object expression has a `keepPreviousData` property with which is an identifier. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example5 = () => { + const keepPreviousDataIdentifier = false + const { data } = useRenamedUseQueries({ + queryKey: ['posts'], + queryFn: queryFn, + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) + + return
{JSON.stringify(data)}
+} From 9d24b4d3c6983304e9c0cc1c9b872538a98f0fba Mon Sep 17 00:00:00 2001 From: balazsmatepetro Date: Tue, 31 Oct 2023 19:41:48 +0100 Subject: [PATCH 2/8] chore(codemod): remove the unnecessary `return` statement --- .../codemods/src/v5/keep-previous-data/keep-previous-data.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js b/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js index dc44483ae6..ca4d4d92cc 100644 --- a/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js +++ b/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js @@ -149,8 +149,6 @@ const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => { ) return node - - return node } catch (error) { utils.warn( error.name === AlreadyHasPlaceholderDataProperty.name From 0844154c903c8a45dd2be893dd72a12d8d511b69 Mon Sep 17 00:00:00 2001 From: balazsmatepetro Date: Wed, 1 Nov 2023 10:25:19 +0100 Subject: [PATCH 3/8] feat(codemod): cover the `setQueryDefaults` method usages on the `QueryClient` instance with the codemod For further details see: https://github.com/TanStack/query/pull/6196#discussion_r1377371519 --- .../__testfixtures__/default.input.tsx | 124 ++++++++++++++++- .../__testfixtures__/default.output.tsx | 124 ++++++++++++++++- .../__testfixtures__/named.input.tsx | 129 +++++++++++++++++- .../__testfixtures__/named.output.tsx | 124 ++++++++++++++++- .../keep-previous-data/keep-previous-data.js | 50 +++++-- 5 files changed, 529 insertions(+), 22 deletions(-) diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.input.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.input.tsx index f84f116d82..1257dd0f4f 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.input.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.input.tsx @@ -1,6 +1,6 @@ import * as React from 'react' import axios from 'axios' -import { useQueries, useQuery } from '@tanstack/react-query' +import { useQueries, useQuery, useQueryClient, QueryClient } from '@tanstack/react-query' type Post = { id: number @@ -53,7 +53,7 @@ export const Example3 = () => { queryKey: ['posts'], queryFn: queryFn, keepPreviousData: true, - placeholderData: "somePlaceholderData" + placeholderData: 'somePlaceholderData' }) return
{JSON.stringify(data)}
@@ -88,3 +88,123 @@ export const Example5 = () => { return
{JSON.stringify(data)}
} + +/** + * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform + * this usage. + */ +export const Example6 = () => { + const queryClient = new QueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: true + }) + + useQueryClient().setQueryDefaults(['key'], { + keepPreviousData: true + }) + + const anotherQueryClient = useQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: true + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example7 = () => { + const queryClient = new QueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: () => previousData + }) + + useQueryClient().setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: () => previousData + }) + + const anotherQueryClient = useQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: () => previousData + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example8 = () => { + const queryClient = new QueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + }) + + useQueryClient().setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + }) + + const anotherQueryClient = useQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `false`. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example9 = () => { + const queryClient = new QueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: false, + }) + + useQueryClient().setQueryDefaults(['key'], { + keepPreviousData: false, + }) + + const anotherQueryClient = useQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: false, + }) +} + +/** + * The object expression has a `keepPreviousData` property with which is an identifier. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example10 = () => { + const queryClient = new QueryClient() + const keepPreviousDataIdentifier = false + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) + + useQueryClient().setQueryDefaults(['key'], { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) + + const anotherQueryClient = useQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) +} diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.output.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.output.tsx index 875b6c1b29..ae1339d8d6 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.output.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.output.tsx @@ -1,6 +1,6 @@ import * as React from 'react' import axios from 'axios' -import { keepPreviousData, useQueries, useQuery } from '@tanstack/react-query'; +import { keepPreviousData, useQueries, useQuery, useQueryClient, QueryClient } from '@tanstack/react-query'; type Post = { id: number @@ -53,7 +53,7 @@ export const Example3 = () => { queryKey: ['posts'], queryFn: queryFn, keepPreviousData: true, - placeholderData: "somePlaceholderData" + placeholderData: 'somePlaceholderData' }) return
{JSON.stringify(data)}
@@ -88,3 +88,123 @@ export const Example5 = () => { return
{JSON.stringify(data)}
} + +/** + * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform + * this usage. + */ +export const Example6 = () => { + const queryClient = new QueryClient() + + queryClient.setQueryDefaults(['key'], { + placeholderData: keepPreviousData + }) + + useQueryClient().setQueryDefaults(['key'], { + placeholderData: keepPreviousData + }) + + const anotherQueryClient = useQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + placeholderData: keepPreviousData + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example7 = () => { + const queryClient = new QueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: () => previousData + }) + + useQueryClient().setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: () => previousData + }) + + const anotherQueryClient = useQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: () => previousData + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example8 = () => { + const queryClient = new QueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + }) + + useQueryClient().setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + }) + + const anotherQueryClient = useQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `false`. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example9 = () => { + const queryClient = new QueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: false, + }) + + useQueryClient().setQueryDefaults(['key'], { + keepPreviousData: false, + }) + + const anotherQueryClient = useQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: false, + }) +} + +/** + * The object expression has a `keepPreviousData` property with which is an identifier. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example10 = () => { + const queryClient = new QueryClient() + const keepPreviousDataIdentifier = false + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) + + useQueryClient().setQueryDefaults(['key'], { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) + + const anotherQueryClient = useQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) +} diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx index 4a44d23f24..8796413645 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx @@ -1,6 +1,11 @@ import * as React from 'react' import axios from 'axios' -import { useQueries as useRenamedUseQueries, useQuery as useRenamedUseQuery } from '@tanstack/react-query' +import { + useQueries as useRenamedUseQueries, + useQuery as useRenamedUseQuery, + useQueryClient as useRenamedUseQueryClient, + QueryClient as RenamedQueryClient +} from '@tanstack/react-query' type Post = { id: number @@ -53,7 +58,7 @@ export const Example3 = () => { queryKey: ['posts'], queryFn: queryFn, keepPreviousData: true, - placeholderData: "somePlaceholderData" + placeholderData: 'somePlaceholderData' }) return
{JSON.stringify(data)}
@@ -88,3 +93,123 @@ export const Example5 = () => { return
{JSON.stringify(data)}
} + +/** + * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform + * this usage. + */ +export const Example6 = () => { + const queryClient = new RenamedQueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: true + }) + + useRenamedUseQueryClient().setQueryDefaults(['key'], { + keepPreviousData: true + }) + + const anotherQueryClient = useRenamedUseQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: true + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example7 = () => { + const queryClient = new RenamedQueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: () => previousData + }) + + useRenamedUseQueryClient().setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: () => previousData + }) + + const anotherQueryClient = useRenamedUseQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: () => previousData + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example8 = () => { + const queryClient = new RenamedQueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + }) + + useRenamedUseQueryClient().setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + }) + + const anotherQueryClient = useRenamedUseQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `false`. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example9 = () => { + const queryClient = new RenamedQueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: false, + }) + + useRenamedUseQueryClient().setQueryDefaults(['key'], { + keepPreviousData: false, + }) + + const anotherQueryClient = useRenamedUseQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: false, + }) +} + +/** + * The object expression has a `keepPreviousData` property with which is an identifier. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example10 = () => { + const queryClient = new RenamedQueryClient() + const keepPreviousDataIdentifier = false + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) + + useRenamedUseQueryClient().setQueryDefaults(['key'], { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) + + const anotherQueryClient = useRenamedUseQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) +} diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx index d1235e04b6..230754ad8e 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx @@ -4,6 +4,8 @@ import { keepPreviousData, useQueries as useRenamedUseQueries, useQuery as useRenamedUseQuery, + useQueryClient as useRenamedUseQueryClient, + QueryClient as RenamedQueryClient, } from '@tanstack/react-query'; type Post = { @@ -57,7 +59,7 @@ export const Example3 = () => { queryKey: ['posts'], queryFn: queryFn, keepPreviousData: true, - placeholderData: "somePlaceholderData" + placeholderData: 'somePlaceholderData' }) return
{JSON.stringify(data)}
@@ -92,3 +94,123 @@ export const Example5 = () => { return
{JSON.stringify(data)}
} + +/** + * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform + * this usage. + */ +export const Example6 = () => { + const queryClient = new RenamedQueryClient() + + queryClient.setQueryDefaults(['key'], { + placeholderData: keepPreviousData + }) + + useRenamedUseQueryClient().setQueryDefaults(['key'], { + placeholderData: keepPreviousData + }) + + const anotherQueryClient = useRenamedUseQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + placeholderData: keepPreviousData + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example7 = () => { + const queryClient = new RenamedQueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: () => previousData + }) + + useRenamedUseQueryClient().setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: () => previousData + }) + + const anotherQueryClient = useRenamedUseQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: () => previousData + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example8 = () => { + const queryClient = new RenamedQueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + }) + + useRenamedUseQueryClient().setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + }) + + const anotherQueryClient = useRenamedUseQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `false`. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example9 = () => { + const queryClient = new RenamedQueryClient() + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: false, + }) + + useRenamedUseQueryClient().setQueryDefaults(['key'], { + keepPreviousData: false, + }) + + const anotherQueryClient = useRenamedUseQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: false, + }) +} + +/** + * The object expression has a `keepPreviousData` property with which is an identifier. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example10 = () => { + const queryClient = new RenamedQueryClient() + const keepPreviousDataIdentifier = false + + queryClient.setQueryDefaults(['key'], { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) + + useRenamedUseQueryClient().setQueryDefaults(['key'], { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) + + const anotherQueryClient = useRenamedUseQueryClient() + + anotherQueryClient.setQueryDefaults(['key'], { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + }) +} diff --git a/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js b/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js index ca4d4d92cc..255bb8a3ab 100644 --- a/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js +++ b/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js @@ -3,6 +3,8 @@ const createUtilsObject = require('../../utils') // eslint-disable-next-line @typescript-eslint/no-var-requires const createUseQueryLikeTransformer = require('../../utils/transformers/use-query-like-transformer') // eslint-disable-next-line @typescript-eslint/no-var-requires +const createQueryClientTransformer = require('../../utils/transformers/query-client-transformer') +// eslint-disable-next-line @typescript-eslint/no-var-requires const AlreadyHasPlaceholderDataProperty = require('./utils/already-has-placeholder-data-property') /** @@ -89,24 +91,23 @@ const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => { let isKeepPreviousDataInUse = false - const replacer = (path) => { + const replacer = (path, resolveTargetArgument, transformNode) => { const node = path.node - const functionArguments = [] const { start, end } = getCallExpressionLocation(node) try { - const firstArgument = node.arguments[0] ?? null + const targetArgument = resolveTargetArgument(node) - if (firstArgument && utils.isObjectExpression(firstArgument)) { + if (targetArgument && utils.isObjectExpression(targetArgument)) { const isPlaceholderDataPropertyPresent = - hasPlaceholderDataProperty(firstArgument) + hasPlaceholderDataProperty(targetArgument) - if (hasPlaceholderDataProperty(firstArgument)) { + if (hasPlaceholderDataProperty(targetArgument)) { throw new AlreadyHasPlaceholderDataProperty(node, filePath) } const keepPreviousDataProperty = - getKeepPreviousDataProperty(firstArgument) + getKeepPreviousDataProperty(targetArgument) const keepPreviousDataPropertyHasTrueValue = isObjectPropertyHasTrueBooleanLiteralValue(keepPreviousDataProperty) @@ -122,7 +123,7 @@ const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => { if (keepPreviousDataPropertyHasTrueValue) { // Removing the `keepPreviousData` property from the object. const mutableObjectExpressionProperties = - filterKeepPreviousDataProperty(firstArgument) + filterKeepPreviousDataProperty(targetArgument) if (!isPlaceholderDataPropertyPresent) { isKeepPreviousDataInUse = true @@ -133,14 +134,10 @@ const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => { ) } - functionArguments.push( + return transformNode( + node, jscodeshift.objectExpression(mutableObjectExpressionProperties), ) - - return jscodeshift.callExpression( - node.original.callee, - functionArguments, - ) } } @@ -162,7 +159,29 @@ const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => { createUseQueryLikeTransformer({ jscodeshift, utils, root }).execute( config.hooks, - replacer, + (path) => { + const resolveTargetArgument = (node) => node.arguments[0] ?? null + const transformNode = (node, transformedArgument) => + jscodeshift.callExpression(node.original.callee, [transformedArgument]) + + return replacer(path, resolveTargetArgument, transformNode) + }, + ) + + createQueryClientTransformer({ jscodeshift, utils, root }).execute( + config.queryClientMethods, + (path) => { + const resolveTargetArgument = (node) => node.arguments[1] ?? null + const transformNode = (node, transformedArgument) => { + return jscodeshift.callExpression(node.original.callee, [ + node.arguments[0], + transformedArgument, + ...node.arguments.slice(2, 0), + ]) + } + + return replacer(path, resolveTargetArgument, transformNode) + }, ) return { isKeepPreviousDataInUse } @@ -180,6 +199,7 @@ module.exports = (file, api) => { ...dependencies, config: { hooks: ['useInfiniteQuery', 'useQueries', 'useQuery'], + queryClientMethods: ['setQueryDefaults'], }, }) From 2b71fedfd26e7e9bfc531ca14c711e7a3f74cf21 Mon Sep 17 00:00:00 2001 From: balazsmatepetro Date: Wed, 1 Nov 2023 11:02:58 +0100 Subject: [PATCH 4/8] chore(codemod): rename the `isKeepPreviousDataInUse` to `shouldAddKeepPreviousDataImport` The new name describes better the purpose of this variable. --- .../src/v5/keep-previous-data/keep-previous-data.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js b/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js index 255bb8a3ab..50370843f0 100644 --- a/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js +++ b/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js @@ -89,7 +89,7 @@ const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => { return objectExpression.properties.find(isKeepPreviousDataObjectProperty) } - let isKeepPreviousDataInUse = false + let shouldAddKeepPreviousDataImport = false const replacer = (path, resolveTargetArgument, transformNode) => { const node = path.node @@ -126,7 +126,7 @@ const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => { filterKeepPreviousDataProperty(targetArgument) if (!isPlaceholderDataPropertyPresent) { - isKeepPreviousDataInUse = true + shouldAddKeepPreviousDataImport = true // When the `placeholderData` property is not present, the `placeholderData: keepPreviousData` property will be added. mutableObjectExpressionProperties.push( @@ -184,7 +184,7 @@ const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => { }, ) - return { isKeepPreviousDataInUse } + return { shouldAddKeepPreviousDataImport } } module.exports = (file, api) => { @@ -195,7 +195,7 @@ module.exports = (file, api) => { const dependencies = { jscodeshift, utils, root, filePath } - const { isKeepPreviousDataInUse } = transformUsages({ + const { shouldAddKeepPreviousDataImport } = transformUsages({ ...dependencies, config: { hooks: ['useInfiniteQuery', 'useQueries', 'useQuery'], @@ -203,7 +203,7 @@ module.exports = (file, api) => { }, }) - if (isKeepPreviousDataInUse) { + if (shouldAddKeepPreviousDataImport) { root .find(jscodeshift.ImportDeclaration, { source: { From 63eda96c4a995dc1aa553ee617ba305c5b9453f5 Mon Sep 17 00:00:00 2001 From: balazsmatepetro Date: Wed, 1 Nov 2023 15:53:44 +0100 Subject: [PATCH 5/8] feat(codemod): cover the `QueryClient` class instantiations with the codemod For further details see: https://github.com/TanStack/query/pull/6196#discussion_r1377371519 --- .../src/v5/keep-previous-data/README.md | 32 ++++++++ .../__testfixtures__/default.input.tsx | 74 +++++++++++++++++++ .../__testfixtures__/default.output.tsx | 74 +++++++++++++++++++ .../__testfixtures__/named.input.tsx | 74 +++++++++++++++++++ .../__testfixtures__/named.output.tsx | 74 +++++++++++++++++++ .../keep-previous-data/keep-previous-data.js | 57 +++++++++++++- 6 files changed, 381 insertions(+), 4 deletions(-) create mode 100644 packages/codemods/src/v5/keep-previous-data/README.md diff --git a/packages/codemods/src/v5/keep-previous-data/README.md b/packages/codemods/src/v5/keep-previous-data/README.md new file mode 100644 index 0000000000..5781519c1b --- /dev/null +++ b/packages/codemods/src/v5/keep-previous-data/README.md @@ -0,0 +1,32 @@ +### Intro + +The prerequisite for this code mod is to migrate your usages to the new syntax, so overloads for hooks and `QueryClient` methods shouldn't be available anymore. + +### Affected usages + +Please note, this code mod transforms usages only where the first argument is an object expression. + +The following usage should be transformed by the code mod: + +```ts +const {data} = useQuery({ + queryKey: ['posts'], + queryFn: queryFn, + keepPreviousData: true, +}) +``` + +But the following usage won't be transformed by the code mod, because the first argument an identifier: + +```ts +const hookArgument = { + queryKey: ['posts'], + queryFn: queryFn, + keepPreviousData: true, +} +const {data} = useQuery(hookArgument) +``` + +### Troubleshooting + +In case of any errors, feel free to reach us out via Discord or open an issue. If you open an issue, please provide a code snippet as well, because without a snippet we cannot find the bug in the code mod. diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.input.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.input.tsx index 1257dd0f4f..7f345d7031 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.input.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.input.tsx @@ -208,3 +208,77 @@ export const Example10 = () => { placeholderData: () => previousData }) } + +/** + * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform + * this usage. + */ +export const Example11 = () => { + new QueryClient({ + defaultOptions: { + queries: { + keepPreviousData: true + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example12 = () => { + new QueryClient({ + defaultOptions: { + queries: { + keepPreviousData: true, + placeholderData: () => previousData + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example13 = () => { + new QueryClient({ + defaultOptions: { + queries: { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `false`. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example14 = () => { + new QueryClient({ + defaultOptions: { + queries: { + keepPreviousData: false, + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with which is an identifier. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example15 = () => { + const keepPreviousDataIdentifier = false + new QueryClient({ + defaultOptions: { + queries: { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + } + } + }) +} diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.output.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.output.tsx index ae1339d8d6..b4493d815c 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.output.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.output.tsx @@ -208,3 +208,77 @@ export const Example10 = () => { placeholderData: () => previousData }) } + +/** + * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform + * this usage. + */ +export const Example11 = () => { + new QueryClient({ + defaultOptions: { + queries: { + placeholderData: keepPreviousData + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example12 = () => { + new QueryClient({ + defaultOptions: { + queries: { + keepPreviousData: true, + placeholderData: () => previousData + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example13 = () => { + new QueryClient({ + defaultOptions: { + queries: { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `false`. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example14 = () => { + new QueryClient({ + defaultOptions: { + queries: { + keepPreviousData: false, + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with which is an identifier. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example15 = () => { + const keepPreviousDataIdentifier = false + new QueryClient({ + defaultOptions: { + queries: { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + } + } + }) +} diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx index 8796413645..98ec5e15c5 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx @@ -213,3 +213,77 @@ export const Example10 = () => { placeholderData: () => previousData }) } + +/** + * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform + * this usage. + */ +export const Example11 = () => { + new RenamedQueryClient({ + defaultOptions: { + queries: { + keepPreviousData: true + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example12 = () => { + new RenamedQueryClient({ + defaultOptions: { + queries: { + keepPreviousData: true, + placeholderData: () => previousData + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example13 = () => { + new RenamedQueryClient({ + defaultOptions: { + queries: { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `false`. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example14 = () => { + new RenamedQueryClient({ + defaultOptions: { + queries: { + keepPreviousData: false, + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with which is an identifier. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example15 = () => { + const keepPreviousDataIdentifier = false + new RenamedQueryClient({ + defaultOptions: { + queries: { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + } + } + }) +} diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx index 230754ad8e..f8e8e586ac 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx @@ -214,3 +214,77 @@ export const Example10 = () => { placeholderData: () => previousData }) } + +/** + * The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform + * this usage. + */ +export const Example11 = () => { + new RenamedQueryClient({ + defaultOptions: { + queries: { + placeholderData: keepPreviousData + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example12 = () => { + new RenamedQueryClient({ + defaultOptions: { + queries: { + keepPreviousData: true, + placeholderData: () => previousData + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example13 = () => { + new RenamedQueryClient({ + defaultOptions: { + queries: { + keepPreviousData: true, + placeholderData: 'somePlaceholderData' + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with value `false`. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example14 = () => { + new RenamedQueryClient({ + defaultOptions: { + queries: { + keepPreviousData: false, + } + } + }) +} + +/** + * The object expression has a `keepPreviousData` property with which is an identifier. + * The codemod shouldn't transform this case, only warn the user about the manual migration. + */ +export const Example15 = () => { + const keepPreviousDataIdentifier = false + new RenamedQueryClient({ + defaultOptions: { + queries: { + keepPreviousData: keepPreviousDataIdentifier, + placeholderData: () => previousData + } + } + }) +} diff --git a/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js b/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js index 50370843f0..5b2a9553a9 100644 --- a/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js +++ b/packages/codemods/src/v5/keep-previous-data/keep-previous-data.js @@ -16,11 +16,11 @@ const AlreadyHasPlaceholderDataProperty = require('./utils/already-has-placehold */ const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => { /** - * @param {import('jscodeshift').CallExpression} callExpression + * @param {import('jscodeshift').CallExpression | import('jscodeshift').ExpressionStatement} node * @returns {{start: number, end: number}} */ - const getCallExpressionLocation = (callExpression) => { - const location = callExpression.callee.loc + const getNodeLocation = (node) => { + const location = utils.isCallExpression(node) ? node.callee.loc : node.loc const start = location.start.line const end = location.end.line @@ -93,7 +93,7 @@ const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => { const replacer = (path, resolveTargetArgument, transformNode) => { const node = path.node - const { start, end } = getCallExpressionLocation(node) + const { start, end } = getNodeLocation(node) try { const targetArgument = resolveTargetArgument(node) @@ -184,6 +184,55 @@ const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => { }, ) + const importIdentifierOfQueryClient = utils.getSelectorByImports( + utils.locateImports(['QueryClient']), + 'QueryClient', + ) + + root + .find(jscodeshift.ExpressionStatement, { + expression: { + type: jscodeshift.NewExpression.name, + callee: { + type: jscodeshift.Identifier.name, + name: importIdentifierOfQueryClient, + }, + }, + }) + .filter((path) => path.node.expression) + .replaceWith((path) => { + const resolveTargetArgument = (node) => { + const paths = jscodeshift(node) + .find(jscodeshift.ObjectProperty, { + key: { + type: jscodeshift.Identifier.name, + name: 'keepPreviousData', + }, + }) + .paths() + + return paths.length > 0 ? paths[0].parent.node : null + } + const transformNode = (node, transformedArgument) => { + jscodeshift(node.expression) + .find(jscodeshift.ObjectProperty, { + key: { + type: jscodeshift.Identifier.name, + name: 'queries', + }, + }) + .replaceWith(({ node: mutableNode }) => { + mutableNode.value.properties = transformedArgument.properties + + return mutableNode + }) + + return node + } + + return replacer(path, resolveTargetArgument, transformNode) + }) + return { shouldAddKeepPreviousDataImport } } From 85610884c6814628747e2172051a492189ee3b68 Mon Sep 17 00:00:00 2001 From: balazsmatepetro Date: Wed, 1 Nov 2023 16:00:50 +0100 Subject: [PATCH 6/8] chore(codemod): fix `eslint` issues within the `keep-previous-data` codemod --- .../v5/keep-previous-data/__testfixtures__/default.input.tsx | 2 +- .../v5/keep-previous-data/__testfixtures__/default.output.tsx | 2 +- .../v5/keep-previous-data/__testfixtures__/named.input.tsx | 4 ++-- .../v5/keep-previous-data/__testfixtures__/named.output.tsx | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.input.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.input.tsx index 7f345d7031..9528058885 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.input.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.input.tsx @@ -1,6 +1,6 @@ import * as React from 'react' import axios from 'axios' -import { useQueries, useQuery, useQueryClient, QueryClient } from '@tanstack/react-query' +import { QueryClient, useQueries, useQuery, useQueryClient } from '@tanstack/react-query' type Post = { id: number diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.output.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.output.tsx index b4493d815c..cf0a32a747 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.output.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.output.tsx @@ -1,6 +1,6 @@ import * as React from 'react' import axios from 'axios' -import { keepPreviousData, useQueries, useQuery, useQueryClient, QueryClient } from '@tanstack/react-query'; +import { keepPreviousData, QueryClient, useQueries, useQuery, useQueryClient } from '@tanstack/react-query'; type Post = { id: number diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx index 98ec5e15c5..bbce30bf10 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.input.tsx @@ -1,10 +1,10 @@ import * as React from 'react' import axios from 'axios' import { + QueryClient as RenamedQueryClient, useQueries as useRenamedUseQueries, useQuery as useRenamedUseQuery, - useQueryClient as useRenamedUseQueryClient, - QueryClient as RenamedQueryClient + useQueryClient as useRenamedUseQueryClient } from '@tanstack/react-query' type Post = { diff --git a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx index f8e8e586ac..b8c546f11c 100644 --- a/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx +++ b/packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx @@ -2,10 +2,10 @@ import * as React from 'react' import axios from 'axios' import { keepPreviousData, + QueryClient as RenamedQueryClient, useQueries as useRenamedUseQueries, useQuery as useRenamedUseQuery, useQueryClient as useRenamedUseQueryClient, - QueryClient as RenamedQueryClient, } from '@tanstack/react-query'; type Post = { From 14dab59ac0aa046467db5adeff2f1f12c1724603 Mon Sep 17 00:00:00 2001 From: balazsmatepetro Date: Wed, 1 Nov 2023 16:14:52 +0100 Subject: [PATCH 7/8] chore(codemod): fix the `prettier` issues within the `keep-previous-data` codemod README file --- packages/codemods/src/v5/keep-previous-data/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/codemods/src/v5/keep-previous-data/README.md b/packages/codemods/src/v5/keep-previous-data/README.md index 5781519c1b..a85f43416b 100644 --- a/packages/codemods/src/v5/keep-previous-data/README.md +++ b/packages/codemods/src/v5/keep-previous-data/README.md @@ -9,10 +9,10 @@ Please note, this code mod transforms usages only where the first argument is an The following usage should be transformed by the code mod: ```ts -const {data} = useQuery({ - queryKey: ['posts'], - queryFn: queryFn, - keepPreviousData: true, +const { data } = useQuery({ + queryKey: ['posts'], + queryFn: queryFn, + keepPreviousData: true, }) ``` @@ -24,7 +24,7 @@ const hookArgument = { queryFn: queryFn, keepPreviousData: true, } -const {data} = useQuery(hookArgument) +const { data } = useQuery(hookArgument) ``` ### Troubleshooting From f002b766661250ef07b37f4584a9d80a11ef84c1 Mon Sep 17 00:00:00 2001 From: balazsmatepetro Date: Fri, 3 Nov 2023 16:55:53 +0100 Subject: [PATCH 8/8] chore(codemod): turn off the `sort-imports` ESLint rule in case of the codemods Since the codemods might not respect the import order, it's easier to turn off the `sort-imports` ESLint rule. --- packages/codemods/.eslintrc.cjs | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/codemods/.eslintrc.cjs b/packages/codemods/.eslintrc.cjs index 37758a00b1..a3adafe1b6 100644 --- a/packages/codemods/.eslintrc.cjs +++ b/packages/codemods/.eslintrc.cjs @@ -11,6 +11,7 @@ const config = { files: ['**/__testfixtures__/*'], rules: { 'import/no-unresolved': 'off', + 'sort-imports': 'off', '@typescript-eslint/no-unused-vars': 'off', }, },