Skip to content

Commit

Permalink
fix(reporter): Enable sourcemaps for errors that without column #
Browse files Browse the repository at this point in the history
  • Loading branch information
jjoos committed Aug 8, 2015
1 parent 139ad83 commit 086a542
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ var createErrorFormatter = function (basePath, emitter, SourceMapConsumer) {

if (file && file.sourceMap) {
line = parseInt(line || '0', 10)
column = parseInt(column || '0', 10)

column = parseInt(column, 10)

// When no column is given and we default to 0, it doesn't make sense to only search for smaller
// or equal columns in the sourcemap, let's search for equal or greater columns.
var bias = column ? SourceMapConsumer.GREATEST_LOWER_BOUND : SourceMapConsumer.LEAST_UPPER_BOUND

try {
var original = getSourceMapConsumer(file.sourceMap)
.originalPositionFor({line: line, column: column})
.originalPositionFor({line: line, column: (column || 0), bias: bias})

return util.format('%s:%d:%d <- %s:%d:%d', path, line, column, original.source,
var formattedColumn = column ? util.format(':%s', column) : ''
return util.format('%s:%d%s <- %s:%d:%d', path, line, formattedColumn, original.source,
original.line, original.column)
} catch (e) {
log.warn('SourceMap position not found for trace: %s', msg)
Expand Down
18 changes: 18 additions & 0 deletions test/unit/reporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ describe('reporter', () => {
}
}

MockSourceMapConsumer.GREATEST_LOWER_BOUND = 1
MockSourceMapConsumer.LEAST_UPPER_BOUND = 2

it('should rewrite stack traces', done => {
formatError = m.createErrorFormatter('/some/base', emitter, MockSourceMapConsumer)
var servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
Expand All @@ -98,6 +101,21 @@ describe('reporter', () => {
})
})

it('should rewrite stack traces to the first column when no column is given', done => {
formatError = m.createErrorFormatter('/some/base', emitter, MockSourceMapConsumer)
var servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
servedFiles[0].sourceMap = {content: 'SOURCE MAP a.js'}
servedFiles[1].sourceMap = {content: 'SOURCE MAP b.js'}

emitter.emit('file_list_modified', {served: servedFiles})

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

it('should rewrite relative url stack traces', done => {
formatError = m.createErrorFormatter('/some/base', emitter, MockSourceMapConsumer)
var servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
Expand Down

0 comments on commit 086a542

Please sign in to comment.