Skip to content

Commit 028a7fe

Browse files
Avoid fancy stack traces size computation (#14930)
1 parent d0a923f commit 028a7fe

2 files changed

Lines changed: 13 additions & 21 deletions

File tree

packages/babel-core/src/errors/rewrite-stack-trace.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,6 @@ const ErrorToString = Function.call.bind(Error.prototype.toString);
4646

4747
const SUPPORTED = !!Error.captureStackTrace;
4848

49-
// We add some extra frames to Error.stackTraceLimit, so that we can respect
50-
// the original Error.stackTraceLimit even after removing all our internal
51-
// frames.
52-
// STACK_TRACE_LIMIT_DELTA should be bigger than the expected number of internal
53-
// frames, but not too big because capturing the stack trace is slow (this is
54-
// why Error.stackTraceLimit does not default to Infinity!).
55-
// Increase it if needed.
56-
const STACK_TRACE_LIMIT_DELTA = 100;
57-
5849
const START_HIDNG = "startHiding - secret - don't use this - v1";
5950
const STOP_HIDNG = "stopHiding - secret - don't use this - v1";
6051

@@ -131,7 +122,18 @@ function setupPrepareStackTrace() {
131122

132123
const { prepareStackTrace = defaultPrepareStackTrace } = Error;
133124

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

136138
Error.prepareStackTrace = function stackTraceRewriter(err, trace) {
137139
let newTrace = [];
@@ -160,10 +162,7 @@ function setupPrepareStackTrace() {
160162
}
161163
}
162164

163-
return prepareStackTrace(
164-
err,
165-
newTrace.slice(0, Error.stackTraceLimit - STACK_TRACE_LIMIT_DELTA),
166-
);
165+
return prepareStackTrace(err, newTrace);
167166
};
168167
}
169168

packages/babel-core/test/errors-stacks.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ const fixture = name =>
7676
);
7777

7878
describe("@babel/core errors", function () {
79-
beforeAll(() => {
80-
Error.stackTraceLimit += 100;
81-
});
82-
afterAll(() => {
83-
Error.stackTraceLimit -= 100;
84-
});
85-
8679
it("error inside config function", function () {
8780
expectError(() => {
8881
babel.parseSync("foo;", {

0 commit comments

Comments
 (0)