From 51f1b1c1db39a57901322f261cdc9e5e4dcc50d9 Mon Sep 17 00:00:00 2001 From: Ming <527990618@163.com> Date: Tue, 3 Sep 2024 20:02:05 +0800 Subject: [PATCH] fix(plugin-swc): should verify the version correctly (#3364) Co-authored-by: neverland --- packages/compat/plugin-swc/src/utils.ts | 6 +++++- .../compat/plugin-swc/tests/utils.test.ts | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 packages/compat/plugin-swc/tests/utils.test.ts diff --git a/packages/compat/plugin-swc/src/utils.ts b/packages/compat/plugin-swc/src/utils.ts index 84b5698fd7..c5713efb08 100644 --- a/packages/compat/plugin-swc/src/utils.ts +++ b/packages/compat/plugin-swc/src/utils.ts @@ -50,6 +50,10 @@ async function findUp({ } } +export const isVersionBeyond17 = (version: string): boolean => { + return semver.gte(semver.minVersion(version)!, '17.0.0'); +}; + const isBeyondReact17 = async (cwd: string) => { const pkgPath = await findUp({ cwd, filename: 'package.json' }); @@ -67,7 +71,7 @@ const isBeyondReact17 = async (cwd: string) => { return false; } - return semver.satisfies(semver.minVersion(deps.react)!, '>=17.0.0'); + return isVersionBeyond17(deps.react); }; /** diff --git a/packages/compat/plugin-swc/tests/utils.test.ts b/packages/compat/plugin-swc/tests/utils.test.ts new file mode 100644 index 0000000000..03df3b3ef6 --- /dev/null +++ b/packages/compat/plugin-swc/tests/utils.test.ts @@ -0,0 +1,19 @@ +import { isVersionBeyond17 } from '../src/utils'; + +describe('isVersionBeyondReact17', () => { + test('should return true for version 17 and above', () => { + expect(isVersionBeyond17('17.0.0')).toBe(true); + expect(isVersionBeyond17('17.0.1')).toBe(true); + expect(isVersionBeyond17('17.0.1-canary')).toBe(true); + expect(isVersionBeyond17('^17.0.0')).toBe(true); + expect(isVersionBeyond17('~18.2.0')).toBe(true); + expect(isVersionBeyond17('18.3.0-canary')).toBe(true); + }); + + test('should return false for below version 17', () => { + expect(isVersionBeyond17('16.14.0')).toBe(false); + expect(isVersionBeyond17('16.8.0-alpha-1')).toBe(false); + expect(isVersionBeyond17('^16.0.0')).toBe(false); + expect(isVersionBeyond17('~15.0.0')).toBe(false); + }); +});