Skip to content

Commit 337ae03

Browse files
committed
Replace DevTools semver usages with a simpler inlined method
1 parent f0cf832 commit 337ae03

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

packages/react-devtools-shared/src/__tests__/utils-test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import {stackToComponentSources} from 'react-devtools-shared/src/devtools/utils'
1515
import {
1616
format,
1717
formatWithStyles,
18+
semvercmp,
19+
gt,
20+
gte,
1821
} from 'react-devtools-shared/src/backend/utils';
1922
import {
2023
REACT_SUSPENSE_LIST_TYPE as SuspenseList,
@@ -252,4 +255,52 @@ describe('utils', () => {
252255
]);
253256
});
254257
});
258+
259+
describe('semvercmp', () => {
260+
it('should sort semver comparisons ascending', () => {
261+
const unsortedVersions = [
262+
'1.2.3',
263+
'4.11.6',
264+
'4.2.0',
265+
'11.0.0-alpha.1',
266+
'1.5.19',
267+
'11.0.0-alpha.0',
268+
'1.5.5',
269+
'4.1.3',
270+
'2.3.1',
271+
'11.0.0',
272+
'10.5.5',
273+
'11.3.0',
274+
];
275+
276+
const sortedVersions = unsortedVersions.slice().sort(semvercmp);
277+
expect(sortedVersions).toEqual([
278+
'1.2.3',
279+
'1.5.5',
280+
'1.5.19',
281+
'2.3.1',
282+
'4.1.3',
283+
'4.2.0',
284+
'4.11.6',
285+
'10.5.5',
286+
// Specifically check non-numerical pre-release versions
287+
'11.0.0-alpha.0',
288+
'11.0.0-alpha.1',
289+
'11.0.0',
290+
'11.3.0',
291+
]);
292+
});
293+
294+
it('gte should compare versions correctly', () => {
295+
expect(gte('1.2.3', '1.2.1')).toBe(true);
296+
expect(gte('1.2.1', '1.2.1')).toBe(true);
297+
expect(gte('1.2.1', '1.2.2')).toBe(false);
298+
});
299+
300+
it('gt should compare versions correctly', () => {
301+
expect(gt('1.2.3', '1.2.1')).toBe(true);
302+
expect(gt('1.2.1', '1.2.1')).toBe(false);
303+
expect(gt('1.2.1', '1.2.2')).toBe(false);
304+
});
305+
});
255306
});

packages/react-devtools-shared/src/backend/renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* @flow
88
*/
99

10-
import {gt, gte} from 'semver';
1110
import {
1211
ComponentFilterDisplayName,
1312
ComponentFilterElementType,
@@ -39,6 +38,7 @@ import {
3938
utfEncodeString,
4039
} from 'react-devtools-shared/src/utils';
4140
import {sessionStorageGetItem} from 'react-devtools-shared/src/storage';
41+
import {gt, gte} from 'react-devtools-shared/src/backend/utils';
4242
import {
4343
cleanForBridge,
4444
copyToClipboard,

packages/react-devtools-shared/src/backend/utils.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,28 @@ export function isSynchronousXHRSupported(): boolean {
275275
window.document.featurePolicy.allowsFeature('sync-xhr')
276276
);
277277
}
278+
279+
// https://www.npmjs.com/package/semver-compare
280+
export function semvercmp(a: string = '', b: string = ''): number {
281+
const pa = a.split('.');
282+
const pb = b.split('.');
283+
// Handle more than 3 places, like '11.0.0-alpha.1'
284+
const maxLength = Math.max(pa.length, pb.length);
285+
for (let i = 0; i < maxLength; i++) {
286+
const na = +pa[i];
287+
const nb = +pb[i];
288+
if (na > nb) return 1;
289+
if (nb > na) return -1;
290+
if (!isNaN(na) && isNaN(nb)) return 1;
291+
if (isNaN(na) && !isNaN(nb)) return -1;
292+
}
293+
return 0;
294+
}
295+
296+
export function gt(a: string = '', b: string = ''): boolean {
297+
return semvercmp(a, b) === 1;
298+
}
299+
300+
export function gte(a: string = '', b: string = ''): boolean {
301+
return semvercmp(a, b) > -1;
302+
}

packages/react-devtools-shell/src/e2e-regression/app-legacy.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
import * as React from 'react';
66
import * as ReactDOM from 'react-dom';
7-
import {gte} from 'semver';
87
import ListApp from '../e2e-apps/ListApp';
98
import ListAppLegacy from '../e2e-apps/ListAppLegacy';
9+
import {gte} from 'react-devtools-shared/src/backend/utils';
10+
1011
const version = process.env.E2E_APP_REACT_VERSION;
1112

1213
function mountApp(App: () => React$Node) {

0 commit comments

Comments
 (0)