@@ -2,12 +2,18 @@ var postcss = require('postcss');
2
2
var path = require ( 'path' ) ;
3
3
var fs = require ( 'fs' ) ;
4
4
5
- function getSortOrderFromOptions ( options ) {
6
- // If no options use default config
7
- if ( options === null || typeof options !== 'object' || ! options [ 'sort-order' ] ) {
8
- options = { 'sort-order' : 'default' } ;
5
+ function verifyOptions ( options ) {
6
+ if ( options === null || typeof options !== 'object' ) {
7
+ options = { } ;
9
8
}
10
9
10
+ options [ 'sort-order' ] = options [ 'sort-order' ] || 'default' ;
11
+ options [ 'empty-lines-between-children-rules' ] = options [ 'empty-lines-between-children-rules' ] || 0 ;
12
+
13
+ return options ;
14
+ }
15
+
16
+ function getSortOrderFromOptions ( options ) {
11
17
var sortOrder ;
12
18
13
19
if ( Array . isArray ( options [ 'sort-order' ] ) ) {
@@ -42,14 +48,9 @@ function getSortOrderFromOptions(options) {
42
48
return order ;
43
49
}
44
50
45
- function getLinesBetweenChildrenFromOptions ( opts ) {
46
- var options = opts || { } ;
51
+ function getLinesBetweenChildrenFromOptions ( options ) {
47
52
var lines = options [ 'empty-lines-between-children-rules' ] ;
48
53
49
- if ( lines === undefined || lines === null ) {
50
- return 0 ;
51
- }
52
-
53
54
if ( typeof lines !== 'number' || isNaN ( lines ) || ! isFinite ( lines ) || lines < 0 || Math . floor ( lines ) !== lines ) {
54
55
throw new Error ( 'Type of "empty-lines-between-children-rules" option must be integer with positive value.' ) ;
55
56
}
@@ -158,12 +159,13 @@ function fetchAllCommentsAfterNode(comments, nextNode, node) {
158
159
return fetchAllCommentsAfterNode ( comments . concat ( nextNode ) , nextNode . next ( ) , node ) ;
159
160
}
160
161
161
-
162
162
module . exports = postcss . plugin ( 'postcss-sorting' , function ( opts ) {
163
- var linesBetweenChildrenRules = getLinesBetweenChildrenFromOptions ( opts ) ;
163
+ // Verify options and use defaults if not specified
164
+ opts = verifyOptions ( opts ) ;
164
165
165
166
return function ( css ) {
166
167
var order = getSortOrderFromOptions ( opts ) ;
168
+ var linesBetweenChildrenRules = getLinesBetweenChildrenFromOptions ( opts ) ;
167
169
168
170
// Index to place the nodes that shouldn't be sorted
169
171
var lastGroupIndex = order [ '...' ] ? order [ '...' ] . group : Infinity ;
0 commit comments