-
Notifications
You must be signed in to change notification settings - Fork 62
/
checkOrder.js
91 lines (74 loc) · 2.58 KB
/
checkOrder.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
const postcss = require('postcss');
const _ = require('lodash');
const checkAlphabeticalOrder = require('../checkAlphabeticalOrder');
module.exports = function checkOrder({ firstPropData, secondPropData, allPropData, unspecified }) {
function report(result, firstNode = firstPropData, secondNode = secondPropData) {
return {
result,
firstNode,
secondNode,
};
}
if (firstPropData.unprefixedName === secondPropData.unprefixedName) {
// If first property has no prefix and second property has prefix
if (
!postcss.vendor.prefix(firstPropData.name).length &&
postcss.vendor.prefix(secondPropData.name).length
) {
return report(false);
}
return report(true);
}
const firstPropIsUnspecified = !firstPropData.orderData;
const secondPropIsUnspecified = !secondPropData.orderData;
// Check actual known properties
if (!firstPropIsUnspecified && !secondPropIsUnspecified) {
return report(
firstPropData.orderData.expectedPosition <= secondPropData.orderData.expectedPosition
);
}
if (firstPropIsUnspecified && !secondPropIsUnspecified) {
// If first prop is unspecified, look for a specified prop before it to
// compare to the current prop
const priorSpecifiedPropData = _.findLast(allPropData.slice(0, -1), d => Boolean(d.orderData));
if (
priorSpecifiedPropData &&
priorSpecifiedPropData.orderData &&
priorSpecifiedPropData.orderData.expectedPosition > secondPropData.orderData.expectedPosition
) {
return report(false, priorSpecifiedPropData, secondPropData);
}
}
// Now deal with unspecified props
// Starting with bottomAlphabetical as it requires more specific conditionals
if (unspecified === 'bottomAlphabetical' && !firstPropIsUnspecified && secondPropIsUnspecified) {
return report(true);
}
if (unspecified === 'bottomAlphabetical' && firstPropIsUnspecified && secondPropIsUnspecified) {
if (checkAlphabeticalOrder(firstPropData, secondPropData)) {
return report(true);
}
return report(false);
}
if (unspecified === 'bottomAlphabetical' && firstPropIsUnspecified) {
return report(false);
}
if (firstPropIsUnspecified && secondPropIsUnspecified) {
return report(true);
}
if (unspecified === 'ignore' && (firstPropIsUnspecified || secondPropIsUnspecified)) {
return report(true);
}
if (unspecified === 'top' && firstPropIsUnspecified) {
return report(true);
}
if (unspecified === 'top' && secondPropIsUnspecified) {
return report(false);
}
if (unspecified === 'bottom' && secondPropIsUnspecified) {
return report(true);
}
if (unspecified === 'bottom' && firstPropIsUnspecified) {
return report(false);
}
};