cspotcode / cspotcode/node-source-map-support

Fix method for getting function name for stack frames

未关闭
#40 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
JavaScript
星标
16
派生
4
PR 合并指标
30 天内没有已合并 PR

描述

From https://github.com/facebook/jest/pull/12786#issuecomment-1114869813

```
// hubba.test.js
function BadCode() {
throw new Error('noo');
}

function run(fn) {
fn();
}

run(BadCode);
```
compiled to:
```
"use strict";

function BadCode() {
throw new Error('noo');
}

function run(fn) {
fn();
}

run(BadCode);
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJCYWRDb2RlIiwiRXJyb3IiLCJydW4iLCJmbiJdLCJzb3VyY2VzIjpbImh1YmJhLnRlc3QuanMiXSwic291cmNlc0NvbnRlbnQiOlsiZnVuY3Rpb24gQmFkQ29kZSgpIHtcbiAgdGhyb3cgbmV3IEVycm9yKCdub28nKTtcbn1cblxuZnVuY3Rpb24gcnVuKGZuKSB7XG4gIGZuKCk7XG59XG5cbnJ1bihCYWRDb2RlKTtcbiJdLCJtYXBwaW5ncyI6Ijs7QUFBQSxTQUFTQSxPQUFULEdBQW1CO0VBQ2pCLE1BQU0sSUFBSUMsS0FBSixDQUFVLEtBQVYsQ0FBTjtBQUNEOztBQUVELFNBQVNDLEdBQVQsQ0FBYUMsRUFBYixFQUFpQjtFQUNmQSxFQUFFO0FBQ0g7O0FBRURELEdBQUcsQ0FBQ0YsT0FBRCxDQUFIIn0=
```

Stack trace with source-mapping is wrong:
```
Error: noo
at fn (/Users/simen/repos/jest/hubba.test.js:2:9) <----- this stack frame should say `BadCode`
at run (/Users/simen/repos/jest/hubba.test.js:6:3)
at Object. (/Users/simen/repos/jest/hubba.test.js:9:1)
```

source-map-support's algorithm for getting the name of a stack frame: it checks the location of the *next* stack frame. That is why the bug is showing us `at fn` when it should be showing us `at BadCode`. The stack frame happens within `BadCode`, but the location of the *next* stack frame is the invocation site: `fn()`

We would see the same issue with the second stack frame if we did `const runIt = run; runIt(BadCode)` Because it's checking the name at the callsite, it's not checking the invokee's name.

Pretty sure this is not correct and stack frames actually expose additional information we can use to get the correct answer.

In addition to `frame.getLineNumber()` and `frame.getColumnNumber()`, we have `frame.getEnclosingLineNumber()` and `frame.getEnclosingColumnNumber()` which are the location of the invoked function. For example:

```
// getEnclosing*Number() gives us location A
// get*Number() gives us location B
/*A*/function run() {}
/*B*/run()
```

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。