rrd108 / vue-mess-detector

A static code analysis tool for detecting code smells and best practice violations in Vue.js and Nuxt.js projects
MIT License
218 stars 9 forks source link

Feature Request add support for custom rules #342

Open rrd108 opened 1 month ago

rrd108 commented 1 month ago

So anyone can create and use their own rules

rrd108 commented 1 month ago

For this we need to add a custom rule handling system. My quick thougths on this:

  1. add an interface for the custom rules

    interface CustomRule {
    name: string
    description: string
    check: (descriptor: SFCDescriptor, filePath: string) => void
    report: () => Offense[]
    }
  2. 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.

  3. 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)
    }
  })
}
  1. 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 ...
    }