CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tslint

An extensible static analysis linter for the TypeScript language

Pending
Overview
Eval results
Files

cli.mddocs/

CLI Tool

TSLint provides a comprehensive command-line interface for linting TypeScript and JavaScript files with extensive configuration options.

Command Syntax

tslint [options] [files...]

Basic Usage

Linting Files

# Lint specific files
tslint src/app.ts src/utils.ts

# Lint with glob patterns
tslint src/**/*.ts

# Lint TypeScript and JavaScript files
tslint "src/**/*.{ts,tsx,js,jsx}"

# Lint all files in project
tslint --project tsconfig.json

Common Operations

# Auto-fix violations
tslint --fix src/**/*.ts

# Generate initial configuration  
tslint --init

# Check configuration for a file
tslint --print-config src/app.ts

# Run in test mode (for rule development)
tslint --test test/rules/

Command-Line Options

Core Options

# Configuration
-c, --config <path>              # Configuration file path (default: tslint.json)
-p, --project <path>             # TypeScript project path (tsconfig.json)
--print-config <file>            # Print resolved configuration for file

# File Processing  
-e, --exclude <pattern>          # Exclude file patterns (repeatable)
--files <file>                   # Read file list from file

# Output Control
-o, --out <path>                 # Output file (default: stdout)
-s, --format <format>            # Output format (default: stylish)
--formatters-dir <path>          # Custom formatters directory
-q, --quiet                      # Show errors only, suppress warnings
--outputAbsolutePaths            # Use absolute paths in output

# Rule Management
-r, --rules-dir <path>           # Custom rules directory (repeatable)
--fix                            # Auto-fix select rule violations
--force                          # Return exit code 0 even with errors

# Special Modes
--init                           # Generate initial tslint.json
--test [dir]                     # Test mode for rule development
-t, --type-check                 # Enable type checking (deprecated)

# Information
--version                        # Show version number
-h, --help                       # Show help message

Option Details

Configuration Options

# Use specific configuration file
tslint -c custom-tslint.json src/**/*.ts

# Use configuration relative to each file (default behavior)
tslint src/**/*.ts

# Print resolved configuration for debugging
tslint --print-config src/components/App.tsx

Project-Based Linting

# Lint entire TypeScript project
tslint --project .

# Lint project with custom config
tslint --project . -c strict-tslint.json

# Lint specific files from project
tslint --project . src/specific-file.ts

File Exclusion

# Exclude single pattern
tslint --exclude "node_modules/**" src/**/*.ts

# Exclude multiple patterns
tslint --exclude "**/*.spec.ts" --exclude "**/*.d.ts" src/**/*.ts

# Use configuration file exclusions
# In tslint.json:
# {
#   "linterOptions": {
#     "exclude": ["node_modules/**", "dist/**"]
#   }
# }

Output Formatting

# Use built-in formatters
tslint -s json src/**/*.ts              # JSON format
tslint -s checkstyle src/**/*.ts        # Checkstyle XML
tslint -s junit src/**/*.ts             # JUnit XML
tslint -s prose src/**/*.ts             # Prose format

# Output to file
tslint -s json -o lint-results.json src/**/*.ts

# Use custom formatter
tslint -s my-custom --formatters-dir ./formatters src/**/*.ts

# Absolute paths in output
tslint --outputAbsolutePaths src/**/*.ts

Auto-fixing

# Auto-fix all fixable violations
tslint --fix src/**/*.ts

# Auto-fix with specific configuration
tslint --fix -c tslint.json src/**/*.ts

# Combine with other options
tslint --fix --project . --format stylish

Exit Codes

TSLint uses standard exit codes to indicate results:

# Exit code meanings:
# 0 - Success (no lint errors found, or --force used)
# 1 - Lint errors found  
# 2 - Configuration error or invalid usage

Handling Exit Codes

# Ignore lint errors in CI (not recommended)
tslint --force src/**/*.ts
echo "Exit code: $?"  # Always 0

# Proper CI usage
tslint src/**/*.ts
if [ $? -eq 0 ]; then
    echo "Linting passed"
else
    echo "Linting failed"
    exit 1
fi

Advanced Usage Patterns

Multi-Configuration Setup

# Different configs for different file types
tslint -c tslint-strict.json src/core/**/*.ts
tslint -c tslint-loose.json src/legacy/**/*.ts

# Environment-specific linting
tslint -c tslint.production.json src/**/*.ts   # Production rules
tslint -c tslint.development.json src/**/*.ts  # Development rules

Custom Rules Integration

# Use custom rules directory
tslint -r ./custom-rules src/**/*.ts

# Multiple rules directories
tslint -r ./custom-rules -r ./team-rules src/**/*.ts

# Combine with npm packages
tslint -r node_modules/tslint-react/rules src/**/*.tsx

File List Management

# Create file list
find src -name "*.ts" -not -path "*/node_modules/*" > files.txt

# Lint from file list
tslint --files files.txt

# Programmatic file list generation
git diff --name-only --diff-filter=AM master | grep "\.tsx\?$" | xargs tslint

CI/CD Integration

# GitLab CI example
tslint --format checkstyle --out tslint-report.xml src/**/*.ts

# GitHub Actions example  
tslint --format json --out tslint-results.json src/**/*.ts

# Jenkins integration
tslint --format junit --out TEST-tslint.xml src/**/*.ts

Configuration File Generation

Initialization

# Generate basic configuration
tslint --init

# Generates tslint.json:
{
    "defaultSeverity": "error",
    "extend": [
        "tslint:recommended"
    ],
    "jsRules": {},
    "rules": {},
    "rulesDirectory": []
}

Custom Initialization

# Create custom init script
cat > init-tslint.sh << 'EOF'
#!/bin/bash
tslint --init
# Customize generated config
sed -i 's/"tslint:recommended"/"tslint:latest"/' tslint.json
echo "Custom TSLint configuration created"
EOF

chmod +x init-tslint.sh
./init-tslint.sh

Rule Development Testing

Test Mode Usage

# Test all rules in directory
tslint --test test/rules/

# Test specific rule
tslint --test test/rules/no-console/

# Test with custom rules directory
tslint --test test/rules/ --rules-dir custom-rules/

# Verbose test output
tslint --test test/rules/ 2>&1 | tee test-results.log

Test Development Workflow

# 1. Create test files
mkdir -p test/rules/my-new-rule
cat > test/rules/my-new-rule/test.ts.lint << 'EOF'
console.log('test');
~~~~~~~~~~~~~~~~~~~ [Console statements are not allowed]
EOF

# 2. Create test configuration
cat > test/rules/my-new-rule/tslint.json << 'EOF'
{
    "rules": {
        "my-new-rule": true
    }
}
EOF

# 3. Run tests
tslint --test test/rules/my-new-rule/ --rules-dir src/rules/

Performance Optimization

Efficient Linting Strategies

# Use project-based linting for type checking
tslint --project tsconfig.json  # Faster than individual files

# Exclude unnecessary files
tslint --exclude "**/*.d.ts" --exclude "node_modules/**" src/**/*.ts

# Parallel processing with xargs
find src -name "*.ts" | xargs -P 4 -I {} tslint {}

Large Codebase Handling

# Batch processing script
for dir in src/*/; do
    echo "Linting $dir"
    tslint --project "$dir" --format json >> lint-results.json
done

# Memory-efficient processing
find src -name "*.ts" -print0 | xargs -0 -n 50 tslint

Integration Examples

Package.json Scripts

{
  "scripts": {
    "lint": "tslint --project .",
    "lint:fix": "tslint --project . --fix",
    "lint:check": "tslint-config-prettier-check ./tslint.json",
    "lint:ci": "tslint --project . --format checkstyle --out tslint-report.xml",
    "pretest": "npm run lint",
    "precommit": "tslint --project . --format stylish"
  }
}

Makefile Integration

# Makefile
.PHONY: lint lint-fix lint-ci

lint:
	@echo "Running TSLint..."
	@tslint --project . --format stylish

lint-fix:
	@echo "Auto-fixing TSLint violations..."
	@tslint --project . --fix

lint-ci:
	@echo "Running TSLint for CI..."
	@tslint --project . --format checkstyle --out tslint-report.xml
	@tslint --project . --format json --out tslint-results.json

check: lint
	@echo "All checks passed!"

Git Hooks Integration

# Pre-commit hook (.git/hooks/pre-commit)
#!/bin/sh
echo "Running TSLint on staged files..."

# Get staged TypeScript files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep "\.tsx\?$")

if [ "$STAGED_FILES" = "" ]; then
    echo "No TypeScript files staged. Skipping TSLint."
    exit 0
fi

# Run TSLint on staged files
echo "$STAGED_FILES" | xargs tslint

LINT_RESULT=$?

if [ $LINT_RESULT -ne 0 ]; then
    echo "TSLint failed. Fix errors before committing."
    echo "Run 'tslint --fix <files>' to auto-fix some errors."
    exit 1
fi

echo "TSLint passed!"
exit 0

Docker Integration

# Dockerfile
FROM node:14-alpine

WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production

COPY . .

# Lint during build
RUN npx tslint --project . --format stylish

# Or create linting stage
FROM node:14-alpine AS linting
WORKDIR /app  
COPY package*.json ./
RUN npm ci
COPY . .
RUN npx tslint --project . --format json --out /tmp/lint-results.json

FROM node:14-alpine AS production
COPY --from=linting /app .
# Continue with production build

Troubleshooting

Common Issues

Configuration Not Found

# Error: Could not find config file at: tslint.json
# Solutions:
tslint --init                    # Create default config
tslint -c ./path/to/config.json  # Specify config path

Type Checking Issues

# Error: Cannot read property 'getSourceFile' of undefined
# Solutions:
tslint --project .               # Use project-based linting
tslint --type-check src/**/*.ts  # Enable type checking (deprecated)

Performance Issues

# Slow linting on large codebases
# Solutions:
tslint --project . --exclude "**/*.d.ts"  # Exclude declaration files
tslint src/**/*.ts                        # Avoid project mode if not needed

Debugging Commands

# Debug configuration resolution
tslint --print-config src/app.ts

# Verbose output
DEBUG=* tslint src/**/*.ts

# Check file discovery
tslint --project . --files /dev/stdout

# Validate configuration
node -e "console.log(JSON.parse(require('fs').readFileSync('./tslint.json')))"

Migration Helpers

# Check for deprecated options
grep -r "type-check\|no-unused-variable" tslint.json

# Convert to ESLint (when migrating)
npx tslint-to-eslint-config

# Update configurations
npx tslint-config-prettier-check ./tslint.json

Best Practices

Command-Line Usage Guidelines

  1. Use Project Mode: Prefer --project for type-aware rules
  2. Exclude Appropriately: Always exclude node_modules, build outputs, and declaration files
  3. Choose Right Formatter: Use json for CI, stylish for development
  4. Handle Exit Codes: Properly handle exit codes in scripts and CI
  5. Optimize Performance: Use exclusions and appropriate file selection
  6. Version Lock: Pin TSLint version in CI environments
  7. Document Usage: Include common commands in project README

CI/CD Best Practices

# Good CI script
set -e  # Exit on any error

echo "Installing dependencies..."
npm ci

echo "Running TSLint..."
npx tslint --project . --format checkstyle --out tslint-report.xml

echo "TSLint passed successfully!"

Development Workflow

# Development cycle
npm run lint          # Check for issues
npm run lint:fix      # Auto-fix what's possible
# Manual fixes for remaining issues
npm run lint          # Verify all issues resolved
git add . && git commit -m "Fix linting issues"

Install with Tessl CLI

npx tessl i tessl/npm-tslint

docs

cli.md

configuration.md

formatters.md

index.md

linting.md

rules.md

testing.md

tile.json