Skip to content

Commit

Permalink
fix(paths): better detection for paths, fixes windows bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Mar 13, 2017
1 parent 01e9be3 commit 7cf414d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
26 changes: 20 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

'use strict'

var path = require('path')
var parseFilepath = require('parse-filepath')
var pathNormalize = require('normalize-path')

/**
* > Parses each line in stack and pass `info` object
* to the given `plugin` function.
Expand Down Expand Up @@ -77,17 +81,27 @@ module.exports = function cleanStacktraceMetadata (plugin) {
}

return function onEachLine (line, index) {
if (!/at/.test(line)) return line
if (!/at/.test(line)) {
return line
}

var m = line.match(/at (.+) \(?([^:\s]+):(\d+):?(\d+)?\)?$/)
var m = line.match(/at (.+) \(?(.+)\)?$/)

/* istanbul ignore next */
if (!m) return line
if (!m) {
return line
}

var filepath = m[2].slice(-1) === ')' ? m[2].slice(0, -1) : m[2]
var parsed = parseFilepath(pathNormalize(filepath))
var dirname = parsed.dirname
var parts = parsed.basename.split(':')
var filename = path.join(dirname, parts[0])

var info = {
line: Number(m[3]),
column: Number(m[4]),
filename: String(m[2]),
line: Number(parts[1] || 0),
column: Number(parts[2] || 0),
filename: String(filename),
place: String(m[1])
}

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"git:cz": "git-cz",
"commit": "npm-run-all -s test git"
},
"dependencies": {},
"dependencies": {
"normalize-path": "^2.0.1",
"parse-filepath": "^1.0.1"
},
"devDependencies": {
"commitizen": "~2.7.0",
"cz-conventional-changelog": "1.1.5",
Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ test('clean-stacktrace-metadata', function (done) {
done()
})

test('should work for windows paths', function (done) {
cleanStacktraceMetadata(function plugin (line, info) {
test.strictEqual(info.place, 'Test.fn')
test.strictEqual(info.filename, 'C:/projects/stacktrace-metadata/test.js')
test.strictEqual(info.line, 53)
test.strictEqual(info.column, 15)
})('at Test.fn (C:\\projects\\stacktrace-metadata\\test.js:53:15)')
done()
})

test('should work when not defined line and column', function (done) {
function plugin (line, info) {
test.strictEqual(info.line, 0)
test.strictEqual(info.column, 0)
test.strictEqual(info.place, 'quxie')
test.strictEqual(info.filename, '/home/projects/stacktrace-metadata/test.js')
}
var line = 'at quxie /home/projects/stacktrace-metadata/test.js'
cleanStacktraceMetadata(plugin)(line)
done()
})

test('should throw TypeError if `plugin` is not a function', function (done) {
function fixture () {
cleanStacktraceMetadata()
Expand Down

0 comments on commit 7cf414d

Please sign in to comment.