UNPKG

eslint-config-styleguidejs

Version:
145 lines (117 loc) โ€ข 3.76 kB
#!/usr/bin/env node import { ESLint } from 'eslint'; import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); // Test file with intentional violations to trigger all rules const testCode = ` // Test file to validate all ESLint rules exist and work correctly import React from 'react'; import { useState, useEffect } from 'react'; // Test TypeScript interface interface TestInterface { name: string; value: number; readonly _id?: string; } // Test type definition export type TestType = { id: string; data: { field1: string; field2: number; } }; // Test function with various rule violations export const testFunction = (param: { input: string; output: number; }): TestInterface => { const [state, setState] = useState<string>('test'); if(param.input) { return { name: param.input, value: param.output }; } return { name: 'default', value: 0 }; }; // Test React component export const TestComponent = ({ name, value }: { name: string; value: number }) => { const [count, setCount] = useState(0); useEffect(() => { console.log('Component mounted'); }, []); return ( <div className="test" onClick={() => setCount(count + 1)}> <h1>{name}</h1> <p>Value: {value}</p> <p>Count: {count}</p> </div> ); }; // Test Jest-like code describe('test suite', () => { it('should work', () => { expect(true).toBe(true); }); }); `; async function validateRules() { console.log('๐Ÿงช Validating ESLint rules...\n'); try { // Create ESLint instance const eslint = new ESLint({ overrideConfigFile: join(__dirname, '..', 'eslint.config.js'), }); // Lint the test code const results = await eslint.lintText(testCode, { filePath: 'test-rules.tsx' }); const result = results[0]; if (result.errorCount === 0 && result.warningCount === 0) { console.log('โœ… All rules validated successfully!'); console.log('๐Ÿ“Š No rule errors or warnings found.'); return true; } // Check for rule-related errors const ruleErrors = result.messages.filter(msg => msg.ruleId && ( msg.message.includes('Could not find') || msg.message.includes('deprecated') || msg.message.includes('not found in plugin') ) ); if (ruleErrors.length > 0) { console.log('โŒ Found rule validation errors:'); ruleErrors.forEach(error => { console.log(` - ${error.ruleId}: ${error.message}`); }); return false; } // Show other linting issues (these are expected from our test code) const otherIssues = result.messages.filter(msg => msg.ruleId && !ruleErrors.includes(msg) ); if (otherIssues.length > 0) { console.log('โš ๏ธ Found expected linting issues (rules are working):'); otherIssues.forEach(issue => { console.log(` - ${issue.ruleId}: ${issue.message} (line ${issue.line})`); }); } console.log('\nโœ… All ESLint rules exist and are working correctly!'); console.log(`๐Ÿ“Š Total issues found: ${result.errorCount + result.warningCount}`); return true; } catch (error) { console.error('โŒ Error validating rules:', error.message); // Check if it's a rule-related error if (error.message.includes('Could not find') || error.message.includes('deprecated') || error.message.includes('not found in plugin')) { console.error('This appears to be a rule validation error.'); return false; } console.error('This appears to be a configuration error.'); return false; } } // Export the validation function export { validateRules };