Skip to content

Commit

Permalink
feat: progress on codemod to alter global options
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 31, 2024
1 parent 6ec5575 commit f790205
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/codemods/src/__test__/option-codemods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ describe('codemods operating on options', () => {
it('Removes as global option when using Object.assign', () => {
expectCodemodResult(
`Object.assign(fetchMock.config, {${optionName}: true})`,
`Object.assign(fetchMock.config, {})`,
'',
);
});
it('Removes as global option when using Object.assign alongside other options', () => {
expectCodemodResult(
`Object.assign(fetchMock.config, {${optionName}: true, other: 'value'})`,
`Object.assign(fetchMock.config, {other: 'value'})`,
`Object.assign(fetchMock.config, {
other: 'value'
})`,
);
});
it.skip('Removes as global option when using spread', () => {
Expand Down
33 changes: 33 additions & 0 deletions packages/codemods/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,39 @@ export function codemod(source: string, j: JSCodeshift) {
},
}).remove();

const configSets = root
.find(j.CallExpression, {
callee: {
object: {
type: "Identifier",
name: "Object"
},
property: { name: "assign" }
}
})
.filter((path) => {
const firstArg = path.value.arguments[0];
const secondArg = path.value.arguments[1];
return (
firstArg.type === "MemberExpression" &&
firstArg.property.name === "config" &&
firstArg.object.type === "Identifier" &&
firstArg.object.name === fetchMockVariableName
) && (secondArg.type === 'ObjectExpression'
&& secondArg.properties.some(property => property.key.name === 'overwriteRoutes') );
})

configSets.filter((path) => {
const secondArg = path.value.arguments[1];
return (secondArg.properties.length === 1)
}).remove();

configSets.filter((path) => {
const secondArg = path.value.arguments[1];
return (secondArg.properties.length > 1)
})
.find(j.Property, { key: { name: "overwriteRoutes" } }).remove()



const fetchMockMethodCalls = root.find(j.CallExpression, {
Expand Down

0 comments on commit f790205

Please sign in to comment.