|
| 1 | +// copied from 'https://eslint.org/docs/rules/sort-imports' |
| 2 | +// "css" was added to the sort order to enforce `.css` imports last |
| 3 | + |
| 4 | +module.exports = { |
| 5 | + meta: { |
| 6 | + type: 'suggestion', |
| 7 | + |
| 8 | + docs: { |
| 9 | + description: 'enforce sorted import declarations within modules', |
| 10 | + category: 'ECMAScript 6', |
| 11 | + recommended: false, |
| 12 | + url: 'https://eslint.org/docs/rules/sort-imports' |
| 13 | + }, |
| 14 | + |
| 15 | + schema: [ |
| 16 | + { |
| 17 | + type: 'object', |
| 18 | + properties: { |
| 19 | + ignoreCase: { |
| 20 | + type: 'boolean', |
| 21 | + 'default': false |
| 22 | + }, |
| 23 | + memberSyntaxSortOrder: { |
| 24 | + type: 'array', |
| 25 | + items: { |
| 26 | + 'enum': ['none', 'all', 'multiple', 'single', 'css'] |
| 27 | + }, |
| 28 | + uniqueItems: true, |
| 29 | + minItems: 5, |
| 30 | + maxItems: 5 |
| 31 | + }, |
| 32 | + ignoreDeclarationSort: { |
| 33 | + type: 'boolean', |
| 34 | + 'default': false |
| 35 | + }, |
| 36 | + ignoreMemberSort: { |
| 37 | + type: 'boolean', |
| 38 | + 'default': false |
| 39 | + }, |
| 40 | + allowSeparatedGroups: { |
| 41 | + type: 'boolean', |
| 42 | + 'default': false |
| 43 | + } |
| 44 | + }, |
| 45 | + additionalProperties: false |
| 46 | + } |
| 47 | + ], |
| 48 | + |
| 49 | + fixable: 'code', |
| 50 | + |
| 51 | + messages: { |
| 52 | + sortImportsAlphabetically: 'Imports should be sorted alphabetically.', |
| 53 | + sortMembersAlphabetically: |
| 54 | + 'Member "{{memberName}}" of the import declaration should be sorted alphabetically.', |
| 55 | + unexpectedSyntaxOrder: 'Expected "{{syntaxA}}" syntax before "{{syntaxB}}" syntax.' |
| 56 | + } |
| 57 | + }, |
| 58 | + |
| 59 | + create(context) { |
| 60 | + const configuration = context.options[0] || {}, |
| 61 | + ignoreCase = configuration.ignoreCase || false, |
| 62 | + ignoreDeclarationSort = configuration.ignoreDeclarationSort || false, |
| 63 | + ignoreMemberSort = configuration.ignoreMemberSort || false, |
| 64 | + memberSyntaxSortOrder = configuration.memberSyntaxSortOrder || [ |
| 65 | + 'none', |
| 66 | + 'all', |
| 67 | + 'multiple', |
| 68 | + 'single' |
| 69 | + ], |
| 70 | + allowSeparatedGroups = configuration.allowSeparatedGroups || false, |
| 71 | + sourceCode = context.getSourceCode(); |
| 72 | + let previousDeclaration = null; |
| 73 | + |
| 74 | + /** |
| 75 | + * Gets the used member syntax style. |
| 76 | + * |
| 77 | + * import "my-module.js" --> none |
| 78 | + * import * as myModule from "my-module.js" --> all |
| 79 | + * import {myMember} from "my-module.js" --> single |
| 80 | + * import {foo, bar} from "my-module.js" --> multiple |
| 81 | + * @param {ASTNode} node the ImportDeclaration node. |
| 82 | + * @returns {string} used member parameter style, ["all", "multiple", "single"] |
| 83 | + */ |
| 84 | + function usedMemberSyntax(node) { |
| 85 | + if (node.source.value.match(/\.css/)) { |
| 86 | + return 'css'; |
| 87 | + } |
| 88 | + if (node.specifiers.length === 0) { |
| 89 | + return 'none'; |
| 90 | + } |
| 91 | + if (node.specifiers[0].type === 'ImportNamespaceSpecifier') { |
| 92 | + return 'all'; |
| 93 | + } |
| 94 | + if (node.specifiers.length === 1) { |
| 95 | + return 'single'; |
| 96 | + } |
| 97 | + return 'multiple'; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Gets the group by member parameter index for given declaration. |
| 102 | + * @param {ASTNode} node the ImportDeclaration node. |
| 103 | + * @returns {number} the declaration group by member index. |
| 104 | + */ |
| 105 | + function getMemberParameterGroupIndex(node) { |
| 106 | + return memberSyntaxSortOrder.indexOf(usedMemberSyntax(node)); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Gets the local name of the first imported module. |
| 111 | + * @param {ASTNode} node the ImportDeclaration node. |
| 112 | + * @returns {?string} the local name of the first imported module. |
| 113 | + */ |
| 114 | + function getFirstLocalMemberName(node) { |
| 115 | + if (node.specifiers[0]) { |
| 116 | + return node.specifiers[0].local.name; |
| 117 | + } |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Calculates number of lines between two nodes. It is assumed that the given `left` node appears before |
| 123 | + * the given `right` node in the source code. Lines are counted from the end of the `left` node till the |
| 124 | + * start of the `right` node. If the given nodes are on the same line, it returns `0`, same as if they were |
| 125 | + * on two consecutive lines. |
| 126 | + * @param {ASTNode} left node that appears before the given `right` node. |
| 127 | + * @param {ASTNode} right node that appears after the given `left` node. |
| 128 | + * @returns {number} number of lines between nodes. |
| 129 | + */ |
| 130 | + function getNumberOfLinesBetween(left, right) { |
| 131 | + return Math.max(right.loc.start.line - left.loc.end.line - 1, 0); |
| 132 | + } |
| 133 | + |
| 134 | + return { |
| 135 | + ImportDeclaration(node) { |
| 136 | + if (!ignoreDeclarationSort) { |
| 137 | + if ( |
| 138 | + previousDeclaration && |
| 139 | + allowSeparatedGroups && |
| 140 | + getNumberOfLinesBetween(previousDeclaration, node) > 0 |
| 141 | + ) { |
| 142 | + // reset declaration sort |
| 143 | + previousDeclaration = null; |
| 144 | + } |
| 145 | + |
| 146 | + if (previousDeclaration) { |
| 147 | + const currentMemberSyntaxGroupIndex = getMemberParameterGroupIndex(node), |
| 148 | + previousMemberSyntaxGroupIndex = getMemberParameterGroupIndex( |
| 149 | + previousDeclaration |
| 150 | + ); |
| 151 | + let currentLocalMemberName = getFirstLocalMemberName(node), |
| 152 | + previousLocalMemberName = getFirstLocalMemberName(previousDeclaration); |
| 153 | + |
| 154 | + if (ignoreCase) { |
| 155 | + previousLocalMemberName = |
| 156 | + previousLocalMemberName && previousLocalMemberName.toLowerCase(); |
| 157 | + currentLocalMemberName = |
| 158 | + currentLocalMemberName && currentLocalMemberName.toLowerCase(); |
| 159 | + } |
| 160 | + |
| 161 | + /* |
| 162 | + * When the current declaration uses a different member syntax, |
| 163 | + * then check if the ordering is correct. |
| 164 | + * Otherwise, make a default string compare (like rule sort-vars to be consistent) of the first used local member name. |
| 165 | + */ |
| 166 | + if (currentMemberSyntaxGroupIndex !== previousMemberSyntaxGroupIndex) { |
| 167 | + if (currentMemberSyntaxGroupIndex < previousMemberSyntaxGroupIndex) { |
| 168 | + context.report({ |
| 169 | + node, |
| 170 | + messageId: 'unexpectedSyntaxOrder', |
| 171 | + data: { |
| 172 | + syntaxA: |
| 173 | + memberSyntaxSortOrder[currentMemberSyntaxGroupIndex], |
| 174 | + syntaxB: |
| 175 | + memberSyntaxSortOrder[previousMemberSyntaxGroupIndex] |
| 176 | + } |
| 177 | + }); |
| 178 | + } |
| 179 | + } else { |
| 180 | + if ( |
| 181 | + previousLocalMemberName && |
| 182 | + currentLocalMemberName && |
| 183 | + currentLocalMemberName < previousLocalMemberName |
| 184 | + ) { |
| 185 | + context.report({ |
| 186 | + node, |
| 187 | + messageId: 'sortImportsAlphabetically' |
| 188 | + }); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + previousDeclaration = node; |
| 194 | + } |
| 195 | + |
| 196 | + if (!ignoreMemberSort) { |
| 197 | + const importSpecifiers = node.specifiers.filter( |
| 198 | + (specifier) => specifier.type === 'ImportSpecifier' |
| 199 | + ); |
| 200 | + const getSortableName = ignoreCase |
| 201 | + ? (specifier) => specifier.local.name.toLowerCase() |
| 202 | + : (specifier) => specifier.local.name; |
| 203 | + const firstUnsortedIndex = importSpecifiers |
| 204 | + .map(getSortableName) |
| 205 | + .findIndex((name, index, array) => array[index - 1] > name); |
| 206 | + |
| 207 | + if (firstUnsortedIndex !== -1) { |
| 208 | + context.report({ |
| 209 | + node: importSpecifiers[firstUnsortedIndex], |
| 210 | + messageId: 'sortMembersAlphabetically', |
| 211 | + data: { memberName: importSpecifiers[firstUnsortedIndex].local.name }, |
| 212 | + fix(fixer) { |
| 213 | + if ( |
| 214 | + importSpecifiers.some( |
| 215 | + (specifier) => |
| 216 | + sourceCode.getCommentsBefore(specifier).length || |
| 217 | + sourceCode.getCommentsAfter(specifier).length |
| 218 | + ) |
| 219 | + ) { |
| 220 | + // If there are comments in the ImportSpecifier list, don't rearrange the specifiers. |
| 221 | + return null; |
| 222 | + } |
| 223 | + |
| 224 | + return fixer.replaceTextRange( |
| 225 | + [ |
| 226 | + importSpecifiers[0].range[0], |
| 227 | + importSpecifiers[importSpecifiers.length - 1].range[1] |
| 228 | + ], |
| 229 | + importSpecifiers |
| 230 | + |
| 231 | + // Clone the importSpecifiers array to avoid mutating it |
| 232 | + .slice() |
| 233 | + |
| 234 | + // Sort the array into the desired order |
| 235 | + .sort((specifierA, specifierB) => { |
| 236 | + const aName = getSortableName(specifierA); |
| 237 | + const bName = getSortableName(specifierB); |
| 238 | + |
| 239 | + return aName > bName ? 1 : -1; |
| 240 | + }) |
| 241 | + |
| 242 | + // Build a string out of the sorted list of import specifiers and the text between the originals |
| 243 | + .reduce((sourceText, specifier, index) => { |
| 244 | + const textAfterSpecifier = |
| 245 | + index === importSpecifiers.length - 1 |
| 246 | + ? '' |
| 247 | + : sourceCode |
| 248 | + .getText() |
| 249 | + .slice( |
| 250 | + importSpecifiers[index].range[1], |
| 251 | + importSpecifiers[index + 1].range[0] |
| 252 | + ); |
| 253 | + |
| 254 | + return ( |
| 255 | + sourceText + |
| 256 | + sourceCode.getText(specifier) + |
| 257 | + textAfterSpecifier |
| 258 | + ); |
| 259 | + }, '') |
| 260 | + ); |
| 261 | + } |
| 262 | + }); |
| 263 | + } |
| 264 | + } |
| 265 | + } |
| 266 | + }; |
| 267 | + } |
| 268 | +}; |
0 commit comments