Skip to content

Commit

Permalink
feat: first codemod for editing an option implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 31, 2024
1 parent f981e4d commit 6ec5575
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 35 deletions.
87 changes: 55 additions & 32 deletions packages/codemods/src/__test__/option-codemods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { codemod } from '../index';
import jscodeshift from 'jscodeshift';

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

function expectCodemodResult(src, expected) {
expect(codemod(prependFetchMock(src), jscodeshift)).toEqual(
Expand All @@ -12,38 +12,61 @@ 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', () => {})[
('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')
it('Removes as global option when setting directly as property', () => {
expectCodemodResult(`fetchMock.config.${optionName} = true`, '');
});
it('Removes as global option when using Object.assign', () => {
expectCodemodResult(
`Object.assign(fetchMock.config, {${optionName}: true})`,
`Object.assign(fetchMock.config, {})`,
);
});
it('Removes as global option when using Object.assign alongside other options', () => {
expectCodemodResult(
`Object.assign(fetchMock.config, {${optionName}: true, other: 'value'})`,
`Object.assign(fetchMock.config, {other: 'value'})`,
);
});
it.skip('Removes as global option when using spread', () => {
// implement if there is demand
});

[
'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}()`, () => {});
Expand Down
22 changes: 19 additions & 3 deletions packages/codemods/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,32 @@ export function codemod(source: string, j: JSCodeshift) {
.closest(j.VariableDeclarator)
.get().value.id.name;

const usesOfFetchmock = root.find(j.CallExpression, {
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 fetchMockMethodCalls = root.find(j.CallExpression, {
callee: {
object: {
type: 'Identifier',
name: fetchMockVariableName,
},
},
});

usesOfFetchmock
fetchMockMethodCalls
.map((path) => {
const paths = [path];
while (path.parentPath.value.type !== 'ExpressionStatement') {
Expand Down

0 comments on commit 6ec5575

Please sign in to comment.