diff --git a/packages/utils/upgrade/resources/codemods/5.0.0/entity-service-document-service.code.ts b/packages/utils/upgrade/resources/codemods/5.0.0/entity-service-document-service.code.ts index a4f5120167..b7fef3fda1 100644 --- a/packages/utils/upgrade/resources/codemods/5.0.0/entity-service-document-service.code.ts +++ b/packages/utils/upgrade/resources/codemods/5.0.0/entity-service-document-service.code.ts @@ -144,6 +144,8 @@ strapi.entityService.findOne(...[...objectParam_10]); */ +const movedFunctions = ['findOne', 'find', 'count', 'create', 'update', 'delete']; + const transformDeclaration = (path: ASTPath, name: any, j: JSCodeshift) => { const declaration = findClosesDeclaration(path, name, j); @@ -284,6 +286,10 @@ const transform: Transform = (file, api) => { name: 'entityService', }, }, + property: { + type: 'Identifier', + name: (name) => movedFunctions.includes(name), + }, }, }) .replaceWith((path) => { diff --git a/packages/utils/upgrade/resources/codemods/5.0.0/strapi-public-interface.code.ts b/packages/utils/upgrade/resources/codemods/5.0.0/strapi-public-interface.code.ts index 2c85893545..8738789069 100644 --- a/packages/utils/upgrade/resources/codemods/5.0.0/strapi-public-interface.code.ts +++ b/packages/utils/upgrade/resources/codemods/5.0.0/strapi-public-interface.code.ts @@ -90,7 +90,7 @@ const transformFunctionCalls = (identifier: string, root: Collection, j: JSCodes }, }) .forEach((path) => { - // we do a again to avoid ts issues + // we a type guard again to avoid ts issues if (path.value.callee.type === 'Identifier') { path.value.callee.name = 'createStrapi'; } diff --git a/packages/utils/upgrade/resources/codemods/5.0.0/utils-public-interface.code.ts b/packages/utils/upgrade/resources/codemods/5.0.0/utils-public-interface.code.ts index 7c77037f97..7d42adc024 100644 --- a/packages/utils/upgrade/resources/codemods/5.0.0/utils-public-interface.code.ts +++ b/packages/utils/upgrade/resources/codemods/5.0.0/utils-public-interface.code.ts @@ -2,7 +2,7 @@ import { Transform, JSCodeshift, Collection } from 'jscodeshift'; /* -This codemod transforms @strapi/utils imports to change method calls to math the new public interface. +This codemod transforms @strapi/utils imports to change method calls to match the new public interface. It will also warn about removed functions to avoid breaking user code. ESM @@ -111,14 +111,18 @@ const transformImports = (root: Collection, j: JSCodeshift) => { source: { value: '@strapi/utils' }, }) .forEach((path) => { - path.value.specifiers?.forEach((specifier) => { + if (!j.ImportDeclaration.check(path.value)) { + return; + } + + path.value.specifiers.forEach((specifier) => { if (!j.ImportSpecifier.check(specifier)) { return false; } if (removed.includes(specifier.imported.name)) { console.warn( - `Function "${specifier.imported.name}" as removed. You will have to remove it from your code.` + `Function "${specifier.imported.name}" was removed. You will have to remove it from your code.` ); return false; @@ -128,16 +132,16 @@ const transformImports = (root: Collection, j: JSCodeshift) => { for (const primitive of Object.keys(changes)) { const functions = Object.keys(changes[primitive]); - const specifiersToRefactor = path.value.specifiers?.filter((specifier) => { + const specifiersToRefactor = path.value.specifiers.filter((specifier) => { return j.ImportSpecifier.check(specifier) && functions.includes(specifier.imported.name); }); - if (specifiersToRefactor?.length > 0) { - path.value.specifiers?.unshift(j.importSpecifier(j.identifier(primitive))); + if (specifiersToRefactor.length > 0) { + path.value.specifiers.unshift(j.importSpecifier(j.identifier(primitive))); specifiersToRefactor.forEach((specifier) => { - const index = path.value.specifiers?.indexOf(specifier); - path.value.specifiers?.splice(index, 1); + const index = path.value.specifiers.indexOf(specifier); + path.value.specifiers.splice(index, 1); }); } } @@ -193,7 +197,8 @@ const transformImports = (root: Collection, j: JSCodeshift) => { }, }) .forEach((path) => { - // destrucured require + // destructured require + if (j.ObjectPattern.check(path.value.id)) { const properties = path.value.id.properties; @@ -204,7 +209,7 @@ const transformImports = (root: Collection, j: JSCodeshift) => { if (removed.includes(property.value.name)) { console.warn( - `Function "${property.value.name}" as removed. You will have to remove it from your code.` + `Function "${property.value.name}" was removed. You will have to remove it from your code.` ); return false;