From fdacf5264e8b72149dbc2e9c1fa802b4c5ed6b5f Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Fri, 20 Dec 2024 15:37:06 -0800 Subject: [PATCH] fix(compartment-mapper)!: invert keys/values of renames in captureFromMap BREAKING CHANGE: The `CaptureResult.compartmentRenames` record returned by `captureFromMap()` is now _new to old_; previously it was _old to new_. This more accurately solves for the use case of `compartmentRenames`, because the main use case demands the record is inverted. We can plainly see this issue in `captureFromMap`'s test suite (which this change simplifies). In a future change, [branded types](https://www.learningtypescript.com/articles/branded-types) could describe the distinction between key and value, which are currently both `string`. > [!IMPORTANT] > > The only consumer of `captureFromMap` that I'm aware of is _me_, and _me_ doesn't need this to be SemVer major. --- packages/compartment-mapper/src/capture-lite.js | 7 ++++++- packages/compartment-mapper/src/types/external.ts | 11 +++++++++++ .../compartment-mapper/test/capture-lite.test.js | 13 +++---------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/packages/compartment-mapper/src/capture-lite.js b/packages/compartment-mapper/src/capture-lite.js index 20da5b58b0..30a8d8c068 100644 --- a/packages/compartment-mapper/src/capture-lite.js +++ b/packages/compartment-mapper/src/capture-lite.js @@ -250,7 +250,12 @@ const captureCompartmentMap = (compartmentMap, sources) => { return { captureCompartmentMap, captureSources, - compartmentRenames, + compartmentRenames: fromEntries( + entries(compartmentRenames).map(([oldName, newName]) => [ + newName, + oldName, + ]), + ), }; }; diff --git a/packages/compartment-mapper/src/types/external.ts b/packages/compartment-mapper/src/types/external.ts index c4568d11e3..61b7efd3d9 100644 --- a/packages/compartment-mapper/src/types/external.ts +++ b/packages/compartment-mapper/src/types/external.ts @@ -233,8 +233,19 @@ type LinkingOptions = ParserForLanguageOption & * The result of `captureFromMap`. */ export type CaptureResult = { + /** + * Normalized `CompartmentMapDescriptor` + */ captureCompartmentMap: CompartmentMapDescriptor; + /** + * Sources found in the `CompartmentMapDescriptor` + */ captureSources: Sources; + + /** + * A record of renamed {@link CompartmentDescriptor CompartmentDescriptors} + * from _new_ to _original_ name + */ compartmentRenames: Record; }; diff --git a/packages/compartment-mapper/test/capture-lite.test.js b/packages/compartment-mapper/test/capture-lite.test.js index 75d4180e83..90a8543c0f 100644 --- a/packages/compartment-mapper/test/capture-lite.test.js +++ b/packages/compartment-mapper/test/capture-lite.test.js @@ -8,7 +8,7 @@ import { mapNodeModules } from '../src/node-modules.js'; import { makeReadPowers } from '../src/node-powers.js'; import { defaultParserForLanguage } from '../src/import-parsers.js'; -const { keys, entries, fromEntries } = Object; +const { keys } = Object; test('captureFromMap() should resolve with a CaptureResult', async t => { t.plan(5); @@ -26,10 +26,6 @@ test('captureFromMap() should resolve with a CaptureResult', async t => { parserForLanguage: defaultParserForLanguage, }); - const renames = fromEntries( - entries(compartmentRenames).map(([filepath, id]) => [id, filepath]), - ); - t.deepEqual( keys(captureSources).sort(), ['bundle', 'bundle-dep-v0.0.0'], @@ -37,7 +33,7 @@ test('captureFromMap() should resolve with a CaptureResult', async t => { ); t.deepEqual( - keys(renames).sort(), + keys(compartmentRenames).sort(), ['bundle', 'bundle-dep-v0.0.0'], 'compartmentRenames must contain same compartment names as in captureCompartmentMap', ); @@ -82,14 +78,11 @@ test('captureFromMap() should round-trip sources based on parsers', async t => { }, ); - const renames = fromEntries( - entries(compartmentRenames).map(([filepath, id]) => [id, filepath]), - ); const decoder = new TextDecoder(); // the actual source depends on the value of `parserForLanguage` above const actual = decoder.decode(captureSources.bundle['./icando.cjs'].bytes); const expected = await fs.promises.readFile( - path.join(url.fileURLToPath(renames.bundle), 'icando.cjs'), + path.join(url.fileURLToPath(compartmentRenames.bundle), 'icando.cjs'), 'utf-8', ); t.is(actual, expected, 'Source code should not be pre-compiled');