Skip to content

Commit

Permalink
feat: remove codemods for sandbox() method
Browse files Browse the repository at this point in the history
Too many weird use cases to consider - best handled with documentation
  • Loading branch information
wheresrhys committed Sep 16, 2024
1 parent c0adc0c commit 9a06c1e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 64 deletions.
1 change: 1 addition & 0 deletions packages/codemods/src/__test__/integration.test.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// won't rewrite similarly named methods such as jest.mock
// allow passing in a name of a fm variable to treat as an extra fetch-mock
25 changes: 0 additions & 25 deletions packages/codemods/src/__test__/method-codemods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,29 +249,4 @@ fetchMock.calls()`,
});
});
});

describe('sandbox()', () => {
it('replace simple case with fetchHandler', () => {
expectCodemodResult(
`fetchMock.sandbox()`,
`throw new Error("In most cases .sandbox() can now be replaced by .fetchHandler. Refer to the docs on .createInstance() if this does not work for you")
fetchMock.fetchHandler`,
);
});
it('replace complex case with fetchHandler', () => {
expectCodemodResult(
`jest.mock('node-fetch', () => fetchMock.sandbox())`,
`throw new Error("In most cases .sandbox() can now be replaced by .fetchHandler. Refer to the docs on .createInstance() if this does not work for you")
jest.mock('node-fetch', () => fetchMock.fetchHandler)`,
);
});

it('replace complex case with fetchHandler', () => {
expectCodemodResult(
`jest.mock('node-fetch', () => require('fetch-mock').sandbox())`,
`throw new Error("In most cases .sandbox() can now be replaced by .fetchHandler. Refer to the docs on .createInstance() if this does not work for you")
jest.mock('node-fetch', () => require('fetch-mock').fetchHandler)`,
);
});
});
});
31 changes: 3 additions & 28 deletions packages/codemods/src/codemods/methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export function getAllChainedMethodCalls(fetchMockVariableName, root, j) {
import j from 'jscodeshift';
export function getAllChainedMethodCalls(fetchMockVariableName, root) {
return root
.find(j.CallExpression, {
callee: {
Expand All @@ -24,7 +25,7 @@ export function getAllChainedMethodCalls(fetchMockVariableName, root, j) {
});
}

export function simpleMethods(fetchMockVariableName, root, j) {
export function simpleMethods(fetchMockVariableName, root) {
const fetchMockMethodCalls = getAllChainedMethodCalls(
fetchMockVariableName,
root,
Expand Down Expand Up @@ -157,30 +158,4 @@ fetchMock.unmockGlobal();
.find(j.ThrowStatement)
.get().value,
);

const sandboxes = root.find(j.CallExpression, {
callee: {
object: {
type: 'Identifier',
name: fetchMockVariableName,
},
property: {
name: 'sandbox',
},
},
});

sandboxes
.closest(j.ExpressionStatement)
.insertBefore(
j(
'throw new Error("In most cases .sandbox() can now be replaced by .fetchHandler. Refer to the docs on .createInstance() if this does not work for you")',
)
.find(j.ThrowStatement)
.get().value,
);

sandboxes.replaceWith(
j('fetchMock.fetchHandler').find(j.MemberExpression).get().value,
);
}
9 changes: 5 additions & 4 deletions packages/codemods/src/codemods/options.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import j from 'jscodeshift';
import { getAllChainedMethodCalls } from './methods.js';
const simpleOptionNames = ['overwriteRoutes', 'warnOnFallback', 'sendAsJson'];

function appendError(message, path, j) {
function appendError(message, path) {
path
.closest(j.ExpressionStatement)
.insertAfter(
j(`throw new Error("${message}")`).find(j.ThrowStatement).get().value,
);
}
export function simpleOptions(fetchMockVariableName, root, j) {
export function simpleOptions(fetchMockVariableName, root) {
const configSets = root
.find(j.CallExpression, {
callee: {
Expand Down Expand Up @@ -50,8 +51,8 @@ export function simpleOptions(fetchMockVariableName, root, j) {
if (name === 'fallbackToNetwork') {
const errorMessage =
'fallbackToNetwork option is deprecated. Use the `spyGlobal()` method instead';
appendError(errorMessage, propertyAssignments, j);
appendError(errorMessage, objectAssignments, j);
appendError(errorMessage, propertyAssignments);
appendError(errorMessage, objectAssignments);
}
propertyAssignments.remove();
objectAssignments.remove();
Expand Down
15 changes: 8 additions & 7 deletions packages/codemods/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import j from 'jscodeshift';
import { simpleOptions } from './codemods/options.js';
import { simpleMethods } from './codemods/methods.js';

function findFetchMockVariableName(root, j) {
function findFetchMockVariableName(root) {
let fetchMockVariableName;
try {
fetchMockVariableName = root
Expand All @@ -28,17 +29,17 @@ function findFetchMockVariableName(root, j) {
return fetchMockVariableName;
}

export function codemod(source, j) {
export function codemod(source) {
const root = j(source);
const fetchMockVariableName = findFetchMockVariableName(root, j);
simpleMethods(fetchMockVariableName, root, j);
const fetchMockVariableName = findFetchMockVariableName(root);
simpleMethods(fetchMockVariableName, root);
// run after simpleMethods because means the options rewriters have to iterate
// over smaller list of methods
simpleOptions(fetchMockVariableName, root, j);
simpleOptions(fetchMockVariableName, root);

return root.toSource();
}

export default function transformer(file, api) {
return codemod(file.source, api.j);
export default function transformer(file) {
return codemod(file.source);
}

0 comments on commit 9a06c1e

Please sign in to comment.