From d31172fccb6a30fbe8be70e9362625ea932b3ede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 2 May 2023 11:46:50 +0100 Subject: [PATCH] Fix backward compat for semver checks in class®exp feat plugins --- .../src/index.ts | 15 +++++++++++++-- .../src/index.ts | 15 +++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/babel-helper-create-class-features-plugin/src/index.ts b/packages/babel-helper-create-class-features-plugin/src/index.ts index 6698e246570a..1cf906740986 100644 --- a/packages/babel-helper-create-class-features-plugin/src/index.ts +++ b/packages/babel-helper-create-class-features-plugin/src/index.ts @@ -96,9 +96,20 @@ export function createClassFeaturePlugin({ pre(file) { enableFeature(file, feature, loose); + let existingVersion = file.get(versionKey); + if (!process.env.BABEL_8_BREAKING) { + if (typeof existingVersion === "number") { + // Up to version 7.21.4, we used to encode the version as a number + // using the following encoding algorithm: + // version.split(".").reduce((v, x) => v * 1e5 + +x, 0) + const patch = existingVersion % 1e5; + const minor = ((existingVersion - patch) / 1e5) % 1e5; + existingVersion = `7.${minor}.${patch}`; + } + } if ( - !file.get(versionKey) || - semver.lt(file.get(versionKey), PACKAGE_JSON.version) + !existingVersion || + semver.lt(existingVersion, PACKAGE_JSON.version) ) { file.set(versionKey, PACKAGE_JSON.version); } diff --git a/packages/babel-helper-create-regexp-features-plugin/src/index.ts b/packages/babel-helper-create-regexp-features-plugin/src/index.ts index 95c5cdda8c52..24cc1000ef21 100644 --- a/packages/babel-helper-create-regexp-features-plugin/src/index.ts +++ b/packages/babel-helper-create-regexp-features-plugin/src/index.ts @@ -82,9 +82,20 @@ export function createRegExpFeaturePlugin({ } } + let existingVersion = file.get(versionKey); + if (!process.env.BABEL_8_BREAKING) { + if (typeof existingVersion === "number") { + // Up to version 7.21.4, we used to encode the version as a number + // using the following encoding algorithm: + // version.split(".").reduce((v, x) => v * 1e5 + +x, 0) + const patch = existingVersion % 1e5; + const minor = ((existingVersion - patch) / 1e5) % 1e5; + existingVersion = `7.${minor}.${patch}`; + } + } if ( - !file.has(versionKey) || - semver.lt(file.get(versionKey), PACKAGE_JSON.version) + !existingVersion || + semver.lt(existingVersion, PACKAGE_JSON.version) ) { file.set(versionKey, PACKAGE_JSON.version); }