Skip to content

Commit

Permalink
fix(plugin-swc): should verify the version correctly (#3364)
Browse files Browse the repository at this point in the history
Co-authored-by: neverland <[email protected]>
  • Loading branch information
yimingjfe and chenjiahan authored Sep 3, 2024
1 parent 8b7195a commit 51f1b1c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/compat/plugin-swc/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });

Expand All @@ -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);
};

/**
Expand Down
19 changes: 19 additions & 0 deletions packages/compat/plugin-swc/tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 51f1b1c

Please sign in to comment.