|
| 1 | +import { createRule, parseRuleOptions, parseProjectOptions, resolvePath, isIgnored } from '../utils' |
| 2 | +import type { VueModularRuleModule, VueModularRuleContext } from '../types' |
| 3 | +import { resolveImportPath } from '../utils/resolveImportPath' |
| 4 | + |
| 5 | +const defaultOptions = { |
| 6 | + ignores: [] as string[], |
| 7 | +} |
| 8 | + |
| 9 | +export const appImports = createRule<VueModularRuleModule>({ |
| 10 | + create(context: VueModularRuleContext) { |
| 11 | + const options = parseRuleOptions(context, defaultOptions) |
| 12 | + const projectOptions = parseProjectOptions(context) |
| 13 | + const filename = resolvePath(context.filename, projectOptions.rootPath, projectOptions.rootAlias) |
| 14 | + if (!filename) return {} |
| 15 | + if (isIgnored(filename, options.ignores)) return {} |
| 16 | + |
| 17 | + // only run for files inside appPath |
| 18 | + const isInApp = filename.startsWith(projectOptions.appPath) |
| 19 | + if (!isInApp) return {} |
| 20 | + |
| 21 | + return { |
| 22 | + ImportDeclaration(node) { |
| 23 | + const importPath = node.source.value as string |
| 24 | + const resolvedPath = resolveImportPath(filename, importPath, projectOptions.rootPath, projectOptions.rootAlias) |
| 25 | + if (!resolvedPath) return |
| 26 | + |
| 27 | + // Special case: allow app/router.ts to import feature route files |
| 28 | + const isRouterFile = filename === `${projectOptions.appPath}/router.ts` |
| 29 | + if (isRouterFile) { |
| 30 | + // allow imports from feature route files (features/*/routes.ts) |
| 31 | + const isRouteFile = resolvedPath.endsWith('/routes.ts') || resolvedPath.endsWith('/routes') |
| 32 | + if (resolvedPath.startsWith(projectOptions.featuresPath) && isRouteFile) return |
| 33 | + } |
| 34 | + |
| 35 | + // Allow imports from shared |
| 36 | + if (resolvedPath.startsWith(projectOptions.sharedPath)) return |
| 37 | + |
| 38 | + // Allow imports from features from Public API from the root of the feature (features/*/index.ts) |
| 39 | + if (resolvedPath.startsWith(projectOptions.featuresPath)) { |
| 40 | + const featureSegment = resolvedPath.slice(projectOptions.featuresPath.length).split('/')[1] |
| 41 | + const isFeatureRootImport = |
| 42 | + resolvedPath === `${projectOptions.featuresPath}/${featureSegment}/index.ts` || |
| 43 | + resolvedPath === `${projectOptions.featuresPath}/${featureSegment}` || |
| 44 | + resolvedPath === `${projectOptions.featuresPath}/${featureSegment}/index` |
| 45 | + if (isFeatureRootImport) return |
| 46 | + } |
| 47 | + |
| 48 | + // If we reach here, it's a forbidden import |
| 49 | + context.report({ node, messageId: 'forbiddenImport', data: { file: filename, target: importPath } }) |
| 50 | + }, |
| 51 | + } |
| 52 | + }, |
| 53 | + name: 'app-imports', |
| 54 | + recommended: false, |
| 55 | + level: 'error', |
| 56 | + meta: { |
| 57 | + type: 'problem', |
| 58 | + docs: { |
| 59 | + description: 'app/ folder can import from shared/ and features/ (exception: app/router.ts may import feature route files)', |
| 60 | + category: 'Dependency', |
| 61 | + recommended: false, |
| 62 | + }, |
| 63 | + defaultOptions: [defaultOptions], |
| 64 | + schema: [ |
| 65 | + { |
| 66 | + type: 'object', |
| 67 | + properties: { |
| 68 | + ignores: { type: 'array', items: { type: 'string' } }, |
| 69 | + }, |
| 70 | + additionalProperties: false, |
| 71 | + }, |
| 72 | + ], |
| 73 | + messages: { |
| 74 | + forbiddenImport: "App file '{{file}}' must not import from '{{target}}'.", |
| 75 | + }, |
| 76 | + }, |
| 77 | +}) |
0 commit comments