Skip to content

Commit 0f4c026

Browse files
committed
Merge pull request #9 from scalder27/master
Add space between subclasses, children styles
2 parents a4fc81d + 8a6a5c0 commit 0f4c026

File tree

9 files changed

+152
-5
lines changed

9 files changed

+152
-5
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"rules": {
33
"space-before-function-paren": [2, { "named": "never" }],
4+
"space-after-keywords": [2, "always"],
45
"no-shadow-restricted-names": [2],
56
"computed-property-spacing": [2],
67
"no-empty-character-class": [2],

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Autodetect text files
2+
* text=auto
3+
4+
# Force the following filetypes to have unix eols, so Windows does not break them
5+
*.* text eol=lf

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ npm-debug.log
33
test/**/*.actual.css
44
test/**/*.actual.scss
55
dev/
6+
.idea/

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $ npm install postcss-sorting
2121

2222
## Options
2323

24-
Currently, there is only one option.
24+
Currently, there is only two options.
2525

2626
### `sort-order`
2727

@@ -239,6 +239,50 @@ everything would go into five groups: variables, then group with `position`, the
239239

240240
Example: `{ "sort-order": "zen" }`
241241

242+
### `empty-lines-between-children-rules`
243+
244+
Set number of empty lines between nested children rules. By default there is no empty lines between '>child' rules.
245+
246+
Acceptable value: `{Number}` of empty lines
247+
248+
Example: `{ "empty-lines-between-children-rules": 1, "sort-order": [ ["..."], [">child"] ] }`
249+
250+
```scss
251+
/* before */
252+
.block {
253+
position: absolute;
254+
255+
span {
256+
display: inline-block;
257+
}
258+
259+
260+
&__element {
261+
display: none;
262+
}
263+
&:hover {
264+
top: 0;
265+
}
266+
}
267+
268+
/* after */
269+
.block {
270+
position: absolute;
271+
272+
span {
273+
display: inline-block;
274+
}
275+
276+
&__element {
277+
display: none;
278+
}
279+
280+
&:hover {
281+
top: 0;
282+
}
283+
}
284+
```
285+
242286
## Usage
243287

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

index.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,28 @@ function cleanLineBreaks(node) {
5959
return node;
6060
}
6161

62+
function createLineBreaks(lineBreaksCount) {
63+
return new Array(lineBreaksCount + 1).join('\n');
64+
}
65+
66+
function getLinesBetweenChildrenFromOptions(opts) {
67+
var options = opts || {};
68+
var lines = options['empty-lines-between-children-rules'];
69+
70+
if (lines === undefined || lines === null) {
71+
return 0;
72+
}
73+
74+
if (typeof lines !== 'number' || isNaN(lines) || !isFinite(lines) || lines < 0 || Math.floor(lines) !== lines) {
75+
throw new Error('Type of "empty-lines-between-children-rules" option must be integer with positive value.');
76+
}
77+
78+
return lines;
79+
}
80+
6281
module.exports = postcss.plugin('postcss-sorting', function (opts) {
82+
var linesBetweenChildrenRules = getLinesBetweenChildrenFromOptions(opts);
83+
6384
return function (css) {
6485
var order = getSortOrder(opts);
6586

@@ -196,9 +217,14 @@ module.exports = postcss.plugin('postcss-sorting', function (opts) {
196217

197218
var prevNode = node.prev();
198219

199-
if (prevNode && node.groupIndex > prevNode.groupIndex) {
200-
if (node.raws.before) {
201-
node.raws.before = '\n' + node.raws.before;
220+
if (prevNode && node.raws.before) {
221+
if (node.groupIndex > prevNode.groupIndex) {
222+
node.raws.before = createLineBreaks(1) + node.raws.before;
223+
}
224+
225+
// Insert empty lines between children classes
226+
if (node.type === 'rule' && prevNode.type === 'rule' && linesBetweenChildrenRules > 0) {
227+
node.raws.before = createLineBreaks(linesBetweenChildrenRules) + node.raws.before;
202228
}
203229
}
204230
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
"scripts": {
2828
"test": "ava && eslint index.js test/test.js",
2929
"test2": "ava",
30-
"lint": "eslint index.js test/test.js"
30+
"lint": "eslint index.js test/test.js --fix"
3131
}
3232
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.platform-organize {
2+
background-image: linear-gradient(-45deg, #ff83b2 0%, #da82d8 49%, #b480ff 100%);
3+
&__row {
4+
display: flex;
5+
&:nth-child(2) {
6+
flex-direction: row-reverse;
7+
}
8+
&:nth-child(3) {
9+
10+
&:hover {
11+
margin-bottom: 50px;
12+
}
13+
}
14+
}
15+
16+
17+
18+
19+
20+
&__row:nth-child(3) &__text {
21+
padding-right: 80px;
22+
@media (--platform-organize--lap) {
23+
padding-right: 0;
24+
}
25+
}
26+
&__media {
27+
flex: 0 0 auto;
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.platform-organize {
2+
background-image: linear-gradient(-45deg, #ff83b2 0%, #da82d8 49%, #b480ff 100%);
3+
4+
&__row {
5+
display: flex;
6+
7+
&:nth-child(2) {
8+
flex-direction: row-reverse;
9+
}
10+
11+
12+
&:nth-child(3) {
13+
&:hover {
14+
margin-bottom: 50px;
15+
}
16+
}
17+
}
18+
19+
20+
&__row:nth-child(3) &__text {
21+
padding-right: 80px;
22+
@media (--platform-organize--lap) {
23+
padding-right: 0;
24+
}
25+
}
26+
27+
28+
&__media {
29+
flex: 0 0 auto;
30+
}
31+
}

test/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,13 @@ test('Should sort prefixed propertyes as unprefixed if first one not in order, b
189189
'width'
190190
] });
191191
});
192+
193+
test('Should insert empty lines between children classes in accordance with option \'empty-lines-between-children-rules\'', t => {
194+
return run(t, 'lines-between-children', {
195+
'sort-order': [
196+
['...'],
197+
['>child']
198+
],
199+
'empty-lines-between-children-rules': 2
200+
});
201+
});

0 commit comments

Comments
 (0)