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 Identifier/MemberExpression values in createSliceBuilder codemod #2881

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ const slice1 = createSlice({
name: "a",
initialState,
extraReducers: {
[todoAdded]: (state: SliceState, action: PayloadAction<string>) => {
[todoAdded1a]: (state: SliceState, action: PayloadAction<string>) => {
// stuff
},
[todoAdded1b]: someFunc,
todoAdded1c: adapter.someFunc,
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ const slice1 = createSlice({
initialState,

extraReducers: (builder) => {
builder.addCase(todoAdded, (state: SliceState, action: PayloadAction<string>) => {
builder.addCase(todoAdded1a, (state: SliceState, action: PayloadAction<string>) => {
// stuff
});

builder.addCase(todoAdded1b, someFunc);
builder.addCase(todoAdded1c, adapter.someFunc);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const slice1 = createSlice({
},
todoAdded1f: (state, action) => {
//stuff
}
},
[todoAdded1g]: someFunc,
todoAdded1h: adapter.someFunc
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const slice1 = createSlice({
builder.addCase(todoAdded1f, (state, action) => {
//stuff
});

builder.addCase(todoAdded1g, someFunc);
builder.addCase(todoAdded1h, adapter.someFunc);
}
});

Expand Down
47 changes: 23 additions & 24 deletions packages/rtk-codemods/transforms/createSliceBuilder/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { namedTypes } from 'ast-types';
import { ExpressionKind, PatternKind } from 'ast-types/gen/kinds';
import { ExpressionKind, SpreadElementKind } from 'ast-types/gen/kinds';
import {
BlockStatement,
CallExpression,
Expression,
ExpressionStatement,
JSCodeshift,
ObjectExpression,
Expand All @@ -16,48 +12,51 @@ type ObjectKey = ObjectMethod['key'] & ObjectProperty['key'];

function wrapInAddCaseExpression(
j: JSCodeshift,
key: ObjectKey,
params: PatternKind[],
body: BlockStatement | ExpressionKind
addCaseArgs: (ExpressionKind | SpreadElementKind)[]
) {
const identifier = j.identifier('builder');
return j.expressionStatement(
j.callExpression(j.memberExpression(identifier, j.identifier('addCase'), false), [
key,
j.arrowFunctionExpression(params, body),
])
j.callExpression(j.memberExpression(identifier, j.identifier('addCase'), false), addCaseArgs)
);
}

export function reducerPropsToBuilderExpression(j: JSCodeshift, defNode: ObjectExpression) {
const caseExpressions: ExpressionStatement[] = [];
for (let property of defNode.properties) {
let key: ObjectKey = null as any;
let params: PatternKind[] = [];
let body: BlockStatement | ExpressionKind = null as any;
let addCaseArgs: (ExpressionKind | SpreadElementKind)[] = [];
switch (property.type) {
case 'ObjectMethod': {
key = property.key;
params = property.params;
body = property.body;
const { key, params, body } = property;
if (body) {
addCaseArgs = [key, j.arrowFunctionExpression(params, body)];
}
break;
}
case 'ObjectProperty': {
case 'ObjectProperty': {
const { key } = property;

switch (property.value.type) {
case 'ArrowFunctionExpression':
case 'FunctionExpression': {
key = property.key;
params = property.value.params;
body = property.value.body;
const { params, body } = property.value;
if (body) {
addCaseArgs = [key, j.arrowFunctionExpression(params, body)];
}
break;
}
case 'Identifier':
case 'MemberExpression': {
const { value } = property;
addCaseArgs = [key, value];
break;
}
}
}
}
if (!body) {
if (!addCaseArgs.length) {
continue;
}
caseExpressions.push(wrapInAddCaseExpression(j, key, params, body));
caseExpressions.push(wrapInAddCaseExpression(j, addCaseArgs));
}

return j.arrowFunctionExpression([j.identifier('builder')], j.blockStatement(caseExpressions));
Expand Down