Skip to content

Commit

Permalink
fix(revert): micro optimizations are not good thing some times
Browse files Browse the repository at this point in the history
improve tests
  • Loading branch information
tunnckoCore committed Mar 13, 2017
1 parent b9c3781 commit 66fd63a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
62 changes: 30 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,37 @@ module.exports = function stacktraceMetadata (error, options) {
relativePaths: true
}, options)

if (!opts.showStack) {
error.stack = ''
return error
var relativePaths = opts.relativePaths
? relative(opts.cwd)
: function (line) { return line }

var stack = clean(error.stack, function mapper (line, index) {
// hint: use `parent-module` package
// and `line.indexOf(parentModule())`
// if not works correctly

line = relativePaths(line)

if (index === 1) {
return metadata(function meta (_, info) {
error.line = info.line
error.place = info.place
error.column = info.column
error.filename = info.filename
})(line)
}

return line
})

if (opts.showStack) {
error.stack = opts.cleanStack ? stack : error.stack
error.stack = opts.shortStack
? error.stack.split('\n').splice(0, 4).join('\n')
: error.stack
} else {
error.stack = '' // or delete error.stack?
}

if (opts.cleanStack) {
var relativePaths = opts.relativePaths
? relative(opts.cwd)
: function (line) { return line }

error.stack = clean(error.stack, function mapper (line, index) {
// hint: use `parent-module` package
// and `line.indexOf(parentModule())`
// if not works correctly

line = relativePaths(line)

if (index === 1) {
return metadata(function meta (_, info) {
error.line = info.line
error.place = info.place
error.column = info.column
error.filename = info.filename
})(line)
}

return line
})
}

error.stack = opts.shortStack
? error.stack.split('\n').splice(0, 4).join('\n')
: error.stack
}

return error
Expand Down
16 changes: 14 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ test('should have relative paths by default', function (done) {

test.strictEqual(e.message, 'opts.relativePaths: true')
test.strictEqual(e.stack.indexOf('(test.js:41') > 0, true)
test.ok(e.line)
test.ok(e.column)
test.ok(e.filename)
done()
})

Expand All @@ -54,14 +57,17 @@ test('should have absolute paths if opts.relativePaths: false', function (done)

test.strictEqual(e.message, 'foo qux bar')
test.strictEqual(e.stack.indexOf('(test.js:') === -1, true)
test.strictEqual(e.line, 53)
test.ok(e.column)
test.ok(e.filename)
done()
})

test('should clean stack by default', function (done) {
var error = new TypeError('woohooo')
var stack = [
'Error: woohooo',
' at Function.<anonymous> (/home/charlike/apps/stacktrace-metadata/test.js:16:13)',
' at Function.<anonymous> (/home/charlike/apps/stacktrace-metadata/test.js:111:33)',
' at Function.tryCatch (/home/charlike/apps/node_modules/try-catch-callback/index.js:75:14)',
' at Function.tryCatchCallback (/home/charlike/apps/node_modules/try-catch-callback/index.js:58:21)',
' at Function.tryCatch (/home/charlike/apps/node_modules/always-done/node_modules/try-catch-core/index.js:80:26)',
Expand All @@ -84,6 +90,9 @@ test('should clean stack by default', function (done) {

// should new stack be shorter than the old one
test.strictEqual(e.stack.split('\n').length < stack.length, true)
test.strictEqual(e.line, 111)
test.strictEqual(e.column, 33)
test.ok(e.filename)
done()
})

Expand All @@ -97,6 +106,9 @@ test('should not clean stack if opts.cleanStack: false', function (done) {

var internals = e.stack.indexOf('at Module._compile') > 0
test.strictEqual(internals, true)
test.strictEqual(e.line, 100)
test.ok(e.column)
test.ok(e.filename)
done()
})

Expand All @@ -114,7 +126,7 @@ test('should have props like `err.line`, `err.filename` and `err.column`', funct

test.strictEqual(e.name, 'Error')
test.strictEqual(e.message, 'my special error')
test.strictEqual(e.line, 112)
test.strictEqual(e.line, 124)
test.strictEqual(e.column, 13)
test.strictEqual(e.place, 'Function.myQuxTest')
test.strictEqual(e.filename, 'test.js')
Expand Down

0 comments on commit 66fd63a

Please sign in to comment.