forked from Intellicode/eslint-plugin-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort-styles.js
More file actions
157 lines (135 loc) · 4.37 KB
/
sort-styles.js
File metadata and controls
157 lines (135 loc) · 4.37 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* @fileoverview Rule to require StyleSheet object keys to be sorted
* @author Mats Byrkjeland
*/
'use strict';
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const { astHelpers } = require('../util/stylesheet');
const {
getStyleDeclarationsChunks,
getPropertiesChunks,
getStylePropertyIdentifier,
isStyleSheetDeclaration,
isEitherShortHand,
} = astHelpers;
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = (context) => {
const order = context.options[0] || 'asc';
const options = context.options[1] || {};
const { ignoreClassNames } = options;
const { ignoreStyleProperties } = options;
const isValidOrder = order === 'asc' ? (a, b) => a <= b : (a, b) => a >= b;
const sourceCode = context.getSourceCode();
function sort(array) {
return [].concat(array).sort((a, b) => {
const identifierA = getStylePropertyIdentifier(a);
const identifierB = getStylePropertyIdentifier(b);
let sortOrder = 0;
if (isEitherShortHand(identifierA, identifierB)) {
return a.range[0] - b.range[0];
} if (identifierA < identifierB) {
sortOrder = -1;
} else if (identifierA > identifierB) {
sortOrder = 1;
}
return sortOrder * (order === 'asc' ? 1 : -1);
});
}
function report(array, type, node, prev, current) {
const currentName = getStylePropertyIdentifier(current);
const prevName = getStylePropertyIdentifier(prev);
const hasComments = array
.map((prop) => sourceCode.getComments(prop))
.reduce(
(hasComment, comment) => hasComment || comment.leading.length > 0 || comment.trailing > 0,
false
);
context.report({
node,
message: `Expected ${type} to be in ${order}ending order. '${currentName}' should be before '${prevName}'.`,
loc: current.key.loc,
fix: hasComments ? undefined : (fixer) => {
const sortedArray = sort(array);
return array
.map((item, i) => {
if (item !== sortedArray[i]) {
return fixer.replaceText(
item,
sourceCode.getText(sortedArray[i])
);
}
return null;
})
.filter(Boolean);
},
});
}
function checkIsSorted(array, arrayName, node) {
for (let i = 1; i < array.length; i += 1) {
const previous = array[i - 1];
const current = array[i];
if (previous.type !== 'Property' || current.type !== 'Property') {
return;
}
const prevName = getStylePropertyIdentifier(previous);
const currentName = getStylePropertyIdentifier(current);
if (
arrayName === 'style properties'
&& isEitherShortHand(prevName, currentName)
) {
return;
}
if (!isValidOrder(prevName, currentName)) {
return report(array, arrayName, node, previous, current);
}
}
}
return {
CallExpression: function (node) {
if (!isStyleSheetDeclaration(node, context.settings)) {
return;
}
const classDefinitionsChunks = getStyleDeclarationsChunks(node);
if (!ignoreClassNames) {
classDefinitionsChunks.forEach((classDefinitions) => {
checkIsSorted(classDefinitions, 'class names', node);
});
}
if (ignoreStyleProperties) return;
classDefinitionsChunks.forEach((classDefinitions) => {
classDefinitions.forEach((classDefinition) => {
const styleProperties = classDefinition.value.properties;
if (!styleProperties || styleProperties.length < 2) {
return;
}
const stylePropertyChunks = getPropertiesChunks(styleProperties);
stylePropertyChunks.forEach((stylePropertyChunk) => {
checkIsSorted(stylePropertyChunk, 'style properties', node);
});
});
});
},
};
};
module.exports.fixable = 'code';
module.exports.schema = [
{
enum: ['asc', 'desc'],
},
{
type: 'object',
properties: {
ignoreClassNames: {
type: 'boolean',
},
ignoreStyleProperties: {
type: 'boolean',
},
},
additionalProperties: false,
},
];