Skip to content

Commit 681a982

Browse files
committed
Refactor createReducerBuilder transform file
- Eliminated `@ts-ignore` directives to improve TypeScript compliance. - Configured `lineTerminator` in `.toSource.options` for consistent end-of-line across platforms, overriding `jscodeshift`'s default OS-specific EOL. - Removed unnecessary `test.js` file. - Refactored `__fixtures__` files to contain more realistic and practical examples.
1 parent 1cc1d14 commit 681a982

File tree

7 files changed

+172
-125
lines changed

7 files changed

+172
-125
lines changed
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import path from 'path';
2-
import transform, { parser } from './index';
3-
4-
import { runTransformTest } from '../../transformTestUtils';
1+
import path from 'node:path'
2+
import { runTransformTest } from '../../transformTestUtils'
3+
import transform, { parser } from './index'
54

65
runTransformTest(
76
'createReducerBuilder',
87
transform,
98
parser,
109
path.join(__dirname, '__testfixtures__')
11-
);
10+
)
Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,96 @@
1-
import { ExpressionKind, SpreadElementKind } from 'ast-types/gen/kinds';
2-
import {
1+
import type { ExpressionKind, SpreadElementKind } from 'ast-types/gen/kinds'
2+
import type {
33
ExpressionStatement,
44
JSCodeshift,
55
ObjectExpression,
6-
ObjectMethod,
7-
ObjectProperty,
8-
Transform,
9-
} from 'jscodeshift';
10-
11-
type ObjectKey = ObjectMethod['key'] & ObjectProperty['key'];
6+
Transform
7+
} from 'jscodeshift'
8+
import type { TestOptions } from 'jscodeshift/src/testUtils'
129

1310
function wrapInAddCaseExpression(
1411
j: JSCodeshift,
1512
addCaseArgs: (ExpressionKind | SpreadElementKind)[]
1613
) {
17-
const identifier = j.identifier('builder');
14+
const identifier = j.identifier('builder')
1815
return j.expressionStatement(
19-
j.callExpression(j.memberExpression(identifier, j.identifier('addCase'), false), addCaseArgs)
20-
);
16+
j.callExpression(
17+
j.memberExpression(identifier, j.identifier('addCase'), false),
18+
addCaseArgs
19+
)
20+
)
2121
}
2222

23-
export function reducerPropsToBuilderExpression(j: JSCodeshift, defNode: ObjectExpression) {
24-
const caseExpressions: ExpressionStatement[] = [];
25-
for (let property of defNode.properties) {
26-
let addCaseArgs: (ExpressionKind | SpreadElementKind)[] = [];
23+
export function reducerPropsToBuilderExpression(
24+
j: JSCodeshift,
25+
defNode: ObjectExpression
26+
) {
27+
const caseExpressions: ExpressionStatement[] = []
28+
for (const property of defNode.properties) {
29+
let addCaseArgs: (ExpressionKind | SpreadElementKind)[] = []
2730
switch (property.type) {
2831
case 'ObjectMethod': {
29-
const { key, params, body } = property;
32+
const { key, params, body } = property
3033
if (body) {
31-
addCaseArgs = [key, j.arrowFunctionExpression(params, body)];
34+
addCaseArgs = [key, j.arrowFunctionExpression(params, body)]
3235
}
33-
break;
36+
break
3437
}
3538
case 'ObjectProperty': {
36-
const { key } = property;
39+
const { key } = property
3740

3841
switch (property.value.type) {
3942
case 'ArrowFunctionExpression':
4043
case 'FunctionExpression': {
41-
const { params, body } = property.value;
44+
const { params, body } = property.value
4245
if (body) {
43-
addCaseArgs = [key, j.arrowFunctionExpression(params, body)];
46+
addCaseArgs = [key, j.arrowFunctionExpression(params, body)]
4447
}
45-
break;
48+
break
4649
}
4750
case 'Identifier':
4851
case 'MemberExpression': {
49-
const { value } = property;
50-
addCaseArgs = [key, value];
51-
break;
52+
const { value } = property
53+
addCaseArgs = [key, value]
54+
break
5255
}
5356
}
5457
}
5558
}
5659
if (!addCaseArgs.length) {
57-
continue;
60+
continue
5861
}
59-
caseExpressions.push(wrapInAddCaseExpression(j, addCaseArgs));
62+
caseExpressions.push(wrapInAddCaseExpression(j, addCaseArgs))
6063
}
6164

62-
return j.arrowFunctionExpression([j.identifier('builder')], j.blockStatement(caseExpressions));
65+
return j.arrowFunctionExpression(
66+
[j.identifier('builder')],
67+
j.blockStatement(caseExpressions)
68+
)
6369
}
6470

6571
const transform: Transform = (file, api) => {
66-
const j = api.jscodeshift;
72+
const j = api.jscodeshift
6773

68-
return (
69-
j(file.source)
70-
// @ts-ignore some expression mismatch
71-
.find(j.CallExpression, {
72-
callee: { name: 'createReducer' },
73-
// @ts-ignore some expression mismatch
74-
arguments: { 1: { type: 'ObjectExpression' } },
75-
})
76-
.forEach((path) => {
77-
const reducerObjectExpression = path.node.arguments[1] as ObjectExpression;
78-
j(path).replaceWith(
79-
j.callExpression(j.identifier('createReducer'), [
80-
path.node.arguments[0],
81-
reducerPropsToBuilderExpression(j, reducerObjectExpression),
82-
])
83-
);
84-
})
85-
.toSource({
86-
arrowParensAlways: true,
87-
})
88-
);
89-
};
74+
return j(file.source)
75+
.find(j.CallExpression, {
76+
callee: { name: 'createReducer' },
77+
arguments: [{}, { type: 'ObjectExpression' }]
78+
})
79+
.forEach((path) => {
80+
const reducerObjectExpression = path.node.arguments[1] as ObjectExpression
81+
j(path).replaceWith(
82+
j.callExpression(j.identifier('createReducer'), [
83+
path.node.arguments[0],
84+
reducerPropsToBuilderExpression(j, reducerObjectExpression)
85+
])
86+
)
87+
})
88+
.toSource({
89+
arrowParensAlways: true,
90+
lineTerminator: '\n'
91+
})
92+
}
9093

91-
export const parser = 'tsx';
94+
export const parser = 'tsx' satisfies TestOptions['parser']
9295

93-
export default transform;
96+
export default transform
Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
1-
const aSlice = createSlice({
2-
name: 'name',
1+
import type { PayloadAction } from '@reduxjs/toolkit'
2+
import { createEntityAdapter, createSlice, nanoid } from '@reduxjs/toolkit'
3+
4+
function withPayload(): any {
5+
throw new Error('Function not implemented.')
6+
}
7+
8+
export interface Todo {
9+
id: string
10+
title: string
11+
}
12+
13+
export const todoAdapter = createEntityAdapter<Todo>()
14+
15+
const todoSlice = createSlice({
16+
name: 'todo',
317
initialState: todoAdapter.getInitialState(),
418
reducers: {
519
property: () => {},
620
method(state, action: PayloadAction<Todo>) {
7-
todoAdapter.addOne(state, action);
21+
todoAdapter.addOne(state, action)
822
},
923
identifier: todoAdapter.removeOne,
1024
preparedProperty: {
11-
prepare: (todo: Todo) => ({ payload: { id: nanoid(), ...todo } }),
25+
prepare: (todo: Omit<Todo, 'id'>) => ({
26+
payload: { id: nanoid(), ...todo }
27+
}),
1228
reducer: () => {}
1329
},
1430
preparedMethod: {
15-
prepare(todo: Todo) {
31+
prepare(todo: Omit<Todo, 'id'>) {
1632
return { payload: { id: nanoid(), ...todo } }
1733
},
1834
reducer(state, action: PayloadAction<Todo>) {
19-
todoAdapter.addOne(state, action);
35+
todoAdapter.addOne(state, action)
2036
}
2137
},
2238
preparedIdentifier: {
2339
prepare: withPayload(),
2440
reducer: todoAdapter.setMany
25-
},
41+
}
2642
}
2743
})

packages/rtk-codemods/transforms/createSliceReducerBuilder/__testfixtures__/basic-ts.output.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
1-
const aSlice = createSlice({
2-
name: 'name',
1+
import type { PayloadAction } from '@reduxjs/toolkit'
2+
import { createEntityAdapter, createSlice, nanoid } from '@reduxjs/toolkit'
3+
4+
function withPayload(): any {
5+
throw new Error('Function not implemented.')
6+
}
7+
8+
export interface Todo {
9+
id: string
10+
title: string
11+
}
12+
13+
export const todoAdapter = createEntityAdapter<Todo>()
14+
15+
const todoSlice = createSlice({
16+
name: 'todo',
317
initialState: todoAdapter.getInitialState(),
418

519
reducers: (create) => ({
620
property: create.reducer(() => {}),
721

822
method: create.reducer((state, action: PayloadAction<Todo>) => {
9-
todoAdapter.addOne(state, action);
23+
todoAdapter.addOne(state, action)
1024
}),
1125

1226
identifier: create.reducer(todoAdapter.removeOne),
13-
preparedProperty: create.preparedReducer((todo: Todo) => ({ payload: { id: nanoid(), ...todo } }), () => {}),
1427

15-
preparedMethod: create.preparedReducer((todo: Todo) => {
28+
preparedProperty: create.preparedReducer((todo: Omit<Todo, 'id'>) => ({
29+
payload: { id: nanoid(), ...todo }
30+
}), () => {}),
31+
32+
preparedMethod: create.preparedReducer((todo: Omit<Todo, 'id'>) => {
1633
return { payload: { id: nanoid(), ...todo } }
1734
}, (state, action: PayloadAction<Todo>) => {
18-
todoAdapter.addOne(state, action);
35+
todoAdapter.addOne(state, action)
1936
}),
2037

2138
preparedIdentifier: create.preparedReducer(withPayload(), todoAdapter.setMany)
Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
1-
const aSlice = createSlice({
2-
name: 'name',
3-
initialState: todoAdapter.getInitialState(),
4-
reducers: {
5-
property: () => {},
6-
method(state, action) {
7-
todoAdapter.setMany(state, action);
8-
},
9-
identifier: todoAdapter.removeOne,
10-
preparedProperty: {
11-
prepare: (todo) => ({ payload: { id: nanoid(), ...todo } }),
12-
reducer: () => {}
13-
},
14-
preparedMethod: {
15-
prepare(todo) {
16-
return { payload: { id: nanoid(), ...todo } }
17-
},
18-
reducer(state, action) {
19-
todoAdapter.setMany(state, action);
20-
}
21-
},
22-
preparedIdentifier: {
23-
prepare: withPayload(),
24-
reducer: todoAdapter.setMany
25-
},
26-
}
27-
})
1+
import { createEntityAdapter, createSlice, nanoid } from '@reduxjs/toolkit'
2+
3+
function withPayload() {
4+
throw new Error('Function not implemented.')
5+
}
6+
7+
export const todoAdapter = createEntityAdapter()
8+
9+
const todoSlice = createSlice({
10+
name: 'todo',
11+
initialState: todoAdapter.getInitialState(),
12+
reducers: {
13+
property: () => { },
14+
method(state, action) {
15+
todoAdapter.addOne(state, action)
16+
},
17+
identifier: todoAdapter.removeOne,
18+
preparedProperty: {
19+
prepare: (todo) => ({
20+
payload: { id: nanoid(), ...todo }
21+
}),
22+
reducer: () => { }
23+
},
24+
preparedMethod: {
25+
prepare(todo) {
26+
return { payload: { id: nanoid(), ...todo } }
27+
},
28+
reducer(state, action) {
29+
todoAdapter.addOne(state, action)
30+
}
31+
},
32+
preparedIdentifier: {
33+
prepare: withPayload(),
34+
reducer: todoAdapter.setMany
35+
}
36+
}
37+
})
Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1-
const aSlice = createSlice({
2-
name: 'name',
3-
initialState: todoAdapter.getInitialState(),
1+
import { createEntityAdapter, createSlice, nanoid } from '@reduxjs/toolkit'
42

5-
reducers: (create) => ({
6-
property: create.reducer(() => {}),
3+
function withPayload() {
4+
throw new Error('Function not implemented.')
5+
}
76

8-
method: create.reducer((state, action) => {
9-
todoAdapter.setMany(state, action);
10-
}),
7+
export const todoAdapter = createEntityAdapter()
118

12-
identifier: create.reducer(todoAdapter.removeOne),
13-
preparedProperty: create.preparedReducer((todo) => ({ payload: { id: nanoid(), ...todo } }), () => {}),
9+
const todoSlice = createSlice({
10+
name: 'todo',
11+
initialState: todoAdapter.getInitialState(),
1412

15-
preparedMethod: create.preparedReducer((todo) => {
16-
return { payload: { id: nanoid(), ...todo } }
17-
}, (state, action) => {
18-
todoAdapter.setMany(state, action);
19-
}),
13+
reducers: (create) => ({
14+
property: create.reducer(() => { }),
2015

21-
preparedIdentifier: create.preparedReducer(withPayload(), todoAdapter.setMany)
22-
})
23-
})
16+
method: create.reducer((state, action) => {
17+
todoAdapter.addOne(state, action)
18+
}),
19+
20+
identifier: create.reducer(todoAdapter.removeOne),
21+
22+
preparedProperty: create.preparedReducer((todo) => ({
23+
payload: { id: nanoid(), ...todo }
24+
}), () => { }),
25+
26+
preparedMethod: create.preparedReducer((todo) => {
27+
return { payload: { id: nanoid(), ...todo } }
28+
}, (state, action) => {
29+
todoAdapter.addOne(state, action)
30+
}),
31+
32+
preparedIdentifier: create.preparedReducer(withPayload(), todoAdapter.setMany)
33+
})
34+
})

packages/rtk-codemods/transforms/createSliceReducerBuilder/test.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)