@hailstails/hone 1.4.0 → 1.5.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/ADOPTING.md +8 -8
- package/CHANGELOG.md +14 -0
- package/MIGRATING.md +17 -0
- package/README.md +10 -10
- package/api/testing.js +2 -2
- package/chunks/{chunk-FEWCJU7H.js → chunk-2O46EB4E.js} +6 -0
- package/chunks/{chunk-6SM3HKWO.js → chunk-5U4VTFD5.js} +88 -51
- package/core/check/tool-command.js +379 -56
- package/core/mechanism-port/runner-result-types.d.ts +4 -0
- package/docs/adding-a-discipline-or-mechanism.md +2 -0
- package/docs/extending.md +1 -0
- package/docs/output-contract.md +17 -6
- package/mechanisms/audit/CD-27/test-body-logic.js +5 -2
- package/mechanisms/audit/runner/runner.js +5 -3
- package/mechanisms/eslint/runner/runner.js +7 -3
- package/methods/assessment-template.md +2 -2
- package/methods/lenses/clean-architecture.md +7 -8
- package/methods/lenses/defended-on-intent.md +2 -2
- package/methods/lenses/di-discipline.md +10 -13
- package/methods/lenses/function-first.md +16 -18
- package/package.json +1 -1
- package/rules/CD-1/CHECKS.md +4 -2
- package/rules/CD-1/README.md +6 -19
- package/rules/CD-10/CHECKS.md +5 -3
- package/rules/CD-10/README.md +6 -18
- package/rules/CD-11/CHECKS.md +4 -2
- package/rules/CD-11/README.md +6 -16
- package/rules/CD-12/CHECKS.md +4 -2
- package/rules/CD-12/README.md +6 -16
- package/rules/CD-13/CHECKS.md +4 -2
- package/rules/CD-13/README.md +6 -16
- package/rules/CD-14/CHECKS.md +3 -1
- package/rules/CD-14/README.md +6 -19
- package/rules/CD-15/CHECKS.md +3 -1
- package/rules/CD-15/README.md +5 -20
- package/rules/CD-16/CHECKS.md +3 -1
- package/rules/CD-16/README.md +6 -6
- package/rules/CD-17/CHECKS.md +3 -1
- package/rules/CD-17/README.md +6 -19
- package/rules/CD-18/CHECKS.md +4 -2
- package/rules/CD-18/README.md +6 -16
- package/rules/CD-19/CHECKS.md +8 -6
- package/rules/CD-19/README.md +6 -20
- package/rules/CD-2/CHECKS.md +5 -3
- package/rules/CD-2/README.md +6 -21
- package/rules/CD-20/CHECKS.md +3 -1
- package/rules/CD-20/README.md +6 -6
- package/rules/CD-21/CHECKS.md +4 -2
- package/rules/CD-21/README.md +6 -15
- package/rules/CD-22/CHECKS.md +4 -2
- package/rules/CD-22/README.md +6 -15
- package/rules/CD-23/CHECKS.md +5 -3
- package/rules/CD-23/README.md +6 -17
- package/rules/CD-24/CHECKS.md +3 -1
- package/rules/CD-24/README.md +6 -6
- package/rules/CD-25/CHECKS.md +3 -1
- package/rules/CD-25/README.md +5 -21
- package/rules/CD-26/CHECKS.md +5 -3
- package/rules/CD-26/README.md +6 -20
- package/rules/CD-27/CHECKS.md +16 -7
- package/rules/CD-27/README.md +6 -20
- package/rules/CD-28/CHECKS.md +4 -2
- package/rules/CD-28/README.md +6 -16
- package/rules/CD-29/CHECKS.md +4 -2
- package/rules/CD-29/README.md +6 -14
- package/rules/CD-3/CHECKS.md +3 -1
- package/rules/CD-3/README.md +6 -17
- package/rules/CD-30/CHECKS.md +4 -2
- package/rules/CD-30/README.md +6 -15
- package/rules/CD-31/CHECKS.md +4 -2
- package/rules/CD-31/README.md +6 -14
- package/rules/CD-32/CHECKS.md +3 -1
- package/rules/CD-32/README.md +2 -0
- package/rules/CD-33/CHECKS.md +3 -1
- package/rules/CD-33/README.md +2 -0
- package/rules/CD-34/CHECKS.md +3 -1
- package/rules/CD-34/README.md +2 -0
- package/rules/CD-39/CHECKS.md +3 -1
- package/rules/CD-39/README.md +2 -0
- package/rules/CD-4/CHECKS.md +4 -2
- package/rules/CD-4/README.md +6 -17
- package/rules/CD-40/CHECKS.md +3 -1
- package/rules/CD-40/README.md +6 -20
- package/rules/CD-41/CHECKS.md +4 -2
- package/rules/CD-41/README.md +6 -22
- package/rules/CD-42/CHECKS.md +3 -1
- package/rules/CD-42/README.md +6 -16
- package/rules/CD-43/CHECKS.md +3 -1
- package/rules/CD-43/README.md +6 -18
- package/rules/CD-44/CHECKS.md +3 -1
- package/rules/CD-44/README.md +6 -17
- package/rules/CD-45/CHECKS.md +4 -2
- package/rules/CD-45/README.md +6 -16
- package/rules/CD-46/CHECKS.md +3 -1
- package/rules/CD-46/README.md +2 -0
- package/rules/CD-47/CHECKS.md +4 -2
- package/rules/CD-47/README.md +4 -20
- package/rules/CD-5/CHECKS.md +4 -2
- package/rules/CD-5/README.md +6 -19
- package/rules/CD-6/CHECKS.md +3 -1
- package/rules/CD-6/README.md +6 -19
- package/rules/CD-7/CHECKS.md +3 -1
- package/rules/CD-7/README.md +6 -20
- package/rules/CD-7a/CHECKS.md +3 -1
- package/rules/CD-7a/README.md +6 -17
- package/rules/CD-8/CHECKS.md +3 -1
- package/rules/CD-8/README.md +6 -17
- package/rules/CD-9/CHECKS.md +4 -2
- package/rules/CD-9/README.md +6 -17
- package/rules/README.md +71 -54
- package/rules/SPIRIT.md +3 -3
- package/rules/cases.md +94 -0
- package/rules/numbers.md +155 -0
- package/rules/principles.md +89 -0
- package/rules/records.md +150 -0
- package/rules/rules.md +149 -0
- package/rules/sources.md +142 -0
- package/rules/typescript/checks.md +219 -0
- package/rules/typescript/recipes.md +361 -0
- package/rules/typescript/symptoms.json +1005 -0
- package/rules/typescript/symptoms.md +612 -0
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
# Recipes for TypeScript
|
|
2
|
+
|
|
3
|
+
A recipe is a known method that makes a [rule](../rules.md) hold, with the reason it was chosen. Recipes name the tool, the feature and the method. Rules stay as they are.
|
|
4
|
+
|
|
5
|
+
A recipe is "as binding as it's reasoning. The proof of burden is on the deviation to show that the reason it was chosen no longer holds."
|
|
6
|
+
|
|
7
|
+
So every recipe says why it was chosen, and binds as far as that reason holds. **A [deviation record](../records.md#deviation) names the recipe and its reason, and shows that this reason does not hold at this site. Showing that another method also meets the rule is not enough.**
|
|
8
|
+
|
|
9
|
+
Where a reason is marked **proposed**, it is not yet accepted.
|
|
10
|
+
|
|
11
|
+
Outside dependency: what was called a package. An internal package is our own code.
|
|
12
|
+
|
|
13
|
+
## Tests are ours
|
|
14
|
+
|
|
15
|
+
### Written-out answers
|
|
16
|
+
|
|
17
|
+
#### Stub with vi.fn
|
|
18
|
+
|
|
19
|
+
- **Method:** Each collaborator is a `vi.fn()` declared as a const inside the test that uses it. Shared boilerplate or a mock builder is allowed where it hides none of three things: a wide interface, an assumption a test rests on, or coupling between tests. A shared double is therefore a symptom, suspect by default, not a breach. Its answer is `mockReturnValue` or `mockResolvedValue` with a written-out value. Each call is checked with `toHaveBeenCalledWith` and a written-out argument.
|
|
20
|
+
- **Chosen because:** "these should be vi.fn, with mockResolvedValue, not custom async functions"; "Agents take fake to mean whatever they want it to mean right now." The framework's double has no body of ours to hold logic.
|
|
21
|
+
- **You can tell because:** No double has a body, a branch or a table. Every call the unit makes is asserted.
|
|
22
|
+
- **Industry name:** stub and spy; Test Stub (Meszaros, *xUnit Test Patterns*).
|
|
23
|
+
|
|
24
|
+
A fake is not a recipe. It is a [deviation record](../records.md#deviation), and the bar it must meet is: "Moving a fake is only allowed if the test cannot be done with deterministic stubs or hardcoded things."
|
|
25
|
+
|
|
26
|
+
#### Ports in the argument
|
|
27
|
+
|
|
28
|
+
- **Method:** The unit takes its ports in its one argument object, or from the factory that built it. The test passes stubs the same way. No `vi.mock`, no global stub, no `process.env` set in a test.
|
|
29
|
+
- **Chosen because:** "If it calls something and uses its output we hardcode that output using DI."; "there should be absolutely no reason to be setting process env in a test, if you are doing this you have severely messed up your dependency management".
|
|
30
|
+
- **You can tell because:** The test imports only the unit and its types. Nothing is patched.
|
|
31
|
+
- **Industry name:** dependency injection; constructor injection (Fowler); Pure DI (Seemann).
|
|
32
|
+
|
|
33
|
+
### Straight-through tests
|
|
34
|
+
|
|
35
|
+
#### it.each over typed rows
|
|
36
|
+
|
|
37
|
+
- **Method:** Several cases of one behaviour are one `it.each<Row>([...])`. `Row` is a declared type. Each row writes out its input and its expected output.
|
|
38
|
+
- **Chosen because:** "It's just boilerplate for cases being tested."
|
|
39
|
+
- **You can tell because:** Every row is a literal. No row is built, mapped or picked.
|
|
40
|
+
- **Industry name:** Parameterized Test (Meszaros), with rows typed and written out.
|
|
41
|
+
|
|
42
|
+
#### Assert to narrow
|
|
43
|
+
|
|
44
|
+
- **Method:** A value that may be absent or one of several outcomes is narrowed with the framework's `assert` first. After that, plain `expect` reads it.
|
|
45
|
+
- **Chosen because:** "use vi assert to validate it's existence first to narrow the type to exclude the need for optional chain".
|
|
46
|
+
- **You can tell because:** No `?.`, `&&`, `if` or ternary in the test.
|
|
47
|
+
- **Industry name:** Guard Assertion (Meszaros).
|
|
48
|
+
|
|
49
|
+
#### Whole written-out expectation
|
|
50
|
+
|
|
51
|
+
- **Method:** Assert the whole returned value with `toEqual` against a written-out literal. Read list items by bare index after an assert, never with `find`.
|
|
52
|
+
- **Chosen because:** "no find, use bare access in tests for determinstic insistence on behaviours".
|
|
53
|
+
- **You can tell because:** The expected value is a literal in the test.
|
|
54
|
+
- **Industry name:** Literal Value (Meszaros).
|
|
55
|
+
|
|
56
|
+
#### Failure asserted as a value
|
|
57
|
+
|
|
58
|
+
- **Method:** A failure is checked as the returned variant: `expect(result).toEqual({ outcome: "failure", ... })`. An adapter whose caller's contract is a throw is checked with `expect(call).toThrow(...)`.
|
|
59
|
+
- **Chosen because (proposed):** "more error throwing, not allowed". A `try` or `catch` in a test is a branch that decides whether the test passes.
|
|
60
|
+
- **You can tell because:** No `try`, `catch` or `throw` in any test.
|
|
61
|
+
- **Industry name:** none; it follows from Failures are values.
|
|
62
|
+
|
|
63
|
+
### Tested where it's owned
|
|
64
|
+
|
|
65
|
+
#### Companion test file
|
|
66
|
+
|
|
67
|
+
- **Method:** `name.ts` has `name.test.ts` beside it. The test imports the unit and runs it.
|
|
68
|
+
- **Chosen because:** "if its a unit test it covers the unit it is the companion file to".
|
|
69
|
+
- **You can tell because:** Each file with a runtime export has a sibling that runs it.
|
|
70
|
+
- **Industry name:** the same convention as Go's `_test.go` files beside the package.
|
|
71
|
+
|
|
72
|
+
#### Integration test suffix
|
|
73
|
+
|
|
74
|
+
- **Method:** A test of a binding to the outside is named `name.integration.test.ts`. Every other test file is a unit test.
|
|
75
|
+
- **Chosen because:** "a useful pattern to ensure boundaries are clear when to maintain and keep pyramid checkable."
|
|
76
|
+
- **You can tell because:** The two kinds can be counted and checked by file name.
|
|
77
|
+
- **Industry name:** the same convention as Maven Failsafe's `*IT.java` integration tests.
|
|
78
|
+
|
|
79
|
+
#### Integration test owns its scratch
|
|
80
|
+
|
|
81
|
+
- **Method:** Only an integration test touches a real file or subprocess. It makes its own temporary directory, gives every subprocess an explicit environment, and removes the directory after. The environment comes from a unit of ours with its own test, not a test helper.
|
|
82
|
+
- **Chosen because (proposed):** An ambient setting broke the shared checkout three times; each fix to one instance left the class open.
|
|
83
|
+
- **You can tell because:** No unit test touches disk or spawns a process. No integration test reads `process.env` or inherits git settings.
|
|
84
|
+
- **Industry name:** Fresh Fixture (Meszaros).
|
|
85
|
+
|
|
86
|
+
## One place decides
|
|
87
|
+
|
|
88
|
+
### Doer or orchestrator
|
|
89
|
+
|
|
90
|
+
#### Composed orchestrator
|
|
91
|
+
|
|
92
|
+
- **Method:** An orchestrator's body is a sequence of named calls to our units. Each failed result returns at once, unchanged. Any computing or branching between calls moves into a named doer.
|
|
93
|
+
- **Chosen because:** "If it's long it's doing too many things inline. I hold to functions are doers or are orchestrator a teaching."
|
|
94
|
+
- **You can tell because:** The orchestrator holds calls, early returns of failures, and a final return.
|
|
95
|
+
- **Industry name:** the Integration Operation Segregation Principle (Westphal); Composed Method (Beck).
|
|
96
|
+
|
|
97
|
+
#### Functional iteration
|
|
98
|
+
|
|
99
|
+
- **Method:** Work over several values uses `map`, `filter`, `flatMap` or `reduce`, returning new values. No `for`, `for…of` or `while`. A stream is drained with `for await` only in its source's adapter, at an authorised location.
|
|
100
|
+
- **Chosen because:** "For of mutates by its nature. It doesn't return. It's state holding required or side effect based."
|
|
101
|
+
- **You can tell because:** No loop keyword outside a recorded drain. Nothing is pushed into an outer binding.
|
|
102
|
+
- **Industry name:** Collection Pipeline; Replace Loop with Pipeline (Fowler).
|
|
103
|
+
|
|
104
|
+
### Handed what it uses
|
|
105
|
+
|
|
106
|
+
#### One argument object
|
|
107
|
+
|
|
108
|
+
- **Method:** A function whose signature is ours to choose takes one argument: an object with named fields, whose declared type lists only the fields the unit reads. A function whose shape an outside API fixes (a callback to `map` or `reduce`) takes what that API passes. No tuple carries data.
|
|
109
|
+
- **Chosen because:** "Shows up in tests where to avoid writing a large object for 2 fields that impact th test they double cast. It shows the minimum interface principle."; "Functions with no outside influence should take 1. 2 is debatable. But for agents the goal is always 1."; "nope, you will not use tupple like arrays for data".
|
|
110
|
+
- **You can tell because:** No function of ours has two or more parameters unless an outside API fixes its shape. No test casts to build an input.
|
|
111
|
+
- **Industry name:** Introduce Parameter Object (Fowler), always one object.
|
|
112
|
+
|
|
113
|
+
#### Narrow ports
|
|
114
|
+
|
|
115
|
+
- **Method:** A port interface lists only the methods its one consumer calls. One adapter fulfils several narrow ports.
|
|
116
|
+
- **Chosen because:** "Rather than writing 6 different concrete things 1 for each."; "narrowed ports that still help us find the seams where the dependencies link up".
|
|
117
|
+
- **You can tell because:** Each port's methods are all called by its consumer.
|
|
118
|
+
- **Industry name:** role interface (Fowler); Interface Segregation.
|
|
119
|
+
|
|
120
|
+
#### Entity module
|
|
121
|
+
|
|
122
|
+
- **Method:** An entity's type and its logic share one module. Its functions take the entity.
|
|
123
|
+
- **Chosen because:** "The anaemic concern there is that by writing types with no logic you are failing coupling/cohesion and single responsibility principles."
|
|
124
|
+
- **You can tell because:** No file holds only types, except port declarations.
|
|
125
|
+
- **Industry name:** DDD entity (Evans).
|
|
126
|
+
|
|
127
|
+
Entity module also serves One reason per module.
|
|
128
|
+
|
|
129
|
+
### Guard first
|
|
130
|
+
|
|
131
|
+
#### Guard clauses
|
|
132
|
+
|
|
133
|
+
- **Method:** Each ending condition is `if (...) return ...;` at the top. There is no `else` after a return. The work is the last return.
|
|
134
|
+
- **Chosen because:** "If nested it's a poorly modelled set of questions for a single job."
|
|
135
|
+
- **You can tell because:** No `if` sits inside another `if`. There is no `else` after an exit.
|
|
136
|
+
- **Industry name:** Replace Nested Conditional with Guard Clauses (Fowler).
|
|
137
|
+
|
|
138
|
+
### One reason per module
|
|
139
|
+
|
|
140
|
+
#### Screaming folders
|
|
141
|
+
|
|
142
|
+
- **Method:** Folders and files are named for the domain capability they hold. None is called utils, helpers, types or common.
|
|
143
|
+
- **Chosen because:** "all types have a purpose. that purpose is its reason to exist and its reason to change"; screaming architecture "making those boundaries easier to see when broken".
|
|
144
|
+
- **You can tell because:** Each path names a domain concept.
|
|
145
|
+
- **Industry name:** Screaming Architecture (Martin); package by feature.
|
|
146
|
+
|
|
147
|
+
[Entity module](#entity-module) also serves this rule.
|
|
148
|
+
|
|
149
|
+
## No hidden effects
|
|
150
|
+
|
|
151
|
+
The principle decides.
|
|
152
|
+
|
|
153
|
+
### Copy, never write in
|
|
154
|
+
|
|
155
|
+
- **Method:** A changed value is a new one: `{ ...order, total }` or `[...items].sort()`. Nothing writes to, deletes from, or rebinds a parameter.
|
|
156
|
+
- **Chosen because:** "this is giving mutation of input args vibes, not okay"; "pure functions are required".
|
|
157
|
+
- **You can tell because:** No assignment to a parameter or its fields.
|
|
158
|
+
- **Industry name:** immutability; Value Object (Fowler); functional core.
|
|
159
|
+
|
|
160
|
+
### Closure factory
|
|
161
|
+
|
|
162
|
+
- **Method:** Settings and changing state live in a factory, `createX({ settings })`. It returns an object of functions that close over private bindings. We author no `class`. A library's class is built only in its adapter.
|
|
163
|
+
- **Chosen because:** "this is a bunch of nullable and state management mess". Proposed: Once a method travels as a value, its `this` is a fact the checker does not prove.
|
|
164
|
+
- **You can tell because:** No `class` keyword in our code. No unit reads `this`.
|
|
165
|
+
- **Industry name:** Private Members (Crockford).
|
|
166
|
+
|
|
167
|
+
[Functional iteration](#functional-iteration) also serves this principle.
|
|
168
|
+
|
|
169
|
+
## External behaviour sits at a boundary
|
|
170
|
+
|
|
171
|
+
### Outside dependencies stay at boundaries
|
|
172
|
+
|
|
173
|
+
#### Adapter per outside party
|
|
174
|
+
|
|
175
|
+
- **Method:** An outside dependency or runtime module is imported only in the adapter file for the party it reaches. That adapter fulfils a port and returns our types. Nothing re-exports the outside dependency. `JSON.parse` is called inside the adapter for whoever produced the JSON.
|
|
176
|
+
- **Chosen because:** "I didn't want a json parse wrapper. I wanted a boundary on whoever made json"; "Json parse has no contract… Http fetch library for example has complexity to bind".
|
|
177
|
+
- **You can tell because:** Each package import sits in a file with a boundary record.
|
|
178
|
+
- **Industry name:** Adapter; Gateway; anti-corruption layer; Hexagonal Architecture (Cockburn).
|
|
179
|
+
|
|
180
|
+
### Ports declared inside
|
|
181
|
+
|
|
182
|
+
#### Port in the domain
|
|
183
|
+
|
|
184
|
+
- **Method:** The port's interface is declared in the domain, in a module of its own, named for the capability. It sits apart from the adapter that fulfils it and from the unit that uses it; both import it from there. Storage is such a port, named for what the domain asks.
|
|
185
|
+
- **Chosen because:** "In clean architecture the port is domain and to be kept separate to the concrete implementation or use."; "then host facts source should be declared separately to its concrete implementation to protiect dependency inversion".
|
|
186
|
+
- **You can tell because:** No port is declared in an adapter's module or in its consumer's. No business-logic file imports an adapter. No query or driver type in business logic.
|
|
187
|
+
- **Industry name:** dependency inversion (Martin); Clean Architecture.
|
|
188
|
+
|
|
189
|
+
#### World values through a port
|
|
190
|
+
|
|
191
|
+
- **Method:** The current time, new ids, random values and process lifecycle each come from a port: `clock.now()`, `ids.next()`. A stub returns a written-out value.
|
|
192
|
+
- **Chosen because:** "date creates non determinism and leaks the world in"; "we have a uuid factory, you should not be importing dependencies".
|
|
193
|
+
- **You can tell because:** No `Date.now()`, `new Date()`, `randomUUID` or `process.exit` outside their adapters.
|
|
194
|
+
- **Industry name:** Volatile Dependencies (Seemann).
|
|
195
|
+
|
|
196
|
+
#### Composition root
|
|
197
|
+
|
|
198
|
+
- **Method:** One entry-point module builds every adapter and passes each one in.
|
|
199
|
+
- **Chosen because (proposed):** If a unit builds its own adapter, it imports it, so the port is not passed in and the test cannot stub it.
|
|
200
|
+
- **You can tell because:** No other module calls an adapter's factory.
|
|
201
|
+
- **Industry name:** Composition Root (Seemann).
|
|
202
|
+
|
|
203
|
+
## No leak across a boundary
|
|
204
|
+
|
|
205
|
+
The principle decides.
|
|
206
|
+
|
|
207
|
+
### Zod at the producer's adapter
|
|
208
|
+
|
|
209
|
+
- **Method:** The adapter for whoever produced the data parses it once with a Zod schema. The `z.infer` type is the wire type and stays in the adapter. The adapter maps it to a domain type of ours. A missing required value, environment included, is a failure, never a default.
|
|
210
|
+
- **Chosen because:** "my insistence on not writing complex logic to parse for cd 4 by being explicit on the tool and method"; "Hand rolled is potentially a messy and lossy option?"; "Zod is the wire."
|
|
211
|
+
- **You can tell because:** No schema, `z.infer` type or text past the adapter. Each crossing is parsed once.
|
|
212
|
+
- **Industry name:** parse, don't validate (King).
|
|
213
|
+
|
|
214
|
+
### Map out at the adapter
|
|
215
|
+
|
|
216
|
+
- **Method:** Business logic hands the adapter our own value. Only the adapter turns it into the receiver's form (`JSON.stringify`, a status, a header).
|
|
217
|
+
- **Chosen because:** "As we should be runtime objects not json as soon as we leave a boundary".
|
|
218
|
+
- **You can tell because:** No outside form is built in business logic.
|
|
219
|
+
- **Industry name:** anticorruption layer (Evans).
|
|
220
|
+
|
|
221
|
+
### Failure mapping at the adapter
|
|
222
|
+
|
|
223
|
+
- **Method:** The adapter turns a library's throw into a failure variant of ours. At the program's edge, it turns our failure into the caller's form: an exit code, a status, or a throw where that caller's contract is exceptions.
|
|
224
|
+
- **Chosen because (proposed):** "They are about speaking the language of the caller at the boundary."
|
|
225
|
+
- **You can tell because:** No `throw` except at an adapter whose caller expects one.
|
|
226
|
+
- **Industry name:** anticorruption layer (Evans), applied to failures.
|
|
227
|
+
|
|
228
|
+
Failure mapping at the adapter also serves Failures are values.
|
|
229
|
+
|
|
230
|
+
## The type checker proves it
|
|
231
|
+
|
|
232
|
+
### Failures are values
|
|
233
|
+
|
|
234
|
+
#### Result union
|
|
235
|
+
|
|
236
|
+
- **Method:** One shared result type tagged `result`, carrying `value` or `error`. The error is a tagged union of our own with a `kind`, as plain data. A caller returns a failed result at once, unchanged; where it must choose by failure, it is a `switch` on `kind` with no `default`.
|
|
237
|
+
- **Chosen because:** "it makes the output clear to type system instead of hidden in throws".
|
|
238
|
+
- **You can tell because:** No `throw` between units of ours. No failure is returned as `null`, `false`, `[]` or a default.
|
|
239
|
+
- **Industry name:** Result type (Wlaschin).
|
|
240
|
+
|
|
241
|
+
[Failure mapping at the adapter](#failure-mapping-at-the-adapter) also serves this rule.
|
|
242
|
+
|
|
243
|
+
### Declared, not asserted
|
|
244
|
+
|
|
245
|
+
#### Named, exported type
|
|
246
|
+
|
|
247
|
+
- **Method:** Declare and export a named type. Annotate the factory and its return with it. Never `ReturnType<typeof x>`.
|
|
248
|
+
- **Chosen because (proposed):** A derived type couples the consumer to the producer's implementation, not its contract.
|
|
249
|
+
- **You can tell because:** Every type a unit returns has its own name.
|
|
250
|
+
- **Industry name:** none; ours.
|
|
251
|
+
|
|
252
|
+
#### Fix the declaration, not the value
|
|
253
|
+
|
|
254
|
+
- **Method:** When a cast seems needed, narrow the declared input to the fields used, or tighten the generic so the checker proves it. No `as`, no `!`, no type guard of ours.
|
|
255
|
+
- **Chosen because:** "the correct amount of casting is **0**"; "this feels like its hiding a problem with out modelling".
|
|
256
|
+
- **You can tell because:** Zero casts except `as const`.
|
|
257
|
+
- **Industry name:** none; ours.
|
|
258
|
+
|
|
259
|
+
#### Bounded context generic
|
|
260
|
+
|
|
261
|
+
- **Method:** A middleware that reads a context field takes a generic bounded so the field must exist. Each layer widens it.
|
|
262
|
+
- **Chosen because (proposed):** A field read from an open context is either cast or defaulted. Both hide a missing capability until run time.
|
|
263
|
+
- **You can tell because:** No middleware checks for, or defaults, a field it reads.
|
|
264
|
+
- **Industry name:** none; ours.
|
|
265
|
+
|
|
266
|
+
#### Shared strict base config
|
|
267
|
+
|
|
268
|
+
- **Method:** All strict flags sit in one base config that every `tsconfig.json` extends. No file turns one off.
|
|
269
|
+
- **Chosen because (proposed):** One place sets strictness, so no project weakens it unseen.
|
|
270
|
+
- **You can tell because:** The resolved config of every project has every flag on.
|
|
271
|
+
- **Industry name:** none; ours.
|
|
272
|
+
|
|
273
|
+
[Zod at the producer's adapter](#zod-at-the-producers-adapter) and [Assert to narrow](#assert-to-narrow) also serve this rule. The framework's assertion is not a claim of ours.
|
|
274
|
+
|
|
275
|
+
### Kinds are named
|
|
276
|
+
|
|
277
|
+
#### Tagged union and exhaustive switch
|
|
278
|
+
|
|
279
|
+
- **Method:** Kinds are a string-literal tag on a union. Choosing is a `switch` with one case per kind and no `default`, so the checker proves every kind is handled. A mode or state is a literal union (`"compact" | "full"`), never a `boolean`.
|
|
280
|
+
- **Chosen because:** "boolean inputs are a smell, disallowed"; "no boolean inputs, breaks clean code".
|
|
281
|
+
- **You can tell because:** No `default:` on a closed union. No boolean parameter or field stands for a state.
|
|
282
|
+
- **Industry name:** algebraic data type; exhaustiveness checking (TypeScript Handbook), with no `default` branch.
|
|
283
|
+
|
|
284
|
+
## Our code reads in the common language
|
|
285
|
+
|
|
286
|
+
### One word everywhere
|
|
287
|
+
|
|
288
|
+
#### Domain vocabulary list
|
|
289
|
+
|
|
290
|
+
- **Method:** One list of domain terms, one word per concept, read before naming. A new concept goes on the list before it is named in code.
|
|
291
|
+
- **Chosen because:** "to stop naming things 10 different ways for the same concept."
|
|
292
|
+
- **You can tell because:** One concept has one spelling across the code.
|
|
293
|
+
- **Industry name:** ubiquitous language (Evans).
|
|
294
|
+
|
|
295
|
+
#### Whole words
|
|
296
|
+
|
|
297
|
+
- **Method:** Every name is whole words, at every scope, lambdas included. No single letters, abbreviations or filler words such as data, result or options.
|
|
298
|
+
- **Chosen because:** "dont care if its a lambda function or not, meaningful names always"; "no, the problem is the lack of a whole word".
|
|
299
|
+
- **You can tell because:** No name is on the shorthand or filler list.
|
|
300
|
+
- **Industry name:** descriptive names (Google TypeScript Style Guide), at every scope.
|
|
301
|
+
|
|
302
|
+
#### Meaning in names, not comments
|
|
303
|
+
|
|
304
|
+
- **Method:** What a comment would say goes into a name, a type or a union. The only annotations are a tracked pointer such as `(#42)` and a tool directive covered by a record.
|
|
305
|
+
- **Chosen because:** "If the code isn't self explanatory that's the problem. You shouldn't need usage notes at all."
|
|
306
|
+
- **You can tell because:** No comment or JSDoc in our code.
|
|
307
|
+
- **Industry name:** none; ours. It is stricter than Martin's *Clean Code*, which keeps a section on good comments.
|
|
308
|
+
|
|
309
|
+
[One argument object](#one-argument-object) also serves this rule.
|
|
310
|
+
|
|
311
|
+
Casing is a check that runs the adopter's own lint. It is not a recipe. Basic logic in the language's own constructs is a case under the principle; its recipe is the native array and object methods, chosen because an outside dependency's language for them is "language obscuring standard operations".
|
|
312
|
+
|
|
313
|
+
## Outside risk is carried on purpose
|
|
314
|
+
|
|
315
|
+
### Outside dependencies justified
|
|
316
|
+
|
|
317
|
+
#### Dependency record
|
|
318
|
+
|
|
319
|
+
- **Method:** One [dependency record](../records.md#dependency-record) per direct dependency, tools included, names the native way weighed and why it fell short.
|
|
320
|
+
- **Chosen because:** "Packages need a justification for the maintenance they add."
|
|
321
|
+
- **You can tell because:** Every entry in `package.json` has a record.
|
|
322
|
+
- **Industry name:** architecture decision record (Nygard).
|
|
323
|
+
|
|
324
|
+
Proposed, not accepted: the case [An adopter is not handed our risk](../cases.md#proposed-cases), under Outside dependencies justified. A peer dependency is a direct dependency of ours, so it needs a record. The risk it weighs is carried by whoever runs it, the adopter included.
|
|
325
|
+
|
|
326
|
+
### Fixed versions
|
|
327
|
+
|
|
328
|
+
#### Exact pins and a lock file
|
|
329
|
+
|
|
330
|
+
- **Method:** Every version we run is exact: dev dependencies, tools, CI runtimes and actions carry no `^`, `~` or tag, and the lock file is committed. The range a published package offers its adopters is not a version we run.
|
|
331
|
+
- **Chosen because:** "dependencies must be pinned"; "If we ship with dependencies we need lock files."
|
|
332
|
+
- **You can tell because:** No range, and no tag such as `latest`.
|
|
333
|
+
- **Industry name:** Pinned-Dependencies (OpenSSF Scorecard).
|
|
334
|
+
|
|
335
|
+
### Tools live in the repo
|
|
336
|
+
|
|
337
|
+
#### Repo scripts only
|
|
338
|
+
|
|
339
|
+
- **Method:** Every tool is a dev dependency, run by a `package.json` script. Nothing runs through `npx`, `dlx` or a fetch at run time. A standard open-source tool wins over a custom script.
|
|
340
|
+
- **Chosen because:** "if this is common script it should be in package json, not run through tsx"; "I don't want a custom script, can you get something more standardised and provided by open source or something?"
|
|
341
|
+
- **You can tell because:** Every command in CI and the docs is a script name.
|
|
342
|
+
- **Industry name:** Twelve-Factor, Dependencies.
|
|
343
|
+
|
|
344
|
+
### Upstream stays untouched
|
|
345
|
+
|
|
346
|
+
#### Extension outside the vendored code
|
|
347
|
+
|
|
348
|
+
- **Method:** A change is made through the vendored code's extension points or config, in a folder of ours.
|
|
349
|
+
- **Chosen because:** "this must be justified as additional drift as impossible to apply as an extension instead".
|
|
350
|
+
- **You can tell because:** No diff inside the vendored folder. An edit there breaches Upstream stays untouched. It is held only by an [authorised location](../records.md#authorised-location) on Edit inside vendored code that names its case and when it should be dropped.
|
|
351
|
+
- **Industry name:** Open-Closed Principle (Martin).
|
|
352
|
+
|
|
353
|
+
### Shipped risk declared
|
|
354
|
+
|
|
355
|
+
Proposed, not accepted: the rule, its case [Ships code for others](../cases.md#proposed-cases), and this recipe.
|
|
356
|
+
|
|
357
|
+
#### Declared, not bundled
|
|
358
|
+
|
|
359
|
+
- **Method:** A published package bundles no dependency. Each one is a pinned `dependencies` entry or a `peerDependencies` declaration.
|
|
360
|
+
- **Chosen because:** "Do we still bundle Zod which means a Zod cve won't show up accurately?"; "Otherwise we ship as a peer dependency declaration only."
|
|
361
|
+
- **Industry name:** Twelve-Factor, Dependencies.
|