From 23201b0f3ff34f04ae80152847d35d228550ca31 Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Sat, 31 Aug 2024 20:09:37 +0100 Subject: [PATCH] feat: more option codemods --- .../src/__test__/option-codemods.test.js | 6 +- packages/codemods/src/index.ts | 102 +++++++++--------- packages/codemods/try.ts | 11 ++ jsconfig.json => tsconfig.json | 3 +- 4 files changed, 64 insertions(+), 58 deletions(-) create mode 100644 packages/codemods/try.ts rename jsconfig.json => tsconfig.json (69%) diff --git a/packages/codemods/src/__test__/option-codemods.test.js b/packages/codemods/src/__test__/option-codemods.test.js index 42a47431..fb6382bf 100644 --- a/packages/codemods/src/__test__/option-codemods.test.js +++ b/packages/codemods/src/__test__/option-codemods.test.js @@ -12,11 +12,7 @@ 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 when setting directly as property', () => { expectCodemodResult(`fetchMock.config.${optionName} = true`, ''); diff --git a/packages/codemods/src/index.ts b/packages/codemods/src/index.ts index ec7371df..7abb22b6 100644 --- a/packages/codemods/src/index.ts +++ b/packages/codemods/src/index.ts @@ -17,55 +17,53 @@ export function codemod(source: string, j: JSCodeshift) { .closest(j.VariableDeclarator) .get().value.id.name; - 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 configSets = root - .find(j.CallExpression, { - callee: { - object: { - type: "Identifier", - name: "Object" - }, - property: { name: "assign" } - } - }) - .filter((path) => { - const firstArg = path.value.arguments[0]; - const secondArg = path.value.arguments[1]; - return ( - firstArg.type === "MemberExpression" && - firstArg.property.name === "config" && - firstArg.object.type === "Identifier" && - firstArg.object.name === fetchMockVariableName - ) && (secondArg.type === 'ObjectExpression' - && secondArg.properties.some(property => property.key.name === 'overwriteRoutes') ); - }) - - configSets.filter((path) => { - const secondArg = path.value.arguments[1]; - return (secondArg.properties.length === 1) - }).remove(); - - configSets.filter((path) => { - const secondArg = path.value.arguments[1]; - return (secondArg.properties.length > 1) - }) - .find(j.Property, { key: { name: "overwriteRoutes" } }).remove() - + const configSets = root + .find(j.CallExpression, { + callee: { + object: { + type: 'Identifier', + name: 'Object', + }, + property: { name: 'assign' }, + }, + }) + .filter((path) => { + const firstArg = path.value.arguments[0]; + const secondArg = path.value.arguments[1]; + return ( + firstArg.type === 'MemberExpression' && + firstArg.property.name === 'config' && + firstArg.object.type === 'Identifier' && + firstArg.object.name === fetchMockVariableName && + secondArg.type === 'ObjectExpression' + ); + }); + ['overwriteRoutes', 'warnOnFallback', 'sendAsJson'].forEach((name) => { + root + .find(j.AssignmentExpression, { + left: { + type: 'MemberExpression', + property: { name }, + object: { + type: 'MemberExpression', + property: { name: 'config' }, + object: { + type: 'Identifier', + name: fetchMockVariableName, + }, + }, + }, + }) + .remove(); + configSets.find(j.Property, { key: { name } }).remove(); + }); + configSets + .filter((path) => { + const secondArg = path.value.arguments[1]; + return secondArg.properties.length === 0; + }) + .remove(); const fetchMockMethodCalls = root.find(j.CallExpression, { callee: { @@ -74,9 +72,7 @@ export function codemod(source: string, j: JSCodeshift) { name: fetchMockVariableName, }, }, - }); - fetchMockMethodCalls - .map((path) => { + }).map((path) => { const paths = [path]; while (path.parentPath.value.type !== 'ExpressionStatement') { path = path.parentPath; @@ -85,7 +81,9 @@ export function codemod(source: string, j: JSCodeshift) { } } return paths; - }) + }); + + fetchMockMethodCalls .forEach((path) => { const callee = path.value.callee as MemberExpression; const property = callee.property as Identifier; diff --git a/packages/codemods/try.ts b/packages/codemods/try.ts new file mode 100644 index 00000000..9e114a2f --- /dev/null +++ b/packages/codemods/try.ts @@ -0,0 +1,11 @@ +import { codemod } from './src/index'; +import jscodeshift from 'jscodeshift'; + +codemod( + ` +import fetchMock from 'fetch-mock'; +Object.assign(fetchMock.config, {overwriteRoutes: true, other: 'value'}) +Object.assign(fetchMock.config, {overwriteRoutes: true}) +`, + jscodeshift, +); diff --git a/jsconfig.json b/tsconfig.json similarity index 69% rename from jsconfig.json rename to tsconfig.json index fe332093..811ceaec 100644 --- a/jsconfig.json +++ b/tsconfig.json @@ -7,5 +7,6 @@ "target": "es2021" }, "include": ["./packages/**/src/*.ts"], - "exclude": ["node_modules", "packages/**/__tests__"] + "exclude": ["node_modules", "packages/**/__tests__"], + "ts-node": {"esm": true} }