@cmodjs/rulebook 0.8.0 → 0.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmodjs/rulebook",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "Checks every file, reply, tool call, and commit against your rules and linters before it lands.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/api.ts CHANGED
@@ -52,6 +52,7 @@ export type Context = {
52
52
  readonly root: string
53
53
  readonly filename: string
54
54
  readonly sourceCode: SourceCode
55
+ readonly previous?: SourceCode
55
56
  readonly call?: Call
56
57
  report(problem: Problem): void
57
58
  }
package/src/check.ts CHANGED
@@ -94,10 +94,12 @@ export async function checkFile(settings: Settings, rules: readonly LoadedRule[]
94
94
  const mayPin = text.includes('rulebook-expect')
95
95
  const claudeMd = isDoc || probe || (mayPin && written.some((line) => PIN.test(line))) ? readClaudeMd(text) : undefined
96
96
  const memory = isDoc ? claudeMd : undefined
97
+ const before = options.previous === undefined || !isDoc ? options.previous : readClaudeMd(options.previous).lines.join('\n')
97
98
  const prompt: CheckedFile = {
98
99
  root: settings.root,
99
100
  path,
100
101
  ...(memory === undefined ? { text, lines: written } : { text: memory.lines.join('\n'), lines: memory.lines }),
102
+ ...(before === undefined ? {} : { previous: { text: before, lines: before.split('\n') } }),
101
103
  ...(path === COMMAND ? { shell: parseShell(text) } : {}),
102
104
  ...(options.call === undefined ? {} : { call: options.call }),
103
105
  }
@@ -125,7 +127,7 @@ export async function checkFile(settings: Settings, rules: readonly LoadedRule[]
125
127
  const failures = [...runs.flatMap((run) => ('failure' in run ? [{ rule: run.rule.id, reason: run.failure }] : [])), ...lints.flatMap((lint) => lint.failures)]
126
128
  const found = [...runs.flatMap((run) => ('found' in run ? run.found.map(({ line, message, isWhole }) => ({ line, message: message ?? run.rule.message ?? run.rule.description, rule: run.rule.id, level: run.rule.level === 'off' ? 'error' as const : run.rule.level, ...(isWhole === true ? { isWhole } : {}) })) : [])), ...lints.flatMap((lint) => lint.flags)]
127
129
  const unscoped = new Set(rules.filter((rule) => !inScope(rule, options)).map(({ id }) => id))
128
- const flagged: readonly Flag[] = probe ? probeFindings(found, pins.filter(({ rule }) => !unscoped.has(rule))) : [...found, ...pins.map((pin) => ({ rule: 'rulebook/pins', line: pin.line, level: 'error' as const, message: 'A rulebook-expect pin works only in a probe file. Remove it.' }))]
130
+ const flagged: readonly Flag[] = probe ? probeFindings(found, pins.filter(({ rule }) => !unscoped.has(rule)), written.findLastIndex((line) => line.trim() !== '') + 1) :[...found, ...pins.map((pin) => ({ rule: 'rulebook/pins', line: pin.line, level: 'error' as const, message: 'A rulebook-expect pin works only in a probe file. Remove it.' }))]
129
131
 
130
132
  return {
131
133
  path,
@@ -299,17 +301,18 @@ export const noSuppressionsRule: StaticRule = {
299
301
 
300
302
  type Flag = Omit<Reported, 'isExisting' | 'quote'> & { readonly isWhole?: true }
301
303
 
302
- function probeFindings(found: readonly Flag[], pins: readonly Pin[]): readonly Flag[] {
304
+ function probeFindings(found: readonly Flag[], pins: readonly Pin[], lastLine: number): readonly Flag[] {
303
305
  const open = [...pins]
306
+ const isWholePin = (pin: Pin): boolean => pin.target > lastLine
304
307
  const unexpected = found.filter((finding) => {
305
- const index = open.findIndex((pin) => pin.rule === finding.rule && pin.target === finding.line)
308
+ const index = open.findIndex((pin) => pin.rule === finding.rule && (finding.isWhole === true ? isWholePin(pin) : !isWholePin(pin) && pin.target === finding.line))
306
309
  if (index === -1) return true
307
310
  open.splice(index, 1)
308
311
  return false
309
312
  })
310
313
  return [
311
314
  ...unexpected.map((finding) => ({ ...finding, level: 'error' as const, message: `${finding.message} (no pin expects this flag)` })),
312
- ...open.map((pin) => ({ rule: pin.rule, line: pin.line, level: 'error' as const, message: `The pin expects ${pin.rule} to flag line ${pin.target}, and it does not.` })),
315
+ ...open.map((pin) => ({ rule: pin.rule, line: pin.line, level: 'error' as const, message: `The pin expects ${pin.rule} to flag ${isWholePin(pin) ? 'the whole file' : `line ${pin.target}`}, and it does not.` })),
313
316
  ]
314
317
  }
315
318
 
package/src/commit.ts CHANGED
@@ -117,10 +117,10 @@ function withAddedErrors(reports: readonly FileReport[], added: readonly FileRep
117
117
  return [...merged.values()]
118
118
  }
119
119
 
120
- async function committed(root: string, path: string): Promise<string> {
120
+ async function committed(root: string, path: string): Promise<string | undefined> {
121
121
  const show = Bun.spawn(['git', 'show', `HEAD:${path}`], { cwd: root, env: process.env, stdout: 'pipe', stderr: 'ignore' })
122
122
  const [text, code] = await Promise.all([new Response(show.stdout).text(), show.exited])
123
- return code === 0 ? text : ''
123
+ return code === 0 ? text : undefined
124
124
  }
125
125
 
126
126
  async function hasHead(root: string): Promise<boolean> {
package/src/edit.ts CHANGED
@@ -40,7 +40,7 @@ export async function checkEdit(call: Call, root: string): Promise<EditAnswer> {
40
40
  const settings = await loadSettings(dirname(file), root)
41
41
  const { rules } = await loadRules(settings)
42
42
  const isWhole = next === ''
43
- const report = await checkFile(settings, rules, file, next, { scope: 'edit', previous: isWhole ? '' : previous ?? '', imported: () => loadImportedFiles(root), call })
43
+ const report = await checkFile(settings, rules, file, next, { scope: 'edit', previous: isWhole ? '' : previous,imported: () => loadImportedFiles(root), call })
44
44
  return editAnswer(report)
45
45
  }
46
46
 
package/src/rules.ts CHANGED
@@ -14,12 +14,12 @@ export type Level = 'error' | 'warning'
14
14
 
15
15
  export type Finding = { readonly line: number; readonly message?: string; readonly isWhole?: true }
16
16
 
17
- export type CheckedFile = {
17
+ type Text = { readonly text: string; readonly lines: readonly string[]; readonly shell?: ParsedShell }
18
+
19
+ export type CheckedFile = Text & {
18
20
  readonly root: string
19
21
  readonly path: string
20
- readonly text: string
21
- readonly lines: readonly string[]
22
- readonly shell?: ParsedShell
22
+ readonly previous?: Text
23
23
  readonly call?: Call
24
24
  }
25
25
 
@@ -131,6 +131,7 @@ async function visited(source: string, create: Rule['create'], file: CheckedFile
131
131
  root: file.root,
132
132
  filename: file.path.startsWith('@') ? file.path : resolve(file.root, file.path),
133
133
  sourceCode,
134
+ ...(file.previous === undefined ? {} : { previous: await sourceCodeOf(file.previous, language) }),
134
135
  ...(file.call === undefined ? {} : { call: file.call }),
135
136
  report: (problem) => {
136
137
  found.push('loc' in problem ? { line: problem.loc.start.line } : findingOf(problem.node))
@@ -161,9 +162,9 @@ function isShell(node: Node): node is Shell {
161
162
  return 'commands' in node
162
163
  }
163
164
 
164
- const sourceCodes = new WeakMap<CheckedFile, Promise<SourceCode>>()
165
+ const sourceCodes = new WeakMap<Text, Promise<SourceCode>>()
165
166
 
166
- function sourceCodeOf(file: CheckedFile, language: string | undefined): Promise<SourceCode> {
167
+ function sourceCodeOf(file: Text, language: string | undefined): Promise<SourceCode> {
167
168
  const known = sourceCodes.get(file)
168
169
  if (known !== undefined) return known
169
170
  const sourceCode = parsed(file, language)
@@ -171,7 +172,7 @@ function sourceCodeOf(file: CheckedFile, language: string | undefined): Promise<
171
172
  return sourceCode
172
173
  }
173
174
 
174
- async function parsed({ text, lines, shell }: CheckedFile, language: string | undefined): Promise<SourceCode> {
175
+ async function parsed({ text, lines, shell }: Text, language: string | undefined): Promise<SourceCode> {
175
176
  if (shell !== undefined) return { text, lines, ast: { commands: commandsOf(shell, lines), reads: shell.reads, writes: shell.writes, fetches: shell.fetches, isFullyParsed: shell.isFullyParsed } }
176
177
  if (language === undefined) return { text, lines, ast: { text } }
177
178
  const { parse } = await (astGrep ??= loadAstGrep())