vibe-and-thrive 1.0.0
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/.claude/commands/add-tests.md +240 -0
- package/.claude/commands/e2e-scaffold.md +212 -0
- package/.claude/commands/explain.md +110 -0
- package/.claude/commands/fix-types.md +238 -0
- package/.claude/commands/refactor.md +184 -0
- package/.claude/commands/review.md +136 -0
- package/.claude/commands/security-check.md +223 -0
- package/.claude/commands/styleguide.md +446 -0
- package/.claude/commands/tdd-feature.md +227 -0
- package/.claude/commands/vibe-check.md +112 -0
- package/.pre-commit-hooks.yaml +77 -0
- package/LICENSE +21 -0
- package/README.md +167 -0
- package/bin/vibe-check.js +19 -0
- package/dist/cli.d.ts +13 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +206 -0
- package/dist/cli.js.map +1 -0
- package/dist/eslint-plugin/index.d.ts +66 -0
- package/dist/eslint-plugin/index.d.ts.map +1 -0
- package/dist/eslint-plugin/index.js +67 -0
- package/dist/eslint-plugin/index.js.map +1 -0
- package/dist/eslint-plugin/rules/max-function-length.d.ts +8 -0
- package/dist/eslint-plugin/rules/max-function-length.d.ts.map +1 -0
- package/dist/eslint-plugin/rules/max-function-length.js +69 -0
- package/dist/eslint-plugin/rules/max-function-length.js.map +1 -0
- package/dist/eslint-plugin/rules/no-any-type.d.ts +8 -0
- package/dist/eslint-plugin/rules/no-any-type.d.ts.map +1 -0
- package/dist/eslint-plugin/rules/no-any-type.js +29 -0
- package/dist/eslint-plugin/rules/no-any-type.js.map +1 -0
- package/dist/eslint-plugin/rules/no-debug-statements.d.ts +8 -0
- package/dist/eslint-plugin/rules/no-debug-statements.d.ts.map +1 -0
- package/dist/eslint-plugin/rules/no-debug-statements.js +59 -0
- package/dist/eslint-plugin/rules/no-debug-statements.js.map +1 -0
- package/dist/eslint-plugin/rules/no-deep-nesting.d.ts +8 -0
- package/dist/eslint-plugin/rules/no-deep-nesting.d.ts.map +1 -0
- package/dist/eslint-plugin/rules/no-deep-nesting.js +56 -0
- package/dist/eslint-plugin/rules/no-deep-nesting.js.map +1 -0
- package/dist/eslint-plugin/rules/no-empty-catch.d.ts +8 -0
- package/dist/eslint-plugin/rules/no-empty-catch.d.ts.map +1 -0
- package/dist/eslint-plugin/rules/no-empty-catch.js +31 -0
- package/dist/eslint-plugin/rules/no-empty-catch.js.map +1 -0
- package/dist/eslint-plugin/rules/no-magic-numbers.d.ts +8 -0
- package/dist/eslint-plugin/rules/no-magic-numbers.d.ts.map +1 -0
- package/dist/eslint-plugin/rules/no-magic-numbers.js +58 -0
- package/dist/eslint-plugin/rules/no-magic-numbers.js.map +1 -0
- package/dist/eslint-plugin/rules/no-snake-case-props.d.ts +8 -0
- package/dist/eslint-plugin/rules/no-snake-case-props.d.ts.map +1 -0
- package/dist/eslint-plugin/rules/no-snake-case-props.js +48 -0
- package/dist/eslint-plugin/rules/no-snake-case-props.js.map +1 -0
- package/dist/hooks/check-any-types.d.ts +6 -0
- package/dist/hooks/check-any-types.d.ts.map +1 -0
- package/dist/hooks/check-any-types.js +73 -0
- package/dist/hooks/check-any-types.js.map +1 -0
- package/dist/hooks/check-commented-code.d.ts +6 -0
- package/dist/hooks/check-commented-code.d.ts.map +1 -0
- package/dist/hooks/check-commented-code.js +81 -0
- package/dist/hooks/check-commented-code.js.map +1 -0
- package/dist/hooks/check-console-error.d.ts +6 -0
- package/dist/hooks/check-console-error.d.ts.map +1 -0
- package/dist/hooks/check-console-error.js +41 -0
- package/dist/hooks/check-console-error.js.map +1 -0
- package/dist/hooks/check-debug-statements.d.ts +6 -0
- package/dist/hooks/check-debug-statements.d.ts.map +1 -0
- package/dist/hooks/check-debug-statements.js +120 -0
- package/dist/hooks/check-debug-statements.js.map +1 -0
- package/dist/hooks/check-deep-nesting.d.ts +6 -0
- package/dist/hooks/check-deep-nesting.d.ts.map +1 -0
- package/dist/hooks/check-deep-nesting.js +116 -0
- package/dist/hooks/check-deep-nesting.js.map +1 -0
- package/dist/hooks/check-docker-platform.d.ts +6 -0
- package/dist/hooks/check-docker-platform.d.ts.map +1 -0
- package/dist/hooks/check-docker-platform.js +42 -0
- package/dist/hooks/check-docker-platform.js.map +1 -0
- package/dist/hooks/check-dry-violations.d.ts +6 -0
- package/dist/hooks/check-dry-violations.d.ts.map +1 -0
- package/dist/hooks/check-dry-violations.js +124 -0
- package/dist/hooks/check-dry-violations.js.map +1 -0
- package/dist/hooks/check-empty-catch.d.ts +6 -0
- package/dist/hooks/check-empty-catch.d.ts.map +1 -0
- package/dist/hooks/check-empty-catch.js +111 -0
- package/dist/hooks/check-empty-catch.js.map +1 -0
- package/dist/hooks/check-function-length.d.ts +6 -0
- package/dist/hooks/check-function-length.d.ts.map +1 -0
- package/dist/hooks/check-function-length.js +152 -0
- package/dist/hooks/check-function-length.js.map +1 -0
- package/dist/hooks/check-hardcoded-urls.d.ts +6 -0
- package/dist/hooks/check-hardcoded-urls.d.ts.map +1 -0
- package/dist/hooks/check-hardcoded-urls.js +124 -0
- package/dist/hooks/check-hardcoded-urls.js.map +1 -0
- package/dist/hooks/check-magic-numbers.d.ts +6 -0
- package/dist/hooks/check-magic-numbers.d.ts.map +1 -0
- package/dist/hooks/check-magic-numbers.js +116 -0
- package/dist/hooks/check-magic-numbers.js.map +1 -0
- package/dist/hooks/check-secrets.d.ts +6 -0
- package/dist/hooks/check-secrets.d.ts.map +1 -0
- package/dist/hooks/check-secrets.js +138 -0
- package/dist/hooks/check-secrets.js.map +1 -0
- package/dist/hooks/check-snake-case-ts.d.ts +6 -0
- package/dist/hooks/check-snake-case-ts.d.ts.map +1 -0
- package/dist/hooks/check-snake-case-ts.js +78 -0
- package/dist/hooks/check-snake-case-ts.js.map +1 -0
- package/dist/hooks/check-todo-fixme.d.ts +6 -0
- package/dist/hooks/check-todo-fixme.d.ts.map +1 -0
- package/dist/hooks/check-todo-fixme.js +41 -0
- package/dist/hooks/check-todo-fixme.js.map +1 -0
- package/dist/hooks/check-unsafe-html.d.ts +6 -0
- package/dist/hooks/check-unsafe-html.d.ts.map +1 -0
- package/dist/hooks/check-unsafe-html.js +101 -0
- package/dist/hooks/check-unsafe-html.js.map +1 -0
- package/dist/hooks/index.d.ts +29 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +54 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/lint-staged/config.d.ts +20 -0
- package/dist/lint-staged/config.d.ts.map +1 -0
- package/dist/lint-staged/config.js +27 -0
- package/dist/lint-staged/config.js.map +1 -0
- package/dist/utils/file-reader.d.ts +24 -0
- package/dist/utils/file-reader.d.ts.map +1 -0
- package/dist/utils/file-reader.js +140 -0
- package/dist/utils/file-reader.js.map +1 -0
- package/dist/utils/patterns.d.ts +27 -0
- package/dist/utils/patterns.d.ts.map +1 -0
- package/dist/utils/patterns.js +84 -0
- package/dist/utils/patterns.js.map +1 -0
- package/dist/utils/reporters.d.ts +21 -0
- package/dist/utils/reporters.d.ts.map +1 -0
- package/dist/utils/reporters.js +115 -0
- package/dist/utils/reporters.js.map +1 -0
- package/dist/utils/types.d.ts +71 -0
- package/dist/utils/types.d.ts.map +1 -0
- package/dist/utils/types.js +5 -0
- package/dist/utils/types.js.map +1 -0
- package/integrations/cursorrules.template +147 -0
- package/integrations/eslint.config.js +34 -0
- package/integrations/lint-staged.config.js +34 -0
- package/integrations/ruff.toml +125 -0
- package/integrations/vibe-check.yml +116 -0
- package/integrations/vscode-settings.json +127 -0
- package/package.json +81 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
# Fix Types
|
|
2
|
+
|
|
3
|
+
Fix TypeScript type errors without using `any`. Create proper interfaces and types.
|
|
4
|
+
|
|
5
|
+
## Instructions
|
|
6
|
+
|
|
7
|
+
When asked to fix TypeScript types:
|
|
8
|
+
|
|
9
|
+
### Step 1: Understand the Error
|
|
10
|
+
|
|
11
|
+
Read the TypeScript error message carefully:
|
|
12
|
+
- What type is expected?
|
|
13
|
+
- What type is being provided?
|
|
14
|
+
- Where is the mismatch?
|
|
15
|
+
|
|
16
|
+
### Step 2: Investigate the Data
|
|
17
|
+
|
|
18
|
+
Before creating types:
|
|
19
|
+
1. Check if types already exist elsewhere in the codebase
|
|
20
|
+
2. Look at the API response or data source
|
|
21
|
+
3. Understand the shape of the data
|
|
22
|
+
|
|
23
|
+
### Step 3: Create Proper Types
|
|
24
|
+
|
|
25
|
+
Instead of using `any`, create specific interfaces:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
// DON'T do this
|
|
29
|
+
const data: any = await fetchUser();
|
|
30
|
+
|
|
31
|
+
// DO this
|
|
32
|
+
interface User {
|
|
33
|
+
id: number;
|
|
34
|
+
email: string;
|
|
35
|
+
name: string;
|
|
36
|
+
createdAt: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const data: User = await fetchUser();
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Step 4: Handle Uncertainty
|
|
43
|
+
|
|
44
|
+
When you don't know the exact type:
|
|
45
|
+
|
|
46
|
+
**Option 1: Use `unknown` and narrow**
|
|
47
|
+
```typescript
|
|
48
|
+
function processData(data: unknown) {
|
|
49
|
+
if (isUser(data)) {
|
|
50
|
+
// TypeScript now knows data is User
|
|
51
|
+
console.log(data.email);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isUser(data: unknown): data is User {
|
|
56
|
+
return (
|
|
57
|
+
typeof data === 'object' &&
|
|
58
|
+
data !== null &&
|
|
59
|
+
'email' in data &&
|
|
60
|
+
'id' in data
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
**Option 2: Use generics**
|
|
66
|
+
```typescript
|
|
67
|
+
async function fetchData<T>(url: string): Promise<T> {
|
|
68
|
+
const response = await fetch(url);
|
|
69
|
+
return response.json();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const user = await fetchData<User>('/api/user');
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
**Option 3: Use partial types for incomplete data**
|
|
76
|
+
```typescript
|
|
77
|
+
interface UserInput {
|
|
78
|
+
email: string;
|
|
79
|
+
name?: string; // Optional during creation
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
interface User extends UserInput {
|
|
83
|
+
id: number;
|
|
84
|
+
createdAt: string;
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Common Type Fixes
|
|
89
|
+
|
|
90
|
+
### API Response Types
|
|
91
|
+
```typescript
|
|
92
|
+
// Instead of: const response: any = await api.get('/users')
|
|
93
|
+
|
|
94
|
+
interface ApiResponse<T> {
|
|
95
|
+
data: T;
|
|
96
|
+
status: number;
|
|
97
|
+
message?: string;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
interface User {
|
|
101
|
+
id: number;
|
|
102
|
+
email: string;
|
|
103
|
+
name: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const response: ApiResponse<User[]> = await api.get('/users');
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Event Handler Types
|
|
110
|
+
```typescript
|
|
111
|
+
// Instead of: const handleChange = (e: any) => {}
|
|
112
|
+
|
|
113
|
+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
114
|
+
setValue(e.target.value);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
|
118
|
+
e.preventDefault();
|
|
119
|
+
};
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Object with Dynamic Keys
|
|
123
|
+
```typescript
|
|
124
|
+
// Instead of: const cache: any = {}
|
|
125
|
+
|
|
126
|
+
const cache: Record<string, User> = {};
|
|
127
|
+
// or
|
|
128
|
+
const cache: { [key: string]: User } = {};
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Union Types for Multiple Possibilities
|
|
132
|
+
```typescript
|
|
133
|
+
// Instead of: function process(input: any)
|
|
134
|
+
|
|
135
|
+
type ProcessInput = string | number | Buffer;
|
|
136
|
+
|
|
137
|
+
function process(input: ProcessInput) {
|
|
138
|
+
if (typeof input === 'string') {
|
|
139
|
+
// TypeScript knows input is string here
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Third-Party Library Types
|
|
145
|
+
```typescript
|
|
146
|
+
// If library lacks types, create a declaration file
|
|
147
|
+
|
|
148
|
+
// types/some-library.d.ts
|
|
149
|
+
declare module 'some-library' {
|
|
150
|
+
export function doThing(input: string): Promise<Result>;
|
|
151
|
+
|
|
152
|
+
export interface Result {
|
|
153
|
+
success: boolean;
|
|
154
|
+
data: unknown;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Output Format
|
|
160
|
+
|
|
161
|
+
```markdown
|
|
162
|
+
## Type Fix: [filename]
|
|
163
|
+
|
|
164
|
+
### The Error
|
|
165
|
+
```
|
|
166
|
+
[Original TypeScript error message]
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Analysis
|
|
170
|
+
[What's causing the error and why]
|
|
171
|
+
|
|
172
|
+
### Solution
|
|
173
|
+
|
|
174
|
+
**Step 1: Create interfaces**
|
|
175
|
+
```typescript
|
|
176
|
+
[New interface definitions]
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
**Step 2: Apply types**
|
|
180
|
+
|
|
181
|
+
Before:
|
|
182
|
+
```typescript
|
|
183
|
+
[Original code with any/type errors]
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
After:
|
|
187
|
+
```typescript
|
|
188
|
+
[Fixed code with proper types]
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Why This Is Better
|
|
192
|
+
- [Benefit 1: e.g., "IDE autocomplete now works"]
|
|
193
|
+
- [Benefit 2: e.g., "Catches typos at compile time"]
|
|
194
|
+
- [Benefit 3: e.g., "Documents the data shape"]
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Never Use These (Without Good Reason)
|
|
198
|
+
|
|
199
|
+
| Avoid | Use Instead |
|
|
200
|
+
|-------|-------------|
|
|
201
|
+
| `any` | Specific type, `unknown`, or generic |
|
|
202
|
+
| `@ts-ignore` | Fix the actual type error |
|
|
203
|
+
| `as any` | Proper type assertion or type guard |
|
|
204
|
+
| `// @ts-nocheck` | Fix file's type errors |
|
|
205
|
+
|
|
206
|
+
## When `any` Might Be Acceptable
|
|
207
|
+
|
|
208
|
+
Rare cases where `any` is okay (document why!):
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
// Working with truly dynamic data that can't be typed
|
|
212
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
213
|
+
function logAnything(data: any): void {
|
|
214
|
+
console.log(JSON.stringify(data));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Interfacing with untyped third-party library
|
|
218
|
+
// TODO: Create proper types when time permits
|
|
219
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
220
|
+
const result: any = legacyLibrary.doThing();
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Type Inference Tips
|
|
224
|
+
|
|
225
|
+
Let TypeScript infer when it can:
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
// Unnecessary explicit type
|
|
229
|
+
const name: string = 'Alice'; // TypeScript already knows this is string
|
|
230
|
+
|
|
231
|
+
// Let it infer
|
|
232
|
+
const name = 'Alice';
|
|
233
|
+
|
|
234
|
+
// But DO be explicit for function return types
|
|
235
|
+
function getUser(id: number): User | null {
|
|
236
|
+
// ...
|
|
237
|
+
}
|
|
238
|
+
```
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# Refactor Code
|
|
2
|
+
|
|
3
|
+
Guided refactoring with explanations of why each change improves the code.
|
|
4
|
+
|
|
5
|
+
## Instructions
|
|
6
|
+
|
|
7
|
+
When asked to refactor code:
|
|
8
|
+
|
|
9
|
+
### Step 1: Understand Current State
|
|
10
|
+
|
|
11
|
+
Before changing anything:
|
|
12
|
+
1. Read and understand the existing code
|
|
13
|
+
2. Identify what it's supposed to do
|
|
14
|
+
3. Check for existing tests
|
|
15
|
+
4. Note the current patterns in use
|
|
16
|
+
|
|
17
|
+
### Step 2: Identify Refactoring Opportunities
|
|
18
|
+
|
|
19
|
+
Look for:
|
|
20
|
+
|
|
21
|
+
**Structural Issues**
|
|
22
|
+
- Functions doing too many things
|
|
23
|
+
- Deep nesting
|
|
24
|
+
- Long parameter lists
|
|
25
|
+
- Feature envy (method uses another class's data more than its own)
|
|
26
|
+
|
|
27
|
+
**Duplication**
|
|
28
|
+
- Copy-pasted code
|
|
29
|
+
- Similar functions that could be generalized
|
|
30
|
+
- Repeated patterns
|
|
31
|
+
|
|
32
|
+
**Naming**
|
|
33
|
+
- Unclear variable/function names
|
|
34
|
+
- Inconsistent naming conventions
|
|
35
|
+
- Names that don't match behavior
|
|
36
|
+
|
|
37
|
+
**Complexity**
|
|
38
|
+
- Overly clever code
|
|
39
|
+
- Unnecessary abstractions
|
|
40
|
+
- Missing abstractions
|
|
41
|
+
|
|
42
|
+
### Step 3: Plan the Refactoring
|
|
43
|
+
|
|
44
|
+
Before making changes, explain:
|
|
45
|
+
1. What you're going to change
|
|
46
|
+
2. Why each change improves the code
|
|
47
|
+
3. What risks to watch for
|
|
48
|
+
|
|
49
|
+
### Step 4: Make Changes Incrementally
|
|
50
|
+
|
|
51
|
+
For each change:
|
|
52
|
+
1. Show the before state
|
|
53
|
+
2. Show the after state
|
|
54
|
+
3. Explain the improvement
|
|
55
|
+
4. Verify behavior is preserved
|
|
56
|
+
|
|
57
|
+
### Step 5: Verify
|
|
58
|
+
|
|
59
|
+
After refactoring:
|
|
60
|
+
1. Run existing tests (if any)
|
|
61
|
+
2. Manually verify the code still works
|
|
62
|
+
3. Check that the refactoring achieved its goals
|
|
63
|
+
|
|
64
|
+
## Output Format
|
|
65
|
+
|
|
66
|
+
```markdown
|
|
67
|
+
## Refactoring: [filename or description]
|
|
68
|
+
|
|
69
|
+
### Current State
|
|
70
|
+
[Brief description of current code and its issues]
|
|
71
|
+
|
|
72
|
+
### Refactoring Plan
|
|
73
|
+
1. [Change 1] - [Why]
|
|
74
|
+
2. [Change 2] - [Why]
|
|
75
|
+
3. [Change 3] - [Why]
|
|
76
|
+
|
|
77
|
+
### Change 1: [Description]
|
|
78
|
+
|
|
79
|
+
**Before:**
|
|
80
|
+
```[language]
|
|
81
|
+
[original code]
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**After:**
|
|
85
|
+
```[language]
|
|
86
|
+
[refactored code]
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**Why this is better:**
|
|
90
|
+
- [Benefit 1]
|
|
91
|
+
- [Benefit 2]
|
|
92
|
+
|
|
93
|
+
### Change 2: [Description]
|
|
94
|
+
[... repeat pattern ...]
|
|
95
|
+
|
|
96
|
+
### Final Result
|
|
97
|
+
|
|
98
|
+
**Before (complete):**
|
|
99
|
+
```[language]
|
|
100
|
+
[all original code]
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
**After (complete):**
|
|
104
|
+
```[language]
|
|
105
|
+
[all refactored code]
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Summary of Improvements
|
|
109
|
+
- [Improvement 1]
|
|
110
|
+
- [Improvement 2]
|
|
111
|
+
- [Improvement 3]
|
|
112
|
+
|
|
113
|
+
### Verification
|
|
114
|
+
- [ ] Existing tests pass
|
|
115
|
+
- [ ] Behavior unchanged
|
|
116
|
+
- [ ] Code is cleaner/simpler
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Common Refactorings
|
|
120
|
+
|
|
121
|
+
### Extract Function
|
|
122
|
+
**When:** Code does multiple things or is deeply nested
|
|
123
|
+
```python
|
|
124
|
+
# Before
|
|
125
|
+
def process_order(order):
|
|
126
|
+
# validate
|
|
127
|
+
if not order.items:
|
|
128
|
+
raise Error("No items")
|
|
129
|
+
if not order.customer:
|
|
130
|
+
raise Error("No customer")
|
|
131
|
+
# calculate
|
|
132
|
+
total = sum(item.price for item in order.items)
|
|
133
|
+
# save
|
|
134
|
+
db.save(order)
|
|
135
|
+
|
|
136
|
+
# After
|
|
137
|
+
def process_order(order):
|
|
138
|
+
validate_order(order)
|
|
139
|
+
order.total = calculate_total(order)
|
|
140
|
+
save_order(order)
|
|
141
|
+
|
|
142
|
+
def validate_order(order):
|
|
143
|
+
if not order.items:
|
|
144
|
+
raise Error("No items")
|
|
145
|
+
if not order.customer:
|
|
146
|
+
raise Error("No customer")
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Replace Conditionals with Polymorphism
|
|
150
|
+
**When:** Multiple if/else checking type
|
|
151
|
+
|
|
152
|
+
### Introduce Parameter Object
|
|
153
|
+
**When:** Function has many related parameters
|
|
154
|
+
|
|
155
|
+
### Replace Magic Numbers with Constants
|
|
156
|
+
**When:** Hardcoded numbers with unclear meaning
|
|
157
|
+
|
|
158
|
+
### Simplify Conditionals
|
|
159
|
+
**When:** Complex boolean expressions
|
|
160
|
+
|
|
161
|
+
### Remove Dead Code
|
|
162
|
+
**When:** Code that's never executed
|
|
163
|
+
|
|
164
|
+
## Refactoring Modes
|
|
165
|
+
|
|
166
|
+
### Safe Refactoring
|
|
167
|
+
Only make changes that clearly preserve behavior:
|
|
168
|
+
> "Safely refactor this without changing behavior"
|
|
169
|
+
|
|
170
|
+
### Aggressive Refactoring
|
|
171
|
+
Restructure more significantly:
|
|
172
|
+
> "Refactor this to follow clean code principles"
|
|
173
|
+
|
|
174
|
+
### Targeted Refactoring
|
|
175
|
+
Fix a specific issue:
|
|
176
|
+
> "Refactor to reduce the nesting in this function"
|
|
177
|
+
|
|
178
|
+
## Important Rules
|
|
179
|
+
|
|
180
|
+
1. **Never change behavior** while refactoring
|
|
181
|
+
2. **Make small changes** - one refactoring at a time
|
|
182
|
+
3. **Test after each change** if possible
|
|
183
|
+
4. **Explain every change** - this is a teaching tool
|
|
184
|
+
5. **Keep it simple** - don't over-engineer
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# Code Review
|
|
2
|
+
|
|
3
|
+
Review code for issues, improvements, and best practices.
|
|
4
|
+
|
|
5
|
+
## Instructions
|
|
6
|
+
|
|
7
|
+
When asked to review code, perform a thorough analysis looking for:
|
|
8
|
+
|
|
9
|
+
### 1. Security Issues (Critical)
|
|
10
|
+
|
|
11
|
+
- [ ] SQL injection vulnerabilities
|
|
12
|
+
- [ ] XSS (cross-site scripting) risks
|
|
13
|
+
- [ ] Hardcoded secrets or credentials
|
|
14
|
+
- [ ] Unsafe data handling
|
|
15
|
+
- [ ] Missing authentication/authorization checks
|
|
16
|
+
- [ ] Insecure dependencies
|
|
17
|
+
|
|
18
|
+
### 2. Error Handling (High)
|
|
19
|
+
|
|
20
|
+
- [ ] Empty catch blocks
|
|
21
|
+
- [ ] Missing error boundaries
|
|
22
|
+
- [ ] Unhandled promise rejections
|
|
23
|
+
- [ ] Missing null/undefined checks
|
|
24
|
+
- [ ] Silent failures
|
|
25
|
+
|
|
26
|
+
### 3. Type Safety (High for TypeScript)
|
|
27
|
+
|
|
28
|
+
- [ ] Usage of `any` type
|
|
29
|
+
- [ ] Missing type annotations
|
|
30
|
+
- [ ] Type assertions that could fail
|
|
31
|
+
- [ ] Inconsistent types
|
|
32
|
+
|
|
33
|
+
### 4. Code Quality (Medium)
|
|
34
|
+
|
|
35
|
+
- [ ] Functions over 50 lines
|
|
36
|
+
- [ ] Deep nesting (4+ levels)
|
|
37
|
+
- [ ] Code duplication
|
|
38
|
+
- [ ] Magic numbers
|
|
39
|
+
- [ ] Unclear variable names
|
|
40
|
+
- [ ] Missing or outdated comments
|
|
41
|
+
|
|
42
|
+
### 5. Performance (Medium)
|
|
43
|
+
|
|
44
|
+
- [ ] N+1 query patterns
|
|
45
|
+
- [ ] Missing memoization for expensive operations
|
|
46
|
+
- [ ] Unnecessary re-renders (React)
|
|
47
|
+
- [ ] Large bundle imports
|
|
48
|
+
- [ ] Missing pagination
|
|
49
|
+
|
|
50
|
+
### 6. Maintainability (Low)
|
|
51
|
+
|
|
52
|
+
- [ ] Dead code
|
|
53
|
+
- [ ] Commented-out code
|
|
54
|
+
- [ ] TODOs that should be addressed
|
|
55
|
+
- [ ] Inconsistent patterns
|
|
56
|
+
- [ ] Missing tests
|
|
57
|
+
|
|
58
|
+
## Output Format
|
|
59
|
+
|
|
60
|
+
Structure your review like this:
|
|
61
|
+
|
|
62
|
+
```markdown
|
|
63
|
+
## Code Review: [filename or description]
|
|
64
|
+
|
|
65
|
+
### Summary
|
|
66
|
+
[1-2 sentence overview of the code quality]
|
|
67
|
+
|
|
68
|
+
### Critical Issues
|
|
69
|
+
These must be fixed before merging:
|
|
70
|
+
|
|
71
|
+
1. **[Issue Title]** (Line X)
|
|
72
|
+
- Problem: [What's wrong]
|
|
73
|
+
- Risk: [What could happen]
|
|
74
|
+
- Fix: [How to fix it]
|
|
75
|
+
```
|
|
76
|
+
[Code suggestion if applicable]
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Improvements
|
|
80
|
+
These should be addressed:
|
|
81
|
+
|
|
82
|
+
1. **[Issue Title]** (Line X)
|
|
83
|
+
- Current: [What it does now]
|
|
84
|
+
- Better: [What it should do]
|
|
85
|
+
- Why: [Benefit of changing]
|
|
86
|
+
|
|
87
|
+
### Minor Suggestions
|
|
88
|
+
Nice to have, low priority:
|
|
89
|
+
|
|
90
|
+
1. **[Suggestion]** (Line X)
|
|
91
|
+
- [Brief explanation]
|
|
92
|
+
|
|
93
|
+
### What's Good
|
|
94
|
+
[Acknowledge good patterns and practices in the code]
|
|
95
|
+
|
|
96
|
+
### Verdict
|
|
97
|
+
[ ] Ready to merge
|
|
98
|
+
[ ] Needs minor changes
|
|
99
|
+
[ ] Needs significant changes
|
|
100
|
+
[ ] Needs rewrite
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Severity Guide
|
|
104
|
+
|
|
105
|
+
| Severity | Block Merge? | Examples |
|
|
106
|
+
|----------|--------------|----------|
|
|
107
|
+
| Critical | Yes | Security vulnerabilities, data loss risks |
|
|
108
|
+
| High | Yes | Missing error handling, type safety issues |
|
|
109
|
+
| Medium | Review | Performance issues, code quality |
|
|
110
|
+
| Low | No | Style preferences, minor improvements |
|
|
111
|
+
|
|
112
|
+
## Review Modes
|
|
113
|
+
|
|
114
|
+
### Quick Review
|
|
115
|
+
Focus only on critical and high-severity issues:
|
|
116
|
+
> "Quick review of this code"
|
|
117
|
+
|
|
118
|
+
### Full Review
|
|
119
|
+
Check everything:
|
|
120
|
+
> "Full review of src/api/users.ts"
|
|
121
|
+
|
|
122
|
+
### Security Review
|
|
123
|
+
Focus on security concerns:
|
|
124
|
+
> "Security review of the authentication flow"
|
|
125
|
+
|
|
126
|
+
### Performance Review
|
|
127
|
+
Focus on performance:
|
|
128
|
+
> "Performance review of the dashboard page"
|
|
129
|
+
|
|
130
|
+
## Be Constructive
|
|
131
|
+
|
|
132
|
+
- Explain **why** something is an issue, not just that it is
|
|
133
|
+
- Provide **specific** suggestions for fixes
|
|
134
|
+
- Acknowledge what's **done well**
|
|
135
|
+
- Be **respectful** - we all make mistakes
|
|
136
|
+
- Focus on the **code**, not the coder
|