Skip to content

Commit 28e0050

Browse files
authored
fix: fix stack-tracking tailing new line issue (#13)
1 parent 961e97d commit 28e0050

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

src/utils/__tests__/stack-tracking.test.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { withStackTracking } from '../stack-tracking';
1+
import { parseStack, withStackTracking } from '../stack-tracking';
22

33
describe('withStackTracking', () => {
44
it('works correctly', () => {
@@ -33,3 +33,67 @@ describe('withStackTracking', () => {
3333
});
3434
});
3535
});
36+
37+
const stack0 = `Error:
38+
at /foo/bar/buz.js:12:34
39+
at /foo/bar/buz/qux.js:56:789`;
40+
41+
const stack1 = `Error:
42+
at Map.forEach (/foo/bar/buz.js:12:34)
43+
at Map.forEach (/foo/bar/buz/qux.js:56:789)`;
44+
45+
describe('parseStack', () => {
46+
it('parses stack text into CodeStack', () => {
47+
expect(parseStack(stack0, 0)).toEqual({
48+
composed: '/foo/bar/buz.js:12:34',
49+
filename: '/foo/bar/buz.js',
50+
line: 12,
51+
col: 34,
52+
});
53+
expect(parseStack(stack0, 1)).toEqual({
54+
composed: '/foo/bar/buz/qux.js:56:789',
55+
filename: '/foo/bar/buz/qux.js',
56+
line: 56,
57+
col: 789,
58+
});
59+
expect(parseStack(stack1, 0)).toEqual({
60+
composed: '/foo/bar/buz.js:12:34',
61+
filename: '/foo/bar/buz.js',
62+
line: 12,
63+
col: 34,
64+
});
65+
expect(parseStack(stack1, 1)).toEqual({
66+
composed: '/foo/bar/buz/qux.js:56:789',
67+
filename: '/foo/bar/buz/qux.js',
68+
line: 56,
69+
col: 789,
70+
});
71+
});
72+
73+
it('uses deepest stack if received too much depth', () => {
74+
expect(parseStack(stack0, 2)).toEqual({
75+
composed: '/foo/bar/buz/qux.js:56:789',
76+
filename: '/foo/bar/buz/qux.js',
77+
line: 56,
78+
col: 789,
79+
});
80+
expect(parseStack(stack0, 100)).toEqual({
81+
composed: '/foo/bar/buz/qux.js:56:789',
82+
filename: '/foo/bar/buz/qux.js',
83+
line: 56,
84+
col: 789,
85+
});
86+
expect(parseStack(stack1, 2)).toEqual({
87+
composed: '/foo/bar/buz/qux.js:56:789',
88+
filename: '/foo/bar/buz/qux.js',
89+
line: 56,
90+
col: 789,
91+
});
92+
expect(parseStack(stack1, 100)).toEqual({
93+
composed: '/foo/bar/buz/qux.js:56:789',
94+
filename: '/foo/bar/buz/qux.js',
95+
line: 56,
96+
col: 789,
97+
});
98+
});
99+
});

src/utils/stack-tracking.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ export interface CodeStack {
55
col: number;
66
}
77

8-
const parseStack = (stack: string, depth: number): CodeStack => {
9-
if (!stack) throw new Error('Invalid Stack Information: make sure to run with node');
8+
export const parseStack = (stack: string, depth: number): CodeStack => {
109
let cursor = 0;
1110
for (let i = depth + 1; stack && i > 0; i--) {
12-
cursor = stack.indexOf('\n', cursor) + 1;
11+
cursor = stack.indexOf('\n', cursor + 1);
1312
}
14-
// Ignore the case if the stack doesn't include any new line
13+
cursor = (cursor === -1 ? stack.lastIndexOf('\n') : cursor) + 8 /* '____at_' */;
1514
const composed = stack
16-
.slice(cursor + /* '____at_' ('_'=' ') */ 7, stack.indexOf('\n', cursor))
15+
.slice(cursor, (stack.indexOf('\n', cursor) + 1 || Infinity) - 1)
1716
.replace(STACK_PARENTHESES, '');
1817

1918
if (!stack) throw new Error('Invalid Stack Information: invalid depth');

0 commit comments

Comments
 (0)