Skip to content

Commit

Permalink
feat: allow manually specifying extra variables that hold fetch-mock …
Browse files Browse the repository at this point in the history
…instances
  • Loading branch information
wheresrhys committed Sep 18, 2024
1 parent 517a6ac commit 317ede7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
21 changes: 19 additions & 2 deletions packages/codemods/src/__test__/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { it, describe } from 'vitest';
import { it, describe, expect } from 'vitest';
import transformer from '../index';

function expectCodemodResult(src, expected) {
expect(transformer({ source: src })).toEqual(expected);
}

describe('integration', () => {
it('allow passing in one or more variable names for fetch-mock', () => {});
it('allow passing in one or more variable names for fetch-mock', () => {
process.env.FM_VARIABLES = 'fm1,fm2';
expectCodemodResult(
`const fetchMock = require('fetch-mock');
fetchMock.mock("blah", 200);
fm1.mock("blah", 200);
fm2.mock("blah", 200);`,
`const fetchMock = require('fetch-mock');
fetchMock.route("blah", 200);
fm1.route("blah", 200);
fm2.route("blah", 200);`,
);
});
it('can operate on a js file', () => {});
it('can operate on a ts file', () => {});
it('can operate on a jsx file', () => {});
Expand Down
7 changes: 3 additions & 4 deletions packages/codemods/src/__test__/method-codemods.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, it, expect } from 'vitest';
import { codemod } from '../index';
import jscodeshift from 'jscodeshift';

const prependFetchMock = (src, fetchMockVariableName) =>
`const ${fetchMockVariableName} = require('fetch-mock');\n${src}`;
Expand All @@ -10,9 +9,9 @@ function expectCodemodResult(
expected,
fetchMockVariableName = 'fetchMock',
) {
expect(
codemod(prependFetchMock(src, fetchMockVariableName), jscodeshift),
).toEqual(prependFetchMock(expected, fetchMockVariableName));
expect(codemod(prependFetchMock(src, fetchMockVariableName))).toEqual(
prependFetchMock(expected, fetchMockVariableName),
);
}

describe('codemods operating on methods', () => {
Expand Down
5 changes: 1 addition & 4 deletions packages/codemods/src/__test__/option-codemods.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { describe, it, expect } from 'vitest';
import { codemod } from '../index';
import jscodeshift from 'jscodeshift';

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

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

describe('codemods operating on options', () => {
Expand Down
13 changes: 10 additions & 3 deletions packages/codemods/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ function findFetchMockVariableName(root) {
return fetchMockVariableName;
}

export function codemod(source) {
export function codemod(source, variableName) {
const root = j(source);
const fetchMockVariableName = findFetchMockVariableName(root);
const fetchMockVariableName = variableName || findFetchMockVariableName(root);
simpleMethods(fetchMockVariableName, root);
// run after simpleMethods because means the options rewriters have to iterate
// over smaller list of methods
Expand All @@ -41,5 +41,12 @@ export function codemod(source) {
}

export default function transformer(file) {
return codemod(file.source);
let modifiedSource = codemod(file.source);
if (process.env.FM_VARIABLES) {
const extraVariables = process.env.FM_VARIABLES.split(',');
extraVariables.forEach((variableName) => {
modifiedSource = codemod(modifiedSource, variableName);
});
}
return modifiedSource;
}

0 comments on commit 317ede7

Please sign in to comment.