Skip to content

Commit f946b42

Browse files
committed
Refactor to allow stylelint-order access to sorting without running postcss-sorting as plugin
1 parent 153c620 commit f946b42

File tree

5 files changed

+135
-144
lines changed

5 files changed

+135
-144
lines changed

index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const order = require('./lib/order');
2-
const propertiesOrder = require('./lib/properties-order');
31
const validateOptions = require('./lib/validateOptions');
42
const isString = require('./lib/isString');
3+
const getContainingNode = require('./lib/getContainingNode');
4+
const sortNode = require('./lib/order/sortNode');
5+
const sortNodeProperties = require('./lib/properties-order/sortNodeProperties');
56

67
module.exports = (opts) => {
78
return {
@@ -37,10 +38,21 @@ function plugin(css, opts) {
3738
}
3839

3940
if (opts.order) {
40-
order(css, opts);
41+
css.walk((input) => {
42+
const node = getContainingNode(input);
43+
44+
sortNode(node, opts.order);
45+
});
4146
}
4247

4348
if (opts['properties-order']) {
44-
propertiesOrder(css, opts);
49+
css.walk((input) => {
50+
const node = getContainingNode(input);
51+
52+
sortNodeProperties(node, {
53+
order: opts['properties-order'],
54+
unspecifiedPropertiesPosition: opts['unspecified-properties-position'] || 'bottom',
55+
});
56+
});
4557
}
4658
}

lib/order/index.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

lib/order/sortNode.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const createExpectedOrder = require('../createExpectedOrder');
2+
const isRuleWithNodes = require('../isRuleWithNodes');
3+
const processLastComments = require('../processLastComments');
4+
const processMostNodes = require('../processMostNodes');
5+
const sorting = require('../sorting');
6+
7+
module.exports = function sortNode(node, optsOrder) {
8+
if (!isRuleWithNodes(node)) {
9+
return;
10+
}
11+
12+
const expectedOrder = createExpectedOrder(optsOrder);
13+
14+
// Nodes for sorting
15+
let processed = [];
16+
17+
// Add indexes to nodes
18+
node.each((childNode, index) => {
19+
processed = processMostNodes(childNode, index, expectedOrder, processed);
20+
});
21+
22+
// Add last comments in the rule. Need this because last comments are not belonging to anything
23+
node.each((childNode, index) => {
24+
processed = processLastComments(childNode, index, processed);
25+
});
26+
27+
// Sort declarations saved for sorting
28+
processed.sort(sorting.sortByIndexes);
29+
30+
// Enforce semicolon for the last node
31+
node.raws.semicolon = true;
32+
33+
// Replace rule content with sorted one
34+
node.removeAll();
35+
node.append(processed);
36+
};

lib/properties-order/index.js

Lines changed: 0 additions & 101 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
let createExpectedPropertiesOrder = require('../createExpectedPropertiesOrder');
2+
let getComments = require('../getComments');
3+
let getPropertiesOrderData = require('../getPropertiesOrderData');
4+
let isCustomProperty = require('../isCustomProperty');
5+
let isRuleWithNodes = require('../isRuleWithNodes');
6+
let isStandardSyntaxProperty = require('../isStandardSyntaxProperty');
7+
let sorting = require('../sorting');
8+
let vendor = require('../vendor');
9+
10+
module.exports = function sortNodeProperties(node, { order, unspecifiedPropertiesPosition }) {
11+
if (!isRuleWithNodes(node)) {
12+
return;
13+
}
14+
15+
let isAlphabetical = order === 'alphabetical';
16+
let expectedOrder = isAlphabetical ? null : createExpectedPropertiesOrder(order);
17+
18+
let allRuleNodes = [];
19+
let declarations = [];
20+
21+
node.each((childNode, index) => {
22+
if (
23+
childNode.type === 'decl' &&
24+
isStandardSyntaxProperty(childNode.prop) &&
25+
!isCustomProperty(childNode.prop)
26+
) {
27+
let unprefixedPropName = vendor.unprefixed(childNode.prop);
28+
29+
// Hack to allow -moz-osx-font-smoothing to be understood
30+
// just like -webkit-font-smoothing
31+
if (unprefixedPropName.indexOf('osx-') === 0) {
32+
unprefixedPropName = unprefixedPropName.slice(4);
33+
}
34+
35+
let propData = {
36+
name: childNode.prop,
37+
unprefixedName: unprefixedPropName,
38+
orderData: isAlphabetical
39+
? null
40+
: getPropertiesOrderData(expectedOrder, unprefixedPropName),
41+
node: childNode,
42+
initialIndex: index,
43+
unspecifiedPropertiesPosition,
44+
};
45+
46+
// add a marker
47+
childNode.sortProperty = true;
48+
49+
// If comment on separate line before node, use node's indexes for comment
50+
let commentsBefore = getComments.beforeDeclaration([], childNode.prev(), propData);
51+
52+
// If comment on same line with the node and node, use node's indexes for comment
53+
let commentsAfter = getComments.afterDeclaration([], childNode.next(), propData);
54+
55+
declarations = declarations.concat(commentsBefore, propData, commentsAfter);
56+
}
57+
});
58+
59+
if (isAlphabetical) {
60+
declarations.sort(sorting.sortDeclarationsAlphabetically);
61+
} else {
62+
declarations.sort(sorting.sortDeclarations);
63+
}
64+
65+
let foundDeclarations = false;
66+
67+
node.each((childNode) => {
68+
if (childNode.sortProperty) {
69+
if (!foundDeclarations) {
70+
foundDeclarations = true;
71+
72+
declarations.forEach((item) => {
73+
allRuleNodes.push(item.node);
74+
});
75+
}
76+
} else {
77+
allRuleNodes.push(childNode);
78+
}
79+
});
80+
81+
node.removeAll();
82+
node.append(allRuleNodes);
83+
};

0 commit comments

Comments
 (0)