|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const naturalCompare = require('natural-compare-lite'); |
| 4 | +const astUtils = require('../utils/ast'); |
| 5 | + |
| 6 | +/** |
| 7 | + * Gets the property name of the given `Property` node. |
| 8 | + * |
| 9 | + * - If the property's key is an `Identifier` node, this returns the key's name |
| 10 | + * whether it's a computed property or not. |
| 11 | + * - If the property has a static name, this returns the static name. |
| 12 | + * - Otherwise, this returns null. |
| 13 | + * |
| 14 | + * @param {ASTNode} node - The `Property` node to get. |
| 15 | + * @returns {string|null} The property name or null. |
| 16 | + * @private |
| 17 | + */ |
| 18 | +function getPropertyName(node) { |
| 19 | + return astUtils.getStaticPropertyName(node) || node.key.name || null; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Functions which check that the given 2 names are in specific order. |
| 24 | + * |
| 25 | + * Postfix `I` is meant insensitive. |
| 26 | + * Postfix `N` is meant natural. |
| 27 | + * |
| 28 | + * @private |
| 29 | + */ |
| 30 | +const isValidOrders = { |
| 31 | + asc(a, b) { |
| 32 | + return a <= b; |
| 33 | + }, |
| 34 | + ascI(a, b) { |
| 35 | + return a.toLowerCase() <= b.toLowerCase(); |
| 36 | + }, |
| 37 | + ascN(a, b) { |
| 38 | + return naturalCompare(a, b) <= 0; |
| 39 | + }, |
| 40 | + ascIN(a, b) { |
| 41 | + return naturalCompare(a.toLowerCase(), b.toLowerCase()) <= 0; |
| 42 | + }, |
| 43 | + desc(a, b) { |
| 44 | + return isValidOrders.asc(b, a); |
| 45 | + }, |
| 46 | + descI(a, b) { |
| 47 | + return isValidOrders.ascI(b, a); |
| 48 | + }, |
| 49 | + descN(a, b) { |
| 50 | + return isValidOrders.ascN(b, a); |
| 51 | + }, |
| 52 | + descIN(a, b) { |
| 53 | + return isValidOrders.ascIN(b, a); |
| 54 | + }, |
| 55 | +}; |
| 56 | + |
| 57 | +module.exports = { |
| 58 | + meta: { |
| 59 | + type: 'suggestion', |
| 60 | + |
| 61 | + docs: { |
| 62 | + description: 'require interface keys to be sorted', |
| 63 | + category: 'Stylistic Issues', |
| 64 | + recommended: false, |
| 65 | + url: 'https://eslint.org/docs/rules/sort-keys', // TODO |
| 66 | + }, |
| 67 | + // fixable: 'code', |
| 68 | + |
| 69 | + schema: [ |
| 70 | + { |
| 71 | + enum: ['asc', 'desc'], |
| 72 | + }, |
| 73 | + { |
| 74 | + type: 'object', |
| 75 | + properties: { |
| 76 | + caseSensitive: { |
| 77 | + type: 'boolean', |
| 78 | + }, |
| 79 | + natural: { |
| 80 | + type: 'boolean', |
| 81 | + }, |
| 82 | + }, |
| 83 | + additionalProperties: false, |
| 84 | + }, |
| 85 | + ], |
| 86 | + }, |
| 87 | + create: function create(context) { |
| 88 | + // Parse options. |
| 89 | + const order = context.options[0] || 'asc'; |
| 90 | + const options = context.options[1]; |
| 91 | + const insensitive = (options && options.caseSensitive) === false; |
| 92 | + const natural = Boolean(options && options.natural); |
| 93 | + const computedOrder = [order, insensitive && 'I', natural && 'N'] |
| 94 | + .filter(Boolean) |
| 95 | + .join(''); |
| 96 | + const isValidOrder = isValidOrders[computedOrder]; |
| 97 | + |
| 98 | + // The stack to save the previous property's name for each object literals. |
| 99 | + let stack = null; |
| 100 | + |
| 101 | + const visitor = node => { |
| 102 | + const { prevName } = stack; |
| 103 | + const thisName = getPropertyName(node); |
| 104 | + |
| 105 | + stack.prevName = thisName || prevName; |
| 106 | + |
| 107 | + if (!prevName || !thisName) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + if (!isValidOrder(prevName, thisName)) { |
| 112 | + context.report({ |
| 113 | + node, |
| 114 | + loc: node.key.loc, |
| 115 | + message: |
| 116 | + "Expected interface keys to be in {{natural}}{{insensitive}}{{order}}ending order. '{{thisName}}' should be before '{{prevName}}'.", |
| 117 | + data: { |
| 118 | + thisName, |
| 119 | + prevName, |
| 120 | + order, |
| 121 | + insensitive: insensitive ? 'insensitive ' : '', |
| 122 | + natural: natural ? 'natural ' : '', |
| 123 | + }, |
| 124 | + }); |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + return { |
| 129 | + TSInterfaceDeclaration() { |
| 130 | + stack = { |
| 131 | + upper: stack, |
| 132 | + prevName: null, |
| 133 | + }; |
| 134 | + }, |
| 135 | + |
| 136 | + 'TSInterfaceDeclaration:exit'() { |
| 137 | + stack = stack.upper; |
| 138 | + }, |
| 139 | + |
| 140 | + TSPropertySignature(node) { |
| 141 | + return visitor(node); |
| 142 | + }, |
| 143 | + |
| 144 | + TSMethodSignature(node) { |
| 145 | + return visitor(node); |
| 146 | + }, |
| 147 | + }; |
| 148 | + }, |
| 149 | +}; |
0 commit comments