ccjk 1.5.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +249 -297
- package/README.ko.md +241 -290
- package/README.md +222 -364
- package/README.zh-CN.md +553 -295
- package/dist/chunks/claude-code-config-manager.mjs +7 -7
- package/dist/chunks/claude-code-incremental-manager.mjs +1 -1
- package/dist/chunks/codex-config-switch.mjs +3 -3
- package/dist/chunks/codex-provider-manager.mjs +1 -1
- package/dist/chunks/codex-uninstaller.mjs +2 -2
- package/dist/chunks/commands.mjs +1 -1
- package/dist/chunks/features.mjs +10 -10
- package/dist/chunks/simple-config.mjs +321 -389
- package/dist/chunks/smart-guide.mjs +234 -0
- package/dist/cli.mjs +1795 -433
- package/dist/i18n/locales/en/configuration.json +12 -1
- package/dist/i18n/locales/en/marketplace.json +84 -0
- package/dist/i18n/locales/en/menu.json +38 -1
- package/dist/i18n/locales/en/skills.json +140 -0
- package/dist/i18n/locales/en/smartGuide.json +49 -0
- package/dist/i18n/locales/en/subagent.json +69 -0
- package/dist/i18n/locales/en/superpowers.json +58 -0
- package/dist/i18n/locales/zh-CN/configuration.json +12 -1
- package/dist/i18n/locales/zh-CN/marketplace.json +84 -0
- package/dist/i18n/locales/zh-CN/menu.json +38 -1
- package/dist/i18n/locales/zh-CN/skills.json +140 -0
- package/dist/i18n/locales/zh-CN/smartGuide.json +49 -0
- package/dist/i18n/locales/zh-CN/subagent.json +69 -0
- package/dist/i18n/locales/zh-CN/superpowers.json +58 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +366 -7
- package/package.json +26 -27
- package/templates/common/skills/en/brainstorming.md +64 -0
- package/templates/common/skills/en/code-review.md +81 -0
- package/templates/common/skills/en/documentation-gen.md +808 -0
- package/templates/common/skills/en/executing-plans.md +75 -0
- package/templates/common/skills/en/git-commit.md +216 -0
- package/templates/common/skills/en/interview.md +223 -0
- package/templates/common/skills/en/migration-assistant.md +312 -0
- package/templates/common/skills/en/performance-profiling.md +576 -0
- package/templates/common/skills/en/pr-review.md +341 -0
- package/templates/common/skills/en/refactoring.md +384 -0
- package/templates/common/skills/en/security-audit.md +462 -0
- package/templates/common/skills/en/systematic-debugging.md +82 -0
- package/templates/common/skills/en/tdd-workflow.md +93 -0
- package/templates/common/skills/en/verification.md +81 -0
- package/templates/common/skills/en/workflow.md +370 -0
- package/templates/common/skills/en/writing-plans.md +78 -0
- package/templates/common/skills/zh-CN/documentation-gen.md +807 -0
- package/templates/common/skills/zh-CN/migration-assistant.md +318 -0
- package/templates/common/skills/zh-CN/performance-profiling.md +746 -0
- package/templates/common/skills/zh-CN/pr-review.md +341 -0
- package/templates/common/skills/zh-CN/refactoring.md +384 -0
- package/templates/common/skills/zh-CN/security-audit.md +462 -0
- package/templates/common/smart-guide/en/smart-guide.md +72 -0
- package/templates/common/smart-guide/zh-CN/smart-guide.md +72 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: pr-review
|
|
3
|
+
description: Comprehensive PR review with security, performance, and code quality checks
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
author: CCJK
|
|
6
|
+
category: review
|
|
7
|
+
triggers:
|
|
8
|
+
- /pr-review
|
|
9
|
+
- /review-pr
|
|
10
|
+
- /pr
|
|
11
|
+
use_when:
|
|
12
|
+
- "User wants to review a pull request"
|
|
13
|
+
- "PR needs code review"
|
|
14
|
+
- "Merge request review"
|
|
15
|
+
- "User mentions reviewing changes"
|
|
16
|
+
auto_activate: true
|
|
17
|
+
priority: 8
|
|
18
|
+
difficulty: intermediate
|
|
19
|
+
tags:
|
|
20
|
+
- pr
|
|
21
|
+
- review
|
|
22
|
+
- git
|
|
23
|
+
- code-quality
|
|
24
|
+
allowed-tools:
|
|
25
|
+
- Bash(git *)
|
|
26
|
+
- Read
|
|
27
|
+
- Grep
|
|
28
|
+
- Glob
|
|
29
|
+
- LSP
|
|
30
|
+
context: fork
|
|
31
|
+
user-invocable: true
|
|
32
|
+
hooks:
|
|
33
|
+
- type: SkillActivate
|
|
34
|
+
command: echo "Starting PR review..."
|
|
35
|
+
- type: SkillComplete
|
|
36
|
+
command: echo "PR review completed"
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
# PR Review Skill
|
|
40
|
+
|
|
41
|
+
Comprehensive pull request review workflow with automated checks for code quality, security, performance, and test coverage.
|
|
42
|
+
|
|
43
|
+
## Workflow Steps
|
|
44
|
+
|
|
45
|
+
### 1. Change Analysis
|
|
46
|
+
|
|
47
|
+
First, analyze the PR changes:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
# Get current branch and target branch
|
|
51
|
+
git branch --show-current
|
|
52
|
+
git log --oneline origin/main..HEAD
|
|
53
|
+
|
|
54
|
+
# Get changed files
|
|
55
|
+
git diff --name-only origin/main...HEAD
|
|
56
|
+
|
|
57
|
+
# Get detailed diff statistics
|
|
58
|
+
git diff --stat origin/main...HEAD
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
**Analysis Checklist:**
|
|
62
|
+
- [ ] Identify all modified files
|
|
63
|
+
- [ ] Categorize changes (features, fixes, refactors, docs)
|
|
64
|
+
- [ ] Check for breaking changes
|
|
65
|
+
- [ ] Verify commit message quality
|
|
66
|
+
- [ ] Check for merge conflicts
|
|
67
|
+
|
|
68
|
+
### 2. Code Quality Check
|
|
69
|
+
|
|
70
|
+
Review code quality across all changed files:
|
|
71
|
+
|
|
72
|
+
**For each changed file:**
|
|
73
|
+
|
|
74
|
+
1. **Read the file** using the Read tool
|
|
75
|
+
2. **Check for code smells:**
|
|
76
|
+
- Duplicated code
|
|
77
|
+
- Long functions (>50 lines)
|
|
78
|
+
- Complex conditionals (>3 levels)
|
|
79
|
+
- Magic numbers/strings
|
|
80
|
+
- Inconsistent naming conventions
|
|
81
|
+
|
|
82
|
+
3. **Verify best practices:**
|
|
83
|
+
- Proper error handling
|
|
84
|
+
- Input validation
|
|
85
|
+
- Logging appropriateness
|
|
86
|
+
- Comment quality
|
|
87
|
+
- Code documentation
|
|
88
|
+
|
|
89
|
+
4. **Check TypeScript/JavaScript specific:**
|
|
90
|
+
- Type safety (TypeScript)
|
|
91
|
+
- Async/await usage
|
|
92
|
+
- Promise handling
|
|
93
|
+
- Memory leak potential
|
|
94
|
+
- Proper imports/exports
|
|
95
|
+
|
|
96
|
+
### 3. Security Review
|
|
97
|
+
|
|
98
|
+
Perform security analysis:
|
|
99
|
+
|
|
100
|
+
**Security Checklist:**
|
|
101
|
+
- [ ] No hardcoded credentials or API keys
|
|
102
|
+
- [ ] No sensitive data in logs
|
|
103
|
+
- [ ] Proper input sanitization
|
|
104
|
+
- [ ] SQL injection prevention
|
|
105
|
+
- [ ] XSS prevention
|
|
106
|
+
- [ ] CSRF protection
|
|
107
|
+
- [ ] Authentication/authorization checks
|
|
108
|
+
- [ ] Dependency vulnerabilities
|
|
109
|
+
|
|
110
|
+
**Search for common security issues:**
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
# Search for potential secrets
|
|
114
|
+
git grep -i "password\|secret\|api_key\|token" -- '*.ts' '*.js' '*.json'
|
|
115
|
+
|
|
116
|
+
# Check for console.log in production code
|
|
117
|
+
git grep "console\\.log" -- 'src/**/*.ts' 'src/**/*.js'
|
|
118
|
+
|
|
119
|
+
# Look for eval or dangerous functions
|
|
120
|
+
git grep -E "eval\(|Function\(|setTimeout.*string|setInterval.*string"
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### 4. Performance Review
|
|
124
|
+
|
|
125
|
+
Analyze performance implications:
|
|
126
|
+
|
|
127
|
+
**Performance Checklist:**
|
|
128
|
+
- [ ] No unnecessary re-renders (React)
|
|
129
|
+
- [ ] Efficient data structures
|
|
130
|
+
- [ ] Proper caching strategies
|
|
131
|
+
- [ ] Database query optimization
|
|
132
|
+
- [ ] No N+1 query problems
|
|
133
|
+
- [ ] Lazy loading where appropriate
|
|
134
|
+
- [ ] Bundle size impact
|
|
135
|
+
- [ ] Memory usage considerations
|
|
136
|
+
|
|
137
|
+
**Check for performance anti-patterns:**
|
|
138
|
+
- Synchronous operations in loops
|
|
139
|
+
- Unnecessary API calls
|
|
140
|
+
- Large data processing without pagination
|
|
141
|
+
- Missing indexes (database)
|
|
142
|
+
- Inefficient algorithms (O(n²) or worse)
|
|
143
|
+
|
|
144
|
+
### 5. Test Coverage
|
|
145
|
+
|
|
146
|
+
Verify test coverage for changes:
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
# Find test files for changed files
|
|
150
|
+
git diff --name-only origin/main...HEAD | grep -E '\.(test|spec)\.(ts|js)$'
|
|
151
|
+
|
|
152
|
+
# Check if new code has corresponding tests
|
|
153
|
+
# For each changed source file, verify test file exists
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
**Test Coverage Checklist:**
|
|
157
|
+
- [ ] Unit tests for new functions
|
|
158
|
+
- [ ] Integration tests for new features
|
|
159
|
+
- [ ] Edge case coverage
|
|
160
|
+
- [ ] Error handling tests
|
|
161
|
+
- [ ] Mock/stub usage appropriateness
|
|
162
|
+
- [ ] Test naming clarity
|
|
163
|
+
- [ ] Assertion quality
|
|
164
|
+
|
|
165
|
+
### 6. Architecture & Design
|
|
166
|
+
|
|
167
|
+
Review architectural decisions:
|
|
168
|
+
|
|
169
|
+
**Architecture Checklist:**
|
|
170
|
+
- [ ] Follows project patterns
|
|
171
|
+
- [ ] Proper separation of concerns
|
|
172
|
+
- [ ] SOLID principles adherence
|
|
173
|
+
- [ ] DRY principle (Don't Repeat Yourself)
|
|
174
|
+
- [ ] Appropriate abstraction levels
|
|
175
|
+
- [ ] Dependency injection usage
|
|
176
|
+
- [ ] Module coupling/cohesion
|
|
177
|
+
|
|
178
|
+
### 7. Documentation Review
|
|
179
|
+
|
|
180
|
+
Check documentation quality:
|
|
181
|
+
|
|
182
|
+
**Documentation Checklist:**
|
|
183
|
+
- [ ] README updates (if needed)
|
|
184
|
+
- [ ] API documentation
|
|
185
|
+
- [ ] Inline comments for complex logic
|
|
186
|
+
- [ ] JSDoc/TSDoc comments
|
|
187
|
+
- [ ] CHANGELOG updates
|
|
188
|
+
- [ ] Migration guides (breaking changes)
|
|
189
|
+
|
|
190
|
+
## Output Format
|
|
191
|
+
|
|
192
|
+
Provide review results in the following structured format:
|
|
193
|
+
|
|
194
|
+
```markdown
|
|
195
|
+
# PR Review Summary
|
|
196
|
+
|
|
197
|
+
## Overview
|
|
198
|
+
- **Branch**: [branch-name]
|
|
199
|
+
- **Target**: [target-branch]
|
|
200
|
+
- **Files Changed**: [count]
|
|
201
|
+
- **Lines Added**: [count]
|
|
202
|
+
- **Lines Removed**: [count]
|
|
203
|
+
|
|
204
|
+
## Change Summary
|
|
205
|
+
[Brief description of what this PR does]
|
|
206
|
+
|
|
207
|
+
## Review Results
|
|
208
|
+
|
|
209
|
+
### ✅ Strengths
|
|
210
|
+
- [List positive aspects]
|
|
211
|
+
- [Good practices observed]
|
|
212
|
+
- [Well-implemented features]
|
|
213
|
+
|
|
214
|
+
### ⚠️ Issues Found
|
|
215
|
+
|
|
216
|
+
#### 🔴 Critical Issues
|
|
217
|
+
- **[File:Line]**: [Description]
|
|
218
|
+
- **Impact**: [Explanation]
|
|
219
|
+
- **Recommendation**: [How to fix]
|
|
220
|
+
|
|
221
|
+
#### 🟡 Warnings
|
|
222
|
+
- **[File:Line]**: [Description]
|
|
223
|
+
- **Suggestion**: [Improvement recommendation]
|
|
224
|
+
|
|
225
|
+
#### 🔵 Suggestions
|
|
226
|
+
- **[File:Line]**: [Description]
|
|
227
|
+
- **Enhancement**: [Optional improvement]
|
|
228
|
+
|
|
229
|
+
### Security Analysis
|
|
230
|
+
- **Status**: ✅ Pass / ⚠️ Issues Found / 🔴 Critical
|
|
231
|
+
- **Findings**: [List security concerns if any]
|
|
232
|
+
|
|
233
|
+
### Performance Analysis
|
|
234
|
+
- **Status**: ✅ Good / ⚠️ Concerns / 🔴 Issues
|
|
235
|
+
- **Findings**: [List performance concerns if any]
|
|
236
|
+
|
|
237
|
+
### Test Coverage
|
|
238
|
+
- **Status**: ✅ Adequate / ⚠️ Insufficient / 🔴 Missing
|
|
239
|
+
- **Coverage**: [Percentage if available]
|
|
240
|
+
- **Missing Tests**: [List areas needing tests]
|
|
241
|
+
|
|
242
|
+
### Code Quality Score
|
|
243
|
+
- **Overall**: [Score out of 10]
|
|
244
|
+
- **Maintainability**: [Score]
|
|
245
|
+
- **Readability**: [Score]
|
|
246
|
+
- **Testability**: [Score]
|
|
247
|
+
|
|
248
|
+
## Recommendations
|
|
249
|
+
|
|
250
|
+
### Must Fix (Before Merge)
|
|
251
|
+
1. [Critical issue 1]
|
|
252
|
+
2. [Critical issue 2]
|
|
253
|
+
|
|
254
|
+
### Should Fix (High Priority)
|
|
255
|
+
1. [Important issue 1]
|
|
256
|
+
2. [Important issue 2]
|
|
257
|
+
|
|
258
|
+
### Nice to Have (Optional)
|
|
259
|
+
1. [Enhancement 1]
|
|
260
|
+
2. [Enhancement 2]
|
|
261
|
+
|
|
262
|
+
## Approval Status
|
|
263
|
+
- [ ] ✅ **Approved** - Ready to merge
|
|
264
|
+
- [ ] ⚠️ **Approved with Comments** - Can merge but address comments
|
|
265
|
+
- [ ] 🔴 **Changes Requested** - Must fix issues before merge
|
|
266
|
+
|
|
267
|
+
## Additional Notes
|
|
268
|
+
[Any other observations or context]
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Best Practices
|
|
272
|
+
|
|
273
|
+
1. **Be Constructive**: Focus on improvement, not criticism
|
|
274
|
+
2. **Be Specific**: Point to exact files and lines
|
|
275
|
+
3. **Explain Why**: Don't just say what's wrong, explain the impact
|
|
276
|
+
4. **Suggest Solutions**: Provide actionable recommendations
|
|
277
|
+
5. **Acknowledge Good Work**: Highlight positive aspects
|
|
278
|
+
6. **Consider Context**: Understand project constraints and deadlines
|
|
279
|
+
7. **Use Examples**: Show better alternatives when suggesting changes
|
|
280
|
+
|
|
281
|
+
## Common Review Patterns
|
|
282
|
+
|
|
283
|
+
### Pattern 1: Feature Addition
|
|
284
|
+
- Verify feature completeness
|
|
285
|
+
- Check for feature flags
|
|
286
|
+
- Validate error handling
|
|
287
|
+
- Ensure backward compatibility
|
|
288
|
+
|
|
289
|
+
### Pattern 2: Bug Fix
|
|
290
|
+
- Verify root cause addressed
|
|
291
|
+
- Check for regression tests
|
|
292
|
+
- Validate fix doesn't introduce new issues
|
|
293
|
+
- Review related code areas
|
|
294
|
+
|
|
295
|
+
### Pattern 3: Refactoring
|
|
296
|
+
- Ensure behavior unchanged
|
|
297
|
+
- Verify test coverage maintained
|
|
298
|
+
- Check for performance impact
|
|
299
|
+
- Validate code simplification
|
|
300
|
+
|
|
301
|
+
### Pattern 4: Dependency Update
|
|
302
|
+
- Review changelog
|
|
303
|
+
- Check for breaking changes
|
|
304
|
+
- Verify compatibility
|
|
305
|
+
- Test critical paths
|
|
306
|
+
|
|
307
|
+
## Integration with CI/CD
|
|
308
|
+
|
|
309
|
+
If CI/CD results are available, incorporate them:
|
|
310
|
+
|
|
311
|
+
```bash
|
|
312
|
+
# Check CI status
|
|
313
|
+
gh pr checks [PR-number]
|
|
314
|
+
|
|
315
|
+
# View test results
|
|
316
|
+
gh pr view [PR-number] --json statusCheckRollup
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
## Final Checklist
|
|
320
|
+
|
|
321
|
+
Before completing review:
|
|
322
|
+
|
|
323
|
+
- [ ] All changed files reviewed
|
|
324
|
+
- [ ] Security concerns addressed
|
|
325
|
+
- [ ] Performance implications considered
|
|
326
|
+
- [ ] Test coverage verified
|
|
327
|
+
- [ ] Documentation checked
|
|
328
|
+
- [ ] Architecture alignment confirmed
|
|
329
|
+
- [ ] Breaking changes identified
|
|
330
|
+
- [ ] Approval status determined
|
|
331
|
+
- [ ] Actionable feedback provided
|
|
332
|
+
- [ ] Review summary formatted
|
|
333
|
+
|
|
334
|
+
## Notes
|
|
335
|
+
|
|
336
|
+
- Use LSP tools for type checking and linting results
|
|
337
|
+
- Leverage Grep for pattern matching across codebase
|
|
338
|
+
- Use Glob to find related files
|
|
339
|
+
- Read files completely for context
|
|
340
|
+
- Consider project-specific guidelines in CLAUDE.md
|
|
341
|
+
- Adapt review depth based on PR size and complexity
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: refactoring
|
|
3
|
+
description: Intelligent code refactoring with safety checks and best practices
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
author: CCJK
|
|
6
|
+
category: dev
|
|
7
|
+
triggers:
|
|
8
|
+
- /refactor
|
|
9
|
+
- /rf
|
|
10
|
+
- /cleanup
|
|
11
|
+
use_when:
|
|
12
|
+
- "User wants to refactor code"
|
|
13
|
+
- "Code needs cleanup"
|
|
14
|
+
- "Improve code structure"
|
|
15
|
+
- "User mentions refactoring"
|
|
16
|
+
auto_activate: true
|
|
17
|
+
priority: 7
|
|
18
|
+
difficulty: intermediate
|
|
19
|
+
tags:
|
|
20
|
+
- refactoring
|
|
21
|
+
- code-quality
|
|
22
|
+
- cleanup
|
|
23
|
+
allowed-tools:
|
|
24
|
+
- Read
|
|
25
|
+
- Write
|
|
26
|
+
- Edit
|
|
27
|
+
- Grep
|
|
28
|
+
- Glob
|
|
29
|
+
- mcp__ide__getDiagnostics
|
|
30
|
+
- Bash(npm test)
|
|
31
|
+
- Bash(pnpm test)
|
|
32
|
+
- Bash(yarn test)
|
|
33
|
+
context: fork
|
|
34
|
+
user-invocable: true
|
|
35
|
+
hooks:
|
|
36
|
+
- type: PreToolUse
|
|
37
|
+
matcher: Edit
|
|
38
|
+
command: echo "Creating backup before edit..."
|
|
39
|
+
- type: PostToolUse
|
|
40
|
+
matcher: Edit
|
|
41
|
+
command: echo "Edit completed, verifying..."
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
# Refactoring
|
|
45
|
+
|
|
46
|
+
## Context
|
|
47
|
+
$ARGUMENTS
|
|
48
|
+
|
|
49
|
+
## Instructions
|
|
50
|
+
|
|
51
|
+
Follow safe and systematic refactoring practices:
|
|
52
|
+
|
|
53
|
+
### Safety First
|
|
54
|
+
|
|
55
|
+
Before any refactoring:
|
|
56
|
+
|
|
57
|
+
1. **Verify Tests Exist**
|
|
58
|
+
- Check for existing test coverage
|
|
59
|
+
- Run tests to ensure they pass
|
|
60
|
+
- If no tests exist, write them first
|
|
61
|
+
|
|
62
|
+
2. **Create Backup**
|
|
63
|
+
- Git commit current state
|
|
64
|
+
- Or create manual backup of files
|
|
65
|
+
|
|
66
|
+
3. **Small Steps**
|
|
67
|
+
- Make one change at a time
|
|
68
|
+
- Test after each change
|
|
69
|
+
- Commit frequently
|
|
70
|
+
|
|
71
|
+
### Refactoring Catalog
|
|
72
|
+
|
|
73
|
+
Choose the appropriate refactoring technique:
|
|
74
|
+
|
|
75
|
+
#### 1. Extract Method
|
|
76
|
+
**When**: Function is too long or has duplicated code
|
|
77
|
+
**How**:
|
|
78
|
+
```typescript
|
|
79
|
+
// Before
|
|
80
|
+
function processOrder(order) {
|
|
81
|
+
// validate order
|
|
82
|
+
if (!order.items || order.items.length === 0) {
|
|
83
|
+
throw new Error('Empty order')
|
|
84
|
+
}
|
|
85
|
+
// calculate total
|
|
86
|
+
let total = 0
|
|
87
|
+
for (const item of order.items) {
|
|
88
|
+
total += item.price * item.quantity
|
|
89
|
+
}
|
|
90
|
+
// apply discount
|
|
91
|
+
if (order.coupon) {
|
|
92
|
+
total *= (1 - order.coupon.discount)
|
|
93
|
+
}
|
|
94
|
+
return total
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// After
|
|
98
|
+
function processOrder(order) {
|
|
99
|
+
validateOrder(order)
|
|
100
|
+
const subtotal = calculateSubtotal(order.items)
|
|
101
|
+
return applyDiscount(subtotal, order.coupon)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function validateOrder(order) {
|
|
105
|
+
if (!order.items || order.items.length === 0) {
|
|
106
|
+
throw new Error('Empty order')
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function calculateSubtotal(items) {
|
|
111
|
+
return items.reduce((sum, item) =>
|
|
112
|
+
sum + item.price * item.quantity, 0)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function applyDiscount(amount, coupon) {
|
|
116
|
+
return coupon ? amount * (1 - coupon.discount) : amount
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
#### 2. Rename Symbol
|
|
121
|
+
**When**: Name doesn't clearly express intent
|
|
122
|
+
**How**:
|
|
123
|
+
- Use IDE refactoring tools when possible
|
|
124
|
+
- Search and replace carefully
|
|
125
|
+
- Update documentation and comments
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
// Before
|
|
129
|
+
function calc(x, y) { return x * y }
|
|
130
|
+
|
|
131
|
+
// After
|
|
132
|
+
function calculateArea(width, height) {
|
|
133
|
+
return width * height
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### 3. Move to Module
|
|
138
|
+
**When**: Code belongs in different module
|
|
139
|
+
**How**:
|
|
140
|
+
- Identify logical grouping
|
|
141
|
+
- Move related functions together
|
|
142
|
+
- Update imports/exports
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
// Before: utils.ts
|
|
146
|
+
export function validateEmail(email) { ... }
|
|
147
|
+
export function sendEmail(to, subject) { ... }
|
|
148
|
+
export function formatDate(date) { ... }
|
|
149
|
+
|
|
150
|
+
// After: email.ts
|
|
151
|
+
export function validateEmail(email) { ... }
|
|
152
|
+
export function sendEmail(to, subject) { ... }
|
|
153
|
+
|
|
154
|
+
// After: date.ts
|
|
155
|
+
export function formatDate(date) { ... }
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### 4. Inline Variable
|
|
159
|
+
**When**: Variable doesn't add clarity
|
|
160
|
+
**How**:
|
|
161
|
+
```typescript
|
|
162
|
+
// Before
|
|
163
|
+
const isValid = user.age >= 18
|
|
164
|
+
return isValid
|
|
165
|
+
|
|
166
|
+
// After
|
|
167
|
+
return user.age >= 18
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
#### 5. Extract Interface
|
|
171
|
+
**When**: Multiple implementations share behavior
|
|
172
|
+
**How**:
|
|
173
|
+
```typescript
|
|
174
|
+
// Before
|
|
175
|
+
class FileLogger {
|
|
176
|
+
log(message: string) { ... }
|
|
177
|
+
}
|
|
178
|
+
class ConsoleLogger {
|
|
179
|
+
log(message: string) { ... }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// After
|
|
183
|
+
interface Logger {
|
|
184
|
+
log(message: string): void
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
class FileLogger implements Logger {
|
|
188
|
+
log(message: string) { ... }
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
class ConsoleLogger implements Logger {
|
|
192
|
+
log(message: string) { ... }
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
#### 6. Replace Conditional with Polymorphism
|
|
197
|
+
**When**: Complex conditionals based on type
|
|
198
|
+
**How**:
|
|
199
|
+
```typescript
|
|
200
|
+
// Before
|
|
201
|
+
function getSpeed(vehicle) {
|
|
202
|
+
if (vehicle.type === 'car') {
|
|
203
|
+
return vehicle.enginePower * 2
|
|
204
|
+
} else if (vehicle.type === 'bike') {
|
|
205
|
+
return vehicle.gears * 10
|
|
206
|
+
} else if (vehicle.type === 'plane') {
|
|
207
|
+
return vehicle.thrust * 100
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// After
|
|
212
|
+
interface Vehicle {
|
|
213
|
+
getSpeed(): number
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
class Car implements Vehicle {
|
|
217
|
+
getSpeed() { return this.enginePower * 2 }
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
class Bike implements Vehicle {
|
|
221
|
+
getSpeed() { return this.gears * 10 }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
class Plane implements Vehicle {
|
|
225
|
+
getSpeed() { return this.thrust * 100 }
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
#### 7. Simplify Conditional
|
|
230
|
+
**When**: Complex boolean logic
|
|
231
|
+
**How**:
|
|
232
|
+
```typescript
|
|
233
|
+
// Before
|
|
234
|
+
if (user.age >= 18 && user.hasLicense && !user.isSuspended) {
|
|
235
|
+
allowDriving()
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// After
|
|
239
|
+
function canDrive(user) {
|
|
240
|
+
return user.age >= 18
|
|
241
|
+
&& user.hasLicense
|
|
242
|
+
&& !user.isSuspended
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (canDrive(user)) {
|
|
246
|
+
allowDriving()
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
#### 8. Remove Dead Code
|
|
251
|
+
**When**: Code is never executed
|
|
252
|
+
**How**:
|
|
253
|
+
- Use coverage tools to identify
|
|
254
|
+
- Check git history for context
|
|
255
|
+
- Remove confidently with version control
|
|
256
|
+
|
|
257
|
+
### Step-by-Step Process
|
|
258
|
+
|
|
259
|
+
```markdown
|
|
260
|
+
## Refactoring Session: [Target]
|
|
261
|
+
|
|
262
|
+
### 1. Pre-Refactoring
|
|
263
|
+
- [ ] Tests exist and pass
|
|
264
|
+
- [ ] Code committed to git
|
|
265
|
+
- [ ] Refactoring goal identified
|
|
266
|
+
|
|
267
|
+
### 2. Refactoring Steps
|
|
268
|
+
**Step 1**: [Refactoring technique]
|
|
269
|
+
- Files affected: [list]
|
|
270
|
+
- Changes: [description]
|
|
271
|
+
- Tests: ✅ Passing
|
|
272
|
+
|
|
273
|
+
**Step 2**: [Next technique]
|
|
274
|
+
- Files affected: [list]
|
|
275
|
+
- Changes: [description]
|
|
276
|
+
- Tests: ✅ Passing
|
|
277
|
+
|
|
278
|
+
### 3. Post-Refactoring
|
|
279
|
+
- [ ] All tests still pass
|
|
280
|
+
- [ ] Code is cleaner/clearer
|
|
281
|
+
- [ ] No functionality changed
|
|
282
|
+
- [ ] Documentation updated
|
|
283
|
+
- [ ] Changes committed
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### Verification Checklist
|
|
287
|
+
|
|
288
|
+
After refactoring, verify:
|
|
289
|
+
|
|
290
|
+
- [ ] **Tests Pass**: All existing tests still pass
|
|
291
|
+
- [ ] **Behavior Unchanged**: Functionality is identical
|
|
292
|
+
- [ ] **Code Quality Improved**: Code is cleaner/clearer
|
|
293
|
+
- [ ] **No New Issues**: No new bugs introduced
|
|
294
|
+
- [ ] **Documentation Updated**: Comments and docs reflect changes
|
|
295
|
+
- [ ] **Performance Maintained**: No performance regression
|
|
296
|
+
|
|
297
|
+
### Red Flags - Stop Refactoring If:
|
|
298
|
+
|
|
299
|
+
- Tests start failing unexpectedly
|
|
300
|
+
- You're changing behavior, not structure
|
|
301
|
+
- You're adding features (that's not refactoring)
|
|
302
|
+
- You've been refactoring for hours without committing
|
|
303
|
+
- You're not sure what the code does
|
|
304
|
+
|
|
305
|
+
### Best Practices
|
|
306
|
+
|
|
307
|
+
1. **Refactor OR Add Features, Never Both**
|
|
308
|
+
- Separate refactoring commits from feature commits
|
|
309
|
+
- Makes code review easier
|
|
310
|
+
- Easier to revert if needed
|
|
311
|
+
|
|
312
|
+
2. **Keep Tests Green**
|
|
313
|
+
- Run tests frequently
|
|
314
|
+
- Fix failing tests immediately
|
|
315
|
+
- Don't continue if tests are red
|
|
316
|
+
|
|
317
|
+
3. **Small, Focused Changes**
|
|
318
|
+
- One refactoring technique at a time
|
|
319
|
+
- Commit after each successful refactoring
|
|
320
|
+
- Easy to review and understand
|
|
321
|
+
|
|
322
|
+
4. **Use IDE Tools**
|
|
323
|
+
- Automated refactoring is safer
|
|
324
|
+
- Less prone to typos
|
|
325
|
+
- Updates all references automatically
|
|
326
|
+
|
|
327
|
+
5. **Code Smells to Watch For**
|
|
328
|
+
- Long functions (>20 lines)
|
|
329
|
+
- Duplicated code
|
|
330
|
+
- Large classes (>300 lines)
|
|
331
|
+
- Long parameter lists (>3 params)
|
|
332
|
+
- Complex conditionals
|
|
333
|
+
- Magic numbers/strings
|
|
334
|
+
|
|
335
|
+
### Common Refactoring Patterns
|
|
336
|
+
|
|
337
|
+
**Pattern 1: Extract and Compose**
|
|
338
|
+
```
|
|
339
|
+
Long function → Extract methods → Compose readable flow
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
**Pattern 2: Consolidate Duplication**
|
|
343
|
+
```
|
|
344
|
+
Duplicated code → Extract common logic → Reuse
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
**Pattern 3: Simplify Complexity**
|
|
348
|
+
```
|
|
349
|
+
Complex logic → Break into steps → Clear intent
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
**Pattern 4: Improve Names**
|
|
353
|
+
```
|
|
354
|
+
Unclear names → Rename → Self-documenting code
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
### Output Format
|
|
358
|
+
|
|
359
|
+
```markdown
|
|
360
|
+
## Refactoring Complete: [Target]
|
|
361
|
+
|
|
362
|
+
### Changes Made
|
|
363
|
+
1. **[Technique]**: [Description]
|
|
364
|
+
- Files: [list]
|
|
365
|
+
- Reason: [why this refactoring]
|
|
366
|
+
|
|
367
|
+
2. **[Technique]**: [Description]
|
|
368
|
+
- Files: [list]
|
|
369
|
+
- Reason: [why this refactoring]
|
|
370
|
+
|
|
371
|
+
### Metrics
|
|
372
|
+
- Lines of code: [before] → [after]
|
|
373
|
+
- Functions: [before] → [after]
|
|
374
|
+
- Complexity: [before] → [after]
|
|
375
|
+
|
|
376
|
+
### Verification
|
|
377
|
+
- ✅ All tests pass
|
|
378
|
+
- ✅ Behavior unchanged
|
|
379
|
+
- ✅ Code quality improved
|
|
380
|
+
- ✅ Documentation updated
|
|
381
|
+
|
|
382
|
+
### Next Steps
|
|
383
|
+
- [Optional follow-up refactorings]
|
|
384
|
+
```
|