|
| 1 | +/** |
| 2 | + * @fileoverview Enforce default props alphabetical sorting |
| 3 | + * @author Vladimir Kattsov |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const variableUtil = require('../util/variable'); |
| 8 | + |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | +// Rule Definition |
| 11 | +// ------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +module.exports = { |
| 14 | + meta: { |
| 15 | + docs: { |
| 16 | + description: 'Enforce default props alphabetical sorting', |
| 17 | + category: 'Stylistic Issues', |
| 18 | + recommended: false |
| 19 | + }, |
| 20 | + |
| 21 | + schema: [{ |
| 22 | + type: 'object', |
| 23 | + properties: { |
| 24 | + ignoreCase: { |
| 25 | + type: 'boolean' |
| 26 | + } |
| 27 | + }, |
| 28 | + additionalProperties: false |
| 29 | + }] |
| 30 | + }, |
| 31 | + |
| 32 | + create: function(context) { |
| 33 | + const sourceCode = context.getSourceCode(); |
| 34 | + const configuration = context.options[0] || {}; |
| 35 | + const ignoreCase = configuration.ignoreCase || false; |
| 36 | + const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []); |
| 37 | + |
| 38 | + /** |
| 39 | + * Get properties name |
| 40 | + * @param {Object} node - Property. |
| 41 | + * @returns {String} Property name. |
| 42 | + */ |
| 43 | + function getPropertyName(node) { |
| 44 | + if (node.key || ['MethodDefinition', 'Property'].indexOf(node.type) !== -1) { |
| 45 | + return node.key.name; |
| 46 | + } else if (node.type === 'MemberExpression') { |
| 47 | + return node.property.name; |
| 48 | + // Special case for class properties |
| 49 | + // (babel-eslint@5 does not expose property name so we have to rely on tokens) |
| 50 | + } else if (node.type === 'ClassProperty') { |
| 51 | + const tokens = context.getFirstTokens(node, 2); |
| 52 | + return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value; |
| 53 | + } |
| 54 | + return ''; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Checks if the Identifier node passed in looks like a defaultProps declaration. |
| 59 | + * @param {ASTNode} node The node to check. Must be an Identifier node. |
| 60 | + * @returns {Boolean} `true` if the node is a defaultProps declaration, `false` if not |
| 61 | + */ |
| 62 | + function isDefaultPropsDeclaration(node) { |
| 63 | + const propName = getPropertyName(node); |
| 64 | + return (propName === 'defaultProps' || propName === 'getDefaultProps'); |
| 65 | + } |
| 66 | + |
| 67 | + function getKey(node) { |
| 68 | + return sourceCode.getText(node.key || node.argument); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Find a variable by name in the current scope. |
| 73 | + * @param {string} name Name of the variable to look for. |
| 74 | + * @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise. |
| 75 | + */ |
| 76 | + function findVariableByName(name) { |
| 77 | + const variable = variableUtil.variablesInScope(context).find(item => item.name === name); |
| 78 | + |
| 79 | + if (!variable || !variable.defs[0] || !variable.defs[0].node) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + if (variable.defs[0].node.type === 'TypeAlias') { |
| 84 | + return variable.defs[0].node.right; |
| 85 | + } |
| 86 | + |
| 87 | + return variable.defs[0].node.init; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Checks if defaultProps declarations are sorted |
| 92 | + * @param {Array} declarations The array of AST nodes being checked. |
| 93 | + * @returns {void} |
| 94 | + */ |
| 95 | + function checkSorted(declarations) { |
| 96 | + declarations.reduce((prev, curr, idx, decls) => { |
| 97 | + if (/SpreadProperty$/.test(curr.type)) { |
| 98 | + return decls[idx + 1]; |
| 99 | + } |
| 100 | + |
| 101 | + let prevPropName = getKey(prev); |
| 102 | + let currentPropName = getKey(curr); |
| 103 | + |
| 104 | + if (ignoreCase) { |
| 105 | + prevPropName = prevPropName.toLowerCase(); |
| 106 | + currentPropName = currentPropName.toLowerCase(); |
| 107 | + } |
| 108 | + |
| 109 | + if (currentPropName < prevPropName) { |
| 110 | + context.report({ |
| 111 | + node: curr, |
| 112 | + message: 'Default prop types declarations should be sorted alphabetically' |
| 113 | + }); |
| 114 | + |
| 115 | + return prev; |
| 116 | + } |
| 117 | + |
| 118 | + return curr; |
| 119 | + }, declarations[0]); |
| 120 | + } |
| 121 | + |
| 122 | + function checkNode(node) { |
| 123 | + switch (node && node.type) { |
| 124 | + case 'ObjectExpression': |
| 125 | + checkSorted(node.properties); |
| 126 | + break; |
| 127 | + case 'Identifier': |
| 128 | + const propTypesObject = findVariableByName(node.name); |
| 129 | + if (propTypesObject && propTypesObject.properties) { |
| 130 | + checkSorted(propTypesObject.properties); |
| 131 | + } |
| 132 | + break; |
| 133 | + case 'CallExpression': |
| 134 | + const innerNode = node.arguments && node.arguments[0]; |
| 135 | + if (propWrapperFunctions.has(node.callee.name) && innerNode) { |
| 136 | + checkNode(innerNode); |
| 137 | + } |
| 138 | + break; |
| 139 | + default: |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // -------------------------------------------------------------------------- |
| 145 | + // Public API |
| 146 | + // -------------------------------------------------------------------------- |
| 147 | + |
| 148 | + return { |
| 149 | + ClassProperty: function(node) { |
| 150 | + if (!isDefaultPropsDeclaration(node)) { |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + checkNode(node.value); |
| 155 | + }, |
| 156 | + |
| 157 | + MemberExpression: function(node) { |
| 158 | + if (!isDefaultPropsDeclaration(node)) { |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + checkNode(node.parent.right); |
| 163 | + } |
| 164 | + }; |
| 165 | + } |
| 166 | +}; |
0 commit comments