Skip to content

Commit

Permalink
fix css parse error handling (fixes #5)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed May 8, 2016
1 parent 24536e0 commit cf0a51b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
19 changes: 14 additions & 5 deletions lib/postcssToCsso.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ function getInfo(node) {
}

function parseToCsso(str, context, node) {
var cssoNode = parse(str, {
context: context
});
var cssoNode;

try {
cssoNode = parse(str, {
context: context
});
} catch (e) {
if (e.name === 'CssSyntaxError') {
throw node.error(e.message, { index: e.parseError.offset });
}

throw e;
}

cssoNode.info = getInfo(node);

Expand Down Expand Up @@ -69,8 +79,7 @@ function postcssToCsso(node) {
case 'decl':
return parseToCsso(
(node.raws.before || '').trimLeft() +
node.prop + ':' + node.value +
(node.important ? '!important' : ''),
node.toString(),
'declaration',
node
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"travis": "npm run codestyle-and-test"
},
"dependencies": {
"csso": "^2.0.0",
"csso": "^2.1.0",
"postcss": "^5.0.0"
},
"devDependencies": {
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ it('should keep the shape of original postcss nodes', function() {
});
});

it('error handling', function() {
return postcss([
postcssCsso
]).process('.test { color: * }').then(function() {
assert(false, 'shouldn\'t to be successful');
}, function(error) {
assert.equal(error.name, 'CssSyntaxError');
assert.equal(error.line, 1);
assert.equal(error.column, 16);
});
});

// currently works only is used as linked package
// TODO: find the way to use csso compress tests
try {
Expand Down

0 comments on commit cf0a51b

Please sign in to comment.