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
Markdown
# ๐ 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