From 27eda56db266986fbe635e850e4c194724186ef8 Mon Sep 17 00:00:00 2001
From: Lctrs <Lctrs@users.noreply.github.com>
Date: Wed, 22 Feb 2023 14:06:20 +0100
Subject: [PATCH] fix(versioning/composer): support range versions
 normalization (#20558)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 lib/modules/versioning/composer/index.spec.ts |  8 +++
 lib/modules/versioning/composer/index.ts      | 49 ++++++++++---------
 2 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/lib/modules/versioning/composer/index.spec.ts b/lib/modules/versioning/composer/index.spec.ts
index 4df706a988..21b03a642a 100644
--- a/lib/modules/versioning/composer/index.spec.ts
+++ b/lib/modules/versioning/composer/index.spec.ts
@@ -61,6 +61,14 @@ describe('modules/versioning/composer/index', () => {
     ${'1.2.3'}        | ${true}
     ${'2.5'}          | ${true}
     ${'v2.5'}         | ${true}
+    ${'^1.0|^2.0'}    | ${true}
+    ${'^1.0 | ^2.0'}  | ${true}
+    ${'^1.0||^2.0'}   | ${true}
+    ${'^1.0 || ^2.0'} | ${true}
+    ${'~1.0|~2.0'}    | ${true}
+    ${'~1.0 | ~2.0'}  | ${true}
+    ${'~1.0||~2.0'}   | ${true}
+    ${'~1.0 || ~2.0'} | ${true}
   `('isValid("$version") === $expected', ({ version, expected }) => {
     const res = !!semver.isValid(version);
     expect(res).toBe(expected);
diff --git a/lib/modules/versioning/composer/index.ts b/lib/modules/versioning/composer/index.ts
index cad5fabfe8..4574046421 100644
--- a/lib/modules/versioning/composer/index.ts
+++ b/lib/modules/versioning/composer/index.ts
@@ -67,28 +67,33 @@ function normalizeVersion(input: string): string {
 }
 
 function composer2npm(input: string): string {
-  const cleanInput = normalizeVersion(input);
-  if (npm.isVersion(cleanInput)) {
-    return cleanInput;
-  }
-  if (npm.isVersion(padZeroes(cleanInput))) {
-    return padZeroes(cleanInput);
-  }
-  const [versionId, stability] = getVersionParts(cleanInput);
-  let output = versionId;
-
-  // ~4 to ^4 and ~4.1 to ^4.1
-  output = output.replace(
-    regEx(/(?:^|\s)~([1-9][0-9]*(?:\.[0-9]*)?)(?: |$)/g),
-    '^$1'
-  );
-  // ~0.4 to >=0.4 <1
-  output = output.replace(
-    regEx(/(?:^|\s)~(0\.[1-9][0-9]*)(?: |$)/g),
-    '>=$1 <1'
-  );
-
-  return output + stability;
+  return input
+    .split(regEx(/\s*\|\|?\s*/g))
+    .map((part): string => {
+      const cleanInput = normalizeVersion(part);
+      if (npm.isVersion(cleanInput)) {
+        return cleanInput;
+      }
+      if (npm.isVersion(padZeroes(cleanInput))) {
+        return padZeroes(cleanInput);
+      }
+      const [versionId, stability] = getVersionParts(cleanInput);
+      let output = versionId;
+
+      // ~4 to ^4 and ~4.1 to ^4.1
+      output = output.replace(
+        regEx(/(?:^|\s)~([1-9][0-9]*(?:\.[0-9]*)?)(?: |$)/g),
+        '^$1'
+      );
+      // ~0.4 to >=0.4 <1
+      output = output.replace(
+        regEx(/(?:^|\s)~(0\.[1-9][0-9]*)(?: |$)/g),
+        '>=$1 <1'
+      );
+
+      return output + stability;
+    })
+    .join(' || ');
 }
 
 function equals(a: string, b: string): boolean {
-- 
GitLab