Skip to content

Commit

Permalink
feat: remiving options from methods more or less works
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Sep 2, 2024
1 parent 3b7daf7 commit e976e7e
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 57 deletions.
92 changes: 61 additions & 31 deletions packages/codemods/src/__test__/option-codemods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,75 @@ describe('codemods operating on options', () => {
});

[
'sticky',
'once',
'get',
'getOnce',
'post',
'postOnce',
'put',
'putOnce',
'delete',
'deleteOnce',
'head',
'headOnce',
'patch',
'patchOnce',
'any',
'anyOnce',
// all though the src doesn't actually handle these explicitly,
// these tests ensure that the renaming of these methods to ones that
// do get handled happens first
'getAnyOnce',
'mock',
// 'sticky',
// 'once',
// 'any',
// 'anyOnce',
// 'get',
// 'getAny',
// 'getOnce',
// 'getAnyOnce',
// 'post',
// 'postAny',
// 'postOnce',
// 'postAnyOnce',
// 'put',
// 'putAny',
// 'putOnce',
// 'putAnyOnce',
// 'delete',
// 'deleteAny',
// 'deleteOnce',
// 'deleteAnyOnce',
// 'head',
// 'headAny',
// 'headOnce',
// 'headAnyOnce',
// 'patch',
// 'patchAny',
// 'patchOnce',
// 'patchAnyOnce'
].forEach((methodName) => {
describe(`when using ${methodName}`, () => {
it(`Removes as option on first parameter of ${methodName}()`, () => {});
if (/any/i.test(methodName)) {
it(`Removes as option on third parameter of ${methodName}()`, () => {
expectCodemodResult(
`fetchMock.${methodName}(200, {name: 'rio', ${optionName}: true})`,
`fetchMock.${methodName}(200, {
name: 'rio'
})`,
);
});

it(`Removes as option on third parameter of ${methodName}()`, () => {});
it(`Removes third parameter of ${methodName}() if no other options remain`, () => {
expectCodemodResult(
`fetchMock.${methodName}('*', 200, {${optionName}: true})`,
`fetchMock.${methodName}('*', 200)`,
);
});
} else {
it(`Removes as option on first parameter of ${methodName}()`, () => {
expectCodemodResult(
`fetchMock.${methodName}({url: '*', response: 200, ${optionName}: true})`,
`fetchMock.${methodName}({
url: '*',
response: 200
})`,
);
});
it(`Removes as option on third parameter of ${methodName}()`, () => {
expectCodemodResult(
`fetchMock.${methodName}('*', 200, {name: 'rio', ${optionName}: true})`,
`fetchMock.${methodName}('*', 200, {
name: 'rio'
})`,
);
});

it(`Removes third parameter of ${methodName}() if no other options remain`, () => {});
it(`Removes third parameter of ${methodName}() if no other options remain`, () => {
expectCodemodResult(
`fetchMock.${methodName}('*', 200, {${optionName}: true})`,
`fetchMock.${methodName}('*', 200)`,
);
});
}
});
});
});
describe('acting on combinations of the 3 options together', () => {});
});
describe('fallbackToNetwork', () => {
// try to replace fallbackToNetwork: always with spyGlobal()... but probably just insert an error / comment that points at the docs
Expand Down
33 changes: 10 additions & 23 deletions packages/codemods/src/codemods/methods.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function simpleMethods(fetchMockVariableName, root, j) {
const fetchMockMethodCalls = root
export function getAllChainedMethodCalls(fetchMockVariableName, root, j) {
return root
.find(j.CallExpression, {
callee: {
object: {
Expand All @@ -18,6 +18,14 @@ export function simpleMethods(fetchMockVariableName, root, j) {
}
return paths;
});
}

export function simpleMethods(fetchMockVariableName, root, j) {
const fetchMockMethodCalls = getAllChainedMethodCalls(
fetchMockVariableName,
root,
j,
);

fetchMockMethodCalls.forEach((path) => {
const method = path.value.callee.property.name;
Expand All @@ -34,28 +42,7 @@ export function simpleMethods(fetchMockVariableName, root, j) {
path.value.callee.property.name = `${httpMethod}Once`;
}
if (prependStar) {
// const options =
path.value.arguments.unshift(j.stringLiteral('*'));
// [1];
// if (!options) {
// path.value.arguments.push(
// j(`const options = {method: '${httpMethod}'}`)
// .find(j.ObjectExpression)
// .get().value,
// );
// } else if (options.type === 'Literal') {
// path.value.arguments[1] = j(
// `const options = {name: ${options.raw}, method: '${httpMethod}'}`,
// )
// .find(j.ObjectExpression)
// .get().value;
// } else if (options.type === 'ObjectExpression') {
// options.properties.push(
// j(`const options = {method: '${httpMethod}'}`)
// .find(j.Property)
// .get().value,
// );
// }
}
});
});
Expand Down
59 changes: 58 additions & 1 deletion packages/codemods/src/codemods/options.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getAllChainedMethodCalls } from './methods.js';
const simpleOptionNames = ['overwriteRoutes', 'warnOnFallback', 'sendAsJson'];
export function simpleOptions(fetchMockVariableName, root, j) {
const configSets = root
.find(j.CallExpression, {
Expand All @@ -20,7 +22,7 @@ export function simpleOptions(fetchMockVariableName, root, j) {
secondArg.type === 'ObjectExpression'
);
});
['overwriteRoutes', 'warnOnFallback', 'sendAsJson'].forEach((name) => {
simpleOptionNames.forEach((name) => {
root
.find(j.AssignmentExpression, {
left: {
Expand All @@ -46,4 +48,59 @@ export function simpleOptions(fetchMockVariableName, root, j) {
return secondArg.properties.length === 0;
})
.remove();

const fetchMockMethodCalls = getAllChainedMethodCalls(
fetchMockVariableName,
root,
j,
);

[
'once',
'route',
'sticky',
'any',
'anyOnce',
'get',
'getOnce',
'post',
'postOnce',
'put',
'putOnce',
'delete',
'deleteOnce',
'head',
'headOnce',
'patch',
'patchOnce',
].some((methodName) => {
const optionsObjects = fetchMockMethodCalls
.filter((path) => path.value.callee.property.name === methodName)
.map((path) => {
return j(path)
.find(j.ObjectExpression)
.filter((path) => {
return path.value.properties.some(({ key }) =>
simpleOptionNames.includes(key.name),
);
})
.paths();
});

if (!optionsObjects.length) {
return;
}
simpleOptionNames.forEach((optionName) => {
optionsObjects
.find(j.Property, {
key: { name: optionName },
})
.remove();
});
optionsObjects
.filter((path) => {
return path.value.properties.length === 0;
})
.remove();
});
}
5 changes: 4 additions & 1 deletion packages/codemods/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ function findFetchMockVariableName(root, j) {
export function codemod(source, j) {
const root = j(source);
const fetchMockVariableName = findFetchMockVariableName(root, j);
simpleOptions(fetchMockVariableName, root, j);
simpleMethods(fetchMockVariableName, root, j);
// run after simpleMethods because means the options rewriters have to iterate
// over smaller list of methods
simpleOptions(fetchMockVariableName, root, j);

return root.toSource();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/codemods/try.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ console.log(
codemod(
`
import fetchMock from 'fetch-mock';
fetchMock.getAny(200, {name: 'who'})
fetchMock.once({url: '*', response: 200, sendAsJson: true})
`,
jscodeshift,
),
Expand Down

0 comments on commit e976e7e

Please sign in to comment.