stacktracejs/stacktrace.js

Off-by-one error when applying source maps

Opened this issue · 0 comments

Stacktrace.js misnumbers columns after applying a source map.

This is probably because the source maps spec says that lines and columns are 0-based, whereas browsers generally start counting at 1.

Strangely however the issue doesn't seem to affect line numbers, only columns.

Test case demonstrating the bug:

let StackTrace = require("stacktrace-js");

// No source map: the stack trace says that the error occurred at
// line 1 column 1.

let code = "error_at_line_1_column_1()\n" + "//# sourceURL=code.js\n";

let sourceCache = {
  "code.js": code
};

try {
  eval(code);
} catch (err) {
  StackTrace.fromError(err, { sourceCache }).then(stack => {
    console.log("No source map:");
    console.log(stack[0]);
  });
}

// Add a source map. Now it'll say give the error at
// line 1 column 0.

code += "//# sourceMappingURL=code.js.map\n";

let sourceMap = {
  version: 3,
  sources: ["mapped.js"],
  mappings: "AAAA"
};

sourceCache = {
  "code.js": code,
  "code.js.map": JSON.stringify(sourceMap)
};

try {
  eval(code);
} catch (err) {
  StackTrace.fromError(err, { sourceCache }).then(stack => {
    console.log("With source map:")
    console.log(stack[0]);
  });
}

Script output:

No source map:
{ columnNumber: 1,
  lineNumber: 1,
  fileName: 'code.js',
  functionName: 'eval',
  source: '    at eval (code.js:1:1)' }
With source map:
{ columnNumber: 0,
  lineNumber: 1,
  fileName: 'mapped.js',
  functionName: 'eval' }