Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Do not crash if unable to figure out location
Browse files Browse the repository at this point in the history
  • Loading branch information
markusn committed Feb 12, 2019
1 parent 5a0d845 commit 59bdca6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
44 changes: 24 additions & 20 deletions lib/get-loc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,35 @@
const path = require("path");

function getLoc(depth) {
let stack, file, frame;
try {
let stack, file, frame;

const pst = Error.prepareStackTrace;
Error.prepareStackTrace = function (_, innerStack) {
Error.prepareStackTrace = pst;
return innerStack;
};
const pst = Error.prepareStackTrace;
Error.prepareStackTrace = function (_, innerStack) {
Error.prepareStackTrace = pst;
return innerStack;
};

stack = new Error().stack;
depth = !depth || isNaN(depth) ? 1 : depth > stack.length - 2 ? stack.length - 2 : depth;
stack = stack.slice(depth + 1);
stack = new Error().stack;
depth = !depth || isNaN(depth) ? 1 : depth > stack.length - 2 ? stack.length - 2 : depth;
stack = stack.slice(depth + 1);

do {
frame = stack.shift();
file = frame && frame.getFileName();
} while (stack.length && file === "module.js" || file.includes("node_modules"));
do {
frame = stack.shift();
file = frame && frame.getFileName();
} while (stack.length && file === "module.js" || file.includes("node_modules"));

let calleePath;
if (process.cwd()) {
calleePath = path.relative(process.cwd(), frame.getFileName());
} else {
calleePath = frame.getFileName();
}
let calleePath;
if (process.cwd()) {
calleePath = path.relative(process.cwd(), frame.getFileName());
} else {
calleePath = frame.getFileName();
}

return `${calleePath}:${frame.getLineNumber()}`;
return `${calleePath}:${frame.getLineNumber()}`;
} catch (e) {
return undefined;
}
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"Markus Ekholm",
"Jens Carlén"
],
"version": "2.1.2",
"version": "2.1.3",
"scripts": {
"test": "mocha --exit && eslint . --cache && depcheck --ignores=\"prettier\"",
"posttest": "eslint --cache .",
Expand Down

0 comments on commit 59bdca6

Please sign in to comment.