-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
140 lines (119 loc) · 3.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict';
const { readFile } = require('fs').promises;
const path = require('path');
const postcss = require('postcss');
const timsort = require('timsort').sort;
const builtInOrders = [
'alphabetical',
'concentric-css',
'smacss',
];
module.exports = postcss.plugin(
'css-declaration-sorter',
({ order = 'alphabetical' } = {}) => css => {
if (typeof order === 'function')
return processCss({ css, comparator: order });
if (!builtInOrders.includes(order))
return Promise.reject(
Error([
`Invalid built-in order '${order}' provided.`,
`Available built-in orders are: ${builtInOrders}`,
].join('\n'))
);
if (order === 'alphabetical')
return Promise.resolve(processCss({ css, order }));
// Load in the array containing the order from a JSON file
return readFile(path.join(__dirname, '..', 'orders', order) + '.json')
.then(data => processCss({ css, order: JSON.parse(data) }));
}
);
function processCss ({ css, order, comparator }) {
const comments = [];
const rulesCache = [];
css.walk(node => {
const nodes = node.nodes;
const type = node.type;
if (type === 'comment') {
// Don't do anything to root comments or the last newline comment
const isNewlineNode = node.raws.before && ~node.raws.before.indexOf('\n');
const lastNewlineNode = isNewlineNode && !node.next();
const onlyNode = !node.prev() && !node.next();
if (lastNewlineNode || onlyNode || node.parent.type === 'root') {
return;
}
if (isNewlineNode) {
const pairedNode = node.next() ? node.next() : node.prev().prev();
if (pairedNode) {
comments.unshift({
'comment': node,
'pairedNode': pairedNode,
'insertPosition': node.next() ? 'Before' : 'After',
});
node.remove();
}
} else {
const pairedNode = node.prev() ? node.prev() : node.next().next();
if (pairedNode) {
comments.push({
'comment': node,
'pairedNode': pairedNode,
'insertPosition': 'After',
});
node.remove();
}
}
return;
}
// Add rule-like nodes to a cache so that we can remove all
// comment nodes before we start sorting.
const isRule = type === 'rule' || type === 'atrule';
if (isRule && nodes && nodes.length > 1) {
rulesCache.push(nodes);
}
});
// Perform a sort once all comment nodes are removed
rulesCache.forEach(nodes => {
sortCssDeclarations({ nodes, order, comparator });
});
// Add comments back to the nodes they are paired with
comments.forEach(node => {
const pairedNode = node.pairedNode;
node.comment.remove();
pairedNode.parent['insert' + node.insertPosition](pairedNode, node.comment);
});
}
// Sort CSS declarations alphabetically or using the set sorting order
function sortCssDeclarations ({ nodes, order, comparator }) {
if (order === 'alphabetical') {
comparator = defaultComparator;
}
if (comparator) {
timsort(nodes, (a, b) => {
if (a.type === 'decl' && b.type === 'decl') {
return comparator(a.prop, b.prop);
} else {
return compareDifferentType(a, b);
}
});
}
else {
timsort(nodes, (a, b) => {
if (a.type === 'decl' && b.type === 'decl') {
const aIndex = order.indexOf(a.prop);
const bIndex = order.indexOf(b.prop);
return defaultComparator(aIndex, bIndex);
} else {
return compareDifferentType(a, b);
}
});
}
}
function defaultComparator (a, b) {
return a === b ? 0 : a < b ? -1 : 1;
}
function compareDifferentType (a, b) {
if (b.type === 'atrule') {
return 0;
}
return a.type === 'decl' ? -1 : b.type === 'decl' ? 1 : 0;
}