|
| 1 | +/** |
| 2 | + * @fileoverview Rule to require StyleSheet object keys to be sorted |
| 3 | + * @author Mats Byrkjeland |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | +// Requirements |
| 10 | +//------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +const { astHelpers } = require('../util/stylesheet'); |
| 13 | + |
| 14 | +const { getStyleDeclarations, isStyleSheetDeclaration } = astHelpers; |
| 15 | + |
| 16 | +//------------------------------------------------------------------------------ |
| 17 | +// Rule Definition |
| 18 | +//------------------------------------------------------------------------------ |
| 19 | + |
| 20 | +module.exports = (context) => { |
| 21 | + const order = context.options[0] || 'asc'; |
| 22 | + const options = context.options[1] || {}; |
| 23 | + const ignoreClassNames = options.ignoreClassNames; |
| 24 | + const ignoreStyleProperties = options.ignoreStyleProperties; |
| 25 | + const isValidOrder = order === 'asc' ? (a, b) => a <= b : (a, b) => a >= b; |
| 26 | + |
| 27 | + function report(type, node, prev, current) { |
| 28 | + const currentName = current.key.name; |
| 29 | + const prevName = prev.key.name; |
| 30 | + context.report({ |
| 31 | + node, |
| 32 | + message: `Expected ${type} to be in ${order}ending order. '${currentName}' should be before '${prevName}'.`, |
| 33 | + loc: current.key.loc, |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + function checkIsSorted(array, arrayName, node) { |
| 38 | + for (let i = 1; i < array.length; i += 1) { |
| 39 | + const previous = array[i - 1]; |
| 40 | + const current = array[i]; |
| 41 | + |
| 42 | + if (previous.type !== 'Property' || current.type !== 'Property') { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + if (!isValidOrder(previous.key.name, current.key.name)) { |
| 47 | + return report(arrayName, node, previous, current); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return { |
| 53 | + VariableDeclarator: function (node) { |
| 54 | + if (!isStyleSheetDeclaration(node, context.settings)) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + const classDefinitions = getStyleDeclarations(node); |
| 59 | + |
| 60 | + if (!ignoreClassNames) { |
| 61 | + checkIsSorted(classDefinitions, 'class names', node); |
| 62 | + } |
| 63 | + |
| 64 | + if (ignoreStyleProperties) return; |
| 65 | + |
| 66 | + classDefinitions.forEach((classDefinition) => { |
| 67 | + const styleProperties = classDefinition.value.properties; |
| 68 | + if (!styleProperties || styleProperties.length < 2) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + checkIsSorted(styleProperties, 'style properties', node); |
| 73 | + }); |
| 74 | + }, |
| 75 | + }; |
| 76 | +}; |
| 77 | + |
| 78 | +module.exports.schema = [ |
| 79 | + { |
| 80 | + enum: ['asc', 'desc'], |
| 81 | + }, |
| 82 | + { |
| 83 | + type: 'object', |
| 84 | + properties: { |
| 85 | + ignoreClassNames: { |
| 86 | + type: 'boolean', |
| 87 | + }, |
| 88 | + ignoreStyleProperties: { |
| 89 | + type: 'boolean', |
| 90 | + }, |
| 91 | + }, |
| 92 | + additionalProperties: false, |
| 93 | + }, |
| 94 | +]; |
0 commit comments