Skip to content

Commit

Permalink
Merge pull request #4081 from aryaemami59/fix-codemods
Browse files Browse the repository at this point in the history
Fix codemods to work with TypeScript 4.7+
  • Loading branch information
EskiMojo14 committed Jan 28, 2024
2 parents d1fa1b1 + 9eb7331 commit ec379e4
Show file tree
Hide file tree
Showing 35 changed files with 2,321 additions and 1,210 deletions.
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 }
);
});
});
});
});
)
})
})
})
})
}

0 comments on commit ec379e4

Please sign in to comment.