|
| 1 | +import moduleVisitorModule from 'eslint-module-utils/moduleVisitor'; |
| 2 | +import resolveModule from 'eslint-module-utils/resolve'; |
| 3 | +import { minimatch } from 'minimatch'; |
| 4 | +import * as path from 'node:path'; |
| 5 | + |
| 6 | +const moduleVisitor = moduleVisitorModule.default || moduleVisitorModule; |
| 7 | +const resolve = resolveModule.default || resolveModule; |
| 8 | + |
| 9 | +/** |
| 10 | + * @typedef {Object} PatternConfig |
| 11 | + * @property {string} pattern - The pattern to match against resolved imports |
| 12 | + * @property {string} [message] - Custom message to show when the pattern matches |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * Creates an ESLint rule that restricts imports based on their resolved paths. |
| 17 | + * Works with both ESM (import) and CommonJS (require) imports. |
| 18 | + * |
| 19 | + * @type {import('eslint').Rule.RuleModule} |
| 20 | + */ |
| 21 | +export default { |
| 22 | + meta: { |
| 23 | + docs: { |
| 24 | + description: 'Disallow imports that resolve to certain patterns.', |
| 25 | + }, |
| 26 | + messages: { |
| 27 | + restrictedResolvedImport: |
| 28 | + 'Importing from "{{importSource}}" is restricted because it resolves to "{{resolvedPath}}", which matches the pattern "{{pattern}}".{{customMessage}}', |
| 29 | + }, |
| 30 | + type: 'suggestion', |
| 31 | + schema: [ |
| 32 | + { |
| 33 | + type: 'array', |
| 34 | + items: { |
| 35 | + type: 'object', |
| 36 | + properties: { |
| 37 | + pattern: { type: 'string' }, |
| 38 | + message: { type: 'string' }, |
| 39 | + }, |
| 40 | + required: ['pattern'], |
| 41 | + additionalProperties: false, |
| 42 | + }, |
| 43 | + }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + create(context) { |
| 47 | + const options = context.options[0] || []; |
| 48 | + |
| 49 | + if (!Array.isArray(options) || options.length === 0) { |
| 50 | + return {}; |
| 51 | + } |
| 52 | + |
| 53 | + return moduleVisitor( |
| 54 | + (source, node) => { |
| 55 | + // Get the resolved path of the import |
| 56 | + const resolvedPath = resolve(source.value, context); |
| 57 | + |
| 58 | + if (!resolvedPath) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // Normalize the resolved path to use forward slashes |
| 63 | + const normalizedPath = resolvedPath.split(path.sep).join('/'); |
| 64 | + |
| 65 | + // Check each pattern against the resolved path |
| 66 | + for (const option of options) { |
| 67 | + const { pattern, message = '' } = option; |
| 68 | + |
| 69 | + if (minimatch(normalizedPath, pattern)) { |
| 70 | + context.report({ |
| 71 | + node, |
| 72 | + messageId: 'restrictedResolvedImport', |
| 73 | + data: { |
| 74 | + importSource: source.value, |
| 75 | + resolvedPath: normalizedPath, |
| 76 | + pattern, |
| 77 | + customMessage: message ? ` ${message}` : '', |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + // Stop after first match |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + }, |
| 86 | + { commonjs: true, es6: true }, |
| 87 | + ); // This handles both require() and import statements |
| 88 | + }, |
| 89 | +}; |
0 commit comments