Skip to content

Commit

Permalink
feat: lastUrl() codemod implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Sep 1, 2024
1 parent 298a9e2 commit a115a27
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 32 deletions.
43 changes: 35 additions & 8 deletions packages/codemods/src/__test__/method-codemods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ import { describe, it, expect } from 'vitest';
import { codemod } from '../index';
import jscodeshift from 'jscodeshift';

const prependFetchMock = (src) =>
`const fetchMock = require('fetch-mock');\n${src}`;
const prependFetchMock = (src, fetchMockVariableName) =>
`const ${fetchMockVariableName} = require('fetch-mock');\n${src}`;

function expectCodemodResult(src, expected) {
expect(codemod(prependFetchMock(src), jscodeshift)).toEqual(
prependFetchMock(expected),
);
function expectCodemodResult(
src,
expected,
fetchMockVariableName = 'fetchMock',
) {
expect(
codemod(prependFetchMock(src, fetchMockVariableName), jscodeshift),
).toEqual(prependFetchMock(expected, fetchMockVariableName));
}

describe('codemods operating on methods', () => {
describe('converting mock() to route()', () => {
//Next to the first one in a file leave a comment explaining that they need to use mockGlobal() too
//TODO Next to the first one in a file leave a comment explaining that they need to use mockGlobal() too
it('single .mock()', () => {
expectCodemodResult(
'fetchMock.mock("blah", 200)',
Expand Down Expand Up @@ -62,7 +66,30 @@ describe('codemods operating on methods', () => {
});
});

// .lastUrl() => .callHistory.lastCall()?.url
describe('converting lastUrl()', () => {
it('single .lastUrl()', () => {
expectCodemodResult(
'fetchMock.lastUrl()',
'fetchMock.callHistory.lastCall()?.url',
);
});
it('lastUrl() with arguments', () => {
expectCodemodResult(
`fetchMock.lastUrl('name', {method: 'get'})`,
`fetchMock.callHistory.lastCall('name', {method: 'get'})?.url`,
);
});

it('works with other names for fetch-mock', () => {
expectCodemodResult(
`fm.lastUrl('name', {method: 'get'})`,
`fm.callHistory.lastCall('name', {method: 'get'})?.url`,
'fm',
);
});
});

//
// .lastOptions() => .callHistory.lastCall()?.options
// .lastResponse() => .callHistory.lastCall()?.response
// .sandbox() => .fetchHandler(and maybe a comment about.createInstance())
Expand Down
23 changes: 22 additions & 1 deletion packages/codemods/src/codemods/methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function simpleMethods (fetchMockVariableName, root, j) {
export function simpleMethods(fetchMockVariableName, root, j) {
const fetchMockMethodCalls = root
.find(j.CallExpression, {
callee: {
Expand All @@ -25,4 +25,25 @@ export function simpleMethods (fetchMockVariableName, root, j) {
path.value.callee.property.name = 'route';
}
});
const lastUrl = root
.find(j.CallExpression, {
callee: {
object: {
type: 'Identifier',
name: fetchMockVariableName,
},
property: {
name: 'lastUrl',
},
},
})
.closest(j.ExpressionStatement);

lastUrl.replaceWith((path) => {
const oldCall = j(path).find(j.CallExpression).get();
const builder = j(`${fetchMockVariableName}.callHistory.lastCall()?.url`);
const newCall = builder.find(j.CallExpression).get();
newCall.value.arguments = oldCall.value.arguments;
return builder.find(j.ExpressionStatement).get().value;
});
}
3 changes: 1 addition & 2 deletions packages/codemods/src/codemods/options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export function simpleOptions (fetchMockVariableName, root, j) {

export function simpleOptions(fetchMockVariableName, root, j) {
const configSets = root
.find(j.CallExpression, {
callee: {
Expand Down
17 changes: 7 additions & 10 deletions packages/codemods/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {simpleOptions} from './codemods/options'
import {simpleMethods} from './codemods/methods'
import { simpleOptions } from './codemods/options.js';
import { simpleMethods } from './codemods/methods.js';


function findFetchMockVariableName (root, j) {
function findFetchMockVariableName(root, j) {
let fetchMockVariableName;
try {
fetchMockVariableName = root
Expand All @@ -23,19 +22,17 @@ function findFetchMockVariableName (root, j) {
.find(j.ImportDefaultSpecifier)
.get().value.local.name;
} catch (err) {
throw new Error("No fetch-mock references found")
throw new Error('No fetch-mock references found', err);
}
}
return fetchMockVariableName;
}

export function codemod(source, j) {
const root = j(source);
const fetchMockVariableName = findFetchMockVariableName(root, j)

simpleOptions(fetchMockVariableName, root, j)
simpleMethods(fetchMockVariableName, root, j)

const fetchMockVariableName = findFetchMockVariableName(root, j);
simpleOptions(fetchMockVariableName, root, j);
simpleMethods(fetchMockVariableName, root, j);
return root.toSource();
}

Expand Down
12 changes: 12 additions & 0 deletions packages/codemods/try.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { codemod } from './src/index.js';
import jscodeshift from 'jscodeshift';

console.log(
codemod(
`
import fetchMock from 'fetch-mock';
fetchMock.lastUrl(1, 2)
`,
jscodeshift,
),
);
11 changes: 0 additions & 11 deletions packages/codemods/try.ts

This file was deleted.

0 comments on commit a115a27

Please sign in to comment.