Skip to content

Commit

Permalink
fix: changed to use j.ObjectProperty instead of j.Property
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Sep 19, 2024
1 parent 376d5c3 commit 78c2e35
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
3 changes: 2 additions & 1 deletion packages/codemods/src/__test__/option-codemods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ describe('codemods operating on options', () => {
`Object.assign(fetchMock.config, {fallbackToNetwork: true, other: 'value'})`,
`Object.assign(fetchMock.config, {
other: 'value'
})${errorString}`,
});
${errorString}`,
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/codemods/src/codemods/methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const j = require('jscodeshift');
const j = require('jscodeshift').withParser('tsx');

function getAllChainedMethodCalls(fetchMockVariableName, root) {
return root
Expand Down Expand Up @@ -31,7 +31,6 @@ module.exports.simpleMethods = function(fetchMockVariableName, root) {
const fetchMockMethodCalls = getAllChainedMethodCalls(
fetchMockVariableName,
root,
j,
);

fetchMockMethodCalls.forEach((path) => {
Expand All @@ -43,6 +42,7 @@ module.exports.simpleMethods = function(fetchMockVariableName, root) {
if (method === 'resetHistory') {
path.value.callee.property.name = 'clearHistory';
}

['get', 'post', 'put', 'delete', 'head', 'patch'].some((httpMethod) => {
let prependStar = false;
if (method === `${httpMethod}Any`) {
Expand Down
39 changes: 19 additions & 20 deletions packages/codemods/src/codemods/options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const j = require('jscodeshift');
const j = require('jscodeshift').withParser('tsx');
const { getAllChainedMethodCalls } = require('./methods.js');
const simpleOptionNames = ['overwriteRoutes', 'warnOnFallback', 'sendAsJson'];

Expand Down Expand Up @@ -46,7 +46,7 @@ module.exports.simpleOptions = function(fetchMockVariableName, root) {
},
},
});
const objectAssignments = configSets.find(j.Property, { key: { name } });
const objectAssignments = configSets.find(j.ObjectProperty, { key: { name } });

if (name === 'fallbackToNetwork') {
const errorMessage =
Expand All @@ -72,23 +72,23 @@ module.exports.simpleOptions = function(fetchMockVariableName, root) {
);

[
'once',
'route',
'sticky',
'any',
'anyOnce',
// 'once',
// 'route',
// 'sticky',
// 'any',
// 'anyOnce',
'get',
'getOnce',
'post',
'postOnce',
'put',
'putOnce',
'delete',
'deleteOnce',
'head',
'headOnce',
'patch',
'patchOnce',
// 'getOnce',
// 'post',
// 'postOnce',
// 'put',
// 'putOnce',
// 'delete',
// 'deleteOnce',
// 'head',
// 'headOnce',
// 'patch',
// 'patchOnce',
].some((methodName) => {
const optionsObjects = fetchMockMethodCalls
.filter((path) => path.value.callee.property.name === methodName)
Expand All @@ -102,13 +102,12 @@ module.exports.simpleOptions = function(fetchMockVariableName, root) {
})
.paths();
});

if (!optionsObjects.length) {
return;
}
simpleOptionNames.forEach((optionName) => {
optionsObjects
.find(j.Property, {
.find(j.ObjectProperty, {
key: { name: optionName },
})
.remove();
Expand Down
12 changes: 4 additions & 8 deletions packages/codemods/try.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { codemod } from './src/index.js';
import jscodeshift from 'jscodeshift';
const { codemod } = require( './src/index.js');

console.log(
codemod(
`
const fetchMock = require('fetch-mock');
jest.mock('node-fetch', () => fetchMock.fetchHandler)
`,
jscodeshift,
),
`const fetchMock = require('fetch-mock');
fetchMock.get('*', 200, {name: 'rio', sendAsJson: true});
`),
);

0 comments on commit 78c2e35

Please sign in to comment.