Skip to content

Commit

Permalink
feat: codemod restore()
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Sep 15, 2024
1 parent a96f62b commit 21e54af
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
47 changes: 24 additions & 23 deletions packages/codemods/src/codemods/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,37 @@ export function simpleMethods(fetchMockVariableName, root, j) {
}
});
});

root
.find(j.CallExpression, {
callee: {
object: {
type: 'Identifier',
name: fetchMockVariableName,
},
property: {
name: 'reset',
['reset', 'restore'].forEach((methodName) => {
root
.find(j.CallExpression, {
callee: {
object: {
type: 'Identifier',
name: fetchMockVariableName,
},
property: {
name: methodName,
},
},
},
})
.forEach((path) => {
const newExpressions = j(`
})
.forEach((path) => {
const newExpressions = j(`
fetchMock.clearHistory();
fetchMock.removeRoutes({
includeFallback: true,
});
fetchMock.unmockGlobal();
`)
.find(j.ExpressionStatement)
.paths();
const insertLocation = j(path)
.closest(j.ExpressionStatement)
.replaceWith(newExpressions.shift().value);
while (newExpressions.length) {
insertLocation.insertAfter(newExpressions.pop().value);
}
});
.find(j.ExpressionStatement)
.paths();
const insertLocation = j(path)
.closest(j.ExpressionStatement)
.replaceWith(newExpressions.shift().value);
while (newExpressions.length) {
insertLocation.insertAfter(newExpressions.pop().value);
}
});
});

[
['lastUrl', 'url'],
Expand Down
2 changes: 1 addition & 1 deletion packages/codemods/try.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ console.log(
codemod(
`
import fetchMock from 'fetch-mock';
fetchMock.reset();
fetchMock.restore();
`,
jscodeshift,
),
Expand Down

0 comments on commit 21e54af

Please sign in to comment.