Skip to content

Commit

Permalink
fix(codemods): Use .cjs extension for CommonJS files (#7333)
Browse files Browse the repository at this point in the history
* bug(codemods): rename CommonJS files to .cjs

Rename CommonJS files in codemods to distinguish them from ES module
files otherwise used in this project.
Resolve an issue occuring since package.json in codemods is removed when
publishing causing module type data to be lost.

Fix issue #7154

* Fix knip errors

* Add file extension to tests

* Update migration doc

---------

Co-authored-by: Lachlan Collins <[email protected]>
  • Loading branch information
ontanj and lachlancollins authored Apr 25, 2024
1 parent 2ffd9fb commit 2aca521
Show file tree
Hide file tree
Showing 28 changed files with 90 additions and 78 deletions.
4 changes: 2 additions & 2 deletions docs/framework/react/guides/migrating-to-v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ If you want to run it against `.js` or `.jsx` files, please use the command belo
```
npx jscodeshift@latest ./path/to/src/ \
--extensions=js,jsx \
--transform=./node_modules/@tanstack/react-query/build/codemods/src/v5/remove-overloads/remove-overloads.js
--transform=./node_modules/@tanstack/react-query/build/codemods/src/v5/remove-overloads/remove-overloads.cjs
```

If you want to run it against `.ts` or `.tsx` files, please use the command below:
Expand All @@ -100,7 +100,7 @@ If you want to run it against `.ts` or `.tsx` files, please use the command belo
npx jscodeshift@latest ./path/to/src/ \
--extensions=ts,tsx \
--parser=tsx \
--transform=./node_modules/@tanstack/react-query/build/codemods/src/v5/remove-overloads/remove-overloads.js
--transform=./node_modules/@tanstack/react-query/build/codemods/src/v5/remove-overloads/remove-overloads.cjs
```

Please note in the case of `TypeScript` you need to use `tsx` as the parser; otherwise, the codemod won't be applied properly!
Expand Down
2 changes: 1 addition & 1 deletion knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"ignoreWorkspaces": ["examples/**", "integrations/**"],
"workspaces": {
"packages/query-codemods": {
"entry": ["src/v4/*.js", "src/v5/*/*.js"],
"entry": ["src/v4/**/*.cjs", "src/v5/**/*.cjs"],
"ignore": ["**/__testfixtures__/**"]
},
"packages/vue-query": {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const defineTest = require('jscodeshift/dist/testUtils').defineTest

defineTest(__dirname, 'key-transformation.cjs', null, 'default-import', {
parser: 'tsx',
})

defineTest(__dirname, 'key-transformation.cjs', null, 'named-import', {
parser: 'tsx',
})

defineTest(__dirname, 'key-transformation.cjs', null, 'namespaced-import', {
parser: 'tsx',
})

defineTest(
__dirname,
'key-transformation.cjs',
null,
'parameter-is-identifier',
{
parser: 'tsx',
},
)

defineTest(
__dirname,
'key-transformation.cjs',
null,
'parameter-is-object-expression',
{
parser: 'tsx',
},
)

defineTest(__dirname, 'key-transformation.cjs', null, 'type-arguments', {
parser: 'tsx',
})

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const defineTest = require('jscodeshift/dist/testUtils').defineTest

defineTest(
__dirname,
'replace-import-specifier',
'replace-import-specifier.cjs',
null,
'replace-import-specifier',
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createUtilsObject = require('../utils')
const createUtilsObject = require('../utils/index.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createKeyReplacer = require('./utils/replacers/key-replacer')
const createKeyReplacer = require('./utils/replacers/key-replacer.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createUseQueryLikeTransformer = require('../utils/transformers/use-query-like-transformer')
const createUseQueryLikeTransformer = require('../utils/transformers/use-query-like-transformer.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createQueryClientTransformer = require('../utils/transformers/query-client-transformer')
const createQueryClientTransformer = require('../utils/transformers/query-client-transformer.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createQueryCacheTransformer = require('../utils/transformers/query-cache-transformer')
const createQueryCacheTransformer = require('../utils/transformers/query-cache-transformer.cjs')

const transformQueryClientUsages = ({
jscodeshift,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const defineTest = require('jscodeshift/dist/testUtils').defineTest

defineTest(__dirname, 'rename-hydrate', null, 'default-import', {
defineTest(__dirname, 'is-loading.cjs', null, 'default-import', {
parser: 'tsx',
})

defineTest(__dirname, 'rename-hydrate', null, 'named-import', {
defineTest(__dirname, 'is-loading.cjs', null, 'named-import', {
parser: 'tsx',
})
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createUtilsObject = require('../../utils')
const createUtilsObject = require('../../utils/index.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createUseQueryLikeTransformer = require('../../utils/transformers/use-query-like-transformer')
const createUseQueryLikeTransformer = require('../../utils/transformers/use-query-like-transformer.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createQueryClientTransformer = require('../../utils/transformers/query-client-transformer')
const createQueryClientTransformer = require('../../utils/transformers/query-client-transformer.cjs')

const originalName = 'isLoading'
const newName = 'isPending'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const defineTest = require('jscodeshift/dist/testUtils').defineTest

defineTest(__dirname, 'keep-previous-data', null, 'default', {
defineTest(__dirname, 'keep-previous-data.cjs', null, 'default', {
parser: 'tsx',
})

defineTest(__dirname, 'keep-previous-data', null, 'named', {
defineTest(__dirname, 'keep-previous-data.cjs', null, 'named', {
parser: 'tsx',
})
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createUtilsObject = require('../../utils')
const createUtilsObject = require('../../utils/index.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createUseQueryLikeTransformer = require('../../utils/transformers/use-query-like-transformer')
const createUseQueryLikeTransformer = require('../../utils/transformers/use-query-like-transformer.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createQueryClientTransformer = require('../../utils/transformers/query-client-transformer')
const createQueryClientTransformer = require('../../utils/transformers/query-client-transformer.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const AlreadyHasPlaceholderDataProperty = require('./utils/already-has-placeholder-data-property')
const AlreadyHasPlaceholderDataProperty = require('./utils/already-has-placeholder-data-property.cjs')

/**
* @param {import('jscodeshift')} jscodeshift
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +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', {
defineTest(__dirname, 'remove-overloads.cjs', null, 'default-import', {
parser: 'tsx',
})

defineTest(__dirname, 'is-loading', null, 'named-import', {
defineTest(__dirname, 'remove-overloads.cjs', null, 'bug-reports', {
parser: 'tsx',
})
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createUtilsObject = require('../../utils')
const createUtilsObject = require('../../utils/index.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const transformFilterAwareUsages = require('./transformers/filter-aware-usage-transformer')
const transformFilterAwareUsages = require('./transformers/filter-aware-usage-transformer.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const transformQueryFnAwareUsages = require('./transformers/query-fn-aware-usage-transformer')
const transformQueryFnAwareUsages = require('./transformers/query-fn-aware-usage-transformer.cjs')

module.exports = (file, api) => {
const jscodeshift = api.jscodeshift
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createV5UtilsObject = require('../utils')
const createV5UtilsObject = require('../utils/index.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const UnknownUsageError = require('../utils/unknown-usage-error')
const UnknownUsageError = require('../utils/unknown-usage-error.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createQueryClientTransformer = require('../../../utils/transformers/query-client-transformer')
const createQueryClientTransformer = require('../../../utils/transformers/query-client-transformer.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createQueryCacheTransformer = require('../../../utils/transformers/query-cache-transformer')
const createQueryCacheTransformer = require('../../../utils/transformers/query-cache-transformer.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createUseQueryLikeTransformer = require('../../../utils/transformers/use-query-like-transformer')
const createUseQueryLikeTransformer = require('../../../utils/transformers/use-query-like-transformer.cjs')

/**
* @param {import('jscodeshift').api} jscodeshift
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createV5UtilsObject = require('../utils')
const createV5UtilsObject = require('../utils/index.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const UnknownUsageError = require('../utils/unknown-usage-error')
const UnknownUsageError = require('../utils/unknown-usage-error.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createQueryClientTransformer = require('../../../utils/transformers/query-client-transformer')
const createQueryClientTransformer = require('../../../utils/transformers/query-client-transformer.cjs')

/**
* @param {import('jscodeshift').api} jscodeshift
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const UnknownUsageError = require('./unknown-usage-error')
const UnknownUsageError = require('./unknown-usage-error.cjs')

module.exports = ({ jscodeshift, utils }) => {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const defineTest = require('jscodeshift/dist/testUtils').defineTest

defineTest(__dirname, 'remove-overloads', null, 'default-import', {
defineTest(__dirname, 'rename-hydrate.cjs', null, 'default-import', {
parser: 'tsx',
})

defineTest(__dirname, 'remove-overloads', null, 'bug-reports', {
defineTest(__dirname, 'rename-hydrate.cjs', null, 'named-import', {
parser: 'tsx',
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const defineTest = require('jscodeshift/dist/testUtils').defineTest

defineTest(__dirname, 'rename-properties.cjs', null, 'rename-cache-time', {
parser: 'tsx',
})

defineTest(
__dirname,
'rename-properties.cjs',
null,
'rename-use-error-boundary',
{
parser: 'tsx',
},
)

This file was deleted.

0 comments on commit 2aca521

Please sign in to comment.