Skip to content

Commit 2011cc1

Browse files
committed
Add empty-lines-between-media-rules option #16
1 parent 5ad4a1f commit 2011cc1

File tree

5 files changed

+141
-8
lines changed

5 files changed

+141
-8
lines changed

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ $ npm install postcss-sorting
2626
```json
2727
{
2828
"sort-order": "default",
29-
"empty-lines-between-children-rules": 0
29+
"empty-lines-between-children-rules": 0,
30+
"empty-lines-between-media-rules": 0
3031
}
3132
```
3233

@@ -248,7 +249,7 @@ Example: `{ "sort-order": "zen" }`
248249

249250
### `empty-lines-between-children-rules`
250251

251-
Set a number of empty lines between nested children rules. By default there is no empty lines between '>child' rules.
252+
Set a number of empty lines between nested children rules. By default there is no empty lines between `>child` rules.
252253

253254
Acceptable value: `{Number}` of empty lines
254255

@@ -290,6 +291,34 @@ Example: `{ "empty-lines-between-children-rules": 1, "sort-order": [ ["..."], ["
290291
}
291292
```
292293

294+
### `empty-lines-between-media-rules`
295+
296+
Set a number of empty lines between nested media rules. By default there is no empty lines between `@media` rules.
297+
298+
Acceptable value: `{Number}` of empty lines
299+
300+
Example: `{ "empty-lines-between-media-rules": 1, "sort-order": ["@media"] }`
301+
302+
```scss
303+
/* before */
304+
.block {
305+
@media (min-width: 1px) {}
306+
307+
308+
@media (min-width: 2px) {}
309+
@media (min-width: 3px) {}
310+
}
311+
312+
/* after */
313+
.block {
314+
@media (min-width: 1px) {}
315+
316+
@media (min-width: 2px) {}
317+
318+
@media (min-width: 3px) {}
319+
}
320+
```
321+
293322
## Usage
294323

295324
See [PostCSS] docs for examples for your environment.

index.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function verifyOptions(options) {
99

1010
options['sort-order'] = options['sort-order'] || 'default';
1111
options['empty-lines-between-children-rules'] = options['empty-lines-between-children-rules'] || 0;
12+
options['empty-lines-between-media-rules'] = options['empty-lines-between-media-rules'] || 0;
1213

1314
return options;
1415
}
@@ -48,11 +49,11 @@ function getSortOrderFromOptions(options) {
4849
return order;
4950
}
5051

51-
function getLinesBetweenChildrenFromOptions(options) {
52-
var lines = options['empty-lines-between-children-rules'];
52+
function getLinesBetweenRulesFromOptions(name, options) {
53+
var lines = options['empty-lines-between-' + name + '-rules'];
5354

5455
if (typeof lines !== 'number' || isNaN(lines) || !isFinite(lines) || lines < 0 || Math.floor(lines) !== lines) {
55-
throw new Error('Type of "empty-lines-between-children-rules" option must be integer with positive value.');
56+
throw new Error('Type of "empty-lines-between-' + name + '-rules" option must be integer with positive value.');
5657
}
5758

5859
return lines;
@@ -188,6 +189,10 @@ function getApplicableNode(node) {
188189
return node;
189190
}
190191

192+
if (prevNode.type === 'atrule') {
193+
return node;
194+
}
195+
191196
if (prevNode.type === 'comment') {
192197
return getApplicableNode(prevNode);
193198
}
@@ -201,7 +206,8 @@ module.exports = postcss.plugin('postcss-sorting', function (opts) {
201206

202207
return function (css) {
203208
var order = getSortOrderFromOptions(opts);
204-
var linesBetweenChildrenRules = getLinesBetweenChildrenFromOptions(opts);
209+
var linesBetweenChildrenRules = getLinesBetweenRulesFromOptions('children', opts);
210+
var linesBetweenMediaRules = getLinesBetweenRulesFromOptions('media', opts);
205211

206212
css.walk(function (rule) {
207213
// Process only rules and atrules with nodes
@@ -275,15 +281,27 @@ module.exports = postcss.plugin('postcss-sorting', function (opts) {
275281
node.raws.before = createLineBreaks(1) + node.raws.before;
276282
}
277283

284+
var applicableNode;
285+
278286
// Insert empty lines between children classes
279287
if (node.type === 'rule' && linesBetweenChildrenRules > 0) {
280-
// between child rules can be comments, so empty lines should be added to first comment between rules, rather than to rule
281-
var applicableNode = getApplicableNode(node);
288+
// between rules can be comments, so empty lines should be added to first comment between rules, rather than to rule
289+
applicableNode = getApplicableNode(node);
282290

283291
if (applicableNode) {
284292
applicableNode.raws.before = createLineBreaks(linesBetweenChildrenRules) + applicableNode.raws.before;
285293
}
286294
}
295+
296+
// Insert empty lines between media rules
297+
if (node.type === 'atrule' && node.name === 'media' && linesBetweenMediaRules > 0) {
298+
// between rules can be comments, so empty lines should be added to first comment between rules, rather than to rule
299+
applicableNode = getApplicableNode(node);
300+
301+
if (applicableNode) {
302+
applicableNode.raws.before = createLineBreaks(linesBetweenMediaRules) + applicableNode.raws.before;
303+
}
304+
}
287305
}
288306
});
289307
}

test/fixtures/lines-between-media.css

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.block {
2+
@media (--platform-organize--lap) {
3+
padding-right: 0;
4+
}
5+
@media (min-width: 900px) {
6+
padding-right: 0;
7+
}
8+
}
9+
.block {
10+
11+
12+
13+
14+
@media (--platform-organize--lap) {
15+
padding-right: 0;
16+
}
17+
18+
19+
20+
21+
22+
23+
24+
25+
@media (min-width: 900px) {
26+
padding-right: 0;
27+
}
28+
}
29+
.block {
30+
display: none;
31+
32+
@media (min-width: 1px) {}
33+
/* comment 1 */
34+
@media (min-width: 2px) {}
35+
36+
37+
38+
39+
/* comment 2 */
40+
/* comment 3 */
41+
@media (min-width: 3px) {}
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.block {
2+
@media (--platform-organize--lap) {
3+
padding-right: 0;
4+
}
5+
6+
7+
@media (min-width: 900px) {
8+
padding-right: 0;
9+
}
10+
}
11+
.block {
12+
@media (--platform-organize--lap) {
13+
padding-right: 0;
14+
}
15+
16+
17+
@media (min-width: 900px) {
18+
padding-right: 0;
19+
}
20+
}
21+
.block {
22+
display: none;
23+
24+
@media (min-width: 1px) {}
25+
26+
27+
/* comment 1 */
28+
@media (min-width: 2px) {}
29+
30+
31+
/* comment 2 */
32+
/* comment 3 */
33+
@media (min-width: 3px) {}
34+
}

test/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ test('Should insert empty lines between children classes in accordance with opti
216216
});
217217
});
218218

219+
test('Should insert empty lines between @media rules in accordance with option \'empty-lines-between-media-rules\'', t => {
220+
return run(t, 'lines-between-media', {
221+
'sort-order': [
222+
['...'],
223+
['@media']
224+
],
225+
'empty-lines-between-media-rules': 2
226+
});
227+
});
228+
219229
// test('Should sort LESS files', t => {
220230
// return run(t, 'less.less', {}, 'less');
221231
// });

0 commit comments

Comments
 (0)