Skip to content

Commit ecbf3f2

Browse files
committed
Refactor createSliceReducerBuilder 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. - Refactored `__fixtures__` files to contain slightly more realistic and practical examples.
1 parent e9cc2a6 commit ecbf3f2

File tree

6 files changed

+190
-188
lines changed

6 files changed

+190
-188
lines changed
Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1-
createReducer(initialState, {
2-
[todoAdded1a]: (state: SliceState, action: PayloadAction<string>) => {
1+
import type { PayloadAction } from '@reduxjs/toolkit'
2+
import { createEntityAdapter, createReducer } from '@reduxjs/toolkit'
3+
4+
export interface Todo {
5+
id: string
6+
title: string
7+
}
8+
9+
export const todoAdapter = createEntityAdapter<Todo>()
10+
11+
const todoInitialState = todoAdapter.getInitialState()
12+
13+
export type TodoSliceState = typeof todoInitialState
14+
15+
createReducer(todoInitialState, {
16+
[todoAdded1a]: (state: TodoSliceState, action: PayloadAction<string>) => {
317
// stuff
418
},
5-
[todoAdded1b]: someFunc,
6-
todoAdded1c: adapter.someFunc,
7-
});
19+
[todoRemoved]: todoAdapter.removeOne,
20+
todoAdded: todoAdapter.addOne
21+
})
822

9-
createReducer(initialState, {
10-
[todoAdded](state: SliceState, action: PayloadAction<string>) {
23+
createReducer(todoInitialState, {
24+
[todoAdded](state: TodoSliceState, action: PayloadAction<string>) {
1125
// stuff
12-
},
13-
});
26+
}
27+
})
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
createReducer(initialState, (builder) => {
2-
builder.addCase(todoAdded1a, (state: SliceState, action: PayloadAction<string>) => {
1+
import type { PayloadAction } from '@reduxjs/toolkit'
2+
import { createEntityAdapter, createReducer } from '@reduxjs/toolkit'
3+
4+
export interface Todo {
5+
id: string
6+
title: string
7+
}
8+
9+
export const todoAdapter = createEntityAdapter<Todo>()
10+
11+
const todoInitialState = todoAdapter.getInitialState()
12+
13+
export type TodoSliceState = typeof todoInitialState
14+
15+
createReducer(todoInitialState, (builder) => {
16+
builder.addCase(todoAdded1a, (state: TodoSliceState, action: PayloadAction<string>) => {
317
// stuff
418
});
519

6-
builder.addCase(todoAdded1b, someFunc);
7-
builder.addCase(todoAdded1c, adapter.someFunc);
8-
});
20+
builder.addCase(todoRemoved, todoAdapter.removeOne);
21+
builder.addCase(todoAdded, todoAdapter.addOne);
22+
})
923

10-
createReducer(initialState, (builder) => {
11-
builder.addCase(todoAdded, (state: SliceState, action: PayloadAction<string>) => {
24+
createReducer(todoInitialState, (builder) => {
25+
builder.addCase(todoAdded, (state: TodoSliceState, action: PayloadAction<string>) => {
1226
// stuff
1327
});
14-
});
28+
})
Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,19 @@
1-
createReducer(initialState, {
2-
[todoAdded1a]: (state, action) => {
3-
// stuff
4-
},
5-
[todoAdded1b]: (state, action) => action.payload,
6-
[todoAdded1c + "test"]: (state, action) => {
7-
// stuff
8-
},
9-
[todoAdded1d](state, action) {
10-
// stuff
11-
},
12-
[todoAdded1e]: function(state, action) {
13-
// stuff
14-
},
15-
todoAdded1f: (state, action) => {
16-
//stuff
17-
},
18-
[todoAdded1g]: someFunc,
19-
todoAdded1h: adapter.someFunc
20-
});
1+
import { createEntityAdapter, createReducer } from '@reduxjs/toolkit';
2+
3+
export const todoAdapter = createEntityAdapter();
214

5+
const todoInitialState = todoAdapter.getInitialState();
226

23-
createReducer(initialState, {
24-
[todoAdded2a]: (state, action) => {
25-
// stuff
26-
},
27-
[todoAdded2b](state, action) {
28-
// stuff
29-
},
30-
[todoAdded2c]: function(state, action) {
31-
// stuff
32-
}
33-
});
7+
createReducer(todoInitialState, {
8+
[todoAdded1a]: (state, action) => {
9+
// stuff
10+
},
11+
[todoRemoved]: todoAdapter.removeOne,
12+
todoAdded: todoAdapter.addOne
13+
});
14+
15+
createReducer(todoInitialState, {
16+
[todoAdded](state, action) {
17+
// stuff
18+
}
19+
});
Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,20 @@
1-
createReducer(initialState, (builder) => {
2-
builder.addCase(todoAdded1a, (state, action) => {
3-
// stuff
4-
});
1+
import { createEntityAdapter, createReducer } from '@reduxjs/toolkit';
52

6-
builder.addCase(todoAdded1b, (state, action) => action.payload);
3+
export const todoAdapter = createEntityAdapter();
74

8-
builder.addCase(todoAdded1c + "test", (state, action) => {
9-
// stuff
10-
});
5+
const todoInitialState = todoAdapter.getInitialState();
116

12-
builder.addCase(todoAdded1d, (state, action) => {
13-
// stuff
14-
});
7+
createReducer(todoInitialState, (builder) => {
8+
builder.addCase(todoAdded1a, (state, action) => {
9+
// stuff
10+
});
1511

16-
builder.addCase(todoAdded1e, (state, action) => {
17-
// stuff
18-
});
19-
20-
builder.addCase(todoAdded1f, (state, action) => {
21-
//stuff
22-
});
23-
24-
builder.addCase(todoAdded1g, someFunc);
25-
builder.addCase(todoAdded1h, adapter.someFunc);
12+
builder.addCase(todoRemoved, todoAdapter.removeOne);
13+
builder.addCase(todoAdded, todoAdapter.addOne);
2614
});
2715

28-
29-
createReducer(initialState, (builder) => {
30-
builder.addCase(todoAdded2a, (state, action) => {
31-
// stuff
32-
});
33-
34-
builder.addCase(todoAdded2b, (state, action) => {
35-
// stuff
36-
});
37-
38-
builder.addCase(todoAdded2c, (state, action) => {
39-
// stuff
40-
});
41-
});
16+
createReducer(todoInitialState, (builder) => {
17+
builder.addCase(todoAdded, (state, action) => {
18+
// stuff
19+
});
20+
});
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
'createSliceReducerBuilder',
87
transform,
98
parser,
109
path.join(__dirname, '__testfixtures__')
11-
);
10+
)

0 commit comments

Comments
 (0)