Skip to content

Commit

Permalink
feat: more option codemods
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 31, 2024
1 parent f790205 commit 23201b0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 58 deletions.
6 changes: 1 addition & 5 deletions packages/codemods/src/__test__/option-codemods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ function expectCodemodResult(src, expected) {
}

describe('codemods operating on options', () => {
[
'overwriteRoutes',
// ,
// 'warnOnFallback', 'sendAsJson'
].forEach((optionName) => {
['overwriteRoutes', 'warnOnFallback', 'sendAsJson'].forEach((optionName) => {
describe(optionName, () => {
it('Removes as global option when setting directly as property', () => {
expectCodemodResult(`fetchMock.config.${optionName} = true`, '');
Expand Down
102 changes: 50 additions & 52 deletions packages/codemods/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,53 @@ export function codemod(source: string, j: JSCodeshift) {
.closest(j.VariableDeclarator)
.get().value.id.name;

const directConfigSets = root.find(j.AssignmentExpression, {
left: {
type: 'MemberExpression',
property: {name: 'overwriteRoutes'},
object: {
type: 'MemberExpression',
property: {name: 'config'},
object: {
type: 'Identifier',
name: fetchMockVariableName,
}
}
},
}).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 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'
);
});
['overwriteRoutes', 'warnOnFallback', 'sendAsJson'].forEach((name) => {
root
.find(j.AssignmentExpression, {
left: {
type: 'MemberExpression',
property: { name },
object: {
type: 'MemberExpression',
property: { name: 'config' },
object: {
type: 'Identifier',
name: fetchMockVariableName,
},
},
},
})
.remove();
configSets.find(j.Property, { key: { name } }).remove();
});

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

const fetchMockMethodCalls = root.find(j.CallExpression, {
callee: {
Expand All @@ -74,9 +72,7 @@ export function codemod(source: string, j: JSCodeshift) {
name: fetchMockVariableName,
},
},
});
fetchMockMethodCalls
.map((path) => {
}).map((path) => {
const paths = [path];
while (path.parentPath.value.type !== 'ExpressionStatement') {
path = path.parentPath;
Expand All @@ -85,7 +81,9 @@ export function codemod(source: string, j: JSCodeshift) {
}
}
return paths;
})
});

fetchMockMethodCalls
.forEach((path) => {
const callee = path.value.callee as MemberExpression;
const property = callee.property as Identifier;
Expand Down
11 changes: 11 additions & 0 deletions packages/codemods/try.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { codemod } from './src/index';
import jscodeshift from 'jscodeshift';

codemod(
`
import fetchMock from 'fetch-mock';
Object.assign(fetchMock.config, {overwriteRoutes: true, other: 'value'})
Object.assign(fetchMock.config, {overwriteRoutes: true})
`,
jscodeshift,
);
3 changes: 2 additions & 1 deletion jsconfig.json → tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"target": "es2021"
},
"include": ["./packages/**/src/*.ts"],
"exclude": ["node_modules", "packages/**/__tests__"]
"exclude": ["node_modules", "packages/**/__tests__"],
"ts-node": {"esm": true}
}

0 comments on commit 23201b0

Please sign in to comment.