Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request add support for custom rules #342

Open
3 tasks
rrd108 opened this issue Sep 27, 2024 · 1 comment
Open
3 tasks

Feature Request add support for custom rules #342

rrd108 opened this issue Sep 27, 2024 · 1 comment
Labels
enhancement New feature or request P3 Low priority

Comments

@rrd108
Copy link
Owner

rrd108 commented Sep 27, 2024

So anyone can create and use their own rules

  • implement a custom rule handling system
  • export rule generator, so developers can use it
  • add error handling for custom rules
@rrd108 rrd108 added enhancement New feature or request P3 Low priority labels Sep 27, 2024
@rrd108
Copy link
Owner Author

rrd108 commented Sep 27, 2024

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[]
}
  1. 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.

  2. 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'
  )
}
  1. checkRules should handle custom rules
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 ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request P3 Low priority
Projects
None yet
Development

No branches or pull requests

1 participant