Skip to content

The code example generation relies on string matching against error messages, which is fragile and could break if error message formats change. Consider using more robust pattern matching or a configuration-based approach to map rule patterns to examples. #19

@BorDevTech

Description

@BorDevTech

The code example generation relies on string matching against error messages, which is fragile and could break if error message formats change. Consider using more robust pattern matching or a configuration-based approach to map rule patterns to examples.

    // Configuration-based mapping for code examples
    type ExampleConfig = {
      ruleId: string;
      identifierPattern?: RegExp;
      exampleGenerator: (issue: AnalyzedIssue, identifier?: string) => string;
    };

    const exampleConfigs: ExampleConfig[] = [
      {
        ruleId: '@typescript-eslint/no-unused-vars',
        // Regex to extract the unused variable/interface name from the message
        identifierPattern: /'([^']+)' is defined but never used/,
        exampleGenerator: (issue, identifier) => {
          const fileName = issue.file.split('/').pop() || 'file';
          // Use the identifier in the example, fallback to a generic name if not found
          const unusedName = identifier || 'UnusedIdentifier';
          return `#### 💡 Code Example

**❌ Before (causes lint error):**
\`\`\`typescript
// Example of unused variable/interface
interface ${unusedName} {  // ← This interface is defined but never used
  first_name: string;
  last_name: string;
  // ... other properties
}

export async function verify() {
  // Implementation without using ${unusedName}
}
\`\`\`

**✅ After (fixed):**
\`\`\`typescript
// Option 1: Remove the unused interface entirely
// (or use it in your code if needed)
\`\`\`
`;
        }
      },
      // Add more rule configs here as needed
    ];

    // Find a matching config for the ruleId
    const config = exampleConfigs.find(cfg => cfg.ruleId === ruleId);
    if (config) {
      let identifier: string | undefined = undefined;
      if (config.identifierPattern) {
        const match = issue.message.match(config.identifierPattern);
        if (match && match[1]) {
          identifier = match[1];
        }
      }
      return config.exampleGenerator(issue, identifier);
    }
    // Default: no example available
    return '';
  }

Originally posted by @Copilot in #11 (comment)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions