Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix codemods to work with TypeScript 4.7+ #4081

Merged
merged 38 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6f9f662
Fix ESLint and Prettier config
aryaemami59 Jan 15, 2024
23012b7
Fix dependencies
aryaemami59 Jan 15, 2024
21021c7
Fix lint command
aryaemami59 Jan 15, 2024
a594239
Copy `.gitignore` file from Reselect codemod
aryaemami59 Jan 15, 2024
8dd9609
Copy `transformTestUtils.ts` from Reselect codemod
aryaemami59 Jan 15, 2024
21b67f0
Copy `tsconfig.json` from Reselect codemod
aryaemami59 Jan 15, 2024
6bb5060
Fix `vitest.config.ts` file
aryaemami59 Jan 15, 2024
1cc1d14
Copy `cli.js` from Reselect codemod
aryaemami59 Jan 15, 2024
681a982
Refactor `createReducerBuilder` transform file
aryaemami59 Jan 15, 2024
e9cc2a6
Refactor `createSliceBuilder` transform file
aryaemami59 Jan 15, 2024
ecbf3f2
Refactor `createSliceReducerBuilder` transform file
aryaemami59 Jan 15, 2024
17f43b5
Add more scenarios to test for `createReducerBuilder`
aryaemami59 Jan 16, 2024
d07c47b
Add more scenarios to test for `createSliceBuilder`
aryaemami59 Jan 16, 2024
0b2e43a
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Jan 16, 2024
100cd0e
Slightly modify CLI to allow passing multiple glob patterns
aryaemami59 Jan 16, 2024
0f065ec
Update dev dependencies
aryaemami59 Jan 16, 2024
2837b25
Remove leading forward slash from test names
aryaemami59 Jan 16, 2024
27fe907
Update all `README`s
aryaemami59 Jan 16, 2024
dc5db44
Remove unnecessary `update-docs` command
aryaemami59 Jan 16, 2024
111b466
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Jan 16, 2024
1ba09b0
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Jan 17, 2024
de7aca4
Remove `jscodeshift` from root resolutions
aryaemami59 Jan 18, 2024
30fb2a0
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Jan 18, 2024
d3e9899
Update lockfile
aryaemami59 Jan 19, 2024
e3af65b
Update dev dependencies
aryaemami59 Jan 19, 2024
be4e03e
Remove `interopDefault` from `vitest.config.ts` as it is enabled by d…
aryaemami59 Jan 23, 2024
6145da5
Add `test:watch` NPM script
aryaemami59 Jan 23, 2024
49309bb
Rename `vitest.config.ts` to `vitest.config.mts`
aryaemami59 Jan 23, 2024
f459a79
Update dev dependencies
aryaemami59 Jan 23, 2024
2083d02
Add TS 4.7 example snippet
aryaemami59 Jan 23, 2024
2142f47
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Jan 25, 2024
7046ddc
Remove `jscodeshift` from root `package.json`'s `resolutions`
aryaemami59 Jan 25, 2024
7daecc4
Move `@typescript-eslint/parser` from `resolutions` to `devDependencies`
aryaemami59 Jan 25, 2024
0781b0a
Merge branch 'master' into pr/aryaemami59/4081
EskiMojo14 Jan 26, 2024
b3cd091
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Jan 26, 2024
6ffb9e9
Merge branch 'fix-codemods' of https://github.com/aryaemami59/redux-t…
aryaemami59 Jan 26, 2024
5fc7871
Revert "Update all `README`s"
aryaemami59 Jan 26, 2024
9eb7331
Merge branch 'master' into pr/aryaemami59/4081
EskiMojo14 Jan 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"examples/action-listener/*"
],
"devDependencies": {
"@typescript-eslint/eslint-plugin": "6.12.0",
"@typescript-eslint/parser": "6.12.0",
"eslint": "^7.25.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-react-app": "^7.0.1",
Expand All @@ -47,7 +49,6 @@
"@babel/types": "7.19.3",
"esbuild": "0.19.7",
"jest-snapshot": "29.3.1",
"jscodeshift": "0.13.1",
"react-redux": "npm:8.0.2",
"react": "npm:18.2.0",
"react-dom": "npm:18.2.0",
Expand All @@ -65,9 +66,7 @@
"docs/@types/react-dom": "npm:17.0.11",
"docs/@types/react": "npm:17.0.11",
"type-fest": "2.19.0",
"console-testing-library@0.6.1": "patch:console-testing-library@npm%3A0.6.1#./.yarn/patches/console-testing-library-npm-0.6.1-4d9957d402.patch",
"@typescript-eslint/eslint-plugin": "6.12.0",
"@typescript-eslint/parser": "6.12.0"
"console-testing-library@0.6.1": "patch:console-testing-library@npm%3A0.6.1#./.yarn/patches/console-testing-library-npm-0.6.1-4d9957d402.patch"
},
"scripts": {
"build": "yarn build:packages",
Expand Down
2 changes: 0 additions & 2 deletions packages/rtk-codemods/.eslintignore

This file was deleted.

20 changes: 0 additions & 20 deletions packages/rtk-codemods/.eslintrc.js

This file was deleted.

11 changes: 11 additions & 0 deletions packages/rtk-codemods/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"env": { "node": true },
"extends": ["eslint:recommended"],
"ignorePatterns": ["node_modules"],
"parserOptions": { "ecmaVersion": "latest" },
"plugins": ["node"],
"rules": {
"no-unused-vars": [0],
"eol-last": [0]
}
}
32 changes: 31 additions & 1 deletion packages/rtk-codemods/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# Dependencies
/node_modules
/.eslintcache

# Production
/build

# Generated files
.docusaurus
.cache-loader

# Misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
.cache
.yarnrc
.yarn/*
!.yarn/patches
!.yarn/releases
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
.pnp.*
*.tgz

/.eslintcache
5 changes: 0 additions & 5 deletions packages/rtk-codemods/.prettierrc

This file was deleted.

6 changes: 6 additions & 0 deletions packages/rtk-codemods/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"trailingComma": "none",
"printWidth": 80,
"semi": false
}
1 change: 1 addition & 0 deletions packages/rtk-codemods/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ node ./bin/cli.js <TRANSFORM NAME> path/of/files/ or/some**/*glob.js

- [createReducerBuilder](transforms/createReducerBuilder/README.md)
- [createSliceBuilder](transforms/createSliceBuilder/README.md)
- [createSliceReducerBuilder](transforms/createSliceReducerBuilder/README.md)
<!--TRANSFORMS_END-->

## Contributing
Expand Down
43 changes: 35 additions & 8 deletions packages/rtk-codemods/bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
#!/usr/bin/env node
const path = require('path');
import { execaSync } from 'execa'
import { globbySync } from 'globby'
import { createRequire } from 'node:module'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

require('ts-node').register({
project: path.join(__dirname, './tsconfig.json'),
});
const require = createRequire(import.meta.url)

require('codemod-cli').runTransform(
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const transformerDirectory = path.join(
__dirname,
process.argv[2] /* transform name */,
process.argv.slice(3) /* paths or globs */
);
'..',
'transforms',
`${process.argv[2]}/index.ts`
)

const jscodeshiftExecutable = require.resolve('.bin/jscodeshift')

const extensions = 'ts,js,tsx,jsx'

execaSync(
jscodeshiftExecutable,
[
'-t',
transformerDirectory,
'--extensions',
extensions,
...(process.argv.slice(3).length === 1
? globbySync(process.argv[3])
: globbySync(process.argv.slice(3)))
],
{
stdio: 'inherit',
stripFinalNewline: false
}
)
14 changes: 0 additions & 14 deletions packages/rtk-codemods/bin/tsconfig.json

This file was deleted.

28 changes: 16 additions & 12 deletions packages/rtk-codemods/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "@reduxjs/rtk-codemods",
"version": "0.1.0",
"scripts": {
"lint": "eslint --cache .",
"test": "vitest",
"test:coverage": "vitest --coverage",
"update-docs": "codemod-cli update-docs"
"lint": "eslint .",
"test": "vitest --run",
"test:watch": "vitest --watch",
"test:coverage": "vitest --coverage"
},
"bin": "./bin/cli.js",
"files": [
Expand All @@ -22,17 +22,21 @@
"codemod"
],
"dependencies": {
"@types/jscodeshift": "^0.11.5",
"codemod-cli": "^3.2.0",
"typescript": "^4.8.0"
"execa": "^8.0.1",
"globby": "^14.0.0",
"jscodeshift": "^0.15.1",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"devDependencies": {
"eslint": "^7.25.0",
"eslint-config-prettier": "^8.3.0",
"@types/jscodeshift": "^0.11.11",
"@typescript-eslint/parser": "^6.19.1",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.4.0",
"prettier": "^2.2.1",
"vitest": "^0.30.1"
"eslint-plugin-prettier": "^5.1.3",
"prettier": "^3.2.4",
"vitest": "^1.2.1"
},
"engines": {
"node": ">= 16"
Expand Down
93 changes: 50 additions & 43 deletions packages/rtk-codemods/transformTestUtils.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,68 @@
import { describe, vi } from 'vitest';
import type { Transform } from 'jscodeshift';
import globby from 'globby';
import fs from 'fs-extra';
import path from 'path';
import { runInlineTest } from 'jscodeshift/dist/testUtils';
import { globbySync } from 'globby'
import type { Transform } from 'jscodeshift'
import type { TestOptions } from 'jscodeshift/src/testUtils'
import { runInlineTest } from 'jscodeshift/src/testUtils'
import fs from 'node:fs'
import path from 'node:path'
import { describe, it } from 'vitest'

export function runTransformTest(
export const runTransformTest = (
name: string,
transform: Transform,
parser: string,
parser: TestOptions['parser'],
fixturePath: string
) {
describe(name, function () {
globby
.sync('**/*.input.*', {
cwd: fixturePath,
absolute: true,
})
.map((entry) => entry.slice(fixturePath.length))
) => {
describe(name, () => {
globbySync('**/*.input.*', {
cwd: fixturePath,
absolute: true,
objectMode: true
})
.map((entry) => entry.name)
.forEach((filename) => {
let extension = path.extname(filename);
let testName = filename.replace(`.input${extension}`, '');
let testInputPath = path.join(fixturePath, `${testName}${extension}`);
let inputPath = path.join(fixturePath, `${testName}.input${extension}`);
let outputPath = path.join(fixturePath, `${testName}.output${extension}`);
let optionsPath = path.join(fixturePath, `${testName}.options.json`);
let options = fs.pathExistsSync(optionsPath) ? fs.readFileSync(optionsPath) : '{}';
const extension = path.extname(filename)
const testName = filename.replace(`.input${extension}`, '')
const testInputPath = path.join(fixturePath, `${testName}${extension}`)
const inputPath = path.join(
fixturePath,
`${testName}.input${extension}`
)
const outputPath = path.join(
fixturePath,
`${testName}.output${extension}`
)

describe(testName, function () {
beforeEach(function () {
process.env.CODEMOD_CLI_ARGS = options;
});
const inputFileContent = fs.readFileSync(inputPath, 'utf8')

afterEach(function () {
process.env.CODEMOD_CLI_ARGS = '';
});
const expectedOutput = fs.readFileSync(outputPath, 'utf8')

it('transforms correctly', function () {
describe(`${testName}${extension}`, () => {
it('transforms correctly', () => {
runInlineTest(
transform,
{},
{ path: testInputPath, source: fs.readFileSync(inputPath, 'utf8') },
fs.readFileSync(outputPath, 'utf8'),
{
path: testInputPath,
source: inputFileContent
},
expectedOutput,
{ parser }
);
});
)
})

it('is idempotent', function () {
it('is idempotent', () => {
runInlineTest(
transform,
{},
{ path: testInputPath, source: fs.readFileSync(outputPath, 'utf8') },
fs.readFileSync(outputPath, 'utf8'),
{
path: testInputPath,
source: inputFileContent
},
expectedOutput,
{ parser }
);
});
});
});
});
)
})
})
})
})
}