Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ses): Consistently name console methods #2654

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 34 additions & 27 deletions packages/ses/src/error/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
* @import {ErrorInfo, ErrorInfoKind, LogRecord, NoteCallback, LoggedErrorHandler, MakeCausalConsole, MakeLoggingConsoleKit} from "./internal-types.js";
*/

/**
* Explicitly set a function's name, supporting use of arrow functions for which
* source text doesn't include a name and no initial name is set by
* NamedEvaluation
* https://tc39.es/ecma262/multipage/syntax-directed-operations.html#sec-runtime-semantics-namedevaluation
* Instead, we hope that tooling uses only the explicit `name` property.
*
* @template {function} T

Check warning on line 38 in packages/ses/src/error/console.js

View workflow job for this annotation

GitHub Actions / lint

Invalid JSDoc @template "T" type "function"; prefer: "Function"
* @param {string} name
* @param {T} fn
* @returns {T}
*/
const defineName = (name, fn) => defineProperty(fn, 'name', { value: name });

// For our internal debugging purposes, uncomment
// const internalDebugConsole = console;

Expand Down Expand Up @@ -159,16 +173,12 @@

const loggingConsole = fromEntries(
arrayMap(consoleMethodPermits, ([name, _]) => {
// Use an arrow function so that it doesn't come with its own name in
// its printed form. Instead, we're hoping that tooling uses only
// the `.name` property set below.
/**
* @param {...any} args
*/
const method = (...args) => {
const method = defineName(name, (...args) => {
arrayPush(logArray, [name, ...args]);
};
defineProperty(method, 'name', { value: name });
});
return [name, freeze(method)];
}),
);
Expand Down Expand Up @@ -370,7 +380,7 @@
/**
* @param {...any} logArgs
*/
const levelMethod = (...logArgs) => {
const levelMethod = defineName(level, (...logArgs) => {
const subErrors = [];
const argTags = extractErrorArgs(logArgs, subErrors);
if (baseConsole[level]) {
Expand All @@ -379,8 +389,7 @@
}
// @ts-expect-error ConsoleProp vs LogSeverity mismatch
logSubErrors(level, subErrors);
};
defineProperty(levelMethod, 'name', { value: level });
});
return [level, freeze(levelMethod)];
});
const otherMethodNames = arrayFilter(
Expand All @@ -391,13 +400,12 @@
/**
* @param {...any} args
*/
const otherMethod = (...args) => {
const otherMethod = defineName(name, (...args) => {
// @ts-ignore
// eslint-disable-next-line @endo/no-polymorphic-call
baseConsole[name](...args);
return undefined;
};
defineProperty(otherMethod, 'name', { value: name });
});
return [name, freeze(otherMethod)];
});

Expand Down Expand Up @@ -458,23 +466,21 @@
}
return tlogger(...args);
};
const makeNamed = (name, fn) =>
({ [name]: (...args) => fn(...args) })[name];

const baseConsole = fromEntries([
...arrayMap(consoleLevelMethods, ([name]) => [
name,
makeNamed(name, logWithIndent),
defineName(name, (...args) => logWithIndent(...args)),
]),
...arrayMap(consoleOtherMethods, ([name]) => [
name,
makeNamed(name, (...args) => logWithIndent(name, ...args)),
defineName(name, (...args) => logWithIndent(name, ...args)),
]),
]);
// https://console.spec.whatwg.org/#grouping
for (const name of ['group', 'groupCollapsed']) {
if (baseConsole[name]) {
baseConsole[name] = makeNamed(name, (...args) => {
baseConsole[name] = defineName(name, (...args) => {
if (args.length >= 1) {
// Prefix the logged data with "group" or "groupCollapsed".
logWithIndent(...args);
Expand All @@ -484,16 +490,17 @@
arrayPush(indents, ' ');
});
} else {
baseConsole[name] = () => {};
baseConsole[name] = defineName(name, () => {});
}
}
if (baseConsole.groupEnd) {
baseConsole.groupEnd = makeNamed('groupEnd', (...args) => {
arrayPop(indents);
});
} else {
baseConsole.groupEnd = () => {};
}
baseConsole.groupEnd = defineName(
'groupEnd',
baseConsole.groupEnd
? (...args) => {
arrayPop(indents);
}
: () => {},
);
harden(baseConsole);
const causalConsole = makeCausalConsole(
/** @type {VirtualConsole} */ (baseConsole),
Expand All @@ -518,14 +525,14 @@
/**
* @param {...any} args
*/
const method = (...args) => {
const method = defineName(name, (...args) => {
// eslint-disable-next-line @endo/no-polymorphic-call
if (severity === undefined || filter.canLog(severity)) {
// @ts-ignore
// eslint-disable-next-line @endo/no-polymorphic-call
baseConsole[name](...args);
}
};
});
return [name, freeze(method)];
});
const filteringConsole = fromEntries(methods);
Expand Down
Loading