Skip to content

Commit

Permalink
Optimize finding top frame in formatPaths (#5557)
Browse files Browse the repository at this point in the history
  • Loading branch information
Connormiha authored and cpojer committed Feb 14, 2018
1 parent 5b00d97 commit 1ee7df9
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions packages/jest-message-util/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type StackTraceOptions = {
noStackTrace: boolean,
};

const PATH_NODE_MODULES = `${path.sep}node_modules${path.sep}`;
const PATH_EXPECT_BUILD = `${path.sep}expect${path.sep}build${path.sep}`;

// filter for noisy stack trace lines
const JASMINE_IGNORE = /^\s+at(?:(?:.*?vendor\/|jasmine\-)|\s+jasmine\.buildExpectationResult)/;
const JEST_INTERNALS_IGNORE = /^\s+at.*?jest(-.*?)?(\/|\\)(build|node_modules|packages)(\/|\\)/;
Expand Down Expand Up @@ -215,6 +218,22 @@ const formatPaths = (
return STACK_TRACE_COLOR(match[1]) + filePath + STACK_TRACE_COLOR(match[3]);
};

const getTopFrame = (lines: string[]) => {
for (const line of lines) {
if (line.includes(PATH_NODE_MODULES) || line.includes(PATH_EXPECT_BUILD)) {
continue;
}

const parsedFrame = stackUtils.parseLine(line.trim());

if (parsedFrame && parsedFrame.file) {
return parsedFrame;
}
}

return null;
};

export const formatStackTrace = (
stack: string,
config: StackTraceConfig,
Expand All @@ -228,17 +247,7 @@ export const formatStackTrace = (
: null;
lines = removeInternalStackEntries(lines, options);

const topFrame = lines
.map(line => line.trim())
.filter(Boolean)
.filter(
line =>
!line.includes(`${path.sep}node_modules${path.sep}`) &&
!line.includes(`${path.sep}expect${path.sep}build${path.sep}`),
)
.map(line => stackUtils.parseLine(line))
.filter(Boolean)
.filter(parsedFrame => parsedFrame.file)[0];
const topFrame = getTopFrame(lines);

if (topFrame) {
const filename = topFrame.file;
Expand Down

0 comments on commit 1ee7df9

Please sign in to comment.