Skip to content

Commit

Permalink
fix error position in messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Feb 7, 2017
1 parent 47bacdd commit bb5bdcf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
29 changes: 29 additions & 0 deletions examples/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

var Parser = require('../lib/parser');

var parser = new Parser()
.set('at', function() {
var pos = this.position();
var match = this.match(/^@/);
if (match) {
return pos({val: match[0]});
}
})
.set('slash', function() {
var pos = this.position();
var match = this.match(/^\//);
if (match) {
return pos({val: match[0]});
}
})
.set('text', function() {
var pos = this.position();
var match = this.match(/^\w+/);
if (match) {
return pos({val: match[0]});
}
})

var ast = parser.parse('[email protected]:foo/bar.git');
console.log(ast);
9 changes: 6 additions & 3 deletions lib/error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';

var get = require('get-value');

module.exports = function(msg, node) {
var pos = node.position || {start: {column: 0, line: 0}};
var line = pos.start.line;
var column = pos.start.column;
node = node || {};
var pos = node.position || {};
var line = get(node, 'position.end.line') || 1;
var column = get(node, 'position.end.column') || 1;
var source = this.options.source;

var message = source + ' <line:' + line + ' column:' + column + '>: ' + msg;
Expand Down
2 changes: 1 addition & 1 deletion test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('compiler', function() {
cb(new Error('expected an error'));
} catch (err) {
assert(err);
assert.equal(err.message, 'string <line:1 column:1>: compiler "text" is not registered');
assert.equal(err.message, 'string <line:1 column:2>: compiler "text" is not registered');
cb();
}
});
Expand Down

0 comments on commit bb5bdcf

Please sign in to comment.