UNPKG

lwc-linter

Version:

A comprehensive CLI tool for linting Lightning Web Components v8.0.0+ with modern LWC patterns, decorators, lifecycle hooks, and Salesforce platform integration

362 lines (290 loc) โ€ข 9.41 kB
# ๐Ÿ” LWC Linter A comprehensive CLI tool for linting Lightning Web Components with **code quality**, **accessibility (ADA)**, **performance**, **security**, **ESLint**, and **Prettier** integration. ## โœจ Features - ๐Ÿงน **25+ Custom Rules** across 4 categories plus ESLint/Prettier integration - ๐Ÿ”ง **Auto-fix** functionality for fixable issues - ๐Ÿ“Š **Multiple Output Formats** (CLI, JSON, HTML) - โš™๏ธ **Configurable** via `.lwclintrc.json` - ๐ŸŽฏ **Severity Filtering** (info, warn, error) - ๐Ÿšซ **Rule Disabling** (disable specific rules) - ๐Ÿš€ **CI/CD Ready** with proper exit codes - ๐Ÿ“ **ESLint Integration** for comprehensive code analysis - ๐Ÿ’… **Prettier Integration** for consistent formatting - ๐ŸŒ **Interactive HTML Reports** with filtering and Excel export - ๐Ÿš€ **Quick Scan Mode** with auto-browser opening ## ๐Ÿ“ฆ Installation ```bash npm install lwc-linter ``` ## ๐Ÿš€ Quick Start ### ๐Ÿš€ One-Command Scan (Recommended) ```bash # Scan current directory, generate report, and open in browser npx lwc-linter scan # Scan specific directory npx lwc-linter scan ./src # Scan with auto-fix suggestions npx lwc-linter scan ./src --fix ``` ### ๐Ÿ“‹ Traditional Usage ```bash # Basic scan npx lwc-linter ./src # Generate and auto-open HTML report npx lwc-linter ./src --report html --open # Auto-fix issues npx lwc-linter ./src --fix # Show only errors npx lwc-linter ./src --severity error ``` ## ๐ŸŒŸ Interactive Features The HTML report includes: - **๐Ÿ“‚ Expandable/Collapsible** file sections - **๐Ÿ’ก Code fix examples** for every issue - **๐Ÿ” Advanced filtering** by severity and category - **๐Ÿ“Š Excel export** functionality - **๐Ÿ“ฑ Responsive design** for all devices - **๐ŸŽจ Modern UI** with smooth animations ## ๐Ÿ“‹ Available Rules ### ๐Ÿงฉ Code Quality (10 rules) - `no-unused-vars` - Disallow unused variables [fixable] - `no-console` - Disallow console statements [fixable] - `prefer-const` - Prefer const over let [fixable] - `no-var` - Disallow var declarations [fixable] - `camelcase-naming` - Enforce camelCase naming - `max-nesting-depth` - Limit nesting depth - `no-magic-numbers` - Disallow magic numbers - `eslint-integration` - ESLint-style code analysis [fixable] - `prettier-formatting` - Prettier formatting standards [fixable] - `import-organization` - Proper import organization [fixable] ### โ™ฟ Accessibility (6 rules) - `aria-required` - Ensure ARIA attributes [fixable] - `alt-text-required` - Ensure alt text [fixable] - `color-contrast` - Check color contrast - `keyboard-navigation` - Ensure keyboard support - `focus-management` - Proper focus management - `semantic-html` - Encourage semantic HTML ### โšก Performance (5 rules) - `dom-query-optimization` - Optimize DOM queries - `reactive-property-usage` - Proper @track/@api usage - `large-dom-check` - Warn about large DOM - `bundle-size-check` - Check bundle size - `async-operation-check` - Proper async handling ### ๐Ÿ”’ Security (5 rules) - `no-innerhtml` - Prevent XSS via innerHTML - `input-validation` - Ensure input validation - `xss-prevention` - Prevent XSS vulnerabilities - `no-eval` - Prohibit eval() usage - `secure-communication` - Ensure secure practices ## โš™๏ธ Configuration Create a `.lwclintrc.json` file in your project root: ```json { "rules": { "no-console": "warn", "aria-required": "error", "prettier-formatting": "warn", "eslint-integration": "warn" }, "severity": "warn", "fix": false, "exclude": ["node_modules/**", "**/*.test.js"], "accessibility": { "enabled": true, "level": "AA" }, "performance": { "enabled": true, "maxBundleSize": 500000 }, "security": { "enabled": true, "allowInnerHTML": false }, "eslintIntegration": { "enabled": true, "configPath": ".eslintrc.js", "ignorePatterns": ["node_modules/**", "lib/**"] }, "prettierIntegration": { "enabled": true, "configPath": ".prettierrc.js", "printWidth": 100, "tabWidth": 2, "useTabs": false } } ``` ## ๐Ÿ› ๏ธ Command Reference ### Quick Scan Commands ```bash # ๐Ÿš€ Quick scan with browser opening (recommended) npx lwc-linter scan # Scan current directory npx lwc-linter scan ./src # Scan specific directory npx lwc-linter scan --fix # Show auto-fix suggestions npx lwc-linter scan --no-open # Don't open browser # ๐Ÿ”ง Auto-fix command (use after scan) npx lwc-linter ./src --fix # Fix all fixable issues ``` ### Advanced Commands ```bash # List all available rules npx lwc-linter --rules # Custom configuration npx lwc-linter ./src --config .lwclintrc.json # Disable specific rules npx lwc-linter ./src --disable no-console,prettier-formatting # Generate specific report formats npx lwc-linter ./src --report json --output report.json npx lwc-linter ./src --report html --output report.html --open # Filter by severity npx lwc-linter ./src --severity error # Show only errors npx lwc-linter ./src --severity warn # Show warnings and errors ``` ## ๐Ÿ› ๏ธ ESLint Integration The linter includes a comprehensive ESLint configuration: ```javascript // .eslintrc.js (included) module.exports = { parser: '@typescript-eslint/parser', plugins: ['@typescript-eslint', 'prettier'], extends: [ 'eslint:recommended', '@typescript-eslint/recommended', 'prettier' ], rules: { 'prettier/prettier': 'error', '@typescript-eslint/no-unused-vars': 'error', 'no-console': 'warn', 'no-eval': 'error', 'eqeqeq': 'error', // ... and many more } }; ``` ## ๐Ÿ’… Prettier Integration Consistent code formatting with Prettier: ```javascript // .prettierrc.js (included) module.exports = { printWidth: 100, tabWidth: 2, useTabs: false, semi: true, singleQuote: true, trailingComma: 'es5', bracketSpacing: true, arrowParens: 'avoid' }; ``` ## ๐Ÿ“Š Report Features ### CLI Output (default) Beautiful colored terminal output with icons and summary. ### JSON Output ```bash npx lwc-linter ./src --report json --output report.json ``` ### Interactive HTML Report ```bash npx lwc-linter ./src --report html --open ``` **HTML Report Features:** - ๐Ÿ“‚ **Expandable file sections** with click-to-toggle - ๐Ÿ’ก **Code fix examples** showing before/after - ๐Ÿ” **Real-time filtering** by severity and category - ๐Ÿ“Š **Excel export** with comprehensive data - ๐Ÿ“ฑ **Responsive design** for all screen sizes - ๐ŸŽจ **Modern UI** with smooth animations - ๐Ÿ“ˆ **Statistical summaries** with color-coded metrics ## ๐Ÿ› ๏ธ Development ```bash # Install dependencies npm install # Build the project npm run build # Run ESLint npm run lint # Auto-fix ESLint issues npm run lint:fix # Format with Prettier npm run format # Check Prettier formatting npm run format:check # Run tests npm test ``` ## ๐ŸŽฏ Example Workflow ```bash # 1. Quick scan your project npx lwc-linter scan ./src # 2. Review issues in the auto-opened HTML report # - Filter by severity/category # - View code fix examples # - Export to Excel if needed # 3. Auto-fix what you can npx lwc-linter ./src --fix # 4. Re-scan to see improvements npx lwc-linter scan ./src ``` ## ๐Ÿ“ Project Structure ``` lwc-linter/ โ”œโ”€โ”€ bin/cli.js # CLI entry point โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ cli.ts # Enhanced CLI with scan command โ”‚ โ”œโ”€โ”€ core/ # Core functionality โ”‚ โ”‚ โ”œโ”€โ”€ linter.ts # Main linter class โ”‚ โ”‚ โ”œโ”€โ”€ file-processor.ts โ”‚ โ”‚ โ”œโ”€โ”€ rule-manager.ts โ”‚ โ”‚ โ””โ”€โ”€ output-formatter.ts # Enhanced HTML formatter โ”‚ โ”œโ”€โ”€ rules/ # Rule definitions โ”‚ โ”‚ โ”œโ”€โ”€ code-quality.ts โ”‚ โ”‚ โ”œโ”€โ”€ accessibility.ts โ”‚ โ”‚ โ”œโ”€โ”€ performance.ts โ”‚ โ”‚ โ”œโ”€โ”€ security.ts โ”‚ โ”‚ โ””โ”€โ”€ eslint-prettier.ts # ESLint/Prettier integration โ”‚ โ””โ”€โ”€ types/ โ”‚ โ””โ”€โ”€ config.ts # TypeScript types โ”œโ”€โ”€ lib/ # Compiled JavaScript โ”œโ”€โ”€ .eslintrc.js # ESLint configuration โ”œโ”€โ”€ .prettierrc.js # Prettier configuration โ””โ”€โ”€ test-lwc/ # Example test files ``` ## ๐ŸŽฏ Example Output ``` ๐Ÿš€ LWC Quick Scan Starting... ๐Ÿ“ Scanning: /path/to/your/project โœ” Scan completed! Processed 15 files. ๐Ÿ“Š Interactive report generated: lwc-scan-report-2024-01-15.html ๐Ÿ“ˆ Quick Summary: โ— 12 errors โ— 28 warnings โ— 25 fixable issues ๐ŸŒ Opening interactive report in browser... โœ… Report opened successfully! ๐ŸŽฏ In the report you can: โ€ข Filter by severity (errors, warnings, info) โ€ข Filter by category (code quality, accessibility, etc.) โ€ข Expand/collapse file sections โ€ข View code fix examples โ€ข Export data to Excel ๐Ÿ”ง Auto-fix Command: npx lwc-linter "./src" --fix This will automatically fix 25 issues! ``` ## ๐Ÿค Contributing 1. Fork the repository 2. Create your feature branch 3. Follow ESLint and Prettier standards 4. Add tests for new rules 5. Ensure all tests pass 6. Submit a pull request ## ๐Ÿ“„ License MIT License - see LICENSE file for details ## ๐Ÿ†˜ Support - ๐Ÿ“– Documentation: See this README - ๐Ÿ› Issues: GitHub Issues - ๐Ÿ’ฌ Discussions: GitHub Discussions --- Built with โค๏ธ for the Lightning Web Components community