Skip to content

Commit f2e1a98

Browse files
committed
Move functions related to properties-order to its folder
1 parent f946b42 commit f2e1a98

File tree

9 files changed

+102
-102
lines changed

9 files changed

+102
-102
lines changed
File renamed without changes.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const sortDeclarationsAlphabetically = require('./sortDeclarationsAlphabetically');
2+
let vendor = require('./vendor');
3+
4+
module.exports = function sortDeclarations(a, b) {
5+
// If unprefixed prop names are the same, compare the prefixed versions
6+
if (a.node.type === 'decl' && b.node.type === 'decl' && a.unprefixedName === b.unprefixedName) {
7+
// If first property has no prefix and second property has prefix
8+
if (!vendor.prefix(a.name).length && vendor.prefix(b.name).length) {
9+
return 1;
10+
}
11+
12+
if (vendor.prefix(a.name).length && !vendor.prefix(b.name).length) {
13+
return -1;
14+
}
15+
}
16+
17+
if (a.orderData && b.orderData !== undefined) {
18+
// If a and b have the same group index, and a's property index is
19+
// higher than b's property index, in a sorted list a appears after
20+
// b:
21+
if (a.orderData.propertyIndex !== b.orderData.propertyIndex) {
22+
return a.orderData.propertyIndex - b.orderData.propertyIndex;
23+
}
24+
}
25+
26+
if (
27+
a.unspecifiedPropertiesPosition === 'bottom' ||
28+
a.unspecifiedPropertiesPosition === 'bottomAlphabetical' ||
29+
b.unspecifiedPropertiesPosition === 'bottomAlphabetical'
30+
) {
31+
if (a.orderData !== undefined && b.orderData === undefined) {
32+
return -1;
33+
}
34+
35+
if (a.orderData === undefined && b.orderData !== undefined) {
36+
return 1;
37+
}
38+
}
39+
40+
if (a.unspecifiedPropertiesPosition === 'top') {
41+
if (a.orderData !== undefined && b.orderData === undefined) {
42+
return 1;
43+
}
44+
45+
if (a.orderData === undefined && b.orderData !== undefined) {
46+
return -1;
47+
}
48+
}
49+
50+
if (a.unspecifiedPropertiesPosition === 'bottomAlphabetical') {
51+
if (a.orderData === undefined && b.orderData === undefined) {
52+
return sortDeclarationsAlphabetically(a, b);
53+
}
54+
}
55+
56+
// If a and b have the same group index and the same property index,
57+
// in a sorted list they appear in the same order they were in
58+
// original array:
59+
return a.initialIndex - b.initialIndex;
60+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
let shorthandData = require('./shorthandData');
2+
let vendor = require('./vendor');
3+
4+
module.exports = function sortDeclarationsAlphabetically(a, b) {
5+
if (isShorthand(a.unprefixedName, b.unprefixedName)) {
6+
return -1;
7+
}
8+
9+
if (isShorthand(b.unprefixedName, a.unprefixedName)) {
10+
return 1;
11+
}
12+
13+
if (a.unprefixedName === b.unprefixedName) {
14+
if (a.node.type === 'decl' && b.node.type === 'decl') {
15+
// If first property has no prefix and second property has prefix
16+
if (!vendor.prefix(a.name).length && vendor.prefix(b.name).length) {
17+
return 1;
18+
}
19+
20+
if (vendor.prefix(a.name).length && !vendor.prefix(b.name).length) {
21+
return -1;
22+
}
23+
}
24+
25+
return a.initialIndex - b.initialIndex;
26+
}
27+
28+
return a.unprefixedName <= b.unprefixedName ? -1 : 1;
29+
};
30+
31+
function isShorthand(a, b) {
32+
const longhands = shorthandData[a] || [];
33+
34+
return longhands.includes(b);
35+
}

lib/properties-order/sortNodeProperties.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
let createExpectedPropertiesOrder = require('../createExpectedPropertiesOrder');
1+
let createExpectedPropertiesOrder = require('./createExpectedPropertiesOrder');
22
let getComments = require('../getComments');
3-
let getPropertiesOrderData = require('../getPropertiesOrderData');
3+
let getPropertiesOrderData = require('./getPropertiesOrderData');
44
let isCustomProperty = require('../isCustomProperty');
55
let isRuleWithNodes = require('../isRuleWithNodes');
66
let isStandardSyntaxProperty = require('../isStandardSyntaxProperty');
7-
let sorting = require('../sorting');
8-
let vendor = require('../vendor');
7+
let sortDeclarations = require('./sortDeclarations');
8+
let sortDeclarationsAlphabetically = require('./sortDeclarationsAlphabetically');
9+
let vendor = require('./vendor');
910

1011
module.exports = function sortNodeProperties(node, { order, unspecifiedPropertiesPosition }) {
1112
if (!isRuleWithNodes(node)) {
@@ -57,9 +58,9 @@ module.exports = function sortNodeProperties(node, { order, unspecifiedPropertie
5758
});
5859

5960
if (isAlphabetical) {
60-
declarations.sort(sorting.sortDeclarationsAlphabetically);
61+
declarations.sort(sortDeclarationsAlphabetically);
6162
} else {
62-
declarations.sort(sorting.sortDeclarations);
63+
declarations.sort(sortDeclarations);
6364
}
6465

6566
let foundDeclarations = false;
File renamed without changes.

lib/sorting.js

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,7 @@
1-
const shorthandData = require('./shorthandData');
2-
const vendor = require('./vendor');
3-
41
module.exports = {
5-
sortDeclarations,
6-
sortDeclarationsAlphabetically,
72
sortByIndexes,
83
};
94

10-
function sortDeclarations(a, b) {
11-
// If unprefixed prop names are the same, compare the prefixed versions
12-
if (a.node.type === 'decl' && b.node.type === 'decl' && a.unprefixedName === b.unprefixedName) {
13-
// If first property has no prefix and second property has prefix
14-
if (!vendor.prefix(a.name).length && vendor.prefix(b.name).length) {
15-
return 1;
16-
}
17-
18-
if (vendor.prefix(a.name).length && !vendor.prefix(b.name).length) {
19-
return -1;
20-
}
21-
}
22-
23-
if (a.orderData && b.orderData !== undefined) {
24-
// If a and b have the same group index, and a's property index is
25-
// higher than b's property index, in a sorted list a appears after
26-
// b:
27-
if (a.orderData.propertyIndex !== b.orderData.propertyIndex) {
28-
return a.orderData.propertyIndex - b.orderData.propertyIndex;
29-
}
30-
}
31-
32-
if (
33-
a.unspecifiedPropertiesPosition === 'bottom' ||
34-
a.unspecifiedPropertiesPosition === 'bottomAlphabetical' ||
35-
b.unspecifiedPropertiesPosition === 'bottomAlphabetical'
36-
) {
37-
if (a.orderData !== undefined && b.orderData === undefined) {
38-
return -1;
39-
}
40-
41-
if (a.orderData === undefined && b.orderData !== undefined) {
42-
return 1;
43-
}
44-
}
45-
46-
if (a.unspecifiedPropertiesPosition === 'top') {
47-
if (a.orderData !== undefined && b.orderData === undefined) {
48-
return 1;
49-
}
50-
51-
if (a.orderData === undefined && b.orderData !== undefined) {
52-
return -1;
53-
}
54-
}
55-
56-
if (a.unspecifiedPropertiesPosition === 'bottomAlphabetical') {
57-
if (a.orderData === undefined && b.orderData === undefined) {
58-
return sortDeclarationsAlphabetically(a, b);
59-
}
60-
}
61-
62-
// If a and b have the same group index and the same property index,
63-
// in a sorted list they appear in the same order they were in
64-
// original array:
65-
return a.initialIndex - b.initialIndex;
66-
}
67-
68-
function isShorthand(a, b) {
69-
const longhands = shorthandData[a] || [];
70-
71-
return longhands.includes(b);
72-
}
73-
74-
function sortDeclarationsAlphabetically(a, b) {
75-
if (isShorthand(a.unprefixedName, b.unprefixedName)) {
76-
return -1;
77-
}
78-
79-
if (isShorthand(b.unprefixedName, a.unprefixedName)) {
80-
return 1;
81-
}
82-
83-
if (a.unprefixedName === b.unprefixedName) {
84-
if (a.node.type === 'decl' && b.node.type === 'decl') {
85-
// If first property has no prefix and second property has prefix
86-
if (!vendor.prefix(a.name).length && vendor.prefix(b.name).length) {
87-
return 1;
88-
}
89-
90-
if (vendor.prefix(a.name).length && !vendor.prefix(b.name).length) {
91-
return -1;
92-
}
93-
}
94-
95-
return a.initialIndex - b.initialIndex;
96-
}
97-
98-
return a.unprefixedName <= b.unprefixedName ? -1 : 1;
99-
}
100-
1015
function sortByIndexes(a, b) {
1026
// If a and b have the same group index, and a's property index is
1037
// higher than b's property index, in a sorted list a appears after

0 commit comments

Comments
 (0)