Skip to content

Commit

Permalink
Avoid fancy stack traces size computation (#14930)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 14, 2022
1 parent d0a923f commit 028a7fe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
27 changes: 13 additions & 14 deletions packages/babel-core/src/errors/rewrite-stack-trace.ts
Expand Up @@ -46,15 +46,6 @@ const ErrorToString = Function.call.bind(Error.prototype.toString);

const SUPPORTED = !!Error.captureStackTrace;

// We add some extra frames to Error.stackTraceLimit, so that we can respect
// the original Error.stackTraceLimit even after removing all our internal
// frames.
// STACK_TRACE_LIMIT_DELTA should be bigger than the expected number of internal
// frames, but not too big because capturing the stack trace is slow (this is
// why Error.stackTraceLimit does not default to Infinity!).
// Increase it if needed.
const STACK_TRACE_LIMIT_DELTA = 100;

const START_HIDNG = "startHiding - secret - don't use this - v1";
const STOP_HIDNG = "stopHiding - secret - don't use this - v1";

Expand Down Expand Up @@ -131,7 +122,18 @@ function setupPrepareStackTrace() {

const { prepareStackTrace = defaultPrepareStackTrace } = Error;

Error.stackTraceLimit += STACK_TRACE_LIMIT_DELTA;
// We add some extra frames to Error.stackTraceLimit, so that we can
// always show some useful frames even after deleting ours.
// STACK_TRACE_LIMIT_DELTA should be around the maximum expected number
// of internal frames, and not too big because capturing the stack trace
// is slow (this is why Error.stackTraceLimit does not default to Infinity!).
// Increase it if needed.
// However, we only do it if the user did not explicitly set it to 0.
const MIN_STACK_TRACE_LIMIT = 50;
Error.stackTraceLimit &&= Math.max(
Error.stackTraceLimit,
MIN_STACK_TRACE_LIMIT,
);

Error.prepareStackTrace = function stackTraceRewriter(err, trace) {
let newTrace = [];
Expand Down Expand Up @@ -160,10 +162,7 @@ function setupPrepareStackTrace() {
}
}

return prepareStackTrace(
err,
newTrace.slice(0, Error.stackTraceLimit - STACK_TRACE_LIMIT_DELTA),
);
return prepareStackTrace(err, newTrace);
};
}

Expand Down
7 changes: 0 additions & 7 deletions packages/babel-core/test/errors-stacks.js
Expand Up @@ -76,13 +76,6 @@ const fixture = name =>
);

describe("@babel/core errors", function () {
beforeAll(() => {
Error.stackTraceLimit += 100;
});
afterAll(() => {
Error.stackTraceLimit -= 100;
});

it("error inside config function", function () {
expectError(() => {
babel.parseSync("foo;", {
Expand Down

0 comments on commit 028a7fe

Please sign in to comment.