Skip to content

Commit 59db0c1

Browse files
committed
Add special comments to disable processing
1 parent 3ad3b05 commit 59db0c1

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Also available as [Sublime Text plugin], [Atom plugin], and [VS Code plugin].
3232
* [`preserve-empty-lines-between-children-rules`](#preserve-empty-lines-between-children-rules)
3333
* [`empty-lines-before-comment`](#empty-lines-before-comment)
3434
* [`empty-lines-after-comment`](#empty-lines-after-comment)
35+
* [Disabling in style sheet](#disabling-in-style-sheet)
3536
* [Migration from CSSComb](#migration-from-csscomb)
3637
* [Usage](#usage)
3738
* [Text editor](#text-editor)
@@ -504,6 +505,30 @@ Example: `{ "empty-lines-after-comment": 2, "sort-order": [ "..." ] }`
504505
}
505506
```
506507

508+
### Disabling in style sheet
509+
510+
The plugin can be temporarily turned off by using special comments.
511+
512+
```css
513+
/* postcss-sorting: off */
514+
.block1 {
515+
width: 50px;
516+
display: inline-block;
517+
}
518+
/* postcss-sorting: on */
519+
```
520+
521+
Due to plugin nature only comments in the root of stylesheet will affect plugin processing. In this case comments will be treated like regular comments:
522+
523+
```css
524+
.block5 {
525+
/* postcss-sorting: off */
526+
width: 20px;
527+
display: inline-block;
528+
/* postcss-sorting: on */
529+
}
530+
```
531+
507532
### Migration from CSSComb
508533

509534
If you used to use custom sorting order in [CSSComb] you can easily use this sorting order in PostCSS Sorting. `sort-order` option in this plugin is compatible with `sort-order` in CSSComb. Just copy `sort-order` value from CSSComb config to PostCSS Sorting config.

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,21 @@ module.exports = postcss.plugin('postcss-sorting', function (opts) {
221221
var preserveLinesBetweenChildren = opts['preserve-empty-lines-between-children-rules'];
222222
var linesBeforeComment = opts['empty-lines-before-comment'];
223223
var linesAfterComment = opts['empty-lines-after-comment'];
224+
var enableSorting = true;
224225

225226
css.walk(function (rule) {
227+
if (rule.type === 'comment' && rule.parent.type === 'root') {
228+
if (rule.text === 'postcss-sorting: on') {
229+
enableSorting = true;
230+
} else if (rule.text === 'postcss-sorting: off') {
231+
enableSorting = false;
232+
}
233+
}
234+
235+
if (!enableSorting) {
236+
return;
237+
}
238+
226239
// Process only rules and atrules with nodes
227240
if ((rule.type === 'rule' || rule.type === 'atrule') && rule.nodes && rule.nodes.length) {
228241
// Nodes for sorting

test/fixtures/sorting-disabling.css

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* postcss-sorting: off */
2+
.block1 {
3+
width: 50px;
4+
display: inline-block;
5+
}
6+
/* postcss-sorting: on */
7+
8+
.block2 {
9+
width: 40px;
10+
display: inline-block;
11+
}
12+
13+
/* postcss-sorting: off */
14+
.block3 {
15+
width: 20px;
16+
display: inline-block;
17+
18+
/* postcss-sorting: on */
19+
.block3-1 {
20+
width: 70px;
21+
display: block;
22+
}
23+
/* postcss-sorting: off */
24+
}
25+
/* postcss-sorting: on */
26+
27+
.block5 {
28+
width: 20px;
29+
/* postcss-sorting: off */
30+
.block3-1 {
31+
width: 70px;
32+
display: block;
33+
}
34+
/* postcss-sorting: on */
35+
display: inline-block;
36+
}
37+
38+
.block4 {
39+
width: 30px;
40+
display: inline-block;
41+
}
42+
43+
/* postcss-sorting: off */
44+
.block6 {
45+
width: 30px;
46+
display: inline-block;
47+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* postcss-sorting: off */
2+
.block1 {
3+
width: 50px;
4+
display: inline-block;
5+
}
6+
/* postcss-sorting: on */
7+
8+
.block2 {
9+
display: inline-block;
10+
width: 40px;
11+
}
12+
13+
/* postcss-sorting: off */
14+
.block3 {
15+
width: 20px;
16+
display: inline-block;
17+
18+
/* postcss-sorting: on */
19+
.block3-1 {
20+
width: 70px;
21+
display: block;
22+
}
23+
/* postcss-sorting: off */
24+
}
25+
/* postcss-sorting: on */
26+
27+
.block5 {
28+
/* postcss-sorting: on */
29+
display: inline-block;
30+
width: 20px;
31+
32+
/* postcss-sorting: off */
33+
.block3-1 {
34+
display: block;
35+
width: 70px;
36+
}
37+
}
38+
39+
.block4 {
40+
display: inline-block;
41+
width: 30px;
42+
}
43+
44+
/* postcss-sorting: off */
45+
.block6 {
46+
width: 30px;
47+
display: inline-block;
48+
}

test/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,20 @@ test('Should add empty lines after comment', t => {
276276
});
277277
});
278278

279+
test('Should disable/enable sorting by special comments', t => {
280+
return run(t, 'sorting-disabling', {
281+
'sort-order': [
282+
[
283+
'display',
284+
'width'
285+
],
286+
[
287+
'>child'
288+
]
289+
]
290+
});
291+
});
292+
279293
// test('Should sort LESS files', t => {
280294
// return run(t, 'less.less', {}, 'less');
281295
// });

0 commit comments

Comments
 (0)