Skip to content

Commit 608e950

Browse files
committed
Fix adding additional empty line between @media and children #19
1 parent dcb931a commit 608e950

File tree

6 files changed

+59
-10
lines changed

6 files changed

+59
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## 1.3.1
6+
* Fix adding additional empty line if both `empty-lines-between-children-rules` and `empty-lines-between-media-rules` are not 0. #19
7+
58
## 1.3.0
69
* Added `empty-lines-between-media-rules` option which set a number of empty lines between nested media rules. #16
710

index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,16 @@ function fetchAllCommentsAfterNode(comments, nextNode, node, currentInitialIndex
181181
return fetchAllCommentsAfterNode(comments.concat(nextNode), nextNode.next(), node, nextNode.initialIndex);
182182
}
183183

184-
function getApplicableNode(node) {
184+
function getApplicableNode(lookFor, node) {
185185
// find if there any rules before, and skip the comments
186186
var prevNode = node.prev();
187187

188-
if (prevNode.type === 'rule') {
189-
return node;
190-
}
191-
192-
if (prevNode.type === 'atrule') {
188+
if (prevNode.type === lookFor) {
193189
return node;
194190
}
195191

196192
if (prevNode.type === 'comment') {
197-
return getApplicableNode(prevNode);
193+
return getApplicableNode(lookFor, prevNode);
198194
}
199195

200196
return false;
@@ -286,7 +282,7 @@ module.exports = postcss.plugin('postcss-sorting', function (opts) {
286282
// Insert empty lines between children classes
287283
if (node.type === 'rule' && linesBetweenChildrenRules > 0) {
288284
// between rules can be comments, so empty lines should be added to first comment between rules, rather than to rule
289-
applicableNode = getApplicableNode(node);
285+
applicableNode = getApplicableNode('rule', node);
290286

291287
if (applicableNode) {
292288
applicableNode.raws.before = createLineBreaks(linesBetweenChildrenRules) + applicableNode.raws.before;
@@ -296,7 +292,7 @@ module.exports = postcss.plugin('postcss-sorting', function (opts) {
296292
// Insert empty lines between media rules
297293
if (node.type === 'atrule' && node.name === 'media' && linesBetweenMediaRules > 0) {
298294
// between rules can be comments, so empty lines should be added to first comment between rules, rather than to rule
299-
applicableNode = getApplicableNode(node);
295+
applicableNode = getApplicableNode('atrule', node);
300296

301297
if (applicableNode) {
302298
applicableNode.raws.before = createLineBreaks(linesBetweenMediaRules) + applicableNode.raws.before;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-sorting",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "PostCSS plugin to sort rules content with specified order.",
55
"keywords": [
66
"postcss",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.color {
2+
@mixin coloring;
3+
color: white;
4+
@media (--viewport-md) {
5+
color: green;
6+
}
7+
@media (--viewport-lg) {
8+
color: red;
9+
}
10+
span {
11+
color: black;
12+
}
13+
span {
14+
color: pink;
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.color {
2+
@mixin coloring;
3+
4+
color: white;
5+
6+
@media (--viewport-md) {
7+
color: green;
8+
}
9+
10+
@media (--viewport-lg) {
11+
color: red;
12+
}
13+
14+
span {
15+
color: black;
16+
}
17+
18+
span {
19+
color: pink;
20+
}
21+
}

test/test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,19 @@ test('Should insert empty lines between @media rules in accordance with option \
226226
});
227227
});
228228

229+
test('Should not insert additional line between @media and children rules if they have empty lines inside group', t => {
230+
return run(t, 'lines-between-rules-issue-19', {
231+
"sort-order": [
232+
["@mixin"],
233+
["..."],
234+
["@media"],
235+
[">child"]
236+
],
237+
"empty-lines-between-children-rules": 1,
238+
"empty-lines-between-media-rules": 1
239+
});
240+
});
241+
229242
// test('Should sort LESS files', t => {
230243
// return run(t, 'less.less', {}, 'less');
231244
// });

0 commit comments

Comments
 (0)