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