|
| 1 | +'use strict' |
| 2 | + |
| 3 | +import find from 'lodash.find' |
| 4 | +import importType from '../core/importType' |
| 5 | +import isStaticRequire from '../core/staticRequire' |
| 6 | + |
| 7 | +const defaultGroups = ['builtin', 'external', 'parent', 'sibling', 'index'] |
| 8 | + |
| 9 | +// REPORTING |
| 10 | + |
| 11 | +function reverse(array) { |
| 12 | + return array.map(function (v) { |
| 13 | + return { |
| 14 | + name: v.name, |
| 15 | + rank: -v.rank, |
| 16 | + node: v.node, |
| 17 | + } |
| 18 | + }).reverse() |
| 19 | +} |
| 20 | + |
| 21 | +function findOutOfOrder(imported) { |
| 22 | + if (imported.length === 0) { |
| 23 | + return [] |
| 24 | + } |
| 25 | + let maxSeenRankNode = imported[0] |
| 26 | + return imported.filter(function (importedModule) { |
| 27 | + const res = importedModule.rank < maxSeenRankNode.rank |
| 28 | + if (maxSeenRankNode.rank < importedModule.rank) { |
| 29 | + maxSeenRankNode = importedModule |
| 30 | + } |
| 31 | + return res |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +function report(context, imported, outOfOrder, order) { |
| 36 | + outOfOrder.forEach(function (imp) { |
| 37 | + const found = find(imported, function hasHigherRank(importedItem) { |
| 38 | + return importedItem.rank > imp.rank |
| 39 | + }) |
| 40 | + context.report(imp.node, '`' + imp.name + '` import should occur ' + order + |
| 41 | + ' import of `' + found.name + '`') |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +function makeReport(context, imported) { |
| 46 | + const outOfOrder = findOutOfOrder(imported) |
| 47 | + if (!outOfOrder.length) { |
| 48 | + return |
| 49 | + } |
| 50 | + // There are things to report. Try to minimize the number of reported errors. |
| 51 | + const reversedImported = reverse(imported) |
| 52 | + const reversedOrder = findOutOfOrder(reversedImported) |
| 53 | + if (reversedOrder.length < outOfOrder.length) { |
| 54 | + report(context, reversedImported, reversedOrder, 'after') |
| 55 | + return |
| 56 | + } |
| 57 | + report(context, imported, outOfOrder, 'before') |
| 58 | +} |
| 59 | + |
| 60 | +// DETECTING |
| 61 | + |
| 62 | +function computeRank(context, ranks, name, type) { |
| 63 | + return ranks[importType(name, context)] + |
| 64 | + (type === 'import' ? 0 : 100) |
| 65 | +} |
| 66 | + |
| 67 | +function registerNode(context, node, name, type, ranks, imported) { |
| 68 | + const rank = computeRank(context, ranks, name, type) |
| 69 | + if (rank !== -1) { |
| 70 | + imported.push({name, rank, node}) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function isInVariableDeclarator(node) { |
| 75 | + return node && |
| 76 | + (node.type === 'VariableDeclarator' || isInVariableDeclarator(node.parent)) |
| 77 | +} |
| 78 | + |
| 79 | +const types = ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'] |
| 80 | + |
| 81 | +// Creates an object with type-rank pairs. |
| 82 | +// Example: { index: 0, sibling: 1, parent: 1, external: 1, builtin: 2, internal: 2 } |
| 83 | +// Will throw an error if it contains a type that does not exist, or has a duplicate |
| 84 | +function convertGroupsToRanks(groups) { |
| 85 | + const rankObject = groups.reduce(function(res, group, index) { |
| 86 | + if (typeof group === 'string') { |
| 87 | + group = [group] |
| 88 | + } |
| 89 | + group.forEach(function(groupItem) { |
| 90 | + if (types.indexOf(groupItem) === -1) { |
| 91 | + throw new Error('Incorrect configuration of the rule: Unknown type `' + |
| 92 | + JSON.stringify(groupItem) + '`') |
| 93 | + } |
| 94 | + if (res[groupItem] !== undefined) { |
| 95 | + throw new Error('Incorrect configuration of the rule: `' + groupItem + '` is duplicated') |
| 96 | + } |
| 97 | + res[groupItem] = index |
| 98 | + }) |
| 99 | + return res |
| 100 | + }, {}) |
| 101 | + |
| 102 | + const omittedTypes = types.filter(function(type) { |
| 103 | + return rankObject[type] === undefined |
| 104 | + }) |
| 105 | + |
| 106 | + return omittedTypes.reduce(function(res, type) { |
| 107 | + res[type] = groups.length |
| 108 | + return res |
| 109 | + }, rankObject) |
| 110 | +} |
| 111 | + |
| 112 | +module.exports = function importOrderRule (context) { |
| 113 | + const options = context.options[0] || {} |
| 114 | + let ranks |
| 115 | + |
| 116 | + try { |
| 117 | + ranks = convertGroupsToRanks(options.groups || defaultGroups) |
| 118 | + } catch (error) { |
| 119 | + // Malformed configuration |
| 120 | + return { |
| 121 | + Program: function(node) { |
| 122 | + context.report(node, error.message) |
| 123 | + }, |
| 124 | + } |
| 125 | + } |
| 126 | + let imported = [] |
| 127 | + let level = 0 |
| 128 | + |
| 129 | + function incrementLevel() { |
| 130 | + level++ |
| 131 | + } |
| 132 | + function decrementLevel() { |
| 133 | + level-- |
| 134 | + } |
| 135 | + |
| 136 | + return { |
| 137 | + ImportDeclaration: function handleImports(node) { |
| 138 | + if (node.specifiers.length) { // Ignoring unassigned imports |
| 139 | + const name = node.source.value |
| 140 | + registerNode(context, node, name, 'import', ranks, imported) |
| 141 | + } |
| 142 | + }, |
| 143 | + CallExpression: function handleRequires(node) { |
| 144 | + if (level !== 0 || !isStaticRequire(node) || !isInVariableDeclarator(node.parent)) { |
| 145 | + return |
| 146 | + } |
| 147 | + const name = node.arguments[0].value |
| 148 | + registerNode(context, node, name, 'require', ranks, imported) |
| 149 | + }, |
| 150 | + 'Program:exit': function reportAndReset() { |
| 151 | + makeReport(context, imported) |
| 152 | + imported = [] |
| 153 | + }, |
| 154 | + FunctionDeclaration: incrementLevel, |
| 155 | + FunctionExpression: incrementLevel, |
| 156 | + ArrowFunctionExpression: incrementLevel, |
| 157 | + BlockStatement: incrementLevel, |
| 158 | + 'FunctionDeclaration:exit': decrementLevel, |
| 159 | + 'FunctionExpression:exit': decrementLevel, |
| 160 | + 'ArrowFunctionExpression:exit': decrementLevel, |
| 161 | + 'BlockStatement:exit': decrementLevel, |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +module.exports.schema = [ |
| 166 | + { |
| 167 | + type: 'object', |
| 168 | + properties: { |
| 169 | + groups: { |
| 170 | + type: 'array', |
| 171 | + }, |
| 172 | + }, |
| 173 | + additionalProperties: false, |
| 174 | + }, |
| 175 | +] |
0 commit comments