@rmrdeveloper/sideroom-pi 8.0.2 → 8.1.1
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 +64 -2
- package/extensions/done/detect.ts +227 -0
- package/extensions/done/guard.ts +77 -0
- package/extensions/done/index.ts +35 -0
- package/extensions/done/model.ts +43 -0
- package/extensions/guidelines/catalog.ts +1 -0
- package/extensions/guidelines/guard.ts +50 -8
- package/extensions/guidelines/index.ts +6 -0
- package/extensions/rules/catalog.ts +71 -0
- package/extensions/rules/checks.ts +346 -0
- package/extensions/rules/guard.ts +197 -0
- package/extensions/rules/index.ts +22 -0
- package/extensions/rules/lexer.ts +136 -0
- package/extensions/rules/model.ts +220 -0
- package/extensions/todo/execute.ts +2 -2
- package/extensions/todo/index.ts +6 -2
- package/extensions/todo/model.ts +40 -10
- package/package.json +2 -2
- package/skills/sideroom-guidelines/SKILL.md +3 -1
- package/skills/sideroom-guidelines/references/languages/javascript.md +384 -0
|
@@ -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.
|