Skip to content

Commit 8870389

Browse files
authored
ref: Extract source context + tests (#2376)
1 parent 5f95f58 commit 8870389

File tree

4 files changed

+101
-12
lines changed

4 files changed

+101
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- [utils] ref: Extract adding source context incl. tests
6+
57
## 5.11.0
68

79
- [apm] fix: Always attach `contexts.trace` to finished transaction (#2353)

packages/node/src/parsers.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Event, Exception, ExtendedError, StackFrame } from '@sentry/types';
2-
import { basename, dirname, snipLine, SyncPromise } from '@sentry/utils';
2+
import { addContextToFrame, basename, dirname, SyncPromise } from '@sentry/utils';
33
import { readFile } from 'fs';
44
import { LRUMap } from 'lru_map';
55

@@ -203,15 +203,7 @@ function addPrePostContext(
203203
try {
204204
const lines = (sourceFiles[frame.filename] as string).split('\n');
205205

206-
frame.pre_context = lines
207-
.slice(Math.max(0, (frame.lineno || 0) - (linesOfContext + 1)), (frame.lineno || 0) - 1)
208-
.map((line: string) => snipLine(line, 0));
209-
210-
frame.context_line = snipLine(lines[(frame.lineno || 0) - 1], frame.colno || 0);
211-
212-
frame.post_context = lines
213-
.slice(frame.lineno || 0, (frame.lineno || 0) + linesOfContext)
214-
.map((line: string) => snipLine(line, 0));
206+
addContextToFrame(lines, frame, linesOfContext);
215207
} catch (e) {
216208
// anomaly, being defensive in case
217209
// unlikely to ever happen in practice but can definitely happen in theory

packages/utils/src/misc.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Event, Integration, WrappedFunction } from '@sentry/types';
1+
import { Event, Integration, StackFrame, WrappedFunction } from '@sentry/types';
22

33
import { isString } from './is';
4+
import { snipLine } from './string';
45

56
/** Internal */
67
interface SentryGlobal {
@@ -426,3 +427,26 @@ export function getFunctionName(fn: unknown): string {
426427
return defaultFunctionName;
427428
}
428429
}
430+
431+
/**
432+
* This function adds context (pre/post/line) lines to the provided frame
433+
*
434+
* @param lines string[] containing all lines
435+
* @param frame StackFrame that will be mutated
436+
* @param linesOfContext number of context lines we want to add pre/post
437+
*/
438+
export function addContextToFrame(lines: string[], frame: StackFrame, linesOfContext: number = 5): void {
439+
const lineno = frame.lineno || 0;
440+
const maxLines = lines.length;
441+
const sourceLine = Math.max(Math.min(maxLines, lineno - 1), 0);
442+
443+
frame.pre_context = lines
444+
.slice(Math.max(0, sourceLine - linesOfContext), sourceLine)
445+
.map((line: string) => snipLine(line, 0));
446+
447+
frame.context_line = snipLine(lines[Math.min(maxLines - 1, sourceLine)], frame.colno || 0);
448+
449+
frame.post_context = lines
450+
.slice(Math.min(sourceLine + 1, maxLines), sourceLine + 1 + linesOfContext)
451+
.map((line: string) => snipLine(line, 0));
452+
}

packages/utils/test/misc.test.ts

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { getEventDescription, getGlobalObject, parseRetryAfterHeader } from '../src/misc';
1+
import { StackFrame } from '@sentry/types';
2+
3+
import { addContextToFrame, getEventDescription, getGlobalObject, parseRetryAfterHeader } from '../src/misc';
24

35
describe('getEventDescription()', () => {
46
test('message event', () => {
@@ -139,3 +141,72 @@ describe('parseRetryAfterHeader', () => {
139141
).toEqual(13 * 1000);
140142
});
141143
});
144+
145+
describe('addContextToFrame', () => {
146+
const lines = [
147+
'1: a',
148+
'2: b',
149+
'3: c',
150+
'4: d',
151+
'5: e',
152+
'6: f',
153+
'7: g',
154+
'8: h',
155+
'9: i',
156+
'10: j',
157+
'11: k',
158+
'12: l',
159+
'13: m',
160+
'14: n',
161+
];
162+
163+
test('start of file', () => {
164+
const frame: StackFrame = {
165+
lineno: 0,
166+
};
167+
addContextToFrame(lines, frame);
168+
expect(frame.pre_context).toEqual([]);
169+
expect(frame.context_line).toEqual('1: a');
170+
expect(frame.post_context).toEqual(['2: b', '3: c', '4: d', '5: e', '6: f']);
171+
});
172+
173+
test('mid of file', () => {
174+
const frame: StackFrame = {
175+
lineno: 4,
176+
};
177+
addContextToFrame(lines, frame);
178+
expect(frame.pre_context).toEqual(['1: a', '2: b', '3: c']);
179+
expect(frame.context_line).toEqual('4: d');
180+
expect(frame.post_context).toEqual(['5: e', '6: f', '7: g', '8: h', '9: i']);
181+
});
182+
183+
test('end of file', () => {
184+
const frame: StackFrame = {
185+
lineno: 14,
186+
};
187+
addContextToFrame(lines, frame);
188+
expect(frame.pre_context).toEqual(['9: i', '10: j', '11: k', '12: l', '13: m']);
189+
expect(frame.context_line).toEqual('14: n');
190+
expect(frame.post_context).toEqual([]);
191+
});
192+
193+
test('negative', () => {
194+
const frame: StackFrame = {
195+
lineno: -1,
196+
};
197+
addContextToFrame(lines, frame);
198+
expect(frame.pre_context).toEqual([]);
199+
expect(frame.context_line).toEqual('1: a');
200+
expect(frame.post_context).toEqual(['2: b', '3: c', '4: d', '5: e', '6: f']);
201+
});
202+
203+
test('overshoot', () => {
204+
const frame: StackFrame = {
205+
lineno: 999,
206+
};
207+
addContextToFrame(lines, frame);
208+
expect(frame.pre_context).toEqual(['10: j', '11: k', '12: l', '13: m', '14: n']);
209+
expect(frame.context_line).toEqual('14: n');
210+
expect(frame.post_context).toEqual([]);
211+
});
212+
});

0 commit comments

Comments
 (0)