@rmrdeveloper/sideroom-pi 8.1.0 → 8.2.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/README.md +39 -9
- package/extensions/ask/index.ts +12 -2
- package/extensions/ask/model.ts +116 -12
- package/extensions/ask/selection.ts +47 -0
- package/extensions/ask/ui.ts +141 -34
- package/extensions/done/detect.ts +103 -4
- package/extensions/guidelines/catalog.ts +1 -0
- package/extensions/rules/checks.ts +121 -19
- package/extensions/rules/lexer.ts +136 -0
- package/extensions/rules/model.ts +166 -15
- package/extensions/todo/execute.ts +2 -2
- package/extensions/todo/index.ts +6 -2
- package/extensions/todo/model.ts +40 -10
- package/package.json +27 -6
- package/skills/sideroom-grill/SKILL.md +5 -4
- package/skills/sideroom-guidelines/SKILL.md +3 -1
- package/skills/sideroom-guidelines/references/languages/javascript.md +384 -0
package/extensions/todo/index.ts
CHANGED
|
@@ -2,7 +2,11 @@ import type { ExtensionAPI } from '@earendil-works/pi-coding-agent';
|
|
|
2
2
|
import { Text } from '@earendil-works/pi-tui';
|
|
3
3
|
import { executeTodo, type TodoToolDetails } from './execute.ts';
|
|
4
4
|
import { registerGuardEvents } from './guards.ts';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
prepareTodoArguments,
|
|
7
|
+
TOOL_NAME,
|
|
8
|
+
TodoToolParametersSchema,
|
|
9
|
+
} from './model.ts';
|
|
6
10
|
import {
|
|
7
11
|
type BoardLifecycle,
|
|
8
12
|
commitBoard,
|
|
@@ -59,7 +63,7 @@ function registerTodoTool(
|
|
|
59
63
|
description: TODO_DESCRIPTION,
|
|
60
64
|
promptSnippet: TODO_PROMPT_SNIPPET,
|
|
61
65
|
promptGuidelines: TODO_PROMPT_GUIDELINES,
|
|
62
|
-
parameters:
|
|
66
|
+
parameters: TodoToolParametersSchema,
|
|
63
67
|
prepareArguments: prepareTodoArguments,
|
|
64
68
|
executionMode: 'sequential',
|
|
65
69
|
execute: async (_toolCallId, params, _signal, _onUpdate, ctx) => {
|
package/extensions/todo/model.ts
CHANGED
|
@@ -27,6 +27,11 @@ const TodoStatusSchema = Type.Union([
|
|
|
27
27
|
Type.Literal(TODO_STATUS.cancelled),
|
|
28
28
|
]);
|
|
29
29
|
|
|
30
|
+
const TodoActionSchema = Type.Union([
|
|
31
|
+
Type.Literal(TODO_ACTION.propose),
|
|
32
|
+
Type.Literal(TODO_ACTION.update),
|
|
33
|
+
]);
|
|
34
|
+
|
|
30
35
|
const TodoItemSchema = Type.Object({
|
|
31
36
|
id: Type.String({ description: 'Stable identifier for this item' }),
|
|
32
37
|
content: Type.String({ description: 'Short description of the work' }),
|
|
@@ -41,28 +46,53 @@ const TodoPatchSchema = Type.Object({
|
|
|
41
46
|
),
|
|
42
47
|
});
|
|
43
48
|
|
|
49
|
+
const TodoItemsField = Type.Array(TodoItemSchema, {
|
|
50
|
+
maxItems: MAX_TODO_ITEMS,
|
|
51
|
+
description: 'Replacement board for propose, with at most 20 items',
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const TodoPatchesField = Type.Array(TodoPatchSchema, {
|
|
55
|
+
minItems: 1,
|
|
56
|
+
description: 'Patches to apply for update',
|
|
57
|
+
});
|
|
58
|
+
|
|
44
59
|
const ProposeParamsSchema = Type.Object({
|
|
45
60
|
action: Type.Literal(TODO_ACTION.propose),
|
|
46
|
-
items:
|
|
47
|
-
maxItems: MAX_TODO_ITEMS,
|
|
48
|
-
description: 'Replacement board, with at most 20 items',
|
|
49
|
-
}),
|
|
61
|
+
items: TodoItemsField,
|
|
50
62
|
});
|
|
51
63
|
|
|
52
64
|
const UpdateParamsSchema = Type.Object({
|
|
53
65
|
action: Type.Literal(TODO_ACTION.update),
|
|
54
|
-
patches:
|
|
55
|
-
minItems: 1,
|
|
56
|
-
description: 'Patches to apply together',
|
|
57
|
-
}),
|
|
66
|
+
patches: TodoPatchesField,
|
|
58
67
|
});
|
|
59
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Strict validator and types for sideroom_todo arguments. Kept as a
|
|
71
|
+
* discriminated union so invalid action/field combinations are rejected
|
|
72
|
+
* exactly. This schema is not registered with Pi; see
|
|
73
|
+
* TodoToolParametersSchema.
|
|
74
|
+
*/
|
|
60
75
|
export const TodoParamsSchema = Type.Union([
|
|
61
76
|
ProposeParamsSchema,
|
|
62
77
|
UpdateParamsSchema,
|
|
63
78
|
]);
|
|
64
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Tool-facing schema registered with Pi. It must stay a top-level object:
|
|
82
|
+
* Claude Code silently drops every tool of an MCP server when one tool's
|
|
83
|
+
* inputSchema has a non-object top level (anyOf/oneOf/allOf), which is exactly
|
|
84
|
+
* what TodoParamsSchema emits. The discriminated union is still enforced at
|
|
85
|
+
* runtime by parseTodoParams. See docs/extensions/todo.md ("Tool schema
|
|
86
|
+
* compatibility") for the bug references and the condition to revert.
|
|
87
|
+
*/
|
|
88
|
+
export const TodoToolParametersSchema = Type.Object({
|
|
89
|
+
action: TodoActionSchema,
|
|
90
|
+
items: Type.Optional(TodoItemsField),
|
|
91
|
+
patches: Type.Optional(TodoPatchesField),
|
|
92
|
+
});
|
|
93
|
+
|
|
65
94
|
export type TodoParams = Static<typeof TodoParamsSchema>;
|
|
95
|
+
export type TodoToolParameters = Static<typeof TodoToolParametersSchema>;
|
|
66
96
|
export type TodoAction = (typeof TODO_ACTION)[keyof typeof TODO_ACTION];
|
|
67
97
|
export type TodoStatus = (typeof TODO_STATUS)[keyof typeof TODO_STATUS];
|
|
68
98
|
|
|
@@ -127,8 +157,8 @@ type JsonWireValue =
|
|
|
127
157
|
| readonly JsonWireValue[]
|
|
128
158
|
| JsonWireObject;
|
|
129
159
|
|
|
130
|
-
export function prepareTodoArguments(args: unknown):
|
|
131
|
-
return decodeTodoWireArgs(args) as
|
|
160
|
+
export function prepareTodoArguments(args: unknown): TodoToolParameters {
|
|
161
|
+
return decodeTodoWireArgs(args) as TodoToolParameters;
|
|
132
162
|
}
|
|
133
163
|
|
|
134
164
|
function decodeTodoWireArgs(args: unknown): JsonWireValue {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rmrdeveloper/sideroom-pi",
|
|
3
|
-
"version": "8.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "8.2.0",
|
|
4
|
+
"description": "Pi coding-agent package that asks clarifying questions, shows a live todo board, enforces coding guidelines, and will not call it done while your checks are red.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"extensions",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"lint:fix": "biome lint --write extensions scripts commitlint.config.mjs",
|
|
22
22
|
"format": "biome format --write extensions scripts package.json biome.json commitlint.config.mjs .changeset/config.json",
|
|
23
23
|
"format:check": "biome format extensions scripts package.json biome.json commitlint.config.mjs .changeset/config.json",
|
|
24
|
+
"preview": "node scripts/render-preview.mjs",
|
|
24
25
|
"check:if-blocks": "node scripts/check-if-blocks.mjs",
|
|
25
26
|
"check": "npm run types && npm run check:if-blocks && biome check extensions scripts package.json biome.json commitlint.config.mjs .changeset/config.json && npm test"
|
|
26
27
|
},
|
|
@@ -31,13 +32,31 @@
|
|
|
31
32
|
"pi",
|
|
32
33
|
"pi-package",
|
|
33
34
|
"pi-extension",
|
|
35
|
+
"pi-skill",
|
|
36
|
+
"coding-agent",
|
|
37
|
+
"questions",
|
|
38
|
+
"clarifying-questions",
|
|
34
39
|
"questionnaire",
|
|
40
|
+
"planning",
|
|
41
|
+
"spec",
|
|
35
42
|
"todo",
|
|
43
|
+
"task-board",
|
|
44
|
+
"progress",
|
|
36
45
|
"edited-files",
|
|
37
|
-
"sideroom_ask",
|
|
38
|
-
"sideroom_todo",
|
|
39
46
|
"guidelines",
|
|
40
|
-
"
|
|
47
|
+
"code-style",
|
|
48
|
+
"code-quality",
|
|
49
|
+
"guardrails",
|
|
50
|
+
"rules",
|
|
51
|
+
"linting",
|
|
52
|
+
"testing",
|
|
53
|
+
"done",
|
|
54
|
+
"skills",
|
|
55
|
+
"typescript",
|
|
56
|
+
"tui",
|
|
57
|
+
"claude",
|
|
58
|
+
"sideroom_ask",
|
|
59
|
+
"sideroom_todo"
|
|
41
60
|
],
|
|
42
61
|
"license": "MIT",
|
|
43
62
|
"repository": {
|
|
@@ -53,7 +72,9 @@
|
|
|
53
72
|
],
|
|
54
73
|
"skills": [
|
|
55
74
|
"./skills"
|
|
56
|
-
]
|
|
75
|
+
],
|
|
76
|
+
"image": "https://raw.githubusercontent.com/RMRdeveloper/sideroom-pi/main/media/preview.png",
|
|
77
|
+
"video": "https://raw.githubusercontent.com/RMRdeveloper/sideroom-pi/main/media/preview.mp4"
|
|
57
78
|
},
|
|
58
79
|
"peerDependencies": {
|
|
59
80
|
"@earendil-works/pi-coding-agent": "*",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: sideroom-grill
|
|
3
|
-
description: "
|
|
3
|
+
description: "Default interview route before implementation: grill the user in sideroom_ask rounds to settle a non-trivial plan, feature, or decision, and resolve domain-language conflicts along the way. Use whenever a change is not trivial and is not yet a settled spec, or when the user asks to be grilled, wants clarifying questions, or says the plan or terms are not settled. Prefer this skill over any other grilling, interview, or spec skill when more than one is available. Do not use when the user already brought a closed spec or tickets, for a code-first repository scan, or for a simple glossary lookup."
|
|
4
4
|
license: MIT
|
|
5
5
|
metadata:
|
|
6
6
|
author: RMRdeveloper
|
|
@@ -19,9 +19,10 @@ if the effort is too big to hold in one session, say so and stop.
|
|
|
19
19
|
|
|
20
20
|
- Interview in rounds. One `sideroom_ask` call is one round; wait for its
|
|
21
21
|
answers before the next round.
|
|
22
|
-
- Every question carries a recommendation
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
- Every question carries a recommendation: `recommendationIndex` for a
|
|
23
|
+
single-selection question, or `recommendedIndices` (at least one) when the
|
|
24
|
+
question declares `selectionMode: "multiple"`. Write prompts and option copy
|
|
25
|
+
in the language the user is speaking; keep ids and option values in English.
|
|
25
26
|
- Never ask the user what the codebase can answer. Read the code first; ask
|
|
26
27
|
only what the code cannot settle.
|
|
27
28
|
- An *Out of scope* answer closes that thread. Do not re-ask it.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: sideroom-guidelines
|
|
3
|
-
description: "Apply Sideroom's mandatory coding workflow before implementing, refactoring, or fixing Java, Laravel/PHP, TypeScript/TSX, Python, Go, or Rust code. Load this skill and the exact language guide before mutation. Do not use for unrelated prose or domain-only discussions."
|
|
3
|
+
description: "Apply Sideroom's mandatory coding workflow before implementing, refactoring, or fixing Java, Laravel/PHP, JavaScript/JSX, TypeScript/TSX, Python, Go, or Rust code. Load this skill and the exact language guide before mutation. Do not use for unrelated prose or domain-only discussions."
|
|
4
4
|
license: MIT
|
|
5
5
|
metadata:
|
|
6
6
|
author: RMRdeveloper
|
|
@@ -26,6 +26,7 @@ Load this whole skill with `read`, without `offset` or `limit`, before the first
|
|
|
26
26
|
| Path | Read |
|
|
27
27
|
| ------ | ------ |
|
|
28
28
|
| `.java` | `references/languages/java.md` |
|
|
29
|
+
| `.js` `.jsx` `.mjs` `.cjs` | `references/languages/javascript.md` |
|
|
29
30
|
| `.php` | `references/languages/php-laravel.md` |
|
|
30
31
|
| `.ts` `.tsx` | `references/languages/typescript.md` |
|
|
31
32
|
| `.py` | `references/languages/python.md` |
|
|
@@ -74,6 +75,7 @@ Changed code follows every applicable rule in the loaded complete guide. The fin
|
|
|
74
75
|
## References
|
|
75
76
|
|
|
76
77
|
- `references/languages/java.md`
|
|
78
|
+
- `references/languages/javascript.md`
|
|
77
79
|
- `references/languages/php-laravel.md`
|
|
78
80
|
- `references/languages/typescript.md`
|
|
79
81
|
- `references/languages/python.md`
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
# JavaScript Coding Guidelines
|
|
2
|
+
|
|
3
|
+
Rules every JavaScript change must follow.
|
|
4
|
+
|
|
5
|
+
## Index
|
|
6
|
+
|
|
7
|
+
| # | Rule | Tier |
|
|
8
|
+
| --- | --- | --- |
|
|
9
|
+
| 1 | [Braced conditionals](#1-braced-conditionals) | mechanical |
|
|
10
|
+
| 2 | [No magic strings](#2-no-magic-strings) | mechanical |
|
|
11
|
+
| 3 | [Guard clauses](#3-guard-clauses) | structural |
|
|
12
|
+
| 4 | [Fail fast](#4-fail-fast) | structural |
|
|
13
|
+
| 5 | [Explicit error handling](#5-explicit-error-handling) | structural |
|
|
14
|
+
| 6 | [Clear names](#6-clear-names) | structural |
|
|
15
|
+
| 7 | [Command Query Separation](#7-command-query-separation-cqs) | structural |
|
|
16
|
+
| 8 | [Null/undefined handling](#8-nullundefined-handling) | structural |
|
|
17
|
+
| 9 | [Immutability by default](#9-immutability-by-default) | structural |
|
|
18
|
+
| 10 | [Validate once](#10-validate-once) | structural |
|
|
19
|
+
| 11 | [Single Responsibility](#11-single-responsibility-srp) | architectural |
|
|
20
|
+
| 12 | [DRY (real duplication only)](#12-dry-real-duplication-only) | architectural |
|
|
21
|
+
| 13 | [KISS](#13-kiss) | architectural |
|
|
22
|
+
| 14 | [YAGNI](#14-yagni) | architectural |
|
|
23
|
+
| 15 | [Law of Demeter](#15-law-of-demeter) | architectural |
|
|
24
|
+
| 16 | [Composition over inheritance](#16-composition-over-inheritance) | architectural |
|
|
25
|
+
| 17 | [Dependency direction](#17-dependency-direction) | architectural |
|
|
26
|
+
| 18 | [Testability](#18-testability-as-a-design-constraint) | architectural |
|
|
27
|
+
| 19 | [Comments](#19-comments) | architectural |
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
### 1. Braced conditionals
|
|
32
|
+
|
|
33
|
+
RULE: Every `if`/`else` body uses braces, even a one-line guard. Never bind a statement to a conditional by indentation alone.
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
// bad
|
|
37
|
+
if (!user) return 0;
|
|
38
|
+
// good
|
|
39
|
+
if (!user) {
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 2. No magic strings
|
|
45
|
+
|
|
46
|
+
RULE: Name domain/protocol literals once (const, enum, shared contract) and reuse the name. One-off strings with no reuse and no domain meaning may stay inline.
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
// bad
|
|
50
|
+
if (order.status === 'pending_payment') {
|
|
51
|
+
charge(order);
|
|
52
|
+
}
|
|
53
|
+
// good
|
|
54
|
+
const ORDER_STATUS = { PENDING_PAYMENT: 'pending_payment' };
|
|
55
|
+
if (order.status === ORDER_STATUS.PENDING_PAYMENT) {
|
|
56
|
+
charge(order);
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 3. Guard clauses
|
|
61
|
+
|
|
62
|
+
RULE: Validate and exit early. Keep the happy path last, at the shallowest indent. No pyramid nesting.
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
// bad
|
|
66
|
+
function discount(user) {
|
|
67
|
+
if (user) {
|
|
68
|
+
if (user.active) {
|
|
69
|
+
return user.subscribed ? 0.2 : 0;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
// good
|
|
75
|
+
function discount(user) {
|
|
76
|
+
if (!user || !user.active || !user.subscribed) {
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
79
|
+
return 0.2;
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 4. Fail fast
|
|
84
|
+
|
|
85
|
+
RULE: Invalid input, impossible state, or a broken dependency throws immediately with a clear message. No fallback chains or silent defaults.
|
|
86
|
+
WHY: This is for invariants that should never happen if upstream contracts hold — not license to re-check business rules a boundary already validated (see rule 10).
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
// bad
|
|
90
|
+
const zone = zones[order.zoneId] ?? zones.default;
|
|
91
|
+
// good
|
|
92
|
+
const zone = zones[order.zoneId];
|
|
93
|
+
if (!zone) {
|
|
94
|
+
throw new Error(`Unknown zoneId: ${order.zoneId}`);
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### 5. Explicit error handling
|
|
99
|
+
|
|
100
|
+
RULE: Never swallow an error in an empty or generic `catch`. Re-throw with context, or let it propagate.
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
// bad
|
|
104
|
+
async function loadUser(id) {
|
|
105
|
+
try {
|
|
106
|
+
return await api.getUser(id);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// good
|
|
112
|
+
async function loadUser(id) {
|
|
113
|
+
try {
|
|
114
|
+
return await api.getUser(id);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
throw new Error(`Failed to load user ${id}`, { cause: error });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### 6. Clear names
|
|
122
|
+
|
|
123
|
+
RULE: Names reveal role or domain meaning. No `data`, `info`, `temp`, `result`, `obj`, `val`, `x`. A long honest name beats a short vague one.
|
|
124
|
+
|
|
125
|
+
```js
|
|
126
|
+
// bad
|
|
127
|
+
function process(data) {
|
|
128
|
+
return data.filter((x) => x.val > 0);
|
|
129
|
+
}
|
|
130
|
+
// good
|
|
131
|
+
function getActiveSubscriptions(subscriptions) {
|
|
132
|
+
return subscriptions.filter(
|
|
133
|
+
(subscription) => subscription.remainingDays > 0,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### 7. Command Query Separation (CQS)
|
|
139
|
+
|
|
140
|
+
RULE: A function either does something (side effect) or returns something (query) — never both. A "getter" must never mutate.
|
|
141
|
+
|
|
142
|
+
```js
|
|
143
|
+
// bad
|
|
144
|
+
function getNextId(counter) {
|
|
145
|
+
counter.value += 1;
|
|
146
|
+
return counter.value;
|
|
147
|
+
}
|
|
148
|
+
// good
|
|
149
|
+
function peekNextId(counter) {
|
|
150
|
+
return counter.value + 1;
|
|
151
|
+
}
|
|
152
|
+
function incrementCounter(counter) {
|
|
153
|
+
counter.value += 1;
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### 8. Null/undefined handling
|
|
158
|
+
|
|
159
|
+
RULE: Pick one convention (e.g. `undefined` = not set, `null` = explicitly empty) and never mix them for the same meaning. Never overload null to mean several business states — give each state its own explicit value.
|
|
160
|
+
|
|
161
|
+
```js
|
|
162
|
+
// bad
|
|
163
|
+
function discount(user) {
|
|
164
|
+
if (!user) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
if (!user.plan) {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
if (user.plan.discount === 0) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
return user.plan.discount;
|
|
174
|
+
}
|
|
175
|
+
// good
|
|
176
|
+
function discount(user) {
|
|
177
|
+
if (!user?.plan) {
|
|
178
|
+
return 0;
|
|
179
|
+
}
|
|
180
|
+
return user.plan.discount;
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### 9. Immutability by default
|
|
185
|
+
|
|
186
|
+
RULE: Return new values instead of mutating parameters. Never hide a side effect inside what looks like a pure transform.
|
|
187
|
+
|
|
188
|
+
```js
|
|
189
|
+
// bad
|
|
190
|
+
function addItem(cart, item) {
|
|
191
|
+
cart.items.push(item);
|
|
192
|
+
return cart;
|
|
193
|
+
}
|
|
194
|
+
// good
|
|
195
|
+
function addItem(cart, item) {
|
|
196
|
+
return { ...cart, items: [...cart.items, item] };
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### 10. Validate once
|
|
201
|
+
|
|
202
|
+
RULE: Validate at the boundary that owns the input (schema, request DTO). Downstream code trusts that contract — no re-checking the same required/type/range rule further in.
|
|
203
|
+
|
|
204
|
+
```js
|
|
205
|
+
// bad
|
|
206
|
+
function update(profile) {
|
|
207
|
+
if (!profile.email) {
|
|
208
|
+
throw new Error('email required');
|
|
209
|
+
}
|
|
210
|
+
repository.update(profile);
|
|
211
|
+
}
|
|
212
|
+
// good
|
|
213
|
+
function update(validatedProfile) {
|
|
214
|
+
repository.update(validatedProfile);
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### 11. Single Responsibility (SRP)
|
|
219
|
+
|
|
220
|
+
RULE: A function, class, or module has one reason to change. If it validates AND transforms AND persists AND notifies, split it.
|
|
221
|
+
|
|
222
|
+
```js
|
|
223
|
+
// bad
|
|
224
|
+
async function saveUser(data) {
|
|
225
|
+
if (!data.email) {
|
|
226
|
+
throw new Error('email required');
|
|
227
|
+
}
|
|
228
|
+
const normalized = { ...data, email: data.email.trim().toLowerCase() };
|
|
229
|
+
await db.users.insert(normalized);
|
|
230
|
+
await mailer.send(normalized.email, 'welcome');
|
|
231
|
+
}
|
|
232
|
+
// good
|
|
233
|
+
function validateUser(userInput) {
|
|
234
|
+
if (!userInput.email) {
|
|
235
|
+
throw new Error('email required');
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function normalizeUser(userInput) {
|
|
239
|
+
return { ...userInput, email: userInput.email.trim().toLowerCase() };
|
|
240
|
+
}
|
|
241
|
+
async function createUser(userInput) {
|
|
242
|
+
validateUser(userInput);
|
|
243
|
+
const normalizedUser = normalizeUser(userInput);
|
|
244
|
+
await db.users.insert(normalizedUser);
|
|
245
|
+
await notifyWelcome(normalizedUser.email);
|
|
246
|
+
return normalizedUser;
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### 12. DRY (real duplication only)
|
|
251
|
+
|
|
252
|
+
RULE: Extract when at least 2 of 3 hold: (a) the logic appears 3+ times, (b) a future rule change would need to touch all occurrences at once, (c) the copies represent the same domain concept. Do not extract on the first or second occurrence, or extract look-alike code from different concepts.
|
|
253
|
+
|
|
254
|
+
```js
|
|
255
|
+
// bad — extracted after one use
|
|
256
|
+
function formatName(value) {
|
|
257
|
+
return value.trim().toUpperCase();
|
|
258
|
+
}
|
|
259
|
+
// good — the third occurrence confirms one domain concept
|
|
260
|
+
function normalizeTicketStatus(status) {
|
|
261
|
+
return status.trim().toLowerCase();
|
|
262
|
+
}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### 13. KISS
|
|
266
|
+
|
|
267
|
+
RULE: Ship the simplest solution for the current, real requirement. No configurability, hooks, or layers added "just in case."
|
|
268
|
+
|
|
269
|
+
```js
|
|
270
|
+
// bad
|
|
271
|
+
function formatPrice(value, options = defaults) {
|
|
272
|
+
return formatter.format(value, options);
|
|
273
|
+
}
|
|
274
|
+
// good
|
|
275
|
+
function formatPrice(value) {
|
|
276
|
+
return `$${value.toFixed(2)}`;
|
|
277
|
+
}
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### 14. YAGNI
|
|
281
|
+
|
|
282
|
+
RULE: Don't add fields, params, or branches for a use case that hasn't arrived. (KISS simplifies the chosen solution; YAGNI stops you building the unrequested one.)
|
|
283
|
+
|
|
284
|
+
```js
|
|
285
|
+
// bad
|
|
286
|
+
function createInvoice(order, recurring = false, multiCurrency = false) {
|
|
287
|
+
return build(order, recurring, multiCurrency);
|
|
288
|
+
}
|
|
289
|
+
// good
|
|
290
|
+
function createInvoice(order) {
|
|
291
|
+
return { total: order.total, items: order.items };
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### 15. Law of Demeter
|
|
296
|
+
|
|
297
|
+
RULE: Talk only to immediate collaborators. Never reach through an object to grab something several levels deep inside its internal structure.
|
|
298
|
+
|
|
299
|
+
```js
|
|
300
|
+
// bad
|
|
301
|
+
function getCityName(user) {
|
|
302
|
+
return user.address.city.name;
|
|
303
|
+
}
|
|
304
|
+
// good
|
|
305
|
+
function getCityName(user) {
|
|
306
|
+
return user.getCityName();
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### 16. Composition over inheritance
|
|
311
|
+
|
|
312
|
+
RULE: Compose small, focused behaviors instead of building inheritance chains that force unrelated methods onto every subclass.
|
|
313
|
+
|
|
314
|
+
```js
|
|
315
|
+
// bad
|
|
316
|
+
class Animal {
|
|
317
|
+
makeSound() {}
|
|
318
|
+
fly() {}
|
|
319
|
+
}
|
|
320
|
+
class Dog extends Animal {
|
|
321
|
+
makeSound() {
|
|
322
|
+
return 'Woof';
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
// good
|
|
326
|
+
const canBark = { makeSound: () => 'Woof' };
|
|
327
|
+
const canFly = { fly: () => 'Flying' };
|
|
328
|
+
const dog = { ...canBark };
|
|
329
|
+
const bird = { ...canBark, ...canFly };
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### 17. Dependency direction
|
|
333
|
+
|
|
334
|
+
RULE: Inner layers (domain/business logic) never import outer layers (framework, DB, HTTP). Outer layers depend inward, never the reverse.
|
|
335
|
+
|
|
336
|
+
```js
|
|
337
|
+
// bad
|
|
338
|
+
import { MysqlConnection } from '../infra/mysql';
|
|
339
|
+
function calculateInvoiceTotal(invoiceId) {
|
|
340
|
+
const invoice = new MysqlConnection().query(
|
|
341
|
+
'SELECT * FROM invoices WHERE id = ?',
|
|
342
|
+
[invoiceId],
|
|
343
|
+
);
|
|
344
|
+
return invoice.items.reduce((sum, item) => sum + item.price, 0);
|
|
345
|
+
}
|
|
346
|
+
// good
|
|
347
|
+
function calculateInvoiceTotal(invoice) {
|
|
348
|
+
return invoice.items.reduce((sum, item) => sum + item.price, 0);
|
|
349
|
+
}
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
### 18. Testability as a design constraint
|
|
353
|
+
|
|
354
|
+
RULE: Inject dependencies (clock, network, random) instead of hardcoding them. If a function is hard to test, redesign it — don't skip the test.
|
|
355
|
+
|
|
356
|
+
```js
|
|
357
|
+
// bad
|
|
358
|
+
function isSubscriptionExpired(subscription) {
|
|
359
|
+
return subscription.expiresAt < new Date();
|
|
360
|
+
}
|
|
361
|
+
// good
|
|
362
|
+
function isSubscriptionExpired(subscription, now = new Date()) {
|
|
363
|
+
return subscription.expiresAt < now;
|
|
364
|
+
}
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
### 19. Comments
|
|
368
|
+
|
|
369
|
+
RULE: Comment only non-obvious intent — a trade-off, a constraint, a hazard names can't carry. Delete narration, restated logic, and stale TODOs. Test: if deleting the comment leaves the code equally clear, delete it.
|
|
370
|
+
|
|
371
|
+
```js
|
|
372
|
+
// bad
|
|
373
|
+
// increment the counter by 1
|
|
374
|
+
counter++;
|
|
375
|
+
// good
|
|
376
|
+
// Poll because the provider cannot guarantee single webhook delivery.
|
|
377
|
+
setInterval(checkPaymentStatus, 5_000);
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
---
|
|
381
|
+
|
|
382
|
+
**When in doubt:** fail fast, keep it flat, keep it small.
|
|
383
|
+
|
|
384
|
+
**Enforcement note:** rules 1–2 are mechanical and should be backed by a JavaScript formatter/linter. Rules 3–10 are structural checks. Rules 11–19 require review judgment.
|