Skip to content

Commit

Permalink
Merge pull request #1941 from endojs/markm-test-cleanup-diagnostics
Browse files Browse the repository at this point in the history
refactor(ses): refactor console init and test cleanup
  • Loading branch information
erights authored Jan 10, 2024
2 parents 91b0ac9 + fd0c811 commit ce62087
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
55 changes: 28 additions & 27 deletions packages/ses/src/error/tame-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,6 @@ const failFast = message => {
const wrapLogger = (logger, thisArg) =>
freeze((...args) => apply(logger, thisArg, args));

// eslint-disable-next-line no-restricted-globals
const originalConsole = /** @type {VirtualConsole} */ (
// eslint-disable-next-line no-nested-ternary
typeof console !== 'undefined'
? console
: typeof print === 'function'
? // Make a good-enough console for eshost (including only functions that
// log at a specific level with no special argument interpretation).
// https://console.spec.whatwg.org/#logging
(p => freeze({ debug: p, log: p, info: p, warn: p, error: p }))(
// eslint-disable-next-line no-undef
wrapLogger(print),
)
: undefined
);

// Upgrade a log-only console (as in `eshost -h SpiderMonkey`).
if (originalConsole && originalConsole.log) {
for (const methodName of ['warn', 'error']) {
if (!originalConsole[methodName]) {
defineProperty(originalConsole, methodName, {
value: wrapLogger(originalConsole.log, originalConsole),
});
}
}
}

/**
* Wrap console unless suppressed.
* At the moment, the console is considered a host power in the start
Expand Down Expand Up @@ -78,6 +51,34 @@ export const tameConsole = (
getStackString: optGetStackString,
};
}

// eslint-disable-next-line no-restricted-globals
const originalConsole = /** @type {VirtualConsole} */ (
// eslint-disable-next-line no-nested-ternary
typeof globalThis.console !== 'undefined'
? globalThis.console
: typeof globalThis.print === 'function'
? // Make a good-enough console for eshost (including only functions that
// log at a specific level with no special argument interpretation).
// https://console.spec.whatwg.org/#logging
(p => freeze({ debug: p, log: p, info: p, warn: p, error: p }))(
// eslint-disable-next-line no-undef
wrapLogger(globalThis.print),
)
: undefined
);

// Upgrade a log-only console (as in `eshost -h SpiderMonkey`).
if (originalConsole && originalConsole.log) {
for (const methodName of ['warn', 'error']) {
if (!originalConsole[methodName]) {
defineProperty(originalConsole, methodName, {
value: wrapLogger(originalConsole.log, originalConsole),
});
}
}
}

const ourConsole = /** @type {VirtualConsole} */ (
consoleTaming === 'unsafe'
? originalConsole
Expand Down
37 changes: 37 additions & 0 deletions packages/ses/test/error/test-permit-removal-warnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import test from 'ava';
import '../../index.js';
import { assertLogs } from './throws-and-logs.js';
// import { whitelistIntrinsics } from '../../src/permits-intrinsics.js';

const { defineProperties } = Object;
const { apply } = Reflect;

const originalIsArray = Array.isArray;

defineProperties(Array, {
extraRemovableDataProperty: {
value: 'extra removable data property',
configurable: true,
},
isArray: {
value: function isArrayWithCleanablePrototype(...args) {
return apply(originalIsArray, this, args);
},
},
});

test('permit removal warnings', t => {
assertLogs(
t,
() => lockdown(),
[
['warn', 'Removing intrinsics.Array.isArray.prototype'],
[
'warn',
'Tolerating undeletable intrinsics.Array.isArray.prototype === undefined',
],
['warn', 'Removing intrinsics.Array.extraRemovableDataProperty'],
],
{},
);
});

0 comments on commit ce62087

Please sign in to comment.