0
# Code Linting
1
2
ESLint integration with automatic configuration discovery and LoopBack-specific rules for maintaining code quality and consistency.
3
4
## Capabilities
5
6
### lb-eslint Command
7
8
Runs ESLint with automatic configuration discovery and default file extensions.
9
10
**CLI Usage:**
11
12
```bash
13
# Lint current directory
14
lb-eslint .
15
16
# Lint specific files
17
lb-eslint src/index.ts src/utils.ts
18
19
# Lint with fix
20
lb-eslint --fix .
21
22
# Report unused disable directives
23
lb-eslint --report-unused-disable-directives .
24
25
# Lint specific extensions
26
lb-eslint --ext .js,.jsx,.ts,.tsx src/
27
```
28
29
**Note:** ESLint functionality is only available through the `lb-eslint` CLI command. It is not exported as a programmatic function from the main module.
30
31
### Configuration Discovery
32
33
Automatically discovers ESLint configuration files in the project.
34
35
**Configuration Search Order:**
36
1. `.eslintrc.js` in project root
37
2. `.eslintrc.json` in project root
38
3. Falls back to `@loopback/build/config/.eslintrc.js`
39
40
**Default Configuration:**
41
42
```javascript
43
// @loopback/build/config/.eslintrc.js
44
module.exports = {
45
extends: '@loopback/eslint-config',
46
};
47
```
48
49
### Ignore File Support
50
51
Automatically discovers and uses ESLint ignore files.
52
53
```typescript { .api }
54
// Automatically uses .eslintignore if present in project root
55
// Falls back to @loopback/build/config/.eslintignore
56
```
57
58
**Default Ignore Patterns:**
59
60
```
61
node_modules
62
dist
63
```
64
65
### File Extension Handling
66
67
Automatically sets default file extensions if not specified.
68
69
```typescript { .api }
70
// Default extensions: .js,.ts
71
// Only applied if --ext option is not provided
72
```
73
74
**Extension Examples:**
75
76
```bash
77
# Uses default extensions (.js,.ts)
78
lb-eslint src/
79
80
# Custom extensions override defaults
81
lb-eslint --ext .js,.jsx,.ts,.tsx src/
82
```
83
84
### Command Line Option Passthrough
85
86
All ESLint command line options are supported and passed through.
87
88
```typescript { .api }
89
interface ESLintOptions {
90
"--fix": boolean; // Automatically fix problems
91
"--fix-dry-run": boolean; // Show fixes without applying
92
"--ext": string; // File extensions to lint
93
"-c" | "--config": string; // Configuration file path
94
"--ignore-path": string; // Ignore file path
95
"--report-unused-disable-directives": boolean; // Report unused disable comments
96
"--max-warnings": number; // Maximum number of warnings allowed
97
"--output-file": string; // Output file for results
98
"--format": string; // Output format (stylish, json, etc.)
99
"--quiet": boolean; // Report errors only
100
"--cache": boolean; // Enable result caching
101
"--cache-location": string; // Cache file location
102
}
103
```
104
105
### Integration with LoopBack ESLint Config
106
107
Uses `@loopback/eslint-config` for consistent code style across LoopBack projects.
108
109
**Features of @loopback/eslint-config:**
110
- TypeScript support with type-aware rules
111
- Consistent formatting rules aligned with Prettier
112
- Best practices for Node.js and LoopBack development
113
- Import/export validation
114
- Accessibility rules for web components
115
116
### Usage Examples
117
118
**Command Line Usage:**
119
120
```bash
121
# Basic linting with default configuration
122
lb-eslint .
123
124
# Fix issues automatically
125
lb-eslint --fix src/
126
```
127
128
**Package.json Integration:**
129
130
```json
131
{
132
"scripts": {
133
"lint": "lb-eslint --report-unused-disable-directives .",
134
"lint:fix": "lb-eslint --fix .",
135
"lint:check": "lb-eslint ."
136
}
137
}
138
```
139
140
**Custom Configuration:**
141
142
```bash
143
# Use custom config file
144
lb-eslint -c .eslintrc.custom.js src/
145
146
# Use custom ignore file
147
lb-eslint --ignore-path .eslintignore.custom src/
148
149
# Lint specific file types
150
lb-eslint --ext .ts,.d.ts src/
151
```
152
153
### Error Handling
154
155
ESLint errors are properly propagated and handled.
156
157
```typescript { .api }
158
// Process exits with non-zero code on lint errors
159
// Warnings do not cause process exit unless --max-warnings is exceeded
160
// Configuration errors are reported with helpful messages
161
```