fix(compartment-mapper): Avoid some property override pitfalls
MODIFY
packages/compartment-mapper/src/archive.js
packages/compartment-mapper/src/archive.js
@@ -52,7 +52,7 @@ const { keys, entries, fromEntries } = Object;
*/
const renameCompartments = compartments => {
/** @type {Record<string, string>} */
- const renames = {};
+ const renames = Object.create(null);
let n = 0;
for (const [name, compartment] of entries(compartments)) {
const { label } = compartment;
@@ -68,14 +68,14 @@ const renameCompartments = compartments => {
* @param {Record<string, string>} renames
*/
const translateCompartmentMap = (compartments, sources, renames) => {
- const result = {};
+ const result = Object.create(null);
for (const compartmentName of keys(compartments).sort()) {
const compartment = compartments[compartmentName];
const { name, label } = compartment;
// rename module compartments
/** @type {Record<string, ModuleDescriptor>} */
- const modules = {};
+ const modules = Object.create(null);
const compartmentModules = compartment.modules;
if (compartment.modules) {
for (const name of keys(compartmentModules).sort()) {
@@ -223,7 +223,7 @@ const digestLocation = async (powers, moduleLocation, options) => {
} = compartmentMap;
/** @type {Sources} */
- const sources = {};
+ const sources = Object.create(null);
const makeImportHook = makeImportHookMaker(
read,

MODIFY
packages/compartment-mapper/src/bundle.js
packages/compartment-mapper/src/bundle.js
@@ -73,7 +73,7 @@ const sortedModules = (
imports = [],
reexports = [],
} = /** @type {PrecompiledStaticModuleInterface} */ (record);
- const resolvedImports = {};
+ const resolvedImports = Object.create(null);
for (const importSpecifier of [...imports, ...reexports]) {
const resolvedSpecifier = resolve(importSpecifier, moduleSpecifier);
resolvedImports[importSpecifier] = recur(
@@ -156,7 +156,7 @@ export const makeBundle = async (read, moduleLocation, options) => {
entry: { compartment: entryCompartmentName, module: entryModuleSpecifier },
} = compartmentMap;
/** @type {Sources} */
- const sources = {};
+ const sources = Object.create(null);
const makeImportHook = makeImportHookMaker(
read,
@@ -184,7 +184,7 @@ export const makeBundle = async (read, moduleLocation, options) => {
// Create an index of modules so we can resolve import specifiers to the
// index of the corresponding functor.
- const modulesByKey = {};
+ const modulesByKey = Object.create(null);
for (let index = 0; index < modules.length; index += 1) {
const module = modules[index];
module.index = index;

MODIFY
packages/compartment-mapper/src/compartment-map.js
packages/compartment-mapper/src/compartment-map.js
@@ -178,7 +178,7 @@ const assertModules = (allegedModules, path, url) => {
const modules = Object(allegedModules);
assert(
allegedModules === modules || !Array.isArray(modules),
- `modules must be an object, got ${allegedModules} in ${q(url)}`,
+ `modules must be an object, got ${q(allegedModules)} in ${q(url)}`,
);
for (const [key, value] of Object.entries(modules)) {
assertModule(value, `${path}.modules[${q(key)}]`, url);
@@ -259,7 +259,7 @@ const assertScopes = (allegedScopes, path, url) => {
const scopes = Object(allegedScopes);
assert(
allegedScopes === scopes && !Array.isArray(scopes),
- `${path}.scopes must be an object, got ${allegedScopes} in ${q(url)}`,
+ `${path}.scopes must be an object, got ${q(allegedScopes)} in ${q(url)}`,
);
for (const [key, value] of Object.entries(scopes)) {
@@ -367,7 +367,9 @@ const assertCompartments = (allegedCompartments, url) => {
const compartments = Object(allegedCompartments);
assert(
allegedCompartments === compartments || !Array.isArray(compartments),
- `compartments must be an object, got ${allegedCompartments} in ${q(url)}`,
+ `compartments must be an object, got ${q(allegedCompartments)} in ${q(
+ url,
+ )}`,
);
for (const [key, value] of Object.entries(compartments)) {
assertCompartment(value, `compartments[${q(key)}]`, url);

MODIFY
packages/compartment-mapper/src/import-hook.js
packages/compartment-mapper/src/import-hook.js
@@ -55,9 +55,9 @@ function getImportsFromRecord(record) {
export const makeImportHookMaker = (
read,
baseLocation,
- sources = {},
- compartments = {},
- exitModules = {},
+ sources = Object.create(null),
+ compartments = Object.create(null),
+ exitModules = Object.create(null),
computeSha512 = undefined,
) => {
// Set of specifiers for modules whose parser is not using heuristics to determine imports
@@ -72,9 +72,10 @@ export const makeImportHookMaker = (
) => {
// per-compartment:
packageLocation = resolveLocation(packageLocation, baseLocation);
- const packageSources = sources[packageLocation] || {};
+ const packageSources = sources[packageLocation] || Object.create(null);
sources[packageLocation] = packageSources;
- const { modules = {} } = compartments[packageLocation] || {};
+ const { modules = Object.create(null) } =
+ compartments[packageLocation] || {};
/**
* @param {string} specifier

MODIFY
packages/compartment-mapper/src/link.js
packages/compartment-mapper/src/link.js
@@ -328,19 +328,19 @@ export const link = (
const { compartment: entryCompartmentName } = entry;
/** @type {Record<string, Compartment>} */
- const compartments = {};
+ const compartments = Object.create(null);
/** @type {Record<string, ResolveHook>} */
- const resolvers = {};
+ const resolvers = Object.create(null);
for (const [compartmentName, compartmentDescriptor] of entries(
compartmentDescriptors,
)) {
const {
location,
name,
- modules = {},
- parsers: languageForExtension = {},
- types: languageForModuleSpecifier = {},
- scopes = {},
+ modules = Object.create(null),
+ parsers: languageForExtension = Object.create(null),
+ types: languageForModuleSpecifier = Object.create(null),
+ scopes = Object.create(null),
} = compartmentDescriptor;
// Capture the default.

MODIFY
packages/compartment-mapper/src/node-modules.js
packages/compartment-mapper/src/node-modules.js
@@ -404,7 +404,7 @@ const translateGraph = (
tags,
) => {
/** @type {Record<string, CompartmentDescriptor>} */
- const compartments = {};
+ const compartments = Object.create(null);
// For each package, build a map of all the external modules the package can
// import from other packages.
@@ -420,9 +420,9 @@ const translateGraph = (
packageLocation
];
/** @type {Record<string, ModuleDescriptor>} */
- const modules = {};
+ const modules = Object.create(null);
/** @type {Record<string, ScopeDescriptor>} */
- const scopes = {};
+ const scopes = Object.create(null);
/**
* @param {string} dependencyName
* @param {string} packageLocation

fix(compartment-mapper): Avoid some property override pitfalls