Skip to content

Commit

Permalink
fix(reporter): format stack with 1-based column (#3325)
Browse files Browse the repository at this point in the history
Columns in original stack are 1-based, but
SourceMapConsumer.prototype.originalPositionFor(generatedPosition)
accepts 0-based column and returns 0-based column too.
This change converts columns from 1-based to 0-based forth and back.

Closes #3324
  • Loading branch information
segrey authored and johnjbarton committed Jun 17, 2019
1 parent f0c4677 commit 182c04d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/reporter.js
Expand Up @@ -60,11 +60,13 @@ function createErrorFormatter (config, emitter, SourceMapConsumer) {
const bias = column ? SourceMapConsumer.GREATEST_LOWER_BOUND : SourceMapConsumer.LEAST_UPPER_BOUND

try {
const original = getSourceMapConsumer(file.sourceMap).originalPositionFor({ line, column: (column || 0), bias })
const zeroBasedColumn = Math.max(0, (column || 1) - 1)
const original = getSourceMapConsumer(file.sourceMap).originalPositionFor({ line, column: zeroBasedColumn, bias })

// Source maps often only have a local file name, resolve to turn into a full path if
// the path is not absolute yet.
return `${PathUtils.formatPathMapping(resolve(path, original.source), original.line, original.column)} <- ${PathUtils.formatPathMapping(path, line, column)}`
const oneBasedOriginalColumn = original.column == null ? original.column : original.column + 1
return `${PathUtils.formatPathMapping(resolve(path, original.source), original.line, oneBasedOriginalColumn)} <- ${PathUtils.formatPathMapping(path, line, column)}`
} catch (e) {
log.warn(`SourceMap position not found for trace: ${input}`)
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/reporter.spec.js
Expand Up @@ -195,7 +195,7 @@ describe('reporter', () => {

_.defer(() => {
const ERROR = 'at http://localhost:123/base/b.js:2'
expect(formatError(ERROR)).to.equal('at /original/b.js:4:2 <- b.js:2\n')
expect(formatError(ERROR)).to.equal('at /original/b.js:4:3 <- b.js:2\n')
done()
})
})
Expand Down

0 comments on commit 182c04d

Please sign in to comment.