Skip to content

Commit

Permalink
feat(eval): support eval and function statements
Browse files Browse the repository at this point in the history
  • Loading branch information
nicojs committed Feb 19, 2021
1 parent 1d6786e commit ce12cc5
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions src/stack-trace.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
import { CallId } from './call-id';

const stackLineRegex = /(?:[^@]*)@(.*):(\d+):(\d+)/;

export function getCallIdStackTrace(distance = 0): CallId | undefined {
export function getCallIdStackTrace(distance = 0): CallId | null {
const stacks = new Error().stack
?.split('\n')
.map((stackLine) => stackLine.trim());

if (stacks) {
const stackLine = stacks[distance + 1];
if (stackLine) {
const match = stackLineRegex.exec(stackLine);
if (match) {
return {
fileName: match[1],
line: parseInt(match[2], 10),
column: parseInt(match[3], 10),
};
}
return tryParseEval(stackLine) || tryParseNormal(stackLine);
}
}
return;
return null;
}
const evalStackLineRegex = /(?:[^@]*)@(.*) line (\d+) > (?:eval|Function):(\d+):(\d+)/;
function tryParseEval(line: string): CallId | null {
const maybeMatch = evalStackLineRegex.exec(line);

return (
maybeMatch && {
column: 0,
fileName: maybeMatch[1],
line: parseInt(maybeMatch[2], 10),
}
);
}

const stackLineRegex = /(?:[^@]*)@(.*):(\d+):(\d+)/;
function tryParseNormal(line: string): CallId | null {
const maybeMatch = stackLineRegex.exec(line);
return (
maybeMatch && {
fileName: maybeMatch[1],
line: parseInt(maybeMatch[2], 10),
column: parseInt(maybeMatch[3], 10),
}
);
}

/**
Expand All @@ -32,4 +48,23 @@ export function getCallIdStackTrace(distance = 0): CallId | undefined {
* Runnable.prototype.run@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:22156:12
* Runner.prototype.runTest@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:23809:11
* next/<@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:23940:13
*
*
* "getCallIdStackTrace@http://localhost:9876/base/dist/esm/stack-trace.js:4:26
* getCallId@http://localhost:9876/base/dist/esm/get-call-id.js:8:16
* act@http://localhost:9876/base/test/generated/browser.test.js?0ea514c6aa4d1c42930bfff05a2e0152808f5d07:63:12
* @http://localhost:9876/base/test/generated/browser.test.js?0ea514c6aa4d1c42930bfff05a2e0152808f5d07 line 73 > eval:1:1
* @http://localhost:9876/base/test/generated/browser.test.js?0ea514c6aa4d1c42930bfff05a2e0152808f5d07:73:10
* callFn@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:22170:22
* Runnable.prototype.run@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:22156:12
* Runner.prototype.runTest@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:23809:11
* next/<@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:23940:13
* next@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:23717:15
* next/<@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:23727:12
* next@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:23602:15
* Runner.prototype.hook/<@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:23694:10
* timeslice@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:29950:28
* setTimeout handler*timeslice@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:29954:25
* setTimeout handler*timeslice@http://localhost:9876/base/node_modules/mocha/mocha.js?359b65d592046ab717953cd710619a139201af2e:29954:25
* "
*/

0 comments on commit ce12cc5

Please sign in to comment.