0
# CLI Tool
1
2
TSLint provides a comprehensive command-line interface for linting TypeScript and JavaScript files with extensive configuration options.
3
4
## Command Syntax
5
6
```bash
7
tslint [options] [files...]
8
```
9
10
## Basic Usage
11
12
### Linting Files
13
14
```bash
15
# Lint specific files
16
tslint src/app.ts src/utils.ts
17
18
# Lint with glob patterns
19
tslint src/**/*.ts
20
21
# Lint TypeScript and JavaScript files
22
tslint "src/**/*.{ts,tsx,js,jsx}"
23
24
# Lint all files in project
25
tslint --project tsconfig.json
26
```
27
28
### Common Operations
29
30
```bash
31
# Auto-fix violations
32
tslint --fix src/**/*.ts
33
34
# Generate initial configuration
35
tslint --init
36
37
# Check configuration for a file
38
tslint --print-config src/app.ts
39
40
# Run in test mode (for rule development)
41
tslint --test test/rules/
42
```
43
44
## Command-Line Options
45
46
### Core Options
47
48
```bash
49
# Configuration
50
-c, --config <path> # Configuration file path (default: tslint.json)
51
-p, --project <path> # TypeScript project path (tsconfig.json)
52
--print-config <file> # Print resolved configuration for file
53
54
# File Processing
55
-e, --exclude <pattern> # Exclude file patterns (repeatable)
56
--files <file> # Read file list from file
57
58
# Output Control
59
-o, --out <path> # Output file (default: stdout)
60
-s, --format <format> # Output format (default: stylish)
61
--formatters-dir <path> # Custom formatters directory
62
-q, --quiet # Show errors only, suppress warnings
63
--outputAbsolutePaths # Use absolute paths in output
64
65
# Rule Management
66
-r, --rules-dir <path> # Custom rules directory (repeatable)
67
--fix # Auto-fix select rule violations
68
--force # Return exit code 0 even with errors
69
70
# Special Modes
71
--init # Generate initial tslint.json
72
--test [dir] # Test mode for rule development
73
-t, --type-check # Enable type checking (deprecated)
74
75
# Information
76
--version # Show version number
77
-h, --help # Show help message
78
```
79
80
### Option Details
81
82
#### Configuration Options
83
84
```bash
85
# Use specific configuration file
86
tslint -c custom-tslint.json src/**/*.ts
87
88
# Use configuration relative to each file (default behavior)
89
tslint src/**/*.ts
90
91
# Print resolved configuration for debugging
92
tslint --print-config src/components/App.tsx
93
```
94
95
#### Project-Based Linting
96
97
```bash
98
# Lint entire TypeScript project
99
tslint --project .
100
101
# Lint project with custom config
102
tslint --project . -c strict-tslint.json
103
104
# Lint specific files from project
105
tslint --project . src/specific-file.ts
106
```
107
108
#### File Exclusion
109
110
```bash
111
# Exclude single pattern
112
tslint --exclude "node_modules/**" src/**/*.ts
113
114
# Exclude multiple patterns
115
tslint --exclude "**/*.spec.ts" --exclude "**/*.d.ts" src/**/*.ts
116
117
# Use configuration file exclusions
118
# In tslint.json:
119
# {
120
# "linterOptions": {
121
# "exclude": ["node_modules/**", "dist/**"]
122
# }
123
# }
124
```
125
126
#### Output Formatting
127
128
```bash
129
# Use built-in formatters
130
tslint -s json src/**/*.ts # JSON format
131
tslint -s checkstyle src/**/*.ts # Checkstyle XML
132
tslint -s junit src/**/*.ts # JUnit XML
133
tslint -s prose src/**/*.ts # Prose format
134
135
# Output to file
136
tslint -s json -o lint-results.json src/**/*.ts
137
138
# Use custom formatter
139
tslint -s my-custom --formatters-dir ./formatters src/**/*.ts
140
141
# Absolute paths in output
142
tslint --outputAbsolutePaths src/**/*.ts
143
```
144
145
#### Auto-fixing
146
147
```bash
148
# Auto-fix all fixable violations
149
tslint --fix src/**/*.ts
150
151
# Auto-fix with specific configuration
152
tslint --fix -c tslint.json src/**/*.ts
153
154
# Combine with other options
155
tslint --fix --project . --format stylish
156
```
157
158
## Exit Codes
159
160
TSLint uses standard exit codes to indicate results:
161
162
```bash
163
# Exit code meanings:
164
# 0 - Success (no lint errors found, or --force used)
165
# 1 - Lint errors found
166
# 2 - Configuration error or invalid usage
167
```
168
169
### Handling Exit Codes
170
171
```bash
172
# Ignore lint errors in CI (not recommended)
173
tslint --force src/**/*.ts
174
echo "Exit code: $?" # Always 0
175
176
# Proper CI usage
177
tslint src/**/*.ts
178
if [ $? -eq 0 ]; then
179
echo "Linting passed"
180
else
181
echo "Linting failed"
182
exit 1
183
fi
184
```
185
186
## Advanced Usage Patterns
187
188
### Multi-Configuration Setup
189
190
```bash
191
# Different configs for different file types
192
tslint -c tslint-strict.json src/core/**/*.ts
193
tslint -c tslint-loose.json src/legacy/**/*.ts
194
195
# Environment-specific linting
196
tslint -c tslint.production.json src/**/*.ts # Production rules
197
tslint -c tslint.development.json src/**/*.ts # Development rules
198
```
199
200
### Custom Rules Integration
201
202
```bash
203
# Use custom rules directory
204
tslint -r ./custom-rules src/**/*.ts
205
206
# Multiple rules directories
207
tslint -r ./custom-rules -r ./team-rules src/**/*.ts
208
209
# Combine with npm packages
210
tslint -r node_modules/tslint-react/rules src/**/*.tsx
211
```
212
213
### File List Management
214
215
```bash
216
# Create file list
217
find src -name "*.ts" -not -path "*/node_modules/*" > files.txt
218
219
# Lint from file list
220
tslint --files files.txt
221
222
# Programmatic file list generation
223
git diff --name-only --diff-filter=AM master | grep "\.tsx\?$" | xargs tslint
224
```
225
226
### CI/CD Integration
227
228
```bash
229
# GitLab CI example
230
tslint --format checkstyle --out tslint-report.xml src/**/*.ts
231
232
# GitHub Actions example
233
tslint --format json --out tslint-results.json src/**/*.ts
234
235
# Jenkins integration
236
tslint --format junit --out TEST-tslint.xml src/**/*.ts
237
```
238
239
## Configuration File Generation
240
241
### Initialization
242
243
```bash
244
# Generate basic configuration
245
tslint --init
246
247
# Generates tslint.json:
248
{
249
"defaultSeverity": "error",
250
"extend": [
251
"tslint:recommended"
252
],
253
"jsRules": {},
254
"rules": {},
255
"rulesDirectory": []
256
}
257
```
258
259
### Custom Initialization
260
261
```bash
262
# Create custom init script
263
cat > init-tslint.sh << 'EOF'
264
#!/bin/bash
265
tslint --init
266
# Customize generated config
267
sed -i 's/"tslint:recommended"/"tslint:latest"/' tslint.json
268
echo "Custom TSLint configuration created"
269
EOF
270
271
chmod +x init-tslint.sh
272
./init-tslint.sh
273
```
274
275
## Rule Development Testing
276
277
### Test Mode Usage
278
279
```bash
280
# Test all rules in directory
281
tslint --test test/rules/
282
283
# Test specific rule
284
tslint --test test/rules/no-console/
285
286
# Test with custom rules directory
287
tslint --test test/rules/ --rules-dir custom-rules/
288
289
# Verbose test output
290
tslint --test test/rules/ 2>&1 | tee test-results.log
291
```
292
293
### Test Development Workflow
294
295
```bash
296
# 1. Create test files
297
mkdir -p test/rules/my-new-rule
298
cat > test/rules/my-new-rule/test.ts.lint << 'EOF'
299
console.log('test');
300
~~~~~~~~~~~~~~~~~~~ [Console statements are not allowed]
301
EOF
302
303
# 2. Create test configuration
304
cat > test/rules/my-new-rule/tslint.json << 'EOF'
305
{
306
"rules": {
307
"my-new-rule": true
308
}
309
}
310
EOF
311
312
# 3. Run tests
313
tslint --test test/rules/my-new-rule/ --rules-dir src/rules/
314
```
315
316
## Performance Optimization
317
318
### Efficient Linting Strategies
319
320
```bash
321
# Use project-based linting for type checking
322
tslint --project tsconfig.json # Faster than individual files
323
324
# Exclude unnecessary files
325
tslint --exclude "**/*.d.ts" --exclude "node_modules/**" src/**/*.ts
326
327
# Parallel processing with xargs
328
find src -name "*.ts" | xargs -P 4 -I {} tslint {}
329
```
330
331
### Large Codebase Handling
332
333
```bash
334
# Batch processing script
335
for dir in src/*/; do
336
echo "Linting $dir"
337
tslint --project "$dir" --format json >> lint-results.json
338
done
339
340
# Memory-efficient processing
341
find src -name "*.ts" -print0 | xargs -0 -n 50 tslint
342
```
343
344
## Integration Examples
345
346
### Package.json Scripts
347
348
```json
349
{
350
"scripts": {
351
"lint": "tslint --project .",
352
"lint:fix": "tslint --project . --fix",
353
"lint:check": "tslint-config-prettier-check ./tslint.json",
354
"lint:ci": "tslint --project . --format checkstyle --out tslint-report.xml",
355
"pretest": "npm run lint",
356
"precommit": "tslint --project . --format stylish"
357
}
358
}
359
```
360
361
### Makefile Integration
362
363
```makefile
364
# Makefile
365
.PHONY: lint lint-fix lint-ci
366
367
lint:
368
@echo "Running TSLint..."
369
@tslint --project . --format stylish
370
371
lint-fix:
372
@echo "Auto-fixing TSLint violations..."
373
@tslint --project . --fix
374
375
lint-ci:
376
@echo "Running TSLint for CI..."
377
@tslint --project . --format checkstyle --out tslint-report.xml
378
@tslint --project . --format json --out tslint-results.json
379
380
check: lint
381
@echo "All checks passed!"
382
```
383
384
### Git Hooks Integration
385
386
```bash
387
# Pre-commit hook (.git/hooks/pre-commit)
388
#!/bin/sh
389
echo "Running TSLint on staged files..."
390
391
# Get staged TypeScript files
392
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep "\.tsx\?$")
393
394
if [ "$STAGED_FILES" = "" ]; then
395
echo "No TypeScript files staged. Skipping TSLint."
396
exit 0
397
fi
398
399
# Run TSLint on staged files
400
echo "$STAGED_FILES" | xargs tslint
401
402
LINT_RESULT=$?
403
404
if [ $LINT_RESULT -ne 0 ]; then
405
echo "TSLint failed. Fix errors before committing."
406
echo "Run 'tslint --fix <files>' to auto-fix some errors."
407
exit 1
408
fi
409
410
echo "TSLint passed!"
411
exit 0
412
```
413
414
### Docker Integration
415
416
```dockerfile
417
# Dockerfile
418
FROM node:14-alpine
419
420
WORKDIR /app
421
COPY package*.json ./
422
RUN npm ci --only=production
423
424
COPY . .
425
426
# Lint during build
427
RUN npx tslint --project . --format stylish
428
429
# Or create linting stage
430
FROM node:14-alpine AS linting
431
WORKDIR /app
432
COPY package*.json ./
433
RUN npm ci
434
COPY . .
435
RUN npx tslint --project . --format json --out /tmp/lint-results.json
436
437
FROM node:14-alpine AS production
438
COPY --from=linting /app .
439
# Continue with production build
440
```
441
442
## Troubleshooting
443
444
### Common Issues
445
446
#### Configuration Not Found
447
448
```bash
449
# Error: Could not find config file at: tslint.json
450
# Solutions:
451
tslint --init # Create default config
452
tslint -c ./path/to/config.json # Specify config path
453
```
454
455
#### Type Checking Issues
456
457
```bash
458
# Error: Cannot read property 'getSourceFile' of undefined
459
# Solutions:
460
tslint --project . # Use project-based linting
461
tslint --type-check src/**/*.ts # Enable type checking (deprecated)
462
```
463
464
#### Performance Issues
465
466
```bash
467
# Slow linting on large codebases
468
# Solutions:
469
tslint --project . --exclude "**/*.d.ts" # Exclude declaration files
470
tslint src/**/*.ts # Avoid project mode if not needed
471
```
472
473
### Debugging Commands
474
475
```bash
476
# Debug configuration resolution
477
tslint --print-config src/app.ts
478
479
# Verbose output
480
DEBUG=* tslint src/**/*.ts
481
482
# Check file discovery
483
tslint --project . --files /dev/stdout
484
485
# Validate configuration
486
node -e "console.log(JSON.parse(require('fs').readFileSync('./tslint.json')))"
487
```
488
489
### Migration Helpers
490
491
```bash
492
# Check for deprecated options
493
grep -r "type-check\|no-unused-variable" tslint.json
494
495
# Convert to ESLint (when migrating)
496
npx tslint-to-eslint-config
497
498
# Update configurations
499
npx tslint-config-prettier-check ./tslint.json
500
```
501
502
## Best Practices
503
504
### Command-Line Usage Guidelines
505
506
1. **Use Project Mode**: Prefer `--project` for type-aware rules
507
2. **Exclude Appropriately**: Always exclude `node_modules`, build outputs, and declaration files
508
3. **Choose Right Formatter**: Use `json` for CI, `stylish` for development
509
4. **Handle Exit Codes**: Properly handle exit codes in scripts and CI
510
5. **Optimize Performance**: Use exclusions and appropriate file selection
511
6. **Version Lock**: Pin TSLint version in CI environments
512
7. **Document Usage**: Include common commands in project README
513
514
### CI/CD Best Practices
515
516
```bash
517
# Good CI script
518
set -e # Exit on any error
519
520
echo "Installing dependencies..."
521
npm ci
522
523
echo "Running TSLint..."
524
npx tslint --project . --format checkstyle --out tslint-report.xml
525
526
echo "TSLint passed successfully!"
527
```
528
529
### Development Workflow
530
531
```bash
532
# Development cycle
533
npm run lint # Check for issues
534
npm run lint:fix # Auto-fix what's possible
535
# Manual fixes for remaining issues
536
npm run lint # Verify all issues resolved
537
git add . && git commit -m "Fix linting issues"
538
```