|
| 1 | +const meta = {} |
| 2 | +const errors = { |
| 3 | + ExportNamedDeclaration: |
| 4 | + 'Multiple named export declarations; consolidate all named exports into a single statement', |
| 5 | + ExportDefaultDeclaration: |
| 6 | + 'Default export declaration should be adjacent to named export', |
| 7 | + MemberExpression: |
| 8 | + 'Multiple CommonJS exports; consolidate all exports into a single assignment to ' + |
| 9 | + '`module.exports`', |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Check if two nodes are adjacent (only whitespace between them) |
| 14 | + * |
| 15 | + * The two nodes do not have to be sorted in the order they appear in the code. |
| 16 | + * |
| 17 | + * @param {Object} opts Options for the check |
| 18 | + * @param {Object} opts.context The context of the nodes |
| 19 | + * @param {Object} opts.first The first node |
| 20 | + * @param {Object} opts.second The second node |
| 21 | + * @return {Boolean} |
| 22 | + * @private |
| 23 | + */ |
| 24 | +function isAdjacent(opts = {}) { |
| 25 | + const sourceCode = opts.context.getSourceCode() |
| 26 | + |
| 27 | + if (sourceCode.getTokensBetween(opts.first, opts.second).length || |
| 28 | + sourceCode.getTokensBetween(opts.second, opts.first).length) { |
| 29 | + return false |
| 30 | + } |
| 31 | + |
| 32 | + return true |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Determine how many property accesses precede this node |
| 37 | + * |
| 38 | + * For example, `module.exports` = 1, `module.exports.something` = 2 and so on. |
| 39 | + * |
| 40 | + * @param {Object} node The node being visited |
| 41 | + * @return {Number} |
| 42 | + * @private |
| 43 | + */ |
| 44 | +function accessorDepth(node) { |
| 45 | + let depth = 0 |
| 46 | + |
| 47 | + while (node.type === 'MemberExpression') { |
| 48 | + depth++ |
| 49 | + node = node.parent |
| 50 | + } |
| 51 | + |
| 52 | + return depth |
| 53 | +} |
| 54 | + |
| 55 | +function create(context) { |
| 56 | + const exports = { |
| 57 | + named: new Set(), |
| 58 | + default: null, |
| 59 | + last: null, |
| 60 | + } |
| 61 | + |
| 62 | + return { |
| 63 | + ExportDefaultDeclaration(node) { |
| 64 | + exports.default = node |
| 65 | + }, |
| 66 | + |
| 67 | + ExportNamedDeclaration(node) { |
| 68 | + exports.named.add(node) |
| 69 | + exports.last = node |
| 70 | + }, |
| 71 | + |
| 72 | + MemberExpression(node) { |
| 73 | + if (['MemberExpression', 'AssignmentExpression'].indexOf(node.parent.type) === -1) { |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + // Member expressions on the right side of the assignment do not interest us |
| 78 | + if (node.parent.type === 'AssignmentExpression' && node.parent.left !== node) { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + if (node.object.name === 'module' && node.property.name === 'exports') { |
| 83 | + // module.exports.exported.*: assignments this deep should not be considered as exports |
| 84 | + if (accessorDepth(node) > 2) { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + return void exports.named.add(node) |
| 89 | + } |
| 90 | + |
| 91 | + if (node.object.name === 'exports') { |
| 92 | + // exports.exported.*: assignments this deep should not be considered as exports |
| 93 | + if (accessorDepth(node) > 1) { |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + return void exports.named.add(node) |
| 98 | + } |
| 99 | + }, |
| 100 | + |
| 101 | + 'Program:exit': function onExit() { |
| 102 | + if (exports.named.size > 1) { |
| 103 | + for (const node of exports.named) { |
| 104 | + context.report({ |
| 105 | + node, |
| 106 | + message: errors[node.type], |
| 107 | + }) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // There is exactly one named export and a default export -> check if they are adjacent |
| 112 | + if (exports.default && exports.last && exports.named.size === 1) { |
| 113 | + const adjacent = isAdjacent({ |
| 114 | + context, |
| 115 | + first: exports.default, |
| 116 | + second: exports.last, |
| 117 | + }) |
| 118 | + |
| 119 | + if (!adjacent) { |
| 120 | + context.report({ |
| 121 | + node: exports.default, |
| 122 | + message: errors[exports.default.type], |
| 123 | + }) |
| 124 | + } |
| 125 | + } |
| 126 | + }, |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +export default { |
| 131 | + meta, |
| 132 | + create, |
| 133 | +} |
0 commit comments