Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit 261cf26

Browse files
committed
Better function name in Error stacktrace.
Fixes #959. - Display <anonymous> for unnamed functions. - Call cleanFunctionName on it.
1 parent d613557 commit 261cf26

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

packages/devtools-reps/src/reps/error.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
isGrip,
1111
wrapRender,
1212
} = require("./rep-utils");
13+
const { cleanFunctionName } = require("./function");
1314
const { MODE } = require("./constants");
1415

1516
const dom = require("react-dom-factories");
@@ -91,11 +92,7 @@ function getStacktraceElements(preview) {
9192
const result = line.match(/^(.*)@(.*)$/);
9293
let functionName;
9394
let location;
94-
if (!result || result.length !== 3) {
95-
// This line did not match up nicely with the "function@location" pattern for
96-
// some reason.
97-
functionName = line;
98-
} else {
95+
if (result && result.length === 3) {
9996
functionName = result[1];
10097

10198
// If the resource was loaded by base-loader.js, the location looks like:
@@ -104,16 +101,18 @@ function getStacktraceElements(preview) {
104101
location = result[2].split(" -> ").pop();
105102
}
106103

107-
stack.push(
108-
span({
109-
key: "fn" + index,
110-
className: "objectBox-stackTrace-fn"
111-
}, functionName),
112-
span({
113-
key: "location" + index,
114-
className: "objectBox-stackTrace-location"
115-
}, ` (${location})`)
116-
);
104+
if (!functionName) {
105+
functionName = "<anonymous>";
106+
}
107+
108+
stack.push(span({
109+
key: "fn" + index,
110+
className: "objectBox-stackTrace-fn"
111+
}, cleanFunctionName(functionName)),
112+
span({
113+
key: "location" + index,
114+
className: "objectBox-stackTrace-location"
115+
}, ` (${location})`));
117116
});
118117

119118
return span({

packages/devtools-reps/src/reps/tests/__snapshots__/error.js.snap

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ exports[`Error - Eval error renders with expected text for an EvalError 1`] = `
1515
<span
1616
className="objectBox-stackTrace-fn"
1717
key="fn0"
18-
/>
18+
>
19+
&lt;anonymous&gt;
20+
</span>
1921
<span
2022
className="objectBox-stackTrace-location"
2123
key="location0"
@@ -41,7 +43,9 @@ exports[`Error - Internal error renders with expected text for an InternalError
4143
<span
4244
className="objectBox-stackTrace-fn"
4345
key="fn0"
44-
/>
46+
>
47+
&lt;anonymous&gt;
48+
</span>
4549
<span
4650
className="objectBox-stackTrace-location"
4751
key="location0"
@@ -91,7 +95,9 @@ exports[`Error - Multi line stack error renders with expected text for Error obj
9195
<span
9296
className="objectBox-stackTrace-fn"
9397
key="fn2"
94-
/>
98+
>
99+
&lt;anonymous&gt;
100+
</span>
95101
<span
96102
className="objectBox-stackTrace-location"
97103
key="location2"
@@ -117,7 +123,9 @@ exports[`Error - Range error renders with expected text for RangeError 1`] = `
117123
<span
118124
className="objectBox-stackTrace-fn"
119125
key="fn0"
120-
/>
126+
>
127+
&lt;anonymous&gt;
128+
</span>
121129
<span
122130
className="objectBox-stackTrace-location"
123131
key="location0"
@@ -143,7 +151,9 @@ exports[`Error - Reference error renders with expected text for ReferenceError 1
143151
<span
144152
className="objectBox-stackTrace-fn"
145153
key="fn0"
146-
/>
154+
>
155+
&lt;anonymous&gt;
156+
</span>
147157
<span
148158
className="objectBox-stackTrace-location"
149159
key="location0"
@@ -169,7 +179,9 @@ exports[`Error - Simple error renders with expected text for simple error 1`] =
169179
<span
170180
className="objectBox-stackTrace-fn"
171181
key="fn0"
172-
/>
182+
>
183+
&lt;anonymous&gt;
184+
</span>
173185
<span
174186
className="objectBox-stackTrace-location"
175187
key="location0"
@@ -195,7 +207,9 @@ exports[`Error - Syntax error renders with expected text for SyntaxError 1`] = `
195207
<span
196208
className="objectBox-stackTrace-fn"
197209
key="fn0"
198-
/>
210+
>
211+
&lt;anonymous&gt;
212+
</span>
199213
<span
200214
className="objectBox-stackTrace-location"
201215
key="location0"
@@ -221,7 +235,9 @@ exports[`Error - Type error renders with expected text for TypeError 1`] = `
221235
<span
222236
className="objectBox-stackTrace-fn"
223237
key="fn0"
224-
/>
238+
>
239+
&lt;anonymous&gt;
240+
</span>
225241
<span
226242
className="objectBox-stackTrace-location"
227243
key="location0"
@@ -247,7 +263,9 @@ exports[`Error - URI error renders with expected text for URIError 1`] = `
247263
<span
248264
className="objectBox-stackTrace-fn"
249265
key="fn0"
250-
/>
266+
>
267+
&lt;anonymous&gt;
268+
</span>
251269
<span
252270
className="objectBox-stackTrace-location"
253271
key="location0"
@@ -295,7 +313,7 @@ exports[`Error - base-loader.js renders as expected without mode 1`] = `
295313
className="objectBox-stackTrace-fn"
296314
key="fn1"
297315
>
298-
send/&lt;
316+
send
299317
</span>
300318
<span
301319
className="objectBox-stackTrace-location"
@@ -307,7 +325,7 @@ exports[`Error - base-loader.js renders as expected without mode 1`] = `
307325
className="objectBox-stackTrace-fn"
308326
key="fn2"
309327
>
310-
exports.makeInfallible/&lt;
328+
makeInfallible
311329
</span>
312330
<span
313331
className="objectBox-stackTrace-location"
@@ -319,7 +337,7 @@ exports[`Error - base-loader.js renders as expected without mode 1`] = `
319337
className="objectBox-stackTrace-fn"
320338
key="fn3"
321339
>
322-
exports.makeInfallible/&lt;
340+
makeInfallible
323341
</span>
324342
<span
325343
className="objectBox-stackTrace-location"

0 commit comments

Comments
 (0)