Skip to content

Commit

Permalink
Merge pull request #1662 from endojs/markm-burn-more-forEach
Browse files Browse the repository at this point in the history
refactor: convert some forEach to for/of loops
  • Loading branch information
erights authored Jun 30, 2023
2 parents 3dac711 + 09cabb3 commit 5ede1b3
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 47 deletions.
8 changes: 6 additions & 2 deletions packages/captp/src/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ export const makeCapTP = (
const send = obj => {
sendStats[obj.type] = (sendStats[obj.type] || 0) + 1;

WELL_KNOWN_SLOT_PROPERTIES.forEach(prop => sendSlot.add(obj[prop]));
for (const prop of WELL_KNOWN_SLOT_PROPERTIES) {
sendSlot.add(obj[prop]);
}
sendSlot.commit();

// Don't throw here if unplugged, just don't send.
Expand Down Expand Up @@ -768,7 +770,9 @@ export const makeCapTP = (
return false;
}

WELL_KNOWN_SLOT_PROPERTIES.forEach(prop => recvSlot.add(obj[prop]));
for (const prop of WELL_KNOWN_SLOT_PROPERTIES) {
recvSlot.add(obj[prop]);
}
fn(obj);
recvSlot.commit();

Expand Down
4 changes: 2 additions & 2 deletions packages/compartment-mapper/src/node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,9 @@ const graphPackage = async (
}
assign(allDependencies, bundleDependencies);
assign(allDependencies, optionalDependencies);
Object.keys(optionalDependencies).forEach(name => {
for (const name of Object.keys(optionalDependencies)) {
optionals.add(name);
});
}
if (dev) {
assign(allDependencies, devDependencies);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ export const wrap = ({
// the lexer.
if (exportsHaveBeenOverwritten) {
moduleEnvironmentRecord.default = finalExports;
keys(moduleEnvironmentRecord.default || {}).forEach(prop => {
for (const prop of keys(moduleEnvironmentRecord.default || {})) {
if (prop !== 'default')
moduleEnvironmentRecord[prop] = moduleEnvironmentRecord.default[prop];
});
}
}
};

Expand Down
4 changes: 2 additions & 2 deletions packages/compartment-mapper/src/policy-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export const assertPolicy = allegedPolicy => {

assertPackagePolicy(entry, `policy.entry`);

entries(resources).forEach(([key, value]) => {
for (const [key, value] of entries(resources)) {
assertPackagePolicy(value, `policy.resources["${key}"]`);
});
}
};
4 changes: 2 additions & 2 deletions packages/compartment-mapper/test/test-policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ const recursiveEdit = editor => originalPolicy => {
const policyToAlter = JSON.parse(JSON.stringify(originalPolicy));
const recur = obj => {
if (typeof obj === 'object') {
Object.keys(obj).forEach(key => {
for (const key of Object.keys(obj)) {
editor(key, obj);
recur(obj[key]);
});
}
}
return obj;
};
Expand Down
6 changes: 3 additions & 3 deletions packages/eventual-send/test/test-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import { E } from './get-hp.js';
* Mock a Remotable maker.
*
* @template L,R
* @param {string} [_iface='Remotable']
* @param {L} [props={}]
* @param {R} [remoteMethods={}]
* @param {string} [_iface]
* @param {L} [props]
* @param {R} [remoteMethods]
* @returns {L & R & RemotableBrand<L, R>}
*/
const Remotable = (
Expand Down
10 changes: 6 additions & 4 deletions packages/init/src/node-async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ const setAsyncSymbol = (description, symbol) => {
// is returned to the program and would normally be frozen.
const findAsyncSymbolsFromAsyncResource = () => {
let found = 0;
Object.getOwnPropertySymbols(new AsyncResource('Bootstrap')).forEach(sym => {
for (const sym of Object.getOwnPropertySymbols(
new AsyncResource('Bootstrap'),
)) {
const { description } = sym;
if (description && description in asyncHooksSymbols) {
if (setAsyncSymbol(description, sym)) {
found += 1;
}
}
});
}
return found;
};

Expand Down Expand Up @@ -110,7 +112,7 @@ const findAsyncSymbolsFromPromiseCreateHook = () => {
// if (length !== 3) {
// process._rawDebug(`Found ${length} symbols on promise:`, ...symbols);
// }
symbols.forEach(symbol => {
for (const symbol of symbols) {
const value = resource[symbol];
let type;
if (value === asyncId) {
Expand All @@ -127,7 +129,7 @@ const findAsyncSymbolsFromPromiseCreateHook = () => {
if (setAsyncSymbol(type, symbol)) {
found += 1;
}
});
}
return found;
} else {
// This node version is not mutating promises
Expand Down
8 changes: 4 additions & 4 deletions packages/pass-style/src/make-far.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const assertCanBeRemotable = candidate =>
* // https://github.com/Agoric/agoric-sdk/issues/804
*
* @template {{}} T
* @param {InterfaceSpec} [iface='Remotable'] The interface specification for
* @param {InterfaceSpec} [iface] The interface specification for
* the remotable. For now, a string iface must be "Remotable" or begin with
* "Alleged: " or "DebugName: ", to serve as the alleged name. More
* general ifaces are not yet implemented. This is temporary. We include the
Expand All @@ -71,9 +71,9 @@ const assertCanBeRemotable = candidate =>
* Currently, Alice can tell Bob about Carol, where VatA (on Alice's behalf)
* misrepresents Carol's `iface`. VatB and therefore Bob will then see
* Carol's `iface` as misrepresented by VatA.
* @param {undefined} [props=undefined] Currently may only be undefined.
* @param {undefined} [props] Currently may only be undefined.
* That plan is that own-properties are copied to the remotable
* @param {T} [remotable={}] The object used as the remotable
* @param {T} [remotable] The object used as the remotable
* @returns {T & RemotableBrand<{}, T>} remotable, modified for debuggability
*/
export const Remotable = (
Expand Down Expand Up @@ -134,7 +134,7 @@ harden(Remotable);
* @template {{}} T
* @param {string} farName This name will be prepended with `Alleged: `
* for now to form the `Remotable` `iface` argument.
* @param {T} [remotable={}] The object used as the remotable
* @param {T} [remotable] The object used as the remotable
*/
export const Far = (farName, remotable = undefined) => {
const r = remotable === undefined ? /** @type {T} */ ({}) : remotable;
Expand Down
4 changes: 2 additions & 2 deletions packages/ses/test/check-intrinsics.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* @param {object} intrinsics
*/
export function checkIntrinsics(intrinsics) {
Object.keys(intrinsics).forEach(name => {
for (const name of Object.keys(intrinsics)) {
if (intrinsics[name] === undefined) {
throw TypeError(`Malformed intrinsic: ${name}`);
}
});
}
}
24 changes: 12 additions & 12 deletions packages/static-module-record/src/babelPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ function makeModulePlugins(options) {
* @param {string} name - the local name of the exported variable.
*/
const markLiveExport = name => {
topLevelExported[name].forEach(importTo => {
for (const importTo of topLevelExported[name]) {
liveExportMap[importTo] = [name, true];
});
}
return hLiveId;
};

Expand All @@ -141,9 +141,9 @@ function makeModulePlugins(options) {
* @param {string} name - the local name of the exported variable.
*/
const markFixedExport = name => {
topLevelExported[name].forEach(importTo => {
for (const importTo of topLevelExported[name]) {
fixedExportMap[importTo] = [name];
});
}
return hOnceId;
};

Expand Down Expand Up @@ -326,7 +326,7 @@ function makeModulePlugins(options) {
if (!specs) {
return;
}
specs.forEach(spec => {
for (const spec of specs) {
const importTo = spec.local.name;
importDecls.push(importTo);
let importFrom;
Expand Down Expand Up @@ -364,7 +364,7 @@ function makeModulePlugins(options) {
);
updaterSources[importTo] = myUpdaterSources;
}
});
}
}
if (doTransform) {
// Nullify the import declaration.
Expand Down Expand Up @@ -471,9 +471,9 @@ function makeModulePlugins(options) {
collectPatternIdentifiers(path, id),
);
if (doAnalyze) {
vids.forEach(({ name }) => {
for (const { name } of vids) {
topLevelIsOnce[name] = path.scope.getBinding(name).constant;
});
}
}
if (doTransform) {
for (const { name } of vids) {
Expand Down Expand Up @@ -530,17 +530,17 @@ function makeModulePlugins(options) {
const vids = declarations.flatMap(({ id }) =>
collectPatternIdentifiers(path, id),
);
vids.forEach(({ name }) => {
for (const { name } of vids) {
let tle = topLevelExported[name];
if (!tle) {
tle = [];
topLevelExported[name] = tle;
}
tle.push(name);
});
}
}

specs.forEach(spec => {
for (const spec of specs) {
const { local, exported } = spec;
const importFrom =
spec.type === 'ExportNamespaceSpecifier' ? '*' : local.name;
Expand Down Expand Up @@ -594,7 +594,7 @@ function makeModulePlugins(options) {
}
tle.push(importTo);
}
});
}
}
if (doTransform) {
path.replaceWithMultiple(decl ? [replace(path.node, decl)] : []);
Expand Down
24 changes: 12 additions & 12 deletions packages/static-module-record/test/fixtures/large.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ function makeModulePlugins(options) {
* @param {string} name - the local name of the exported variable.
*/
const markLiveExport = name => {
topLevelExported[name].forEach(importTo => {
for (const importTo of (topLevelExported[name])) {
liveExportMap[importTo] = [name, true];
});
}
return hLiveId;
};

Expand All @@ -143,9 +143,9 @@ function makeModulePlugins(options) {
* @param {string} name - the local name of the exported variable.
*/
const markFixedExport = name => {
topLevelExported[name].forEach(importTo => {
for (const importTo of (topLevelExported[name])) {
fixedExportMap[importTo] = [name];
});
}
return hOnceId;
};

Expand Down Expand Up @@ -366,7 +366,7 @@ function makeModulePlugins(options) {
if (!specs) {
return;
}
specs.forEach(spec => {
for (const spec of specs) {
const importTo = spec.local.name;
importDecls.push(importTo);
let importFrom;
Expand Down Expand Up @@ -404,7 +404,7 @@ function makeModulePlugins(options) {
);
updaterSources[importTo] = myUpdaterSources;
}
});
}
}
if (doTransform) {
// Nullify the import declaration.
Expand Down Expand Up @@ -511,9 +511,9 @@ function makeModulePlugins(options) {
collectPatternIdentifiers(path, id),
);
if (doAnalyze) {
vids.forEach(({ name }) => {
for (const { name } of vids) {
topLevelIsOnce[name] = path.scope.getBinding(name).constant;
});
}
}
if (doTransform) {
for (const { name } of vids) {
Expand Down Expand Up @@ -570,17 +570,17 @@ function makeModulePlugins(options) {
const vids = declarations.flatMap(({ id }) =>
collectPatternIdentifiers(path, id),
);
vids.forEach(({ name }) => {
for (const { name } of vids) {
let tle = topLevelExported[name];
if (!tle) {
tle = [];
topLevelExported[name] = tle;
}
tle.push(name);
});
}
}

specs.forEach(spec => {
for (const spec of specs) {
const { local, exported } = spec;
const importFrom =
spec.type === 'ExportNamespaceSpecifier' ? '*' : local.name;
Expand Down Expand Up @@ -618,7 +618,7 @@ function makeModulePlugins(options) {
}
tle.push(importTo);
}
});
}
}
if (doTransform) {
path.replaceWithMultiple(decl ? [replace(path.node, decl)] : []);
Expand Down

0 comments on commit 5ede1b3

Please sign in to comment.