Skip to content

Commit 3e62c91

Browse files
chore: add Cursor rules for state initialization and pre-commit refactors
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 4041cbd commit 3e62c91

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
description: Initialize variables and NgRx state with concrete defaults
3+
globs: **/*.{ts,tsx}
4+
alwaysApply: false
5+
---
6+
7+
# Initialize variables and NgRx state
8+
9+
Initialize primitives and objects at declaration whenever a sensible default exists. The goal is typed, predictable values—not defensive `?.` and `??` scattered through selectors, templates, and components.
10+
11+
## General TypeScript
12+
13+
- Give local variables an initial value when one is known at declaration time.
14+
- Prefer empty-but-valid defaults (`''`, `0`, `[]`, `{}`) over `undefined`.
15+
- Mark fields optional (`?`) only when they are genuinely absent until a specific event (e.g. async load, user action).
16+
17+
## NgRx state
18+
19+
- Define a complete `initialState` that sets every non-optional field on the `State` interface.
20+
- Reuse existing factory helpers or constants for defaults when the codebase already provides them.
21+
- Reserve optional properties for values that truly start unknown; do not make a field optional just to skip initializing it.
22+
23+
```typescript
24+
// ✅ GOOD — complete initialState; optional only where value is truly unknown
25+
export interface State {
26+
loading: boolean;
27+
items: Item[];
28+
selectedId?: string;
29+
}
30+
31+
export const initialState: State = {
32+
loading: false,
33+
items: [],
34+
};
35+
36+
// ❌ BAD — optional everywhere forces nullish handling downstream
37+
export interface State {
38+
loading?: boolean;
39+
items?: Item[];
40+
}
41+
42+
export const initialState: State = {};
43+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
description: Refactor iteration leftovers before committing or opening a PR
3+
alwaysApply: true
4+
---
5+
6+
# Refactor before commit or PR
7+
8+
Before committing or opening a pull request, review the diff for refactoring opportunities in the code you touched.
9+
10+
Iterative changes—especially from agent-assisted edits—often leave redundant helpers, duplicated logic, dead code, or overly narrow abstractions. Clean these up when the refactor is clearly worthwhile and stays within the scope of the change.
11+
12+
- Prefer consolidating duplication and removing unused code over adding new layers.
13+
- Do not expand scope into unrelated files or drive-by refactors.
14+
- If a refactor would be large or risky, note it in the PR instead of blocking the commit.

0 commit comments

Comments
 (0)