|
| 1 | +import { createRule } from '../utils/index.js' |
| 2 | + |
| 3 | +export interface Options { |
| 4 | + amountOfExportsToConsiderModuleAsBarrel: number |
| 5 | +} |
| 6 | + |
| 7 | +export type MessageId = 'avoidBarrel' |
| 8 | + |
| 9 | +const defaultOptions: Options = { |
| 10 | + amountOfExportsToConsiderModuleAsBarrel: 3, |
| 11 | +} |
| 12 | + |
| 13 | +export default createRule<[Options?], MessageId>({ |
| 14 | + name: 'avoid-barrel-files', |
| 15 | + meta: { |
| 16 | + type: 'suggestion', |
| 17 | + docs: { |
| 18 | + description: 'Forbid authoring of barrel files.', |
| 19 | + category: 'Performance', |
| 20 | + recommended: true, |
| 21 | + }, |
| 22 | + schema: [ |
| 23 | + { |
| 24 | + type: 'object', |
| 25 | + properties: { |
| 26 | + amountOfExportsToConsiderModuleAsBarrel: { |
| 27 | + type: 'number', |
| 28 | + description: |
| 29 | + 'Minimum amount of exports to consider module as barrelfile', |
| 30 | + default: 3, |
| 31 | + }, |
| 32 | + }, |
| 33 | + additionalProperties: false, |
| 34 | + }, |
| 35 | + ], |
| 36 | + messages: { |
| 37 | + avoidBarrel: |
| 38 | + 'Avoid barrel files, they slow down performance, and cause large module graphs with modules that go unused.', |
| 39 | + }, |
| 40 | + }, |
| 41 | + defaultOptions: [defaultOptions], |
| 42 | + create(context) { |
| 43 | + const options = context.options[0] || defaultOptions |
| 44 | + const amountOfExportsToConsiderModuleAsBarrel = |
| 45 | + options.amountOfExportsToConsiderModuleAsBarrel |
| 46 | + |
| 47 | + return { |
| 48 | + Program(node) { |
| 49 | + let declarations = 0 |
| 50 | + let exports = 0 |
| 51 | + |
| 52 | + for (const n of node.body) { |
| 53 | + if (n.type === 'VariableDeclaration') { |
| 54 | + declarations += n.declarations.length |
| 55 | + } |
| 56 | + |
| 57 | + if ( |
| 58 | + n.type === 'FunctionDeclaration' || |
| 59 | + n.type === 'ClassDeclaration' || |
| 60 | + n.type === 'TSTypeAliasDeclaration' || |
| 61 | + n.type === 'TSInterfaceDeclaration' |
| 62 | + ) { |
| 63 | + declarations += 1 |
| 64 | + } |
| 65 | + |
| 66 | + if (n.type === 'ExportNamedDeclaration') { |
| 67 | + exports += n.specifiers.length |
| 68 | + } |
| 69 | + |
| 70 | + if (n.type === 'ExportAllDeclaration' && n?.exportKind !== 'type') { |
| 71 | + exports += 1 |
| 72 | + } |
| 73 | + |
| 74 | + if (n.type === 'ExportDefaultDeclaration') { |
| 75 | + if ( |
| 76 | + n.declaration.type === 'FunctionDeclaration' || |
| 77 | + n.declaration.type === 'CallExpression' |
| 78 | + ) { |
| 79 | + declarations += 1 |
| 80 | + } else if (n.declaration.type === 'ObjectExpression') { |
| 81 | + exports += n.declaration.properties.length |
| 82 | + } else { |
| 83 | + exports += 1 |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if ( |
| 89 | + exports > declarations && |
| 90 | + exports > amountOfExportsToConsiderModuleAsBarrel |
| 91 | + ) { |
| 92 | + context.report({ |
| 93 | + node, |
| 94 | + messageId: 'avoidBarrel', |
| 95 | + }) |
| 96 | + } |
| 97 | + }, |
| 98 | + } |
| 99 | + }, |
| 100 | +}) |
0 commit comments