@prestack/thread 0.1.20 → 0.1.22

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 (148) hide show
  1. package/CHANGELOG.md +193 -0
  2. package/dist/core/attach.js +21 -0
  3. package/dist/core/descend.d.ts +2 -0
  4. package/dist/core/descend.js +18 -3
  5. package/dist/core/generators/commit.js +55 -3
  6. package/dist/core/generators/dev.d.ts +1 -0
  7. package/dist/core/generators/dev.js +68 -0
  8. package/dist/core/rows.d.ts +5 -0
  9. package/dist/core/rows.js +45 -0
  10. package/dist/core/tree.d.ts +1 -0
  11. package/dist/core/tree.js +7 -1
  12. package/dist/derivation/generators/build.d.ts +3 -1
  13. package/dist/derivation/generators/build.js +103 -22
  14. package/dist/derivation/generators/release.js +74 -26
  15. package/dist/derivation/release.js +3 -3
  16. package/dist/integrations/cli.d.ts +3 -9
  17. package/dist/integrations/cli.js +96 -36
  18. package/dist/integrations/declared.d.ts +8 -0
  19. package/dist/integrations/declared.js +22 -0
  20. package/dist/integrations/pglite.js +13 -3
  21. package/dist/integrations/target.d.ts +4 -1
  22. package/dist/integrations/target.js +20 -6
  23. package/dist/integrity/attach.js +1 -5
  24. package/dist/integrity/generators/publish.js +2 -1
  25. package/dist/integrity/slopnators/sl_stale_path.js +10 -0
  26. package/dist/integrity/slopnators/sl_unlisted_program.js +2 -2
  27. package/dist/operations/access.js +3 -2
  28. package/dist/operations/capabilities.d.ts +2 -0
  29. package/dist/operations/capabilities.js +2 -0
  30. package/dist/operations/generators/capability.js +234 -24
  31. package/dist/operations/generators/compile.d.ts +21 -0
  32. package/dist/operations/generators/compile.js +151 -51
  33. package/dist/operations/generators/permits.d.ts +2 -1
  34. package/dist/operations/generators/permits.js +52 -47
  35. package/dist/operations/generators/seed.d.ts +3 -0
  36. package/dist/operations/generators/seed.js +25 -3
  37. package/dist/operations/nodes.d.ts +2 -2
  38. package/package.json +1 -1
  39. package/dist/core/actor.ts +0 -21
  40. package/dist/core/attach.ts +0 -613
  41. package/dist/core/chain.ts +0 -145
  42. package/dist/core/condition.ts +0 -227
  43. package/dist/core/descend.ts +0 -1173
  44. package/dist/core/errors/catalogue.ts +0 -314
  45. package/dist/core/errors/classes.ts +0 -51
  46. package/dist/core/errors/error.ts +0 -152
  47. package/dist/core/errors/namespaces.ts +0 -19
  48. package/dist/core/finding.ts +0 -37
  49. package/dist/core/generators/commit.ts +0 -157
  50. package/dist/core/init.ts +0 -222
  51. package/dist/core/invoked.ts +0 -52
  52. package/dist/core/round.ts +0 -74
  53. package/dist/core/rows.ts +0 -459
  54. package/dist/core/scope.ts +0 -20
  55. package/dist/core/thread.ts +0 -29
  56. package/dist/core/tree.ts +0 -90
  57. package/dist/core/types.ts +0 -351
  58. package/dist/derivation/attach.ts +0 -57
  59. package/dist/derivation/generators/build.ts +0 -371
  60. package/dist/derivation/generators/check.ts +0 -118
  61. package/dist/derivation/generators/determinism.ts +0 -145
  62. package/dist/derivation/generators/release.ts +0 -236
  63. package/dist/derivation/release.ts +0 -43
  64. package/dist/integrations/attach.ts +0 -187
  65. package/dist/integrations/cli.ts +0 -615
  66. package/dist/integrations/disk.ts +0 -466
  67. package/dist/integrations/generators/read.ts +0 -127
  68. package/dist/integrations/generators/vault.ts +0 -210
  69. package/dist/integrations/https.ts +0 -216
  70. package/dist/integrations/pglite.ts +0 -206
  71. package/dist/integrations/secret.ts +0 -54
  72. package/dist/integrations/target.ts +0 -656
  73. package/dist/integrations/vault.ts +0 -148
  74. package/dist/integrity/attach.ts +0 -150
  75. package/dist/integrity/check-threads.ts +0 -166
  76. package/dist/integrity/constraint.ts +0 -1246
  77. package/dist/integrity/generators/compiler.ts +0 -138
  78. package/dist/integrity/generators/gate.ts +0 -227
  79. package/dist/integrity/generators/issues.ts +0 -160
  80. package/dist/integrity/generators/publish.ts +0 -232
  81. package/dist/integrity/generators/trial.ts +0 -359
  82. package/dist/integrity/lint.ts +0 -214
  83. package/dist/integrity/lsp/bin.ts +0 -44
  84. package/dist/integrity/lsp/diagnostics.ts +0 -92
  85. package/dist/integrity/lsp/findings.ts +0 -63
  86. package/dist/integrity/lsp/lint.ts +0 -124
  87. package/dist/integrity/lsp/protocol.ts +0 -78
  88. package/dist/integrity/lsp/server.ts +0 -224
  89. package/dist/integrity/slopnator.ts +0 -324
  90. package/dist/integrity/slopnators/sl_broken_lineage.ts +0 -38
  91. package/dist/integrity/slopnators/sl_declared_elsewhere.ts +0 -60
  92. package/dist/integrity/slopnators/sl_derived_drift.ts +0 -101
  93. package/dist/integrity/slopnators/sl_door_in_a_node.ts +0 -250
  94. package/dist/integrity/slopnators/sl_hardcoded_style.ts +0 -55
  95. package/dist/integrity/slopnators/sl_inert_island.ts +0 -84
  96. package/dist/integrity/slopnators/sl_inert_slopnator.ts +0 -93
  97. package/dist/integrity/slopnators/sl_missing_row.ts +0 -140
  98. package/dist/integrity/slopnators/sl_no_barrel.ts +0 -48
  99. package/dist/integrity/slopnators/sl_no_child_process.ts +0 -100
  100. package/dist/integrity/slopnators/sl_no_comment.ts +0 -43
  101. package/dist/integrity/slopnators/sl_no_dispatch_bypass.ts +0 -276
  102. package/dist/integrity/slopnators/sl_no_import_cycle.ts +0 -66
  103. package/dist/integrity/slopnators/sl_no_such_command.ts +0 -81
  104. package/dist/integrity/slopnators/sl_only_takes_a_literal.ts +0 -63
  105. package/dist/integrity/slopnators/sl_outside_an_operation.ts +0 -25
  106. package/dist/integrity/slopnators/sl_screen_holds_only_declared.ts +0 -70
  107. package/dist/integrity/slopnators/sl_secret_literal.ts +0 -68
  108. package/dist/integrity/slopnators/sl_stale_path.ts +0 -178
  109. package/dist/integrity/slopnators/sl_twice_meant.ts +0 -57
  110. package/dist/integrity/slopnators/sl_twice_parented.ts +0 -82
  111. package/dist/integrity/slopnators/sl_undeclared_crossing.ts +0 -481
  112. package/dist/integrity/slopnators/sl_undeclared_destination.ts +0 -113
  113. package/dist/integrity/slopnators/sl_undeclared_element.ts +0 -44
  114. package/dist/integrity/slopnators/sl_undeclared_list.ts +0 -358
  115. package/dist/integrity/slopnators/sl_undeclared_node.ts +0 -75
  116. package/dist/integrity/slopnators/sl_undeclared_operation.ts +0 -257
  117. package/dist/integrity/slopnators/sl_undeclared_variant.ts +0 -152
  118. package/dist/integrity/slopnators/sl_undeclared_webhook.ts +0 -108
  119. package/dist/integrity/slopnators/sl_underived_route.ts +0 -72
  120. package/dist/integrity/slopnators/sl_undescribed.ts +0 -65
  121. package/dist/integrity/slopnators/sl_ungoverned_island.ts +0 -60
  122. package/dist/integrity/slopnators/sl_unlisted_program.ts +0 -46
  123. package/dist/integrity/slopnators/sl_unnamed_island.ts +0 -41
  124. package/dist/integrity/slopnators/sl_unpaired_member.ts +0 -139
  125. package/dist/integrity/slopnators/sl_unpaired_slopnator.ts +0 -71
  126. package/dist/integrity/slopnators/sl_unprefixed_member.ts +0 -110
  127. package/dist/integrity/slopnators/sl_unthreaded.ts +0 -421
  128. package/dist/integrity/slopnators/sl_untranslated_key.ts +0 -140
  129. package/dist/integrity/slopnators/sl_unvaulted_secret.ts +0 -132
  130. package/dist/integrity/slopnators/sl_unversioned.ts +0 -52
  131. package/dist/integrity/sweep.ts +0 -395
  132. package/dist/knowledge/attach.ts +0 -327
  133. package/dist/knowledge/generators/docsgen.ts +0 -186
  134. package/dist/knowledge/generators/glossary.ts +0 -73
  135. package/dist/knowledge/glossary.ts +0 -6
  136. package/dist/operations/access.ts +0 -131
  137. package/dist/operations/attach.ts +0 -408
  138. package/dist/operations/capabilities.ts +0 -88
  139. package/dist/operations/dispatch.ts +0 -294
  140. package/dist/operations/error-results.ts +0 -31
  141. package/dist/operations/generators/capability.ts +0 -1054
  142. package/dist/operations/generators/compile.ts +0 -701
  143. package/dist/operations/generators/permits.ts +0 -224
  144. package/dist/operations/generators/seed.ts +0 -359
  145. package/dist/operations/generators/trial.ts +0 -666
  146. package/dist/operations/nodes.ts +0 -53
  147. package/dist/operations/operations.ts +0 -91
  148. package/dist/operations/result.ts +0 -28
@@ -1,232 +0,0 @@
1
- import { calls, starts } from '../../integrations/cli.ts'
2
- import { existsSync, readFileSync } from '../../integrations/disk.ts'
3
- import { valuesIn } from '../../core/rows.ts'
4
- import { join } from 'node:path'
5
- import { argv, env, execPath, stderr, stdout } from 'node:process'
6
- import { CLI, exit } from '../../core/invoked.ts'
7
- import { THING as TYPED, named } from '../../core/scope.ts'
8
- import { findTree, inTree } from '../../core/tree.ts'
9
-
10
- import type { Secret } from '../../integrations/secret.ts'
11
- import { placedAt, type Target } from '../../integrations/target.ts'
12
- import { opened, sealedIn } from '../../integrations/vault.ts'
13
-
14
- const TREE = findTree()
15
- const ROOT = TREE.at
16
-
17
- const STEP_TIMEOUT = 900_000
18
-
19
- const REGISTRY = 'ht_registry'
20
-
21
- interface Check {
22
- readonly id: string
23
- readonly runs: string
24
- readonly because: string
25
- }
26
-
27
- const CHECKS: readonly Check[] = [
28
- {
29
- id: 'gate',
30
- runs: 'thread gate',
31
- because: 'every step this tree declares, in the order it declares them, which is where the descent and the sweep are',
32
- },
33
- ]
34
-
35
- const PREFIX = /^(?:npx|pnpm)$/
36
-
37
- const words = (said: string): readonly string[] => said.split(/\s+/).filter((one) => one !== '')
38
-
39
- function run(given: readonly string[], extra?: Record<string, string>): number {
40
- const mine = given[0] === 'thread' ? 1 : PREFIX.test(given[0] ?? '') && given[1] === 'thread' ? 2 : 0
41
- const how = { at: ROOT, shows: true, within: STEP_TIMEOUT, holds: extra === undefined ? env : { ...env, ...extra } }
42
- const went = mine === 0 ? starts(given[0] ?? '', given.slice(1), how) : starts(execPath, [...CLI, ...given.slice(mine)], how)
43
- return went.there ? went.code : 1
44
- }
45
-
46
- function refuse(id: string, code: number, because: string, runs: string): never {
47
- stderr.write(`\npublish: REFUSED — \`${id}\` exited ${String(code)}. NOTHING was published.\n`)
48
- stderr.write(`publish: ${because}.\n`)
49
- stderr.write(`publish: what refused is above, in that command's own words — this does not restate it, because a\n`)
50
- stderr.write(`publish: second copy of a refusal drifts from the first. Run \`${runs}\` alone to work on it.\n`)
51
- exit(1)
52
- }
53
-
54
- const given = argv.indexOf('--registry')
55
-
56
- if (given !== -1) {
57
- stderr.write('thread publish takes no url. Where the packages go is the address on the ht_registry row in _threads/targets.ts.\n')
58
- stderr.write('\nit builds, then runs every check, then publishes. A check that fails publishes nothing.\n')
59
- exit(1)
60
- }
61
-
62
- const TOKEN = 'sec_registry_token'
63
-
64
- const PRODUCTION = 'production'
65
-
66
- const secrets = inTree(TREE, 'secrets.ts')
67
-
68
- const declared = existsSync(secrets)
69
- ? ((valuesIn(readFileSync(secrets)) as { SECRETS?: readonly Secret[] }).SECRETS ?? [])
70
- : []
71
-
72
- const vaulted = declared.some((one) => one.id === TOKEN)
73
-
74
- const targets = inTree(TREE, 'targets.ts')
75
-
76
- const registry = placedAt(
77
- existsSync(targets) ? ((valuesIn(readFileSync(targets)) as { TARGETS?: readonly Target[] }).TARGETS ?? []) : [],
78
- REGISTRY,
79
- ).replace(/[/]$/, '')
80
-
81
- if (registry === '') {
82
- stderr.write('\npublish: no target row says where the registry is, so there is nowhere for these packages to go. Give ' + REGISTRY + ' an address in _threads/targets.ts, the absolute url of the registry. NOTHING was published.\n')
83
- exit(1)
84
- }
85
-
86
- if (vaulted && !sealedIn(ROOT, PRODUCTION).includes(TOKEN)) {
87
- stderr.write(`\npublish: ${TOKEN} is declared in _threads/secrets.ts and has no value sealed for ${PRODUCTION}, so nothing was checked and NOTHING was published.\n`)
88
- stderr.write(`publish: run \`thread create secret set ${TOKEN}\` at a terminal and paste the registry token, or take the row out to publish with the login this machine holds.\n`)
89
- exit(1)
90
- }
91
-
92
- const setting = (): string => {
93
- const at = new URL(registry)
94
- return `npm_config_//${at.host}${at.pathname.endsWith('/') ? at.pathname : `${at.pathname}/`}:_authToken`
95
- }
96
-
97
- async function tokened(): Promise<string> {
98
- const values = await opened(ROOT, declared.map((one) => one.id), PRODUCTION)([TOKEN]).catch((thrown: unknown) => {
99
- stderr.write(`\npublish: ${TOKEN} is sealed here and would not open: ${thrown instanceof Error ? thrown.message : String(thrown)}. NOTHING was published.\n`)
100
- exit(1)
101
- })
102
- const token = values[TOKEN]
103
- if (token === undefined) {
104
- stderr.write(`\npublish: the vault opened and answered nothing for ${TOKEN}. NOTHING was published.\n`)
105
- exit(1)
106
- }
107
- return token
108
- }
109
-
110
- const signedAs = env['GITHUB_ACTIONS'] === 'true' ? 'the identity npm trusts this GitHub Actions run under' : 'the login this machine already holds'
111
-
112
- stdout.write(
113
- vaulted
114
- ? `publish: ${TOKEN} is declared and sealed for ${PRODUCTION}. It is opened only after every check passes, and handed to pnpm as its auth setting for ${registry}.\n`
115
- : `publish: no row declares ${TOKEN}, so this publish uses ${signedAs}. Declare it with \`thread create secret\` to publish with a token sealed in this repository instead.\n`,
116
- )
117
-
118
- stdout.write('publish: deriving and building what the packaging rows state first, so every check below reads the files this release ships rather than the source they were written from.\n')
119
-
120
- const made = run(words(`${TYPED} packaged --build`))
121
- stdout.write(`publish: packaged exited ${String(made)}\n`)
122
- if (made !== 0) {
123
- refuse(
124
- 'packaged',
125
- made,
126
- 'a package whose manifest and release note are not what its rows state would go out describing itself as something nobody declared, and one that did not compile would go out promising files it does not have',
127
- `${TYPED} packaged --build`,
128
- )
129
- }
130
-
131
- stdout.write(`\npublish: ${String(CHECKS.length)} check(s) stand between this tree and ${registry}. Any one of them stops it.\n`)
132
- for (const check of CHECKS) stdout.write(` ${check.id} — ${check.runs}\n`)
133
-
134
- for (const [index, check] of CHECKS.entries()) {
135
- const at = `check ${String(index + 1)} of ${String(CHECKS.length)}`
136
- stdout.write(`\npublish: ${at} — ${check.runs} (${check.id})\n`)
137
-
138
- const code = run(words(check.runs))
139
- stdout.write(`publish: ${at} — exited ${String(code)}\n`)
140
- if (code !== 0) refuse(check.id, code, check.because, check.runs)
141
- }
142
-
143
- const numbered = run(words(`${TYPED} packaged --release`))
144
- stdout.write(`publish: packaged --release exited ${String(numbered)}\n`)
145
- if (numbered !== 0) {
146
- refuse(
147
- 'packaged',
148
- numbered,
149
- 'the next release number is written onto the manifests only once every check has passed, and writing it refused, so nothing here carries a number to go out under',
150
- `${TYPED} packaged --release`,
151
- )
152
- }
153
-
154
- const handed = vaulted ? { [setting()]: await tokened() } : undefined
155
-
156
- stdout.write(`\npublish: every check passed. Publishing to ${registry} with ${vaulted ? `${TOKEN} from this repository's vault` : signedAs}.\n`)
157
-
158
- const sent = run(words(`pnpm -r publish --registry ${registry} --no-git-checks --access public`), handed)
159
-
160
- if (sent !== 0) {
161
- stderr.write(`\npublish: the checks passed and the publish itself exited ${String(sent)}.\n`)
162
- const back = run(words(`${TYPED} packaged`))
163
- stderr.write(
164
- back === 0
165
- ? 'publish: the manifests carry the last recorded release again and no note was written, so the next `thread publish` numbers this release once more.\n'
166
- : `publish: the manifests still carry the number that did not go out. Run \`${TYPED} packaged\` to put the last recorded release back.\n`,
167
- )
168
- exit(1)
169
- }
170
-
171
- const recorded = run(words(`${TYPED} packaged --record`))
172
- stdout.write(`publish: packaged --record exited ${String(recorded)}\n`)
173
- if (recorded !== 0) {
174
- stderr.write(`\npublish: the packages went out and the note recording them refused to be written, so the next publish would number this release again. Run \`${TYPED} packaged --record\` and read what it says.\n`)
175
- exit(1)
176
- }
177
-
178
- const SHEET = 'packaging.ts'
179
-
180
- interface Packaged {
181
- readonly id?: string
182
- readonly folder?: string
183
- }
184
-
185
- const sheet = inTree(TREE, SHEET)
186
-
187
- const stated = existsSync(sheet)
188
- ? (valuesIn(readFileSync(sheet)) as { PACKAGES?: readonly Packaged[] })
189
- : {}
190
-
191
- const shipped = stated.PACKAGES ?? []
192
-
193
- function versionAt(folder: string): string {
194
- const at = join(ROOT, folder, 'package.json')
195
- if (!existsSync(at)) return 'no manifest'
196
- try {
197
- return (JSON.parse(readFileSync(at, 'utf8')) as { version?: string }).version ?? 'no version'
198
- } catch {
199
- return 'no version'
200
- }
201
- }
202
-
203
- const git = calls('cli_git', ROOT)
204
-
205
- const asked = async (what: readonly string[]): Promise<string> => (await git.rev_parse(what)).out.trim()
206
-
207
- if ((await asked(['--is-inside-work-tree'])) !== 'true') {
208
- stdout.write("\npublish: it's not a repo, so nothing was committed.\n")
209
- } else if (shipped.length === 0) {
210
- stdout.write('\npublish: no packaging row names a folder, so there is nothing derived here to commit.\n')
211
- } else {
212
- const going = shipped.map((one) => `${named(one.id ?? '')} ${versionAt(one.folder ?? '')}`).join(', ')
213
- const branch = await asked(['--abbrev-ref', 'HEAD'])
214
-
215
- for (const one of shipped) await git.stage([one.folder ?? ''], true, STEP_TIMEOUT)
216
-
217
- if ((await git.diff(['--cached', '--quiet'])).ok) {
218
- stdout.write(`\npublish: ${going} — what is on disk is already what is committed, so this release added no artifact.\n`)
219
- } else {
220
- const wrote = (await git.commit(['-m', `thread publish — ${going}`], true, STEP_TIMEOUT)).code
221
- stdout.write(`publish: commit exited ${String(wrote)}\n`)
222
- if (wrote !== 0) {
223
- stderr.write('\npublish: the packages went out and the files that describe them are not committed, so this release has no record in git.\n')
224
- stderr.write('publish: commit them by hand — everything is staged.\n')
225
- exit(1)
226
- }
227
- stdout.write(`publish: committed the derived artifacts to ${branch} — ${going}.\n`)
228
- }
229
- }
230
-
231
- stdout.write(`\npublish: done. What went out was derived, then built, then passed the gate, in that order. The compile inside \`thread packaged --build\` is the typecheck.\n`)
232
- exit(0)
@@ -1,359 +0,0 @@
1
- import { answeredBy, standing } from '../../integrations/disk.ts'
2
- import { resolve } from 'node:path'
3
-
4
- import type { Linter as LinterType, Rule } from 'eslint'
5
- import { Linter } from 'eslint'
6
-
7
- import type { Constraint, Proof, Sourced } from '../constraint.ts'
8
-
9
- const TREE = findTree()
10
- import { NOT_WRITTEN, ROOT, governs, proofsOf, reads, slopnatorName, sweeps, visits, type Slopnator } from '../slopnator.ts'
11
- import { forget } from '../slopnators/sl_unthreaded.ts'
12
- import { findTree } from '../../core/tree.ts'
13
-
14
- const PROBE = 'probe.tsx'
15
-
16
- const WRAPPINGS: readonly ((source: string) => string)[] = [
17
- (source) => source,
18
- (source) => `const _probe = { ${source} }`,
19
- (source) => `const _probe = <Probe ${source} />`,
20
- (source) => `const _probe = () => { ${source} }`,
21
- (source) => `const _probe = <Probe>{${source}}</Probe>`,
22
- ]
23
-
24
- export interface Scenario {
25
- readonly files: readonly Sourced[]
26
- readonly at: string
27
- }
28
-
29
- export interface Trial {
30
- readonly id: string
31
- readonly rule: string
32
- readonly name: string
33
- readonly scenario: Scenario
34
- readonly verdict: { readonly reports: boolean }
35
- readonly requires: string
36
- }
37
-
38
- export interface Derivation {
39
- readonly trials: readonly Trial[]
40
- }
41
-
42
- export interface Outcome {
43
- readonly id: string
44
- readonly rule: string
45
- readonly passed: boolean
46
- readonly failures: readonly string[]
47
- readonly parsed: boolean
48
- }
49
-
50
- export interface Run {
51
- readonly outcomes: readonly Outcome[]
52
- readonly passed: number
53
- readonly failed: number
54
- readonly measured: number
55
- readonly spent: ReadonlyMap<string, number>
56
- }
57
-
58
- const canonical = (at: string): string => at.replace(/^threads\//, `${TREE.name}/`)
59
-
60
- export function probeAt(rule: Constraint): string {
61
- const path = canonical(rule.where.paths[0] ?? 'src/')
62
- if (path === '.') return PROBE
63
- return path.endsWith('/') ? `${path}${PROBE}` : `${path}/${PROBE}`
64
- }
65
-
66
- function requiresOf(rule: Constraint, reports: boolean): string {
67
- return reports
68
- ? `${rule.id} reports on this source, at a path it governs`
69
- : `${rule.id} stays silent on this source — the fix it recommends is not itself refused`
70
- }
71
-
72
- function scenarioOf(rule: Constraint, proof: Proof): Scenario {
73
- if (typeof proof !== 'string') {
74
- return { at: canonical(proof.at), files: proof.files.map((one) => ({ ...one, at: canonical(one.at) })) }
75
- }
76
- const path = probeAt(rule)
77
- return { files: [{ at: path, source: proof }], at: path }
78
- }
79
-
80
- function spoken(scenario: Scenario): string {
81
- const target = scenario.files.find((file) => file.at === scenario.at) ?? scenario.files[0]
82
- const rest = scenario.files.length - 1
83
- return `${target?.source ?? ''}${rest > 0 ? ` (beside ${String(rest)} more file(s))` : ''}`
84
- }
85
-
86
- export function deriveTrials(all: readonly Constraint[], slopnators: ReadonlyMap<string, Slopnator>): Derivation {
87
- const trials: Trial[] = []
88
-
89
- for (const rule of all) {
90
- const proofs = proofsOf(rule, slopnators)
91
- const put = (proof: Proof, index: number, reports: boolean): void => {
92
- const scenario = scenarioOf(rule, proof)
93
- trials.push({
94
- id: `trl_rule_${rule.id.replace(/-/g, '_')}_${reports ? 'refuses' : 'accepts'}_${String(index)}`,
95
- rule: rule.id,
96
- name: `${rule.id} ${reports ? 'refuses' : 'accepts'}: ${spoken(scenario)}`,
97
- scenario,
98
- verdict: { reports },
99
- requires: requiresOf(rule, reports),
100
- })
101
- }
102
- proofs.wrong.forEach((proof, index) => { put(proof, index, true) })
103
- proofs.right.forEach((proof, index) => { put(proof, index, false) })
104
- }
105
-
106
- return { trials: trials.sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)) }
107
- }
108
-
109
- function config(rule: Constraint, module: Rule.RuleModule, parser: unknown): Parameters<LinterType['verify']>[1] {
110
- return [
111
- {
112
- files: ['**/*.ts', '**/*.tsx'],
113
- languageOptions: {
114
- parser,
115
- parserOptions: { ecmaVersion: 'latest', sourceType: 'module', ecmaFeatures: { jsx: true } },
116
- },
117
- plugins: { rule: { rules: { [rule.id]: module } } },
118
- rules: { [`rule/${rule.id}`]: 'error' },
119
- },
120
- ] as unknown as Parameters<LinterType['verify']>[1]
121
- }
122
-
123
- export function runTrial(trial: Trial, rule: Constraint, module: Rule.RuleModule, parser: unknown, slopnator?: Slopnator): Outcome {
124
- const linter = new Linter()
125
- const failures: string[] = []
126
-
127
- if (!governs(rule, trial.scenario.at)) {
128
- return {
129
- id: trial.id,
130
- rule: rule.id,
131
- passed: false,
132
- parsed: false,
133
- failures: [`the trial's own path "${trial.scenario.at}" is not one the rule governs, so nothing could have run`],
134
- }
135
- }
136
-
137
- const target = trial.scenario.files.find((file) => file.at === trial.scenario.at)
138
-
139
- let parsed = false
140
- let satisfied = false
141
-
142
- answeredBy(standing(ROOT, new Map<string, string>(trial.scenario.files.map((one) => [resolve(ROOT, one.at), one.source]))))
143
- forget()
144
-
145
- try {
146
- if (slopnator !== undefined && !reads(slopnator)) {
147
- parsed = true
148
- satisfied = slopnator.checks(ROOT).length > 0 === trial.verdict.reports
149
- } else if (target === undefined) {
150
- failures.push(`the scenario writes no file at "${trial.scenario.at}", the path it lints, so nothing could have been measured`)
151
- } else if (slopnator !== undefined && sweeps(slopnator)) {
152
- const held = slopnator.holds(undefined, {
153
- filename: trial.scenario.at,
154
- text: target.source,
155
- ancestors: () => [],
156
- comments: () => [],
157
- })
158
- parsed = true
159
- satisfied = (held !== false && held !== NOT_WRITTEN) === trial.verdict.reports
160
- } else {
161
- for (const wrap of WRAPPINGS) {
162
- const text = wrap(target.source)
163
- const messages = linter.verify(text, config(rule, module, parser), trial.scenario.at)
164
- if (messages.some((message) => message.fatal === true)) continue
165
- parsed = true
166
- satisfied = messages.some((message) => message.ruleId === `rule/${rule.id}`) === trial.verdict.reports
167
- break
168
- }
169
- }
170
- } catch (thrown) {
171
- failures.push(
172
- `this trial could not run: ${thrown instanceof Error ? thrown.message : String(thrown)} — its scenario does not describe a world this rule can be measured against`,
173
- )
174
- } finally {
175
- answeredBy(undefined)
176
- forget()
177
- }
178
-
179
- if (failures.length === 0 && !parsed) {
180
- failures.push('no wrapping of this source parsed, so nothing was measured — a trial that cannot run is not a trial that passed')
181
- } else if (failures.length === 0 && !satisfied) {
182
- failures.push(
183
- trial.verdict.reports
184
- ? `expected ${rule.id} to report on this source and it stayed silent — the rule documents a form its own slopnator does not refuse`
185
- : `expected ${rule.id} to stay silent on this source and it reported — the rule refuses the fix it recommends`,
186
- )
187
- }
188
-
189
- return { id: trial.id, rule: rule.id, passed: failures.length === 0, parsed, failures }
190
- }
191
-
192
- const judged = (who: readonly Constraint[], rules: Readonly<Record<string, Rule.RuleModule>>, parser: unknown): Parameters<LinterType['verify']>[1] =>
193
- [
194
- {
195
- files: ['**/*.ts', '**/*.tsx'],
196
- languageOptions: {
197
- parser,
198
- parserOptions: { ecmaVersion: 'latest', sourceType: 'module', ecmaFeatures: { jsx: true } },
199
- },
200
- plugins: { rule: { rules: Object.fromEntries(who.map((one) => [one.id, rules[one.id] as Rule.RuleModule])) } },
201
- rules: Object.fromEntries(who.map((one) => ['rule/' + one.id, 'error'])),
202
- },
203
- ] as unknown as Parameters<LinterType['verify']>[1]
204
-
205
- const failed = (thrown: unknown): string => 'could not run on it: ' + (thrown instanceof Error ? thrown.message : String(thrown))
206
-
207
- const parseable = (source: string, at: string, parser: unknown): ((said: string) => string) =>
208
- WRAPPINGS.find((wrap) => !new Linter().verify(wrap(source), judged([], {}, parser), at).some((one) => one.fatal === true)) ??
209
- ((said: string): string => said)
210
-
211
- function refusals(
212
- scenario: Scenario,
213
- source: string,
214
- judges: readonly Constraint[],
215
- rules: Readonly<Record<string, Rule.RuleModule>>,
216
- parser: unknown,
217
- slopnators: ReadonlyMap<string, Slopnator>,
218
- ): ReadonlyMap<string, ReadonlySet<string>> {
219
- const found = new Map<string, Set<string>>()
220
- const note = (who: string, said: string): void => {
221
- const held = found.get(who) ?? new Set<string>()
222
- held.add(said)
223
- found.set(who, held)
224
- }
225
- const walking = judges.filter((one) => {
226
- const held = slopnators.get(slopnatorName(one.id))
227
- return held !== undefined && visits(held) && rules[one.id] !== undefined
228
- })
229
- const linted = (who: readonly Constraint[]): void => {
230
- const messages = new Linter().verify(source, judged(who, rules, parser), scenario.at)
231
- if (messages.some((one) => one.fatal === true)) return
232
- for (const one of messages) {
233
- const by = String(one.ruleId ?? '').replace('rule/', '')
234
- if (by !== '') note(by, one.message)
235
- }
236
- }
237
-
238
- const files = scenario.files.map((one) => (one.at === scenario.at ? { at: one.at, source } : one))
239
- answeredBy(standing(ROOT, new Map<string, string>(files.map((one) => [resolve(ROOT, one.at), one.source]))))
240
- forget()
241
- try {
242
- try {
243
- linted(walking)
244
- } catch {
245
- for (const one of walking) {
246
- try {
247
- linted([one])
248
- } catch (thrown) {
249
- note(one.id, failed(thrown))
250
- }
251
- }
252
- }
253
- for (const one of judges) {
254
- const held = slopnators.get(slopnatorName(one.id))
255
- if (held === undefined) continue
256
- if (visits(held)) continue
257
- try {
258
- if (sweeps(held)) {
259
- const said = held.holds(undefined, { filename: scenario.at, text: source, ancestors: () => [], comments: () => [] })
260
- if (said !== false && said !== NOT_WRITTEN) note(one.id, typeof said === 'string' ? said : slopnatorName(one.id))
261
- continue
262
- }
263
- if (!reads(held)) for (const said of held.checks(ROOT)) note(one.id, said)
264
- } catch (thrown) {
265
- note(one.id, failed(thrown))
266
- }
267
- }
268
- } finally {
269
- answeredBy(undefined)
270
- forget()
271
- }
272
- return found
273
- }
274
-
275
- export function crossed(
276
- trials: readonly Trial[],
277
- all: readonly Constraint[],
278
- rules: Readonly<Record<string, Rule.RuleModule>>,
279
- parser: unknown,
280
- slopnators: ReadonlyMap<string, Slopnator> = new Map<string, Slopnator>(),
281
- spent: Map<string, number> = new Map<string, number>(),
282
- ): readonly Outcome[] {
283
- const found: Outcome[] = []
284
- const blanks = new Map<string, ReadonlyMap<string, ReadonlySet<string>>>()
285
-
286
- for (const trial of trials) {
287
- if (trial.verdict.reports) continue
288
- const target = trial.scenario.files.find((one) => one.at === trial.scenario.at)
289
- if (target === undefined) continue
290
-
291
- const judges = all.filter((one) => governs(one, trial.scenario.at))
292
- if (!judges.some((one) => one.id !== trial.rule)) continue
293
- const began = Date.now()
294
-
295
- const wrap = parseable(target.source, trial.scenario.at, parser)
296
- const emptied = JSON.stringify([wrap(''), trial.scenario.files.map((one) => (one.at === trial.scenario.at ? { at: one.at, source: '' } : one))])
297
- const blank = blanks.get(emptied) ?? refusals(trial.scenario, wrap(''), judges, rules, parser, slopnators)
298
- blanks.set(emptied, blank)
299
- const said = refusals(trial.scenario, wrap(target.source), judges, rules, parser, slopnators)
300
-
301
- spent.set(trial.rule, (spent.get(trial.rule) ?? 0) + Date.now() - began)
302
-
303
- for (const [who, messages] of said) {
304
- if (who === trial.rule) continue
305
- for (const message of messages) {
306
- if (blank.get(who)?.has(message) === true) continue
307
- found.push({
308
- id: trial.id + '_under_' + who.replace(/-/g, '_'),
309
- rule: trial.rule,
310
- passed: false,
311
- parsed: true,
312
- failures: [
313
- 'the form it recommends is refused by ' + who + ', so this law tells somebody to write what another law bans. ' +
314
- who + ' said: ' + (message.split('Found: ').pop() ?? message),
315
- ],
316
- })
317
- }
318
- }
319
- }
320
-
321
- return found
322
- }
323
-
324
- export function runTrials(
325
- trials: readonly Trial[],
326
- all: readonly Constraint[],
327
- rules: Readonly<Record<string, Rule.RuleModule>>,
328
- parser: unknown,
329
- slopnators?: ReadonlyMap<string, Slopnator>,
330
- ): Run {
331
- const byName = new Map(all.map((rule) => [rule.id, rule]))
332
- const spent = new Map<string, number>()
333
- const outcomes = trials.map((trial): Outcome => {
334
- const rule = byName.get(trial.rule)
335
- const module = rules[trial.rule]
336
- if (rule === undefined || module === undefined) {
337
- return {
338
- id: trial.id,
339
- rule: trial.rule,
340
- passed: false,
341
- parsed: false,
342
- failures: [`the rule "${trial.rule}" has no slopnator on disk, so this trial is about nothing — the file to write is the one its row names in \`at\``],
343
- }
344
- }
345
- const began = Date.now()
346
- const outcome = runTrial(trial, rule, module, parser, slopnators?.get(slopnatorName(rule.id)))
347
- spent.set(trial.rule, (spent.get(trial.rule) ?? 0) + Date.now() - began)
348
- return outcome
349
- })
350
-
351
- return {
352
- outcomes,
353
- passed: outcomes.filter((outcome) => outcome.passed).length,
354
- failed: outcomes.filter((outcome) => !outcome.passed).length,
355
- measured: outcomes.filter((outcome) => outcome.parsed).length,
356
- spent,
357
- }
358
- }
359
-