@hailstails/hone 1.0.0 → 1.1.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.
Files changed (35) hide show
  1. package/ADOPTING.md +33 -7
  2. package/CHANGELOG.md +13 -0
  3. package/MIGRATING.md +31 -6
  4. package/README.md +64 -17
  5. package/api/audit.d.ts +6 -0
  6. package/api/audit.js +9 -1
  7. package/api/testing.js +36 -8
  8. package/chunks/{chunk-XE6RNNZB.js → chunk-63SJV5ZY.js} +1395 -1339
  9. package/chunks/{chunk-RNXPEK2U.js → chunk-BLGGM7AV.js} +2 -2
  10. package/chunks/{chunk-OKQHVZLO.js → chunk-CBWWAKQY.js} +1 -1
  11. package/chunks/{chunk-5X2BR42Y.js → chunk-FR4FASVM.js} +15 -24
  12. package/chunks/chunk-MXSIQPCV.js +13 -0
  13. package/chunks/{chunk-LHVFZMXD.js → chunk-MZTLH4EU.js} +60 -20
  14. package/chunks/chunk-NA5L6PUE.js +15 -0
  15. package/chunks/{chunk-OVEQ37JQ.js → chunk-PVCA3ISA.js} +42 -17
  16. package/chunks/chunk-RWQ4UNKY.js +24 -0
  17. package/chunks/{chunk-YL6WR3QW.js → chunk-Y4SMWOD2.js} +1 -1
  18. package/chunks/chunk-YLZ73C7O.js +0 -0
  19. package/chunks/{chunk-NILRE23T.js → chunk-Z4UBS7WI.js} +1 -1
  20. package/core/check/tool-command.js +702 -22
  21. package/core/conformance/conformance-testing-types.d.ts +2 -0
  22. package/core/mechanism-port/runner-module-types.d.ts +2 -0
  23. package/core/rule-set-reader/authored-rule-set-types.d.ts +2 -0
  24. package/core/values/edition-types.d.ts +16 -0
  25. package/docs/extending.md +31 -9
  26. package/docs/output-contract.md +101 -15
  27. package/mechanisms/audit/CD-19/import-boundaries.js +3 -2
  28. package/mechanisms/audit/CD-27/piece.json +5 -1
  29. package/mechanisms/audit/CD-27/test-body-logic.js +55 -4
  30. package/mechanisms/audit/CD-5/comments-directives.js +4 -3
  31. package/mechanisms/audit/runner/runner.js +6 -5
  32. package/mechanisms/eslint/runner/runner.js +8 -4
  33. package/package.json +1 -1
  34. package/rules/CD-27/CHECKS.md +31 -1
  35. package/rules/CD-27/discipline.json +74 -0
@@ -6,22 +6,42 @@ Read [the rule and its spirit](README.md) first. This page is the mechanics: wha
6
6
 
7
7
  | Clause | Mechanism | Checks |
8
8
  | --- | --- | --- |
9
- | `CD-27#test-body-logic` | audit | branching, `let`, and computing methods in a test file |
9
+ | `CD-27#test-body-logic` | audit | branching, `let`, and computing methods in a test file; edition 2 adds short-circuits, optional chaining, `switch` and computing namespace calls |
10
10
  | `CD-27#focused-tests` | eslint | `.only` on a test or suite, or `fdescribe`, `fit`, `ftest`, `fsuite` |
11
11
  | `CD-27#skipped-tests` | eslint | `.skip` or `.fixme` on a test or suite, or `xdescribe`, `xit`, `xtest`, `xsuite` |
12
12
 
13
13
  ## What the check flags
14
+ `CD-27#test-body-logic` has two editions. Edition 1 runs by default. Edition 2 adds the constructs below and becomes the default in the next major release; see [Settings](#settings) to run it now.
15
+
16
+ Edition 1:
14
17
  - Test files only: `*.test.ts` and `*.test.tsx`. Generated folders such as `node_modules`, `dist`, `build` and `coverage` are skipped.
15
18
  - `if` statements, `try` statements, ternary expressions and `let` declarations.
16
19
  - Any method call with a computing or mutating name, on any receiver: `map`, `filter`, `reduce`, `find`, `forEach`, `join`, `slice`, `split`, `replace`, `includes`, `sort`, `trim`, `keys`, `values`, `push`, `pop`, `splice`, `fill` and similar string and array methods.
17
20
  - Loops are not flagged here; CD-15 covers them. `throw` is CD-10.
18
21
 
22
+ Edition 2 also flags:
23
+ - `??`, `||` and `&&`, and their assignment forms `??=`, `||=` and `&&=`.
24
+ - Optional chaining: `a?.b`, `a?.[key]` and `f?.()`.
25
+ - `switch` statements.
26
+ - Computing calls on a built-in namespace: `JSON.stringify`, `JSON.parse`, `Object.assign`, `Object.fromEntries`, `Array.from` and any `Math` method.
27
+
28
+ Neither edition flags element access such as `calls[0]`, calls to free functions, or a method called through a computed key, such as `rows["map"]`. Helper files that tests import are not checked; only the test files themselves are.
29
+
19
30
  ## What the ESLint check flags
20
31
  Needs ESLint adopted through the projection; see [the ESLint piece](../../mechanisms/eslint/CD-27/README.md).
21
32
  - `hone-cd27/focused-tests` flags `.only` on a `describe`, `it`, `test` or `suite` chain, including `.only.each` and `test.describe.only`, and the bare names `fdescribe`, `fit`, `ftest` and `fsuite`. It reports at the `only` token.
22
33
  - `hone-cd27/skipped-tests` flags `.skip` and `.fixme` on those chains, and the bare names `xdescribe`, `xit`, `xtest` and `xsuite`.
23
34
  - Not flagged: `.todo`, `.concurrent` and `.each`.
24
35
 
36
+ ## Settings
37
+ - `edition`: the edition this clause runs, `"1"` or `"2"`. Without it, the clause runs edition 1, or the latest edition when `checkEditions` in `disciplines.json` is `"latest"`.
38
+
39
+ ```json
40
+ "clauseSettings": {
41
+ "CD-27#test-body-logic": { "edition": "2" }
42
+ }
43
+ ```
44
+
25
45
  ## Example violation
26
46
  ```ts
27
47
  const expected = rows.map((row) => row.id);
@@ -33,6 +53,16 @@ expect(ids).toEqual(["a1", "b2"]);
33
53
  ```
34
54
  Write the expected value out. A method call in the act step is flagged too, so move such logic into the unit under test.
35
55
 
56
+ Under edition 2, write a fallback or an optional call as the value it gives:
57
+
58
+ ```ts
59
+ const expected = given ?? 1;
60
+ ```
61
+
62
+ ```ts
63
+ expect(given).toBe(1);
64
+ ```
65
+
36
66
  ## Exceptions
37
67
  Add an entry to `discipline-exceptions.json` at the repository root. Use `authorised-location` when a test file legitimately needs logic. Use `file-carve-out` for debt in one file: it is forfeited once the file is touched, and needs a `whyChain` as below. See [discipline-exceptions.json](../../README.md#discipline-exceptionsjson).
38
68
 
@@ -45,6 +45,60 @@
45
45
  "expectedLines": [
46
46
  2
47
47
  ]
48
+ },
49
+ {
50
+ "name": "nullish fallback",
51
+ "fileName": "fallback.test.ts",
52
+ "source": "const given: number | undefined = undefined;\nconst expected = given ?? 1;\n",
53
+ "expectedLines": [
54
+ 2
55
+ ],
56
+ "edition": "2"
57
+ },
58
+ {
59
+ "name": "logical operator",
60
+ "fileName": "logical.test.ts",
61
+ "source": "const ready = true;\nconst expected = ready && 1;\n",
62
+ "expectedLines": [
63
+ 2
64
+ ],
65
+ "edition": "2"
66
+ },
67
+ {
68
+ "name": "optional call",
69
+ "fileName": "optional.test.ts",
70
+ "source": "const observers: { enter?: () => void } = {};\nobservers.enter?.();\n",
71
+ "expectedLines": [
72
+ 2
73
+ ],
74
+ "edition": "2"
75
+ },
76
+ {
77
+ "name": "switch statement",
78
+ "fileName": "switch.test.ts",
79
+ "source": "const kind = \"a\";\nswitch (kind) {\n case \"a\":\n break;\n}\n",
80
+ "expectedLines": [
81
+ 2
82
+ ],
83
+ "edition": "2"
84
+ },
85
+ {
86
+ "name": "computing namespace call",
87
+ "fileName": "serialise.test.ts",
88
+ "source": "const row = { id: 1 };\nconst expected = JSON.stringify(row);\n",
89
+ "expectedLines": [
90
+ 2
91
+ ],
92
+ "edition": "2"
93
+ },
94
+ {
95
+ "name": "branching under the second edition",
96
+ "fileName": "branch-second.test.ts",
97
+ "source": "const ok = true;\nif (ok) {\n expect(ok).toBe(true);\n}\n",
98
+ "expectedLines": [
99
+ 2
100
+ ],
101
+ "edition": "2"
48
102
  }
49
103
  ],
50
104
  "allow": [
@@ -65,6 +119,26 @@
65
119
  "fileName": "throws.test.ts",
66
120
  "source": "throw new Error(\"bad\");\n",
67
121
  "expectedLines": []
122
+ },
123
+ {
124
+ "name": "nullish fallback under the first edition",
125
+ "fileName": "fallback.test.ts",
126
+ "source": "const given: number | undefined = undefined;\nconst expected = given ?? 1;\n",
127
+ "expectedLines": []
128
+ },
129
+ {
130
+ "name": "hardcoded expectations under the second edition",
131
+ "fileName": "straight-second.test.ts",
132
+ "source": "const rows = [1, 2];\nexpect(rows).toEqual([1, 2]);\n",
133
+ "expectedLines": [],
134
+ "edition": "2"
135
+ },
136
+ {
137
+ "name": "element access under the second edition",
138
+ "fileName": "index.test.ts",
139
+ "source": "const calls = [[1]];\nexpect(calls[0]).toEqual([1]);\n",
140
+ "expectedLines": [],
141
+ "edition": "2"
68
142
  }
69
143
  ]
70
144
  }