Skip to content

Commit

Permalink
Merge pull request #2602 from reduxjs/feature/rtk-object-codemods
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Aug 16, 2022
2 parents 490c2d1 + 3157ede commit b4e8d94
Show file tree
Hide file tree
Showing 16 changed files with 784 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/rtk-codemods/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# RTK Codemods

This needs some actual content and needs to be an actual package.
5 changes: 5 additions & 0 deletions packages/rtk-codemods/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
automock: false,
roots: ['v2.0/__tests__'],
transform: { '\\.ts$': ['ts-jest'] },
}
29 changes: 29 additions & 0 deletions packages/rtk-codemods/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@reduxjs/rtk-codemods",
"version": "0.0.1",
"author": {
"name": "Lenz Weber",
"email": "[email protected]",
"url": "https://phryneas.de/"
},
"license": "MIT",
"type": "module",
"scripts": {
"test:codemods": "jest --config codemods/jest.config.js"
},
"dependencies": {
"jest": "^27",
"jscodeshift": "^0.13.1"
},
"devDependencies": {
"@types/jest": "^27",
"@types/jscodeshift": "^0.11.5"
},
"publishConfig": {
"access": "public"
},
"files": [
"src",
"dist"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
createReducer(initialState, {
[todoAdded]: (state, action) => {},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
createReducer(initialState, builder => {
builder.addCase(todoAdded, (state, action) => {});
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
createReducer(initialState, {
[todoAdded]: (state, action) => {},
anotherTodoAdded: (state) => {},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
createReducer(initialState, builder => {
builder.addCase(todoAdded, (state, action) => {}).addCase(anotherTodoAdded, (state) => {});
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
createSlice({
extraReducers: {
[todoAdded]: (state, action) => {}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
createSlice({
extraReducers: builder => {
builder.addCase(todoAdded, (state, action) => {});
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
createSlice({
extraReducers: {
[todoAdded]: (state, action) => {},
anotherTodoAdded: (state) => {},
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
createSlice({
extraReducers: builder => {
builder.addCase(todoAdded, (state, action) => {}).addCase(anotherTodoAdded, (state) => {});
}
})
18 changes: 18 additions & 0 deletions packages/rtk-codemods/v2.0/__tests__/createReducerBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineTest } from 'jscodeshift/src/testUtils'

describe('replace object syntax for declaring case reducers with builder', () => {
defineTest(
__dirname,
`./createReducerBuilder`,
null,
`create-reducer-builder/basic`,
{ parser: 'ts' }
)
defineTest(
__dirname,
`./createReducerBuilder`,
null,
`create-reducer-builder/chained`,
{ parser: 'ts' }
)
})
18 changes: 18 additions & 0 deletions packages/rtk-codemods/v2.0/__tests__/createSliceBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineTest } from 'jscodeshift/src/testUtils'

describe('replace object syntax for declaring case reducers with builder', () => {
defineTest(
__dirname,
`./createSliceBuilder`,
null,
`create-slice-builder/basic`,
{ parser: 'ts' }
)
defineTest(
__dirname,
`./createSliceBuilder`,
null,
`create-slice-builder/chained`,
{ parser: 'ts' }
)
})
40 changes: 40 additions & 0 deletions packages/rtk-codemods/v2.0/createReducerBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// @ts-nocheck

function wrapInAddCaseExpression(j, member, arrowArguments) {
return j.callExpression(
j.memberExpression(member, j.identifier('addCase'), false),
arrowArguments
)
}

export function reducerPropsToBuilderExpression(j, defNode) {
const [firstCase, ...restOfCases] = defNode.properties

const expressionStatement = restOfCases.reduce((acc, c) => {
return wrapInAddCaseExpression(j, acc, [c.key, c.value])
}, wrapInAddCaseExpression(j, j.identifier('builder'), [firstCase.key, firstCase.value]))

return j.arrowFunctionExpression(
[j.identifier('builder')],
j.blockStatement([j.expressionStatement(expressionStatement)])
)
}

export default function transformer(file, api) {
const j = api.jscodeshift

return j(file.source)
.find(j.CallExpression, {
callee: { name: 'createReducer' },
arguments: { 1: { type: 'ObjectExpression' } },
})
.forEach((path) => {
j(path).replaceWith(
j.callExpression(j.identifier('createReducer'), [
path.node.arguments[0],
reducerPropsToBuilderExpression(j, path.node.arguments[1]),
])
)
})
.toSource()
}
37 changes: 37 additions & 0 deletions packages/rtk-codemods/v2.0/createSliceBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @ts-nocheck
import { reducerPropsToBuilderExpression } from './createReducerBuilder'

export default function transformer(file, api) {
const j = api.jscodeshift

return j(file.source)
.find(j.CallExpression, {
callee: { name: 'createSlice' },
arguments: { 0: { type: 'ObjectExpression' } },
})
.filter((path) =>
path.node.arguments[0].properties.some(
(p) =>
p.key.name === 'extraReducers' && p.value.type === 'ObjectExpression'
)
)
.forEach((path) => {
j(path).replaceWith(
j.callExpression(j.identifier('createSlice'), [
j.objectExpression(
path.node.arguments[0].properties.map((p) => {
if (p.key.name === 'extraReducers') {
const expressionStatement = reducerPropsToBuilderExpression(
j,
p.value
)
return j.objectProperty(p.key, expressionStatement)
}
return p
})
),
])
)
})
.toSource()
}
Loading

0 comments on commit b4e8d94

Please sign in to comment.