Skip to content

Commit 378a5aa

Browse files
committed
chore(compartment-mapper): use @endo/path-compare
This removes the code copied from `@endo/compartment-mapper` to create `@endo/path-compare` and uses `@endo/path-compare` instead. Ref: #2787
1 parent 695b26c commit 378a5aa

File tree

5 files changed

+6
-56
lines changed

5 files changed

+6
-56
lines changed

packages/compartment-mapper/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"dependencies": {
6363
"@endo/cjs-module-analyzer": "workspace:^",
6464
"@endo/module-source": "workspace:^",
65+
"@endo/path-compare": "workspace:^",
6566
"@endo/trampoline": "workspace:^",
6667
"@endo/zip": "workspace:^",
6768
"ses": "workspace:^"

packages/compartment-mapper/src/compartment-map.js

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,6 @@ const q = JSON.stringify;
1414
// eslint-disable-next-line no-nested-ternary
1515
export const stringCompare = (a, b) => (a === b ? 0 : a < b ? -1 : 1);
1616

17-
/**
18-
* @param {number} length
19-
* @param {string} term
20-
*/
21-
const cumulativeLength = (length, term) => {
22-
return length + term.length;
23-
};
24-
25-
/**
26-
* @param {Array<string> | undefined} a
27-
* @param {Array<string> | undefined} b
28-
*/
29-
export const pathCompare = (a, b) => {
30-
// Undefined is not preferred
31-
if (a === undefined && b === undefined) {
32-
return 0;
33-
}
34-
if (a === undefined) {
35-
return 1;
36-
}
37-
if (b === undefined) {
38-
return -1;
39-
}
40-
// Prefer the shortest dependency path.
41-
if (a.length !== b.length) {
42-
return a.length - b.length;
43-
}
44-
// Otherwise, favor the shortest cumulative length.
45-
const aSum = a.reduce(cumulativeLength, 0);
46-
const bSum = b.reduce(cumulativeLength, 0);
47-
if (aSum !== bSum) {
48-
return aSum - bSum;
49-
}
50-
// Otherwise, compare terms lexically.
51-
assert(a.length === b.length); // Reminder
52-
// This loop guarantees that if any pair of terms is different, including the
53-
// case where one is a prefix of the other, we will return a non-zero value.
54-
for (let i = 0; i < a.length; i += 1) {
55-
const comparison = stringCompare(a[i], b[i]);
56-
if (comparison !== 0) {
57-
return comparison;
58-
}
59-
}
60-
// If all pairs of terms are the same respective lengths, we are guaranteed
61-
// that they are exactly the same or one of them is lexically distinct and would
62-
// have already been caught.
63-
return 0;
64-
};
65-
6617
/**
6718
* @template T
6819
* @param {Iterable<T>} iterable

packages/compartment-mapper/src/digest.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
* } from './types.js'
1717
*/
1818

19-
import {
20-
assertCompartmentMap,
21-
pathCompare,
22-
stringCompare,
23-
} from './compartment-map.js';
19+
import { pathCompare } from '@endo/path-compare';
20+
import { assertCompartmentMap, stringCompare } from './compartment-map.js';
2421

2522
const { create, fromEntries, entries, keys } = Object;
2623

packages/compartment-mapper/src/node-modules.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* } from './types/node-modules.js'
4343
*/
4444

45-
import { pathCompare } from './compartment-map.js';
45+
import { pathCompare } from '@endo/path-compare';
4646
import { inferExportsAndAliases } from './infer-exports.js';
4747
import { parseLocatedJson } from './json.js';
4848
import { join } from './node-module-specifier.js';

yarn.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ __metadata:
426426
dependencies:
427427
"@endo/cjs-module-analyzer": "workspace:^"
428428
"@endo/module-source": "workspace:^"
429+
"@endo/path-compare": "workspace:^"
429430
"@endo/trampoline": "workspace:^"
430431
"@endo/zip": "workspace:^"
431432
ava: "npm:^6.1.3"
@@ -831,7 +832,7 @@ __metadata:
831832
languageName: unknown
832833
linkType: soft
833834

834-
"@endo/path-compare@workspace:packages/path-compare":
835+
"@endo/path-compare@workspace:^, @endo/path-compare@workspace:packages/path-compare":
835836
version: 0.0.0-use.local
836837
resolution: "@endo/path-compare@workspace:packages/path-compare"
837838
dependencies:

0 commit comments

Comments
 (0)