-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sorta get TS-based transforms running
- Loading branch information
1 parent
ef872b7
commit 56bb58b
Showing
17 changed files
with
177 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
**/dist/** | ||
**/etc/** | ||
**/temp/** | ||
**/temp/** | ||
**/__testfixtures__/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/rtk-codemods/v2.0/__testfixtures__/create-reducer-builder/basic.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
createReducer(initialState, builder => { | ||
builder.addCase(todoAdded, (state, action) => {}); | ||
createReducer(initialState, (builder) => { | ||
builder.addCase(todoAdded, (state, action) => {}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-typescript"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
automock: false, | ||
// roots: ['v2.0/__tests__'], | ||
transform: { '\\.ts$': ['ts-jest'] }, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/rtk-codemods2/transforms/createReducerBuilder/__testfixtures__/basic-ts.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
createReducer(initialState, { | ||
[todoAdded]: (state: SliceState, action: PayloadAction<string>) => { | ||
// stuff | ||
}, | ||
}); | ||
|
||
createReducer(initialState, { | ||
[todoAdded](state: SliceState, action: PayloadAction<string>) { | ||
// stuff | ||
}, | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/rtk-codemods2/transforms/createReducerBuilder/__testfixtures__/basic-ts.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
createReducer(initialState, (builder) => { | ||
builder.addCase(todoAdded, (state: SliceState, action: PayloadAction<string>) => { | ||
// stuff | ||
}); | ||
}); | ||
|
||
createReducer(initialState, (builder) => { | ||
builder.addCase(todoAdded, function(state: SliceState, action: PayloadAction<string>) { | ||
// stuff | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
createReducer(initialState, { | ||
[todoAdded]: (state, action) => { | ||
// stuff | ||
}, | ||
}); | ||
|
||
createReducer(initialState, { | ||
[todoAdded](state, action) { | ||
// stuff | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
createReducer(initialState, (builder) => { | ||
builder.addCase(todoAdded, (state, action) => { | ||
// stuff | ||
}); | ||
}); | ||
|
||
createReducer(initialState, (builder) => { | ||
builder.addCase(todoAdded, function(state, action) { | ||
// stuff | ||
}); | ||
}); |
19 changes: 0 additions & 19 deletions
19
packages/rtk-codemods2/transforms/createReducerBuilder/index.js
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
packages/rtk-codemods2/transforms/createReducerBuilder/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { namedTypes } from 'ast-types'; | ||
import { ExpressionKind } from 'ast-types/gen/kinds'; | ||
import { JSCodeshift, Transform } from 'jscodeshift'; | ||
|
||
function wrapInAddCaseExpression( | ||
j: JSCodeshift, | ||
member: namedTypes.Identifier, | ||
arrowArguments: any[] | ||
) { | ||
return j.callExpression( | ||
j.memberExpression(member, j.identifier('addCase'), false), | ||
arrowArguments | ||
); | ||
} | ||
|
||
export function reducerPropsToBuilderExpression( | ||
j: JSCodeshift, | ||
defNode: namedTypes.SpreadElement | ExpressionKind | ||
) { | ||
// @ts-ignore | ||
const [firstCase, ...restOfCases] = defNode.properties; | ||
|
||
const expressionStatement = restOfCases.reduce((acc: any, c: any) => { | ||
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)]) | ||
); | ||
} | ||
|
||
const transform: Transform = (file, api) => { | ||
const j = api.jscodeshift; | ||
|
||
return ( | ||
j(file.source) | ||
// @ts-ignore some expression mismatch | ||
.find(j.CallExpression, { | ||
callee: { name: 'createReducer' }, | ||
// @ts-ignore some expression mismatch | ||
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() | ||
); | ||
}; | ||
|
||
export default transform; |
7 changes: 4 additions & 3 deletions
7
packages/rtk-codemods2/transforms/createReducerBuilder/test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
'use strict'; | ||
require('ts-node').register(); | ||
|
||
const { runTransformTest } = require('codemod-cli'); | ||
|
||
runTransformTest({ | ||
runTransformTest({ | ||
name: 'createReducerBuilder', | ||
path: require.resolve('./index.js'), | ||
path: require.resolve('./index.ts'), | ||
fixtureDir: `${__dirname}/__testfixtures__/`, | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/rtk-codemods2/transforms/createReducerBuilder/test1/basic.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
createReducer(initialState, { | ||
[todoAdded]: (state, action) => { | ||
// stuff | ||
}, | ||
}); | ||
|
||
createReducer(initialState, { | ||
[todoAdded](state, action) { | ||
// stuff | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters