Open rrd108 opened 1 month ago
For this we need to add a custom rule handling system. My quick thougths on this:
add an interface for the custom rules
interface CustomRule {
name: string
description: string
check: (descriptor: SFCDescriptor, filePath: string) => void
report: () => Offense[]
}
src/types/Config.ts
should have a new property customRules?: string[] // Array of paths to custom rule files
They can sit at /vue-mess-detector/
folder by default.
In analyze
we should load the custom rules
import type { CustomRule } from './types/CustomRule'
const loadCustomRules = async (customRulePaths: string[]): Promise<CustomRule[]> => {
const customRules: CustomRule[] = []
for (const path of customRulePaths) {
try {
const module = await import(path)
if (typeof module.default === 'function') {
const rule = module.default()
if (isValidCustomRule(rule)) {
customRules.push(rule)
} else {
console.warn(Invalid custom rule in ${path}
)
}
}
} catch (error) {
console.error(Error loading custom rule from ${path}:
, error)
}
}
return customRules
}
const isValidCustomRule = (rule: any): rule is CustomRule => { return ( typeof rule.name === 'string' && typeof rule.description === 'string' && typeof rule.check === 'function' && typeof rule.report === 'function' ) }
5. `checkRules` should handle custom rules
```ts
export const checkRules = (
descriptor: SFCDescriptor,
filePath: string,
apply: string[],
override: OverrideConfig,
customRules: CustomRule[]
) => {
// ... existing rule checks ...
// Apply custom rules
customRules.forEach(rule => {
if (apply.includes('custom') || apply.includes(rule.name)) {
rule.check(descriptor, filePath)
}
})
}
reportRules
should handle custom rules
export const reportRules = (
groupBy: GroupBy,
sortBy: SortBy,
level: OutputLevel,
override: OverrideConfig,
customRules: CustomRule[]
) => {
// ... existing rule reports ...
// Report custom rules
customRules.forEach(rule => {
processOffenses(rule.report)
})
// ... rest of the function ...
}
So anyone can create and use their own rules