Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Off-by-one error when applying source maps #203

Open
piscisaureus opened this issue Mar 10, 2018 · 0 comments
Open

Off-by-one error when applying source maps #203

piscisaureus opened this issue Mar 10, 2018 · 0 comments

Comments

@piscisaureus
Copy link

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' }
@eriwen eriwen self-assigned this Jan 7, 2020
@eriwen eriwen removed their assignment Mar 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants