|
| 1 | +import path from 'path' |
| 2 | +import { createRule, parseRuleOptions, parseProjectOptions, resolvePath, isIgnored, isTs } from '../utils' |
| 3 | +import type { VueModularRuleModule, VueModularRuleContext } from '../types' |
| 4 | + |
| 5 | +const defaultOptions = { |
| 6 | + suffix: 'Store', |
| 7 | + ignores: ['**/*.d.ts', '**/*.spec.*', '**/*.test.*', '**/*.stories.*'], |
| 8 | +} |
| 9 | + |
| 10 | +export const storeFilenameNoSuffix = createRule<VueModularRuleModule>({ |
| 11 | + create(context: VueModularRuleContext) { |
| 12 | + const options = parseRuleOptions(context, defaultOptions) |
| 13 | + const projectOptions = parseProjectOptions(context) |
| 14 | + const filename = resolvePath(context.filename, projectOptions.rootPath, projectOptions.rootAlias) |
| 15 | + if (!filename) return {} |
| 16 | + if (isIgnored(filename, options.ignores)) return {} |
| 17 | + |
| 18 | + // Only check TypeScript files |
| 19 | + if (!isTs(context.filename)) return {} |
| 20 | + |
| 21 | + // Only check files inside shared/stores or features/*/stores |
| 22 | + const inSharedStores = filename.startsWith(`${projectOptions.sharedPath}/stores`) |
| 23 | + const inFeatureStores = filename.startsWith(projectOptions.featuresPath) && filename.includes('/stores/') |
| 24 | + if (!inSharedStores && !inFeatureStores) return {} |
| 25 | + |
| 26 | + return { |
| 27 | + Program(node) { |
| 28 | + const base = path.basename(context.filename, path.extname(context.filename)) |
| 29 | + |
| 30 | + if (new RegExp(`${options.suffix}$`, 'i').test(base)) { |
| 31 | + context.report({ |
| 32 | + node, |
| 33 | + messageId: 'noStoreSuffix', |
| 34 | + data: { filename: path.basename(context.filename) }, |
| 35 | + }) |
| 36 | + } |
| 37 | + }, |
| 38 | + } |
| 39 | + }, |
| 40 | + name: 'store-filename-no-suffix', |
| 41 | + recommended: true, |
| 42 | + level: 'error', |
| 43 | + meta: { |
| 44 | + type: 'suggestion', |
| 45 | + docs: { |
| 46 | + category: 'Store Rules', |
| 47 | + description: 'Store files must not have "Store" suffix (e.g., auth.ts, not authStore.ts)', |
| 48 | + }, |
| 49 | + defaultOptions: [defaultOptions], |
| 50 | + schema: [ |
| 51 | + { |
| 52 | + type: 'object', |
| 53 | + properties: { |
| 54 | + suffix: { type: 'string' }, |
| 55 | + ignores: { type: 'array', items: { type: 'string' } }, |
| 56 | + }, |
| 57 | + additionalProperties: false, |
| 58 | + }, |
| 59 | + ], |
| 60 | + messages: { |
| 61 | + noStoreSuffix: 'Store filename "{{filename}}" should not include the "Store" suffix (use e.g. "auth.ts").', |
| 62 | + }, |
| 63 | + }, |
| 64 | +}) |
0 commit comments