Skip to content

Commit

Permalink
change location storing in SyntaxMatchError
Browse files Browse the repository at this point in the history
- Changed property to store mismatch offset to `mismatchOffset`
- Changed `offset` property to store bad node offset in source CSS if
any
- Added `loc` property that stores bad node `loc` if any
  • Loading branch information
lahmatiy committed Jan 19, 2017
1 parent 86c802b commit b9d0483
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions lib/syntax/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ function contains(node, searchFor) {
return found;
}

function getLocation(node, point) {
var loc = node && node.loc && node.loc[point];

return loc
? { offset: loc.offset,
line: loc.line,
column: loc.column }
: null;
}

var SyntaxParseError = function(message, syntaxStr, offset) {
var error = new SyntaxError();
error.name = 'SyntaxParseError';
Expand All @@ -32,6 +42,9 @@ var SyntaxParseError = function(message, syntaxStr, offset) {
var MatchError = function(message, syntax, value, badNode) {
var errorOffset = -1;
var css = '';
var error = new SyntaxError(message);
var start = getLocation(badNode, 'start');
var end = getLocation(badNode, 'end');

value.children.each(function(node) {
if (badNode === node || (badNode && contains(node, badNode))) {
Expand All @@ -45,18 +58,23 @@ var MatchError = function(message, syntax, value, badNode) {
errorOffset = css.length;
}

var error = new SyntaxError(message);
error.name = 'SyntaxMatchError';
error.rawMessage = message;
error.syntax = syntax ? translateSyntax(syntax) : '<generic>';
error.css = css;
error.line = badNode && badNode.loc && badNode.loc.start ? badNode.loc.start.line : undefined;
error.column = badNode && badNode.loc && badNode.loc.start ? badNode.loc.start.column : undefined;
error.offset = errorOffset;
error.mismatchOffset = errorOffset;
error.loc = {
source: badNode && badNode.loc && badNode.loc.source || '<unknown>',
start: start,
end: end
};
error.line = start ? start.line : undefined;
error.column = start ? start.column : undefined;
error.offset = start ? start.offset : undefined;
error.message = message + '\n' +
' syntax: ' + error.syntax + '\n' +
' value: ' + (error.css || '<empty string>') + '\n' +
' --------' + new Array(error.offset + 1).join('-') + '^';
' --------' + new Array(error.mismatchOffset + 1).join('-') + '^';

return error;
};
Expand Down

0 comments on commit b9d0483

Please sign in to comment.