Skip to content

ref(utils): reorder regexpa and add tests #7505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 64 additions & 57 deletions packages/utils/src/stacktrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,76 +125,83 @@ function node(getModule?: GetModuleFn): StackLineParserFn {

// eslint-disable-next-line complexity
return (line: string) => {
if (line.match(FILENAME_MATCH)) {
return {
filename: line,
};
}

const lineMatch = line.match(FULL_MATCH);
if (!lineMatch) {
return undefined;
}

let object: string | undefined;
let method: string | undefined;
let functionName: string | undefined;
let typeName: string | undefined;
let methodName: string | undefined;
if (lineMatch) {
let object: string | undefined;
let method: string | undefined;
let functionName: string | undefined;
let typeName: string | undefined;
let methodName: string | undefined;

if (lineMatch[1]) {
functionName = lineMatch[1];
if (lineMatch[1]) {
functionName = lineMatch[1];

let methodStart = functionName.lastIndexOf('.');
if (functionName[methodStart - 1] === '.') {
methodStart--;
}
let methodStart = functionName.lastIndexOf('.');
if (functionName[methodStart - 1] === '.') {
methodStart--;
}

if (methodStart > 0) {
object = functionName.slice(0, methodStart);
method = functionName.slice(methodStart + 1);
const objectEnd = object.indexOf('.Module');
if (objectEnd > 0) {
functionName = functionName.slice(objectEnd + 1);
object = object.slice(0, objectEnd);
if (methodStart > 0) {
object = functionName.slice(0, methodStart);
method = functionName.slice(methodStart + 1);
const objectEnd = object.indexOf('.Module');
if (objectEnd > 0) {
functionName = functionName.slice(objectEnd + 1);
object = object.slice(0, objectEnd);
}
}
typeName = undefined;
}
typeName = undefined;
}

if (method) {
typeName = object;
methodName = method;
}
if (method) {
typeName = object;
methodName = method;
}

if (method === '<anonymous>') {
methodName = undefined;
functionName = undefined;
}

if (method === '<anonymous>') {
methodName = undefined;
functionName = undefined;
if (functionName === undefined) {
methodName = methodName || '<anonymous>';
functionName = typeName ? `${typeName}.${methodName}` : methodName;
}

let filename = lineMatch[2] && lineMatch[2].startsWith('file://') ? lineMatch[2].slice(7) : lineMatch[2];
const isNative = lineMatch[5] === 'native';

if (!filename && lineMatch[5] && !isNative) {
filename = lineMatch[5];
Comment on lines +130 to +176
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an indentation change and order of operations is different - we first run the FULL_MATCH regexp which we expect to match most often and then the fallback

}

const isInternal =
isNative || (filename && !filename.startsWith('/') && !filename.startsWith('.') && !filename.includes(':\\'));

// in_app is all that's not an internal Node function or a module within node_modules
// note that isNative appears to return true even for node core libraries
// see https://github.com/getsentry/raven-node/issues/176

const in_app = !isInternal && filename !== undefined && !filename.includes('node_modules/');

return {
filename,
module: getModule ? getModule(filename) : undefined,
function: functionName,
lineno: parseInt(lineMatch[3], 10) || undefined,
colno: parseInt(lineMatch[4], 10) || undefined,
in_app,
};
}

if (functionName === undefined) {
methodName = methodName || '<anonymous>';
functionName = typeName ? `${typeName}.${methodName}` : methodName;
if (line.match(FILENAME_MATCH)) {
return {
filename: line,
};
}

const filename = lineMatch[2] && lineMatch[2].startsWith('file://') ? lineMatch[2].slice(7) : lineMatch[2];
const isNative = lineMatch[5] === 'native';
const isInternal =
isNative || (filename && !filename.startsWith('/') && !filename.startsWith('.') && filename.indexOf(':\\') !== 1);

// in_app is all that's not an internal Node function or a module within node_modules
// note that isNative appears to return true even for node core libraries
// see https://github.com/getsentry/raven-node/issues/176
const in_app = !isInternal && filename !== undefined && !filename.includes('node_modules/');

return {
filename,
module: getModule ? getModule(filename) : undefined,
function: functionName,
lineno: parseInt(lineMatch[3], 10) || undefined,
colno: parseInt(lineMatch[4], 10) || undefined,
in_app,
};
return undefined;
};
}

Expand Down
199 changes: 198 additions & 1 deletion packages/utils/test/stacktrace.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stripSentryFramesAndReverse } from '../src/stacktrace';
import { nodeStackLineParser, stripSentryFramesAndReverse } from '../src/stacktrace';

describe('Stacktrace', () => {
describe('stripSentryFramesAndReverse()', () => {
Expand Down Expand Up @@ -68,3 +68,200 @@ describe('Stacktrace', () => {
});
});
});

describe('node', () => {
const mockGetModule = jest.fn();
const parser = nodeStackLineParser(mockGetModule);
const node = parser[1];

beforeEach(() => {
mockGetModule.mockReset();
});

it('should return undefined for invalid input', () => {
expect(node('invalid input')).toBeUndefined();
});

it('should extract function, module, filename, lineno, colno, and in_app from valid input', () => {
const input = 'at myFunction (/path/to/file.js:10:5)';

const expectedOutput = {
filename: '/path/to/file.js',
module: undefined,
function: 'myFunction',
lineno: 10,
colno: 5,
in_app: true,
};

expect(node(input)).toEqual(expectedOutput);
});

it('extracts module from getModule', () => {
const input = 'at myFunction (/path/to/file.js:10:5)';
mockGetModule.mockReturnValue('myModule');
expect(node(input)?.module).toEqual('myModule');
});

it('should extract anonymous function name correctly', () => {
const input = 'at /path/to/file.js:10:5';

const expectedOutput = {
filename: '/path/to/file.js',
module: undefined,
function: '<anonymous>',
lineno: 10,
colno: 5,
in_app: true,
};

expect(node(input)).toEqual(expectedOutput);
});

it('should extract method name and type name correctly', () => {
const input = 'at myObject.myMethod (/path/to/file.js:10:5)';

const expectedOutput = {
filename: '/path/to/file.js',
module: undefined,
function: 'myObject.myMethod',
lineno: 10,
colno: 5,
in_app: true,
};

expect(node(input)).toEqual(expectedOutput);
});

it('should handle input with file:// protocol', () => {
const input = 'at myFunction (file:///path/to/file.js:10:5)';

const expectedOutput = {
filename: '/path/to/file.js',
module: undefined,
function: 'myFunction',
lineno: 10,
colno: 5,
in_app: true,
};

expect(node(input)).toEqual(expectedOutput);
});

it('should handle input with no line or column number', () => {
const input = 'at myFunction (/path/to/file.js)';

const expectedOutput = {
filename: '/path/to/file.js',
module: undefined,
function: 'myFunction',
lineno: undefined,
colno: undefined,
in_app: true,
};

expect(node(input)).toEqual(expectedOutput);
});

it('should handle input with "native" flag', () => {
const input = 'at myFunction (native)';

const expectedOutput = {
filename: undefined,
module: undefined,
function: 'myFunction',
lineno: undefined,
colno: undefined,
in_app: false,
};

expect(node(input)).toEqual(expectedOutput);
});

it('should correctly parse a stack trace line with a function name and file URL', () => {
const line = 'at myFunction (file:///path/to/myFile.js:10:20)';
const result = node(line);
expect(result).toEqual({
filename: '/path/to/myFile.js',
function: 'myFunction',
lineno: 10,
colno: 20,
in_app: true,
});
});

it('should correctly parse a stack trace line with a method name and filename', () => {
const line = 'at MyClass.myMethod (/path/to/myFile.js:10:20)';
const result = node(line);
expect(result).toEqual({
filename: '/path/to/myFile.js',
module: undefined,
function: 'MyClass.myMethod',
lineno: 10,
colno: 20,
in_app: true,
});
});

it('should correctly parse a stack trace line with an anonymous function', () => {
const line = 'at Object.<anonymous> (/path/to/myFile.js:10:20)';
const result = node(line);

expect(result).toEqual({
filename: '/path/to/myFile.js',
function: 'Object.<anonymous>',
lineno: 10,
colno: 20,
in_app: true,
});
});

it('should correctly parse a stack trace line with no function or filename', () => {
const line = 'at /path/to/myFile.js:10:20';
const result = node(line);
expect(result).toEqual({
filename: '/path/to/myFile.js',
function: '<anonymous>',
lineno: 10,
colno: 20,
in_app: true,
});
});

it('should correctly parse a stack trace line with a native function', () => {
const line = 'at Object.<anonymous> (native)';
const result = node(line);
expect(result).toEqual({
filename: undefined,
function: 'Object.<anonymous>',
lineno: undefined,
colno: undefined,
in_app: false,
});
});

it('should correctly parse a stack trace line with a module filename', () => {
const line = 'at Object.<anonymous> (/path/to/node_modules/myModule/index.js:10:20)';
const result = node(line);

expect(result).toEqual({
filename: '/path/to/node_modules/myModule/index.js',
function: 'Object.<anonymous>',
lineno: 10,
colno: 20,
in_app: false,
});
});

it('should correctly parse a stack trace line with a Windows filename', () => {
const line = 'at Object.<anonymous> (C:\\path\\to\\myFile.js:10:20)';
const result = node(line);
expect(result).toEqual({
filename: 'C:\\path\\to\\myFile.js',
function: 'Object.<anonymous>',
lineno: 10,
colno: 20,
in_app: true,
});
});
});