Skip to content

Commit bce9e2d

Browse files
committed
Handle multiple consecutive comments #1
1 parent c28129f commit bce9e2d

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,31 +120,45 @@ module.exports = postcss.plugin('postcss-sorting', function (opts) {
120120
node.initialIndex = index;
121121

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

126+
while (previousNode && previousNode.type === 'comment') {
126127
if (previousNode.raws.before && previousNode.raws.before.indexOf('\n') > -1) {
127128
previousNode.groupIndex = node.groupIndex;
128129
previousNode.propertyIndex = node.propertyIndex;
129130
previousNode.initialIndex = index - 1;
130131

131132
var previousNodeClone = cleanLineBreaks(previousNode);
132133

133-
processed.push(previousNodeClone);
134+
commentsBefore.unshift(previousNodeClone);
135+
136+
previousNode = previousNode.prev();
137+
} else {
138+
break;
134139
}
135140
}
136141

142+
if (commentsBefore.length) {
143+
processed = processed.concat(commentsBefore);
144+
}
145+
146+
// Add node itself
137147
processed.push(node);
138148

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

152+
while (nextNode && nextNode.type === 'comment') {
143153
if (nextNode.raws.before && nextNode.raws.before.indexOf('\n') < 0) {
144154
nextNode.groupIndex = node.groupIndex;
145155
nextNode.propertyIndex = node.propertyIndex;
146156
nextNode.initialIndex = index + 1;
157+
147158
processed.push(nextNode);
159+
nextNode = nextNode.next();
160+
} else {
161+
break;
148162
}
149163
}
150164
});

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ test.skip('Should use default config if config is empty', t => {
149149
return run(t, 'at-rules-by-name', { });
150150
});
151151

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

0 commit comments

Comments
 (0)