|
| 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 defaultOrder = ['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, order, name) { |
| 63 | + return order.indexOf(importType(name, context)) |
| 64 | +} |
| 65 | + |
| 66 | +function registerNode(context, node, name, order, imported) { |
| 67 | + const rank = computeRank(context, order, name) |
| 68 | + if (rank !== -1) { |
| 69 | + imported.push({name: name, rank: rank, node: node}) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +function isInVariableDeclarator(node) { |
| 74 | + return node && |
| 75 | + (node.type === 'VariableDeclarator' || isInVariableDeclarator(node.parent)) |
| 76 | +} |
| 77 | + |
| 78 | +module.exports = function importOrderRule (context) { |
| 79 | + const options = context.options[0] || {} |
| 80 | + const order = options.order || defaultOrder |
| 81 | + let imported = [] |
| 82 | + let level = 0 |
| 83 | + |
| 84 | + function incrementLevel() { |
| 85 | + level++ |
| 86 | + } |
| 87 | + function decrementLevel() { |
| 88 | + level-- |
| 89 | + } |
| 90 | + |
| 91 | + return { |
| 92 | + ImportDeclaration: function handleImports(node) { |
| 93 | + if (node.specifiers.length) { // Ignoring unassigned imports |
| 94 | + const name = node.source.value |
| 95 | + registerNode(context, node, name, order, imported) |
| 96 | + } |
| 97 | + }, |
| 98 | + CallExpression: function handleRequires(node) { |
| 99 | + if (level !== 0 || !isStaticRequire(node) || !isInVariableDeclarator(node.parent)) { |
| 100 | + return |
| 101 | + } |
| 102 | + const name = node.arguments[0].value |
| 103 | + registerNode(context, node, name, order, imported) |
| 104 | + }, |
| 105 | + 'Program:exit': function reportAndReset() { |
| 106 | + makeReport(context, imported) |
| 107 | + imported = [] |
| 108 | + }, |
| 109 | + FunctionDeclaration: incrementLevel, |
| 110 | + FunctionExpression: incrementLevel, |
| 111 | + ArrowFunctionExpression: incrementLevel, |
| 112 | + BlockStatement: incrementLevel, |
| 113 | + 'FunctionDeclaration:exit': decrementLevel, |
| 114 | + 'FunctionExpression:exit': decrementLevel, |
| 115 | + 'ArrowFunctionExpression:exit': decrementLevel, |
| 116 | + 'BlockStatement:exit': decrementLevel, |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +module.exports.schema = [ |
| 121 | + { |
| 122 | + type: 'object', |
| 123 | + properties: { |
| 124 | + order: { |
| 125 | + type: 'array', |
| 126 | + uniqueItems: true, |
| 127 | + length: 5, |
| 128 | + items: { |
| 129 | + enum: defaultOrder, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + additionalProperties: false, |
| 134 | + }, |
| 135 | +] |
0 commit comments