-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codemod): isLoading codemod (#6388)
* feat(codemod): implement codemod that replaces the `isLoading` usages with `isPending` * fix(codemod): differentiate shorthand property usages in the codemod * chore(codemod): add the `useMutation` hook as well to the "affected hooks" list --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
- Loading branch information
1 parent
5039113
commit f62e079
Showing
6 changed files
with
582 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
packages/codemods/src/v5/is-loading/__testfixtures__/default-import.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as React from 'react' | ||
import { useQuery } from '@tanstack/react-query' | ||
|
||
export const WithObjectPattern = () => { | ||
const { isError, isLoading, isSuccess, status } = useQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (isError || isLoading || isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify({ isError, isLoading, isSuccess, status })}</div> | ||
} | ||
|
||
export const WithObjectPatternAndRename = () => { | ||
const { isError, isLoading: isLoadingRenamed, isSuccess, status } = useQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (isError || isLoadingRenamed || isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify({ isError, isLoadingRenamed, isSuccess, status })}</div> | ||
} | ||
|
||
export const WithObjectPatternAndRestElement = () => { | ||
const { isError, ...rest } = useQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (isError || rest.isLoading || rest.isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify({ isError, isLoading: rest.isLoading, isSuccess: rest.isSuccess })}</div> | ||
} | ||
|
||
export const WithIdentifier = () => { | ||
const queryResult = useQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (queryResult.isError || queryResult.isLoading || queryResult.isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify(queryResult)}</div> | ||
} | ||
|
||
export const WithCombinations = () => { | ||
function useSomethingElse() { | ||
const { isError, isLoading, isSuccess, status } = useQuery({ | ||
queryKey: ['somethingElse'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
return { isError, isLoading, isSuccess, status } | ||
} | ||
|
||
function useAnotherThing() { | ||
const { isLoading, ...rest } = useQuery({ | ||
queryKey: ['anotherThing'], | ||
queryFn: () => ['anotherData'], | ||
}) | ||
|
||
return { isLoading: rest.isLoading, ...rest } | ||
} | ||
|
||
const { isError, isLoading: isLoadingRenamed, isSuccess, status } = useQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
return isLoadingRenamed | ||
} |
82 changes: 82 additions & 0 deletions
82
packages/codemods/src/v5/is-loading/__testfixtures__/default-import.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import * as React from 'react' | ||
import { useQuery } from '@tanstack/react-query' | ||
|
||
export const WithObjectPattern = () => { | ||
const { isError, isPending, isSuccess, status } = useQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (isError || isPending || isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify({ isError, isPending, isSuccess, status })}</div>; | ||
} | ||
|
||
export const WithObjectPatternAndRename = () => { | ||
const { isError, isPending: isLoadingRenamed, isSuccess, status } = useQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (isError || isLoadingRenamed || isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify({ isError, isLoadingRenamed, isSuccess, status })}</div> | ||
} | ||
|
||
export const WithObjectPatternAndRestElement = () => { | ||
const { isError, ...rest } = useQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (isError || rest.isPending || rest.isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify({ isError, isLoading: rest.isPending, isSuccess: rest.isSuccess })}</div>; | ||
} | ||
|
||
export const WithIdentifier = () => { | ||
const queryResult = useQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (queryResult.isError || queryResult.isPending || queryResult.isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify(queryResult)}</div> | ||
} | ||
|
||
export const WithCombinations = () => { | ||
function useSomethingElse() { | ||
const { isError, isPending, isSuccess, status } = useQuery({ | ||
queryKey: ['somethingElse'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
return { isError, isPending, isSuccess, status }; | ||
} | ||
|
||
function useAnotherThing() { | ||
const { isPending, ...rest } = useQuery({ | ||
queryKey: ['anotherThing'], | ||
queryFn: () => ['anotherData'], | ||
}) | ||
|
||
return { isLoading: rest.isPending, ...rest }; | ||
} | ||
|
||
const { isError, isPending: isLoadingRenamed, isSuccess, status } = useQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
return isLoadingRenamed | ||
} | ||
|
81 changes: 81 additions & 0 deletions
81
packages/codemods/src/v5/is-loading/__testfixtures__/named-import.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as React from 'react' | ||
import { useQuery as useRenamedUseQuery } from '@tanstack/react-query' | ||
|
||
export const WithObjectPattern = () => { | ||
const { isError, isLoading, isSuccess, status } = useRenamedUseQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (isError || isLoading || isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify({ isError, isLoading, isSuccess, status })}</div> | ||
} | ||
|
||
export const WithObjectPatternAndRename = () => { | ||
const { isError, isLoading: isLoadingRenamed, isSuccess, status } = useRenamedUseQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (isError || isLoadingRenamed || isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify({ isError, isLoadingRenamed, isSuccess, status })}</div> | ||
} | ||
|
||
export const WithObjectPatternAndRestElement = () => { | ||
const { isError, ...rest } = useRenamedUseQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (isError || rest.isLoading || rest.isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify({ isError, isLoading: rest.isLoading, isSuccess: rest.isSuccess })}</div> | ||
} | ||
|
||
export const WithIdentifier = () => { | ||
const queryResult = useRenamedUseQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (queryResult.isError || queryResult.isLoading || queryResult.isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify(queryResult)}</div> | ||
} | ||
|
||
export const WithCombinations = () => { | ||
function useSomethingElse() { | ||
const { isError, isLoading, isSuccess, status } = useRenamedUseQuery({ | ||
queryKey: ['somethingElse'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
return { isError, isLoading, isSuccess, status } | ||
} | ||
|
||
function useAnotherThing() { | ||
const { isLoading, ...rest } = useRenamedUseQuery({ | ||
queryKey: ['anotherThing'], | ||
queryFn: () => ['anotherData'], | ||
}) | ||
|
||
return { isLoading: rest.isLoading, ...rest } | ||
} | ||
|
||
const { isError, isLoading: isLoadingRenamed, isSuccess, status } = useRenamedUseQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
return isLoadingRenamed | ||
} |
81 changes: 81 additions & 0 deletions
81
packages/codemods/src/v5/is-loading/__testfixtures__/named-import.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as React from 'react' | ||
import { useQuery as useRenamedUseQuery } from '@tanstack/react-query' | ||
|
||
export const WithObjectPattern = () => { | ||
const { isError, isPending, isSuccess, status } = useRenamedUseQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (isError || isPending || isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify({ isError, isPending, isSuccess, status })}</div>; | ||
} | ||
|
||
export const WithObjectPatternAndRename = () => { | ||
const { isError, isPending: isLoadingRenamed, isSuccess, status } = useRenamedUseQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (isError || isLoadingRenamed || isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify({ isError, isLoadingRenamed, isSuccess, status })}</div> | ||
} | ||
|
||
export const WithObjectPatternAndRestElement = () => { | ||
const { isError, ...rest } = useRenamedUseQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (isError || rest.isPending || rest.isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify({ isError, isLoading: rest.isPending, isSuccess: rest.isSuccess })}</div>; | ||
} | ||
|
||
export const WithIdentifier = () => { | ||
const queryResult = useRenamedUseQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
if (queryResult.isError || queryResult.isPending || queryResult.isSuccess) { | ||
console.log('Do something') | ||
} | ||
|
||
return <div>{JSON.stringify(queryResult)}</div> | ||
} | ||
|
||
export const WithCombinations = () => { | ||
function useSomethingElse() { | ||
const { isError, isPending, isSuccess, status } = useRenamedUseQuery({ | ||
queryKey: ['somethingElse'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
return { isError, isPending, isSuccess, status }; | ||
} | ||
|
||
function useAnotherThing() { | ||
const { isPending, ...rest } = useRenamedUseQuery({ | ||
queryKey: ['anotherThing'], | ||
queryFn: () => ['anotherData'], | ||
}) | ||
|
||
return { isLoading: rest.isPending, ...rest }; | ||
} | ||
|
||
const { isError, isPending: isLoadingRenamed, isSuccess, status } = useRenamedUseQuery({ | ||
queryKey: ['queryKey'], | ||
queryFn: () => ['data'], | ||
}) | ||
|
||
return isLoadingRenamed | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/codemods/src/v5/is-loading/__tests__/is-loading.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const defineTest = require('jscodeshift/dist/testUtils').defineTest | ||
|
||
defineTest(__dirname, 'is-loading', null, 'default-import', { | ||
parser: 'tsx', | ||
}) | ||
|
||
defineTest(__dirname, 'is-loading', null, 'named-import', { | ||
parser: 'tsx', | ||
}) |
Oops, something went wrong.