Skip to content

Commit

Permalink
feat: codemod fro fallbackToNetwork option
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Sep 14, 2024
1 parent 3b0786f commit c272f65
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
23 changes: 22 additions & 1 deletion packages/codemods/src/__test__/option-codemods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,27 @@ describe('codemods operating on options', () => {
});
});
describe('fallbackToNetwork', () => {
// try to replace fallbackToNetwork: always with spyGlobal()... but probably just insert an error / comment that points at the docs
const errorString =
'throw new Error("fallbackToNetwork option is deprecated. Use the `spyGlobal()` method instead")';
it('Removes fallbackToNetwork as global option when setting directly as property', () => {
expectCodemodResult(
`fetchMock.config.fallbackToNetwork = true`,
errorString,
);
});
it('Removes fallbackToNetwork as global option when using Object.assign', () => {
expectCodemodResult(
`Object.assign(fetchMock.config, {fallbackToNetwork: true})`,
errorString,
);
});
it('Removes fallbackToNetwork as global option when using Object.assign alongside other options', () => {
expectCodemodResult(
`Object.assign(fetchMock.config, {fallbackToNetwork: true, other: 'value'})`,
`Object.assign(fetchMock.config, {
other: 'value'
})${errorString}`,
);
});
});
});
43 changes: 29 additions & 14 deletions packages/codemods/src/codemods/options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { getAllChainedMethodCalls } from './methods.js';
const simpleOptionNames = ['overwriteRoutes', 'warnOnFallback', 'sendAsJson'];

function appendError(message, path, j) {
path
.closest(j.ExpressionStatement)
.insertAfter(
j(`throw new Error("${message}")`).find(j.ThrowStatement).get().value,
);
}
export function simpleOptions(fetchMockVariableName, root, j) {
const configSets = root
.find(j.CallExpression, {
Expand All @@ -22,24 +30,31 @@ export function simpleOptions(fetchMockVariableName, root, j) {
secondArg.type === 'ObjectExpression'
);
});
simpleOptionNames.forEach((name) => {
root
.find(j.AssignmentExpression, {
left: {
[...simpleOptionNames, 'fallbackToNetwork'].forEach((name) => {
const propertyAssignments = root.find(j.AssignmentExpression, {
left: {
type: 'MemberExpression',
property: { name },
object: {
type: 'MemberExpression',
property: { name },
property: { name: 'config' },
object: {
type: 'MemberExpression',
property: { name: 'config' },
object: {
type: 'Identifier',
name: fetchMockVariableName,
},
type: 'Identifier',
name: fetchMockVariableName,
},
},
})
.remove();
configSets.find(j.Property, { key: { name } }).remove();
},
});
const objectAssignments = configSets.find(j.Property, { key: { name } });

if (name === 'fallbackToNetwork') {
const errorMessage =
'fallbackToNetwork option is deprecated. Use the `spyGlobal()` method instead';
appendError(errorMessage, propertyAssignments, j);
appendError(errorMessage, objectAssignments, j);
}
propertyAssignments.remove();
objectAssignments.remove();
});

configSets
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.getAnyOnce(200, {sendAsJson: true})
Object.assign(fetchMock.config, {fallbackToNetwork: true})
`,
jscodeshift,
),
Expand Down

0 comments on commit c272f65

Please sign in to comment.