diff --git a/lib/common/yok.ts b/lib/common/yok.ts index da0e737aa5..24bd883866 100644 --- a/lib/common/yok.ts +++ b/lib/common/yok.ts @@ -657,7 +657,7 @@ export class Yok extends Injector implements IInjector { * runtime-traced via reportDeprecation. */ public get dynamicCallRegex(): RegExp { - return /#{([^.]+)\.([^}]+?)(\((.+)\))*}/; + return /#{([^.]+)\.([^}]+?)(\((.+)\))?}/; } /** diff --git a/lib/services/cocoapods-service.ts b/lib/services/cocoapods-service.ts index 63b1c91c8a..b7fdb6c595 100644 --- a/lib/services/cocoapods-service.ts +++ b/lib/services/cocoapods-service.ts @@ -339,12 +339,9 @@ end`.trim(); extensionsPodfile.forEach(({ targetName, podfilePath }) => { // Remove the data between #Begin Podfile and #EndPodfile - const regExpToRemove = new RegExp( - `${this.getExtensionPodfileHeader( - podfilePath, - targetName, - )}[\\s\\S]*?${this.getExtensionPodfileEnd()}`, - "mg", + const regExpToRemove = this.getPodfileSectionRegExp( + this.getExtensionPodfileHeader(podfilePath, targetName), + this.getExtensionPodfileEnd(), ); projectPodFileContent = projectPodFileContent.replace(regExpToRemove, ""); @@ -448,11 +445,9 @@ end`.trim(); this.getProjectPodfilePath(projectRoot), ); // Remove the data between #Begin Podfile and #EndPodfile - const regExpToRemove = new RegExp( - `${this.getPluginPodfileHeader( - podfilePath, - )}[\\s\\S]*?${this.getPluginPodfileEnd()}`, - "mg", + const regExpToRemove = this.getPodfileSectionRegExp( + this.getPluginPodfileHeader(podfilePath), + this.getPluginPodfileEnd(), ); projectPodFileContent = projectPodFileContent.replace(regExpToRemove, ""); projectPodFileContent = this.removePostInstallHook( @@ -655,11 +650,18 @@ end`.trim(); } private getPluginPodfileHeader(pluginPodFilePath: string): string { - // escape special + from the podfile path (pnpm) - pluginPodFilePath = pluginPodFilePath.replace(/\+/g, "\\+"); return `# Begin Podfile - ${pluginPodFilePath}`; } + private getPodfileSectionRegExp(header: string, end: string): RegExp { + // Podfiles written by older CLI versions have "+" in the header path escaped as "\+". + const headerPattern = header + .split("+") + .map((part) => regExpEscape(part)) + .join("\\\\?\\+"); + return new RegExp(`${headerPattern}[\\s\\S]*?${regExpEscape(end)}`, "mg"); + } + private getPluginPodfileEnd(): string { return `# End Podfile${EOL}`; } diff --git a/test/cocoapods-service.ts b/test/cocoapods-service.ts index e61b7778cc..138d2a3133 100644 --- a/test/cocoapods-service.ts +++ b/test/cocoapods-service.ts @@ -810,6 +810,66 @@ end`, ); }); }); + + describe("when the plugin path contains regular expression characters", () => { + const pnpmPluginData: any = { + name: "plugin1", + pluginPlatformsFolderPath: () => + "node_modules/.pnpm/plugin1@1.0.0_a+b(peer@2.0.0)/pluginPlatformsFolderPath", + }; + + const projectPodfileWithHeader = (headerPath: string) => `use_frameworks! + +target "projectName" do +# Begin Podfile - ${headerPath} + +pod 'GoogleAnalytics', '~> 3.1' + +# End Podfile + +end`; + + it("removes the Podfile section added for the plugin", async () => { + const pluginPodfilePath = + cocoapodsService.getPluginPodfilePath(pnpmPluginData); + mockFileSystem(testInjector, "pod 'GoogleAnalytics', '~> 3.1'"); + await cocoapodsService.applyPodfileToProject( + pnpmPluginData.name, + pluginPodfilePath, + mockProjectData, + mockPlatformData, + ); + assert.include(newPodfileContent, pluginPodfilePath); + + cocoapodsService.removePodfileFromProject( + pnpmPluginData.name, + pluginPodfilePath, + mockProjectData, + nativeProjectPath, + ); + + assert.isNull(newPodfileContent); + }); + + it("removes a Podfile section whose header has + escaped", async () => { + const pluginPodfilePath = + cocoapodsService.getPluginPodfilePath(pnpmPluginData); + mockFileSystem( + testInjector, + "", + projectPodfileWithHeader(pluginPodfilePath.split("+").join("\\+")), + ); + + cocoapodsService.removePodfileFromProject( + pnpmPluginData.name, + pluginPodfilePath, + mockProjectData, + nativeProjectPath, + ); + + assert.isNull(newPodfileContent); + }); + }); }); describe("executePodInstall", () => {