Skip to content

Commit

Permalink
Handle multiple consecutive comments #1
Browse files Browse the repository at this point in the history
  • Loading branch information
hudochenkov committed Dec 11, 2015
1 parent c28129f commit bce9e2d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,31 +120,45 @@ module.exports = postcss.plugin('postcss-sorting', function (opts) {
node.initialIndex = index;

// If comment on separate line before node, use node's indexes for comment
if (node.prev() && node.prev().type === 'comment') {
var previousNode = node.prev();
var commentsBefore = [];
var previousNode = node.prev();

while (previousNode && previousNode.type === 'comment') {
if (previousNode.raws.before && previousNode.raws.before.indexOf('\n') > -1) {
previousNode.groupIndex = node.groupIndex;
previousNode.propertyIndex = node.propertyIndex;
previousNode.initialIndex = index - 1;

var previousNodeClone = cleanLineBreaks(previousNode);

processed.push(previousNodeClone);
commentsBefore.unshift(previousNodeClone);

previousNode = previousNode.prev();
} else {
break;
}
}

if (commentsBefore.length) {
processed = processed.concat(commentsBefore);
}

// Add node itself
processed.push(node);

// If comment on same line with the node and node, use node's indexes for comment
if (node.next() && node.next().type === 'comment') {
var nextNode = node.next();
var nextNode = node.next();

while (nextNode && nextNode.type === 'comment') {
if (nextNode.raws.before && nextNode.raws.before.indexOf('\n') < 0) {
nextNode.groupIndex = node.groupIndex;
nextNode.propertyIndex = node.propertyIndex;
nextNode.initialIndex = index + 1;

processed.push(nextNode);
nextNode = nextNode.next();
} else {
break;
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ test.skip('Should use default config if config is empty', t => {
return run(t, 'at-rules-by-name', { });
});

test.skip('Should work correctly with comments in case of 1 group', t => {
test('Should work correctly with comments in case of 1 group', t => {
return run(t, 'single-group-comments', { 'sort-order': [
['border-bottom', 'font-style']
] });
Expand Down

0 comments on commit bce9e2d

Please sign in to comment.