mirror of
https://github.com/facebook/react.git
synced 2026-02-24 12:43:00 +00:00
This is a major nit but this avoids an extra stack frame when we're replaying logs. Normally the `printToConsole` frame doesn't show up because it'd be ignore listed. <img width="421" alt="Screenshot 2024-07-25 at 11 49 39 AM" src="https://github.com/user-attachments/assets/81334c2f-e19e-476a-871e-c4db9dee294e"> When you expand to show ignore listed frames a ton of other frames show up. <img width="516" alt="Screenshot 2024-07-25 at 11 49 47 AM" src="https://github.com/user-attachments/assets/2ab8bdfb-464c-408d-9176-ee2fabc114b6"> The annoying thing about this frame is that it's at the top of the stack where as typically framework stuff ends up at the bottom and something you can ignore. The user space stack comes first. With this fix there's no longer any `printToConsole` frame. <img width="590" alt="Screenshot 2024-07-25 at 12 09 09 PM" src="https://github.com/user-attachments/assets/b8365d53-31f3-43df-abce-172d608d3c9c"> Am I wiling to eat the added complexity and slightly slower performance for this nit? Definitely.
54 lines
1.3 KiB
JavaScript
54 lines
1.3 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
const badgeFormat = '[%s] ';
|
|
const pad = ' ';
|
|
|
|
const bind = Function.prototype.bind;
|
|
|
|
export function bindToConsole(
|
|
methodName: string,
|
|
args: Array<any>,
|
|
badgeName: string,
|
|
): () => any {
|
|
let offset = 0;
|
|
switch (methodName) {
|
|
case 'dir':
|
|
case 'dirxml':
|
|
case 'groupEnd':
|
|
case 'table': {
|
|
// These methods cannot be colorized because they don't take a formatting string.
|
|
// $FlowFixMe
|
|
return bind.apply(console[methodName], [console].concat(args)); // eslint-disable-line react-internal/no-production-logging
|
|
}
|
|
case 'assert': {
|
|
// assert takes formatting options as the second argument.
|
|
offset = 1;
|
|
}
|
|
}
|
|
|
|
const newArgs = args.slice(0);
|
|
if (typeof newArgs[offset] === 'string') {
|
|
newArgs.splice(
|
|
offset,
|
|
1,
|
|
badgeFormat + newArgs[offset],
|
|
pad + badgeName + pad,
|
|
);
|
|
} else {
|
|
newArgs.splice(offset, 0, badgeFormat, pad + badgeName + pad);
|
|
}
|
|
|
|
// The "this" binding in the "bind";
|
|
newArgs.unshift(console);
|
|
|
|
// $FlowFixMe
|
|
return bind.apply(console[methodName], newArgs); // eslint-disable-line react-internal/no-production-logging
|
|
}
|