TypeScript Quality Checker Skill
TypeScript代码质量检查Skill
This skill provides comprehensive TypeScript/JavaScript code quality validation including formatting (Prettier), linting (ESLint), type checking (tsc), security analysis (npm audit), and code complexity analysis. Ensures code meets TypeScript best practices and project standards.
本Skill提供全面的TypeScript/JavaScript代码质量验证,包括格式化(Prettier)、代码检查(ESLint)、类型检查(tsc)、安全分析(npm audit)以及代码复杂度分析。确保代码符合TypeScript最佳实践和项目标准。
- Validating TypeScript/JavaScript code quality before commit
- Running pre-commit quality checks
- CI/CD quality gate validation
- Code review preparation
- Ensuring code style compliance
- Type safety validation
- Security vulnerability detection in dependencies
- 提交代码前验证TypeScript/JavaScript代码质量
- 运行提交前的质量检查
- CI/CD质量门验证
- 代码评审准备
- 确保代码风格合规
- 类型安全性验证
- 检测依赖中的安全漏洞
Quality Check Workflow
质量检查流程
1. Environment Setup
1. 环境搭建
Check Node.js and npm versions
Check Node.js and npm versions
node --version
npm --version
node --version
npm --version
Check TypeScript compiler
Check TypeScript compiler
Check quality tools
Check quality tools
npx eslint --version
npx prettier --version
**Install Development Dependencies:**
```bash
npx eslint --version
npx prettier --version
Install all dev dependencies
Install all dev dependencies
Or install quality tools specifically
Or install quality tools specifically
npm install --save-dev
typescript
eslint
prettier
@typescript-eslint/parser
@typescript-eslint/eslint-plugin
eslint-config-prettier
eslint-plugin-prettier
**Deliverable:** Quality tools ready
---
npm install --save-dev
typescript
eslint
prettier
@typescript-eslint/parser
@typescript-eslint/eslint-plugin
eslint-config-prettier
eslint-plugin-prettier
2. Code Formatting Check (Prettier)
2. 代码格式化检查(Prettier)
Check if code is formatted
Check if code is formatted
npx prettier --check "src/**/*.{ts,tsx,js,jsx,json,css,md}"
npx prettier --check "src/**/*.{ts,tsx,js,jsx,json,css,md}"
Check specific files
Check specific files
npx prettier --check src/index.ts
npx prettier --check src/index.ts
Check with detailed output
Check with detailed output
npx prettier --check --log-level debug "src/**/*.ts"
**Auto-Format Code:**
```bash
npx prettier --check --log-level debug "src/**/*.ts"
Format all code
Format all code
npx prettier --write "src/**/*.{ts,tsx,js,jsx,json,css,md}"
npx prettier --write "src/**/*.{ts,tsx,js,jsx,json,css,md}"
Format specific directory
Format specific directory
npx prettier --write "src/components/**/*.tsx"
npx prettier --write "src/components/**/*.tsx"
Check what would change (dry run)
Check what would change (dry run)
npx prettier --check --list-different "src/**/*.ts"
**Configuration (.prettierrc.json):**
```json
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"arrowParens": "always",
"endOfLine": "lf"
}
.prettierignore:
node_modules/
dist/
build/
coverage/
*.min.js
package-lock.json
Deliverable: Formatting validation report
npx prettier --check --list-different "src/**/*.ts"
**配置文件(.prettierrc.json):**
```json
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"arrowParens": "always",
"endOfLine": "lf"
}
.prettierignore:
node_modules/
dist/
build/
coverage/
*.min.js
package-lock.json
交付成果: 格式化验证报告
3. Type Checking (TypeScript Compiler)
3. 类型检查(TypeScript编译器)
Check with specific config
Check with specific config
npx tsc --noEmit --project tsconfig.json
npx tsc --noEmit --project tsconfig.json
Check and show all errors
Check and show all errors
npx tsc --noEmit --pretty
npx tsc --noEmit --pretty
Incremental type checking
Incremental type checking
npx tsc --noEmit --incremental
**Watch Mode for Development:**
```bash
npx tsc --noEmit --incremental
Watch and type check continuously
Watch and type check continuously
With specific project
With specific project
npx tsc --noEmit --watch --project tsconfig.json
**Configuration (tsconfig.json):**
```json
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"lib": ["ES2020", "DOM"],
"jsx": "react-jsx",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "build"]
}
Deliverable: Type checking report
npx tsc --noEmit --watch --project tsconfig.json
**配置文件(tsconfig.json):**
```json
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"lib": ["ES2020", "DOM"],
"jsx": "react-jsx",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "build"]
}
交付成果: 类型检查报告
4. Linting (ESLint)
4. 代码检查(ESLint)
Lint entire codebase
Lint entire codebase
npx eslint "src/**/*.{ts,tsx,js,jsx}"
npx eslint "src/**/*.{ts,tsx,js,jsx}"
Lint with auto-fix
Lint with auto-fix
npx eslint "src/**/*.{ts,tsx,js,jsx}" --fix
npx eslint "src/**/*.{ts,tsx,js,jsx}" --fix
Lint with detailed output
Lint with detailed output
npx eslint "src/**/*.{ts,tsx,js,jsx}" --format=stylish
npx eslint "src/**/*.{ts,tsx,js,jsx}" --format=stylish
Generate HTML report
Generate HTML report
npx eslint "src/**/*.{ts,tsx,js,jsx}" --format=html --output-file=eslint-report.html
**Check Specific Rules:**
```bash
npx eslint "src/**/*.{ts,tsx,js,jsx}" --format=html --output-file=eslint-report.html
Check for unused variables
Check for unused variables
npx eslint "src/**/*.ts" --rule '@typescript-eslint/no-unused-vars: error'
npx eslint "src/**/*.ts" --rule '@typescript-eslint/no-unused-vars: error'
Check complexity
Check complexity
npx eslint "src/**/*.ts" --rule 'complexity: ["error", 10]'
npx eslint "src/**/*.ts" --rule 'complexity: ["error", 10]'
Check max lines
Check max lines
npx eslint "src/**/*.ts" --rule 'max-lines: ["error", 500]'
**Configuration (.eslintrc.json):**
```json
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
"project": "./tsconfig.json"
},
"plugins": ["@typescript-eslint", "prettier"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier"
],
"rules": {
"prettier/prettier": "error",
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-unused-vars": ["error", {
"argsIgnorePattern": "^_"
}],
"complexity": ["error", 10],
"max-lines": ["error", 500],
"max-lines-per-function": ["error", 50],
"no-console": ["warn", {
"allow": ["warn", "error"]
}]
},
"ignorePatterns": ["dist/", "build/", "node_modules/", "*.config.js"]
}
Deliverable: Linting report
npx eslint "src/**/*.ts" --rule 'max-lines: ["error", 500]'
**配置文件(.eslintrc.json):**
```json
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
"project": "./tsconfig.json"
},
"plugins": ["@typescript-eslint", "prettier"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier"
],
"rules": {
"prettier/prettier": "error",
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-unused-vars": ["error", {
"argsIgnorePattern": "^_"
}],
"complexity": ["error", 10],
"max-lines": ["error", 500],
"max-lines-per-function": ["error", 50],
"no-console": ["warn", {
"allow": ["warn", "error"]
}]
},
"ignorePatterns": ["dist/", "build/", "node_modules/", "*.config.js"]
}
交付成果: 代码检查报告
5. Security Analysis
5. 安全分析
Check for vulnerabilities
Check for vulnerabilities
Detailed audit
Detailed audit
Production dependencies only
Production dependencies only
Fix vulnerabilities
Fix vulnerabilities
Fix including breaking changes
Fix including breaking changes
npm audit fix --force
**Check for Outdated Packages:**
```bash
List outdated packages
List outdated packages
Check specific package
Check specific package
Update packages
Update packages
npm update
**Dependency License Check:**
```bash
Install license checker
Install license checker
npm install --save-dev license-checker
npm install --save-dev license-checker
Check licenses
Check licenses
npx license-checker --summary
npx license-checker --summary
Exclude specific licenses
Exclude specific licenses
npx license-checker --exclude "MIT,Apache-2.0"
**Deliverable:** Security analysis report
---
npx license-checker --exclude "MIT,Apache-2.0"
6. Code Complexity Analysis
6. 代码复杂度分析
Check cyclomatic complexity
Check cyclomatic complexity
npx eslint "src/**/*.ts" --rule 'complexity: ["error", 10]'
npx eslint "src/**/*.ts" --rule 'complexity: ["error", 10]'
Check max depth
Check max depth
npx eslint "src/**/*.ts" --rule 'max-depth: ["error", 4]'
npx eslint "src/**/*.ts" --rule 'max-depth: ["error", 4]'
Check max nested callbacks
Check max nested callbacks
npx eslint "src/**/*.ts" --rule 'max-nested-callbacks: ["error", 3]'
**TypeScript-Specific Complexity:**
```bash
npx eslint "src/**/*.ts" --rule 'max-nested-callbacks: ["error", 3]'
**TypeScript特定复杂度分析:**
```bash
Install complexity tool
Install complexity tool
npm install --save-dev ts-complex
npm install --save-dev ts-complex
Analyze complexity
Analyze complexity
npx ts-complexity src/
**Deliverable:** Complexity analysis report
---
7. Import/Export Validation
7. 导入/导出验证
Install ts-prune
Install ts-prune
npm install --save-dev ts-prune
npm install --save-dev ts-prune
Find unused exports
Find unused exports
Exclude specific files
Exclude specific files
npx ts-prune --ignore "index.ts"
**Check Import Order:**
```bash
npx ts-prune --ignore "index.ts"
Install eslint-plugin-import
Install eslint-plugin-import
npm install --save-dev eslint-plugin-import
npm install --save-dev eslint-plugin-import
Add to .eslintrc.json
Add to .eslintrc.json
{
"plugins": ["import"],
"rules": {
"import/order": ["error", {
"groups": [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index"
],
"newlines-between": "always"
}]
}
}
**Deliverable:** Import validation report
---
{
"plugins": ["import"],
"rules": {
"import/order": ["error", {
"groups": [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index"
],
"newlines-between": "always"
}]
}
}
8. Comprehensive Quality Check
8. 全面质量检查
scripts/quality-check.sh
scripts/quality-check.sh
set -e # Exit on first error
echo "=== TypeScript Quality Checks ==="
echo "1. Code Formatting (Prettier)..."
npx prettier --check "src/**/*.{ts,tsx,js,jsx}"
echo "2. Type Checking (tsc)..."
npx tsc --noEmit
echo "3. Linting (ESLint)..."
npx eslint "src/**/*.{ts,tsx}" --max-warnings=0
echo "4. Security Audit..."
npm audit --production
echo "5. Unused Exports..."
npx ts-prune || true # Don't fail on unused exports
echo "=== All Quality Checks Passed ✅ ==="
**package.json Scripts:**
```json
{
"scripts": {
"lint": "eslint 'src/**/*.{ts,tsx}' --max-warnings=0",
"lint:fix": "eslint 'src/**/*.{ts,tsx}' --fix",
"format": "prettier --write 'src/**/*.{ts,tsx,json,css,md}'",
"format:check": "prettier --check 'src/**/*.{ts,tsx,json,css,md}'",
"type-check": "tsc --noEmit",
"quality": "npm run format:check && npm run type-check && npm run lint",
"test": "jest",
"test:coverage": "jest --coverage"
}
}
Deliverable: Comprehensive quality report
set -e # Exit on first error
echo "=== TypeScript Quality Checks ==="
echo "1. Code Formatting (Prettier)..."
npx prettier --check "src/**/*.{ts,tsx,js,jsx}"
echo "2. Type Checking (tsc)..."
npx tsc --noEmit
echo "3. Linting (ESLint)..."
npx eslint "src/**/*.{ts,tsx}" --max-warnings=0
echo "4. Security Audit..."
npm audit --production
echo "5. Unused Exports..."
npx ts-prune || true # Don't fail on unused exports
echo "=== All Quality Checks Passed ✅ ==="
**package.json脚本:**
```json
{
"scripts": {
"lint": "eslint 'src/**/*.{ts,tsx}' --max-warnings=0",
"lint:fix": "eslint 'src/**/*.{ts,tsx}' --fix",
"format": "prettier --write 'src/**/*.{ts,tsx,json,css,md}'",
"format:check": "prettier --check 'src/**/*.{ts,tsx,json,css,md}'",
"type-check": "tsc --noEmit",
"quality": "npm run format:check && npm run type-check && npm run lint",
"test": "jest",
"test:coverage": "jest --coverage"
}
}
交付成果: 全面质量报告
Quality Check Matrix
质量检查矩阵
| Check | Tool | Threshold | Auto-Fix |
|---|
| Formatting | Prettier | Must pass | Yes |
| Type checking | tsc | 0 errors | No |
| Linting | ESLint | 0 errors | Partial |
| Security | npm audit | 0 critical | Partial |
| Complexity | ESLint | ≤ 10 | No |
| Unused exports | ts-prune | Warning only | No |
| 检查项 | 工具 | 阈值 | 自动修复 |
|---|
| 格式化 | Prettier | 必须通过 | 是 |
| 类型检查 | tsc | 0错误 | 否 |
| 代码检查 | ESLint | 0错误 | 部分支持 |
| 安全 | npm audit | 0严重漏洞 | 部分支持 |
| 复杂度 | ESLint | ≤10 | 否 |
| 未使用导出 | ts-prune | 仅警告 | 否 |
Pre-commit Integration
提交前集成
Setup Husky and lint-staged:
Install tools
Install tools
npm install --save-dev husky lint-staged
npm install --save-dev husky lint-staged
Initialize husky
Initialize husky
npx husky-init && npm install
npx husky-init && npm install
Add pre-commit hook
Add pre-commit hook
npx husky add .husky/pre-commit "npx lint-staged"
**Configuration (package.json):**
```json
{
"lint-staged": {
"*.{ts,tsx}": [
"prettier --write",
"eslint --fix",
"tsc-files --noEmit"
],
"*.{json,css,md}": [
"prettier --write"
]
}
}
Deliverable: Pre-commit hooks configured
npx husky add .husky/pre-commit "npx lint-staged"
**配置文件(package.json):**
```json
{
"lint-staged": {
"*.{ts,tsx}": [
"prettier --write",
"eslint --fix",
"tsc-files --noEmit"
],
"*.{json,css,md}": [
"prettier --write"
]
}
}
交付成果: 提交前钩子已配置
.github/workflows/quality.yml
.github/workflows/quality.yml
name: TypeScript Quality Checks
on: [push, pull_request]
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Check formatting
run: npm run format:check
- name: Type checking
run: npm run type-check
- name: Linting
run: npm run lint
- name: Security audit
run: npm audit --production --audit-level=high
- name: Run tests
run: npm test
- name: Check coverage
run: npm run test:coverage
**Deliverable:** CI/CD quality pipeline
---
name: TypeScript Quality Checks
on: [push, pull_request]
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Check formatting
run: npm run format:check
- name: Type checking
run: npm run type-check
- name: Linting
run: npm run lint
- name: Security audit
run: npm audit --production --audit-level=high
- name: Run tests
run: npm test
- name: Check coverage
run: npm run test:coverage
Quality Check Troubleshooting
质量检查故障排除
Prettier Formatting Failures
Prettier格式化失败
Check what would change
Check what would change
npx prettier --check --list-different "src/**/*.ts"
npx prettier --check --list-different "src/**/*.ts"
npx prettier --write "src/**/*.ts"
npx prettier --write "src/**/*.ts"
Check specific file
Check specific file
npx prettier --check src/index.ts
npx prettier --check src/index.ts
TypeScript Type Errors
TypeScript类型错误
Show detailed errors
Show detailed errors
npx tsc --noEmit --pretty
npx tsc --noEmit --pretty
Check specific file
Check specific file
npx tsc --noEmit src/component.tsx
npx tsc --noEmit src/component.tsx
Skip lib check (faster)
Skip lib check (faster)
npx tsc --noEmit --skipLibCheck
npx tsc --noEmit --skipLibCheck
ESLint Violations
ESLint违规
Show detailed violations
Show detailed violations
npx eslint "src/**/*.ts" --format=stylish
npx eslint "src/**/*.ts" --format=stylish
Auto-fix safe violations
Auto-fix safe violations
npx eslint "src/**/*.ts" --fix
npx eslint "src/**/*.ts" --fix
Ignore specific line (last resort)
Ignore specific line (last resort)
// eslint-disable-next-line rule-name
// eslint-disable-next-line rule-name
Show detailed audit
Show detailed audit
Fix non-breaking changes
Fix non-breaking changes
Override (use with caution)
Override (use with caution)
Quality Report Template
质量报告模板
TypeScript Quality Check Report
TypeScript质量检查报告
- Status: ✅ All checks passed
- Date: 2024-01-15
- Code Base: src/
- 状态: ✅ 所有检查通过
- 日期: 2024-01-15
- 代码库: src/
Formatting (Prettier)
格式化(Prettier)
- Status: ✅ PASS
- Files Checked: 87
- Issues: 0
- 状态: ✅ 通过
- 检查文件数: 87
- 问题数: 0
Type Checking (tsc)
类型检查(tsc)
- Status: ✅ PASS
- Files Checked: 87
- Errors: 0
- Warnings: 0
- 状态: ✅ 通过
- 检查文件数: 87
- 错误数: 0
- 警告数: 0
Linting (ESLint)
代码检查(ESLint)
- Status: ✅ PASS
- Files Checked: 87
- Errors: 0
- Warnings: 0
- 状态: ✅ 通过
- 检查文件数: 87
- 错误数: 0
- 警告数: 0
Security (npm audit)
安全(npm audit)
- Status: ✅ PASS
- Dependencies: 342
- Vulnerabilities: 0 critical, 0 high, 2 moderate
- 状态: ✅ 通过
- 依赖项数: 342
- 漏洞数: 0个严重,0个高危,2个中危
- Status: ✅ PASS
- Average Complexity: 3.8
- Max Complexity: 9
- Files > 10: 0
- 状态: ✅ 通过
- 平均复杂度: 3.8
- 最大复杂度: 9
- 复杂度超10的文件数: 0
All TypeScript quality checks passed successfully. Code is well-formatted, type-safe, lint-free, secure, and maintainable.
所有TypeScript质量检查已成功通过。代码格式规范、类型安全、无代码检查违规、安全可靠且易于维护。
- Continue using strict TypeScript settings
- Keep complexity below 10
- Run pre-commit hooks before commits
- Update moderate-severity vulnerabilities when possible
- 继续使用严格的TypeScript配置
- 保持复杂度低于10
- 提交代码前运行提交前钩子
- 尽可能更新中危漏洞
Integration with Code Quality Specialist
与代码质量专家集成
Input: TypeScript/JavaScript codebase quality check request
Process: Run all TypeScript quality tools and analyze results
Output: Comprehensive quality report with pass/fail status
Next Step: Report to code-quality-specialist for consolidation
输入: TypeScript/JavaScript代码库质量检查请求
流程: 运行所有TypeScript质量工具并分析结果
输出: 包含通过/失败状态的全面质量报告
下一步: 将报告提交给code-quality-specialist进行整合
- Enable Prettier on save (IDE integration)
- Enable ESLint in IDE for real-time feedback
- Use strict TypeScript settings
- Fix type errors immediately
- Use pre-commit hooks
- 在IDE中启用Prettier自动保存格式化
- 在IDE中启用ESLint以获取实时反馈
- 使用严格的TypeScript配置
- 立即修复类型错误
- 使用提交前钩子
- Run full quality check script
- Ensure all checks pass
- Fix issues before pushing
- Review security warnings
- 运行完整的质量检查脚本
- 确保所有检查通过
- 推送前修复所有问题
- 查看安全警告
- Run quality checks on every PR
- Fail build on quality violations
- Generate quality reports
- Track quality metrics over time
- 对每个PR运行质量检查
- 质量违规时构建失败
- 生成质量报告
- 随时间跟踪质量指标
- Verify quality checks passed
- Review type definitions
- Check security scan results
- Validate complexity metrics
- 验证质量检查已通过
- 评审类型定义
- 查看安全扫描结果
- 验证复杂度指标