Reduce/enforce complexity in compartment-shim
MODIFY
packages/compartment-shim/.eslintrc.json
packages/compartment-shim/.eslintrc.json
"es6": true
},
"rules": {
+ "complexity": ["error", 8],
"implicit-arrow-linebreak": "off",
"function-paren-newline": "off",
"arrow-parens": "off",

MODIFY
packages/compartment-shim/src/main.js
packages/compartment-shim/src/main.js
@@ -7,8 +7,6 @@ import { getCurrentRealmRec } from './realmRec.js';
* Compartment()
* The Compartment constructor is a global. A host that wants to execute
* code in a context bound to a new global creates a new compartment.
- * The options are:
- * "endowments": a dictionary of globals to make available in the evaluator.
*/
const privateFields = new WeakMap();

MODIFY
packages/compartment-shim/src/scopeConstants.js
packages/compartment-shim/src/scopeConstants.js
import {
arrayIncludes,
- arrayPush,
getOwnPropertyDescriptor,
getOwnPropertyNames,
objectHasOwnProperty,
@@ -148,31 +147,22 @@ export function getScopeConstants(globalObject, localObject = {}) {
const globalNames = getOwnPropertyNames(globalObject);
const localNames = getOwnPropertyNames(localObject);
- const constants = [];
-
// Collect all valid & immutable identifiers from the endowments.
- for (const name of localNames) {
- if (
- isValidIdentifierName(name) &&
- isImmutableDataProperty(localObject, name)
- ) {
- arrayPush(constants, name);
- }
- }
+ const localConstants = localNames.filter(
+ name =>
+ isValidIdentifierName(name) && isImmutableDataProperty(localObject, name),
+ );
// Collect all valid & immutable identifiers from the global that
// are also not present in the endwoments (immutable or not).
- for (const name of globalNames) {
- if (
+ const globalConstants = globalNames.filter(
+ name =>
// Can't define a constant: it would prevent a
// lookup on the endowments.
!localNames.includes(name) &&
isValidIdentifierName(name) &&
- isImmutableDataProperty(globalObject, name)
- ) {
- arrayPush(constants, name);
- }
- }
+ isImmutableDataProperty(globalObject, name),
+ );
- return constants;
+ return [...globalConstants, ...localConstants];
}

MODIFY
packages/compartment-shim/test/unit/globalObject.test.js
packages/compartment-shim/test/unit/globalObject.test.js
@@ -30,9 +30,8 @@ test('globalObject', t => {
t.equals(Object.getOwnPropertyNames(globalObject).length, 6);
- for (const name of Object.getOwnPropertyNames(globalObject)) {
- const desc = Object.getOwnPropertyDescriptor(globalObject, name);
-
+ const descs = Object.getOwnPropertyDescriptors(globalObject);
+ for (const [name, desc] of Object.entries(descs)) {
if (name === 'Infinity') {
// eslint-disable-next-line no-restricted-globals
t.ok(!isFinite(desc.value), `${name} should be Infinity`);

Reduce/enforce complexity in compartment-shim