stopslop 1.1.0 → 1.2.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/COMPATIBILITY.md CHANGED
@@ -13,6 +13,7 @@ changes are called out in the changelog.
13
13
  | SARIF 2.1.0 report | Stable | New rules and optional properties may be added; existing required fields remain compatible. |
14
14
  | `stopslop.json` schema | Stable | New settings may be added; configuration valid in a 1.x release stays valid in every later 1.x release. |
15
15
  | Programmatic Node.js API | Experimental | Exported functions and TypeScript types may change in a minor release. |
16
+ | Agent plugins (`stopslop` skill and stop hook for Claude Code, Codex, Cursor, Gemini CLI) | Experimental | When the hook runs, what it reports, and the skill's instructions may change in a minor release. |
16
17
 
17
18
  Findings and scores are not a compatibility surface. Detector fixes, threshold
18
19
  calibration, and Knip or Clone Alert updates can change what a release reports
package/README.md CHANGED
@@ -177,18 +177,54 @@ For GitHub Code Scanning, emit SARIF before propagating the gate failure:
177
177
  npx stopslop . --base origin/main --format sarif > stopslop.sarif
178
178
  ```
179
179
 
180
+ Every rule in the SARIF report carries its help, so the alert says why the
181
+ finding matters and how to fix it, not only what failed.
182
+
180
183
  See [CI and automation](docs/ci-and-automation.md) for a complete workflow that
181
184
  uploads SARIF and still preserves StopSlop's exit code.
182
185
 
183
- ## Send the findings back to the agent
184
-
185
- The JSON report is stable and versioned. Feed it into the next coding-agent turn
186
- instead of paraphrasing terminal output:
186
+ ## Catch slop while the agent is still working
187
+
188
+ A check that first runs on the pull request reports code nobody remembers
189
+ writing. StopSlop can run inside the coding session instead, while the code is
190
+ still in front of the agent.
191
+
192
+ This repository is a plugin for Claude Code, Codex, and Cursor and an extension
193
+ for Gemini CLI. Each agent gets the `stopslop` skill, which covers how to run the
194
+ check, read the report, and fix each kind of finding without gaming it: no
195
+ raised thresholds, no ignore globs, no baseline edits on the agent's own
196
+ initiative. Where the agent supports it, the install also adds a stop hook.
197
+
198
+ | Agent | Install | Stop hook |
199
+ | --- | --- | --- |
200
+ | Claude Code | `/plugin marketplace add BaryshevRS/stopslop`<br>`/plugin install stopslop@stopslop` | included |
201
+ | Codex | `codex plugin marketplace add BaryshevRS/stopslop`<br>`codex plugin add stopslop@stopslop` | included; trust it when Codex asks at session start |
202
+ | Cursor | Customize → import `BaryshevRS/stopslop` from a GitHub repository | included |
203
+ | Gemini CLI | `gemini extensions install https://github.com/BaryshevRS/stopslop` | [in project settings](docs/ci-and-automation.md#commit-the-hook-to-the-project) |
204
+ | Others | `npx skills add BaryshevRS/stopslop` (skill only) | — |
205
+
206
+ Before the agent ends a turn that changed JS/TS source, the hook checks the
207
+ uncommitted changes against `HEAD`, or against `.stopslop-baseline.json` when
208
+ the project has one, and hands any new findings back with how to fix each kind.
209
+ Legacy never blocks a turn, a turn that changed no source costs one
210
+ `git status`, and the agent gets one nudge per turn: accepting a finding stays
211
+ your decision. The hook runs the project's pinned `stopslop` from `node_modules`
212
+ and falls back to `npx stopslop@1`.
213
+
214
+ The hook also ships in the npm package, so a project that pins `stopslop` can
215
+ commit it to its own agent settings and give it to every contributor; see
216
+ [Commit the hook to the project](docs/ci-and-automation.md#commit-the-hook-to-the-project).
217
+
218
+ **Any agent or script.** The JSON report is stable and versioned. Feed it into
219
+ the next turn instead of paraphrasing terminal output:
187
220
 
188
221
  ```bash
189
- npx stopslop . --json > slop-report.json
222
+ npx stopslop . --base HEAD --json > slop-report.json
190
223
  ```
191
224
 
225
+ Each finding carries `kind`, `file`, `line`, and `message`, and `rules` says for
226
+ each reported kind why it matters and what resolves it: the same text as
227
+ [docs/rules.md](docs/rules.md) and the rule help in GitHub Code Scanning.
192
228
  God-unit findings include member groups that serve as concrete refactoring
193
229
  boundaries. Clone findings include every occurrence. Baseline and Git-base runs
194
230
  retain the complete score while returning only new findings.
package/context7.json CHANGED
@@ -17,6 +17,8 @@
17
17
  "Use `--fast` only when intentionally skipping duplicate-code and dead-code analysis; it runs the AST checks for cognitive complexity and god units.",
18
18
  "The Node API is ESM and typed: call `resolveConfig()` before `analyze()`, or call `loadConfig()` to read stopslop.json. The slop score always describes the complete analyzed repository.",
19
19
  "Clone detection is powered by Clone Alert and dead-code analysis by Knip. Configure Knip only under the `knip` property in stopslop.json.",
20
- "Knip settings go under `knip` in stopslop.json; a project's own knip.json or package.json#knip is never read. Do not write one up front — run the analysis and follow the `config hints` it prints, which come from Knip and name what is missing."
20
+ "Knip settings go under `knip` in stopslop.json; a project's own knip.json or package.json#knip is never read. Do not write one up front — run the analysis and follow the `config hints` it prints, which come from Knip and name what is missing.",
21
+ "Every reported kind has help: the JSON report's `rules` block and SARIF rule help give `why` and `fix`, the same text as docs/rules.md. Fix the code; do not raise thresholds or edit the baseline to get a clear run.",
22
+ "The repo is a plugin for Claude Code, Codex and Cursor and a Gemini CLI extension: a stopslop skill plus a stop hook reporting findings the uncommitted changes added. Other agents: `npx skills add BaryshevRS/stopslop`."
21
23
  ]
22
24
  }
package/dist/cli.js CHANGED
@@ -162,6 +162,85 @@ function countByKind(findings) {
162
162
  for (const f of findings) counts[f.kind]++;
163
163
  return counts;
164
164
  }
165
+ //#endregion
166
+ //#region src/report/rules.ts
167
+ const RULES_DOC = "https://github.com/BaryshevRS/stopslop/blob/main/docs/rules.md";
168
+ const RULES = {
169
+ "cognitive-complexity": {
170
+ summary: "Function is harder to follow than the threshold allows",
171
+ why: "Cognitive complexity counts what a reader has to hold in mind to follow a function: branches, loops, nesting, and boolean chains (SonarSource specification). Above the threshold a function is hard to review and easy to break, and it keeps growing the same way: each new case lands beside the existing ones instead of in a place of its own.",
172
+ fix: "Return early instead of nesting, and move each independent decision into a function whose name says what it decides. Replace a long if/else or switch over one value with a lookup table. Cutting the body into arbitrary part1/part2 helpers moves the complexity without removing it.",
173
+ config: "cognitiveComplexity"
174
+ },
175
+ "god-module": {
176
+ summary: "Module mixes unrelated responsibilities or has outgrown one file",
177
+ why: "Its top-level definitions fall into groups that do not use each other, or the file is too large and too complex to be one unit. Every change touches a file that does several jobs, and new code keeps landing in it because it is the file already open.",
178
+ fix: "Move each responsibility group to its own module; `--details` and the JSON report list the groups found in the reference graph, the ready-made split boundaries. Members listed as holding cohesion together are shared helpers: move them where every group can import them. A size-axis finding has no clean boundaries, so split it by responsibility by hand.",
179
+ config: "godModule"
180
+ },
181
+ "god-class": {
182
+ summary: "Class mixes unrelated responsibilities or has outgrown one unit",
183
+ why: "Its methods fall into groups that do not share state or call each other, or the class is too large and too complex to be one unit. Every change risks the unrelated behaviour next to it, and the class cannot be tested one responsibility at a time.",
184
+ fix: "Extract each responsibility group into its own class or set of functions, and let the original delegate to them or disappear; `--details` and the JSON report list the groups. A size-axis finding has no clean boundaries, so split it by responsibility by hand.",
185
+ config: "godClass"
186
+ },
187
+ "multiple-exported-classes": {
188
+ summary: "File exports more than one class",
189
+ why: "Several exported classes in one file usually mean code was appended where it was convenient instead of placed where it belongs. Readers cannot find a class by its file name, and the file drifts toward a god module.",
190
+ fix: "Give each exported class its own file named after it. A class that only serves another can stay in the same file without being exported.",
191
+ config: "multipleExportedClasses"
192
+ },
193
+ "duplicate-code": {
194
+ summary: "Block of code is copied in more than one place",
195
+ why: "Copies drift. A bug fixed in one stays in the others, and the copies start to disagree about the same computation. It is usually cheaper to write a block again than to find the helper that already does it, which is how copies accumulate.",
196
+ fix: "Extract the block into one function and call it from every occurrence; the finding lists all of them. Where the copies differ on purpose, make the difference a parameter. Renaming variables does not hide a copy when `duplicates.ignoreIdentifiers` is on.",
197
+ config: "duplicates"
198
+ },
199
+ "unused-export": {
200
+ summary: "Export is not imported anywhere",
201
+ why: "Code nothing uses still has to be read, compiled, kept type-correct, and migrated. Code is added readily and deleted reluctantly, so unused exports only accumulate.",
202
+ fix: "Delete it, or drop the `export` keyword when the file uses it internally. If something Knip does not know about uses it, such as a framework convention or a public package entry, declare that entry under `knip` in stopslop.json instead.",
203
+ config: "deadCode.exports"
204
+ },
205
+ "unused-dependency": {
206
+ summary: "Dependency in package.json is not imported anywhere",
207
+ why: "An unused package still installs, still gets audited and updated, and still widens the supply chain the project trusts.",
208
+ fix: "Remove it from package.json. If it is used in a way Knip cannot see, such as a binary run from a script or a plugin loaded by name, list it in `ignoreDependencies` under `knip` in stopslop.json.",
209
+ config: "deadCode.dependencies"
210
+ },
211
+ "unused-file": {
212
+ summary: "File is not reachable from any entry point",
213
+ why: "Often a superseded version of a module, left next to the one that replaced it. Reported as info and not scored: the result is only as good as the entry points Knip knows.",
214
+ fix: "Resolve the config hints in the report first; a missing entry point makes live files look unreachable. Then delete the file.",
215
+ config: "deadCode.files"
216
+ },
217
+ "orphan-feature": {
218
+ summary: "Function or class is used only by its own test",
219
+ why: "No production code calls it; one test is the only thing that imports it. That is a feature written together with its test and never wired in, and the green test makes it look finished.",
220
+ fix: "Delete the symbol and its test together: deleting only the source breaks the test, and deleting only the test leaves dead code. If the feature is meant to exist, wire it into the production code that needs it.",
221
+ config: "deadCode.orphans"
222
+ }
223
+ };
224
+ function ruleHelp(kind) {
225
+ return {
226
+ ...RULES[kind],
227
+ helpUri: `${RULES_DOC}#${kind}`
228
+ };
229
+ }
230
+ /** Help for the kinds present in a set of findings, keyed by kind. */
231
+ function rulesFor(findings) {
232
+ const rules = {};
233
+ for (const { kind } of findings) rules[kind] ??= ruleHelp(kind);
234
+ return rules;
235
+ }
236
+ /** The rule's help as Markdown: the body of a SARIF rule help and of docs/rules.md. */
237
+ function ruleMarkdown(rule) {
238
+ return [
239
+ `**Why it matters.** ${rule.why}`,
240
+ `**How to fix.** ${rule.fix}`,
241
+ `**Tune or disable:** \`${rule.config}\` in \`stopslop.json\`.`
242
+ ].join("\n\n");
243
+ }
165
244
  const KINDS = [
166
245
  "god-module",
167
246
  "god-class",
@@ -209,6 +288,7 @@ function toJson(result) {
209
288
  },
210
289
  summary: summarize(result),
211
290
  findings: result.findings,
291
+ rules: rulesFor(result.findings),
212
292
  notes: result.notes,
213
293
  configHints: result.configHints,
214
294
  /** Where Knip settings are read from. Nowhere else is consulted. */
@@ -233,9 +313,17 @@ function toSarif(result) {
233
313
  for (const finding of result.findings) {
234
314
  if (seenRules.has(finding.kind)) continue;
235
315
  seenRules.add(finding.kind);
316
+ const rule = ruleHelp(finding.kind);
236
317
  rules.push({
237
318
  id: finding.kind,
238
- shortDescription: { text: describeKind(finding.kind) },
319
+ name: ruleName(finding.kind),
320
+ shortDescription: { text: rule.summary },
321
+ fullDescription: { text: rule.why },
322
+ help: {
323
+ text: `${rule.why}\n\n${rule.fix}\n\nTune or disable: ${rule.config} in stopslop.json.`,
324
+ markdown: ruleMarkdown(rule)
325
+ },
326
+ helpUri: rule.helpUri,
239
327
  defaultConfiguration: { level: sarifLevel(finding.severity) }
240
328
  });
241
329
  }
@@ -261,9 +349,9 @@ function toSarif(result) {
261
349
  function sarifLevel(severity) {
262
350
  return severity === "warn" ? "warning" : "note";
263
351
  }
264
- function describeKind(kind) {
265
- const description = kind.replaceAll("-", " ");
266
- return `${description.charAt(0).toUpperCase()}${description.slice(1)}`;
352
+ /** SARIF rule name, the identifier Code Scanning filters by: `god-class` → `GodClass`. */
353
+ function ruleName(kind) {
354
+ return kind.split("-").map((word) => `${word.charAt(0).toUpperCase()}${word.slice(1)}`).join("");
267
355
  }
268
356
  function locationsFor(root, finding) {
269
357
  const primaryUri = artifactUri(root, finding.file);
@@ -83,7 +83,121 @@ npx stopslop . --format json > slop-report.json
83
83
  The JSON schema version is included in the document. Findings carry stable
84
84
  `kind`, `file`, `symbol`, `line`, `message`, and optional structured `data`
85
85
  fields. God-unit data includes responsibility groups; duplicate-code data
86
- includes clone locations.
86
+ includes clone locations. `rules` holds, for each reported kind, its `summary`,
87
+ `why`, `fix`, the `config` setting that tunes it, and a `helpUri` into
88
+ [rules.md](rules.md), so the agent does not have to guess what a rule wants.
89
+
90
+ ## Run the check inside the agent session
91
+
92
+ The repository is a plugin for Claude Code, Codex, and Cursor and an extension
93
+ for Gemini CLI; the [README](../README.md#catch-slop-while-the-agent-is-still-working)
94
+ lists the install command for each. All of them get the `stopslop` skill. The
95
+ stop hook, `hooks/stop-gate.mjs`, runs when the agent is about to end a turn. It
96
+ asks Git whether any JS/TS source or `package.json` changed, and if something
97
+ did, it runs
98
+
99
+ ```bash
100
+ npx stopslop . --baseline .stopslop-baseline.json --format json # when the file exists
101
+ npx stopslop . --base HEAD --format json # otherwise
102
+ ```
103
+
104
+ and hands the new findings back to the agent with each kind's fix, which the
105
+ agent receives as its next instruction. It does not block on analysis errors,
106
+ and it nudges once per turn, so a finding you decide to keep cannot loop the
107
+ session. It runs the project's own `node_modules/stopslop` when installed,
108
+ otherwise `npx stopslop@1`.
109
+
110
+ | Agent | Hook event | Hook answer |
111
+ | --- | --- | --- |
112
+ | Claude Code, Codex | `Stop` | `decision: "block"` with the findings as `reason` |
113
+ | Gemini CLI | `AfterAgent` | the same |
114
+ | Cursor | `stop` (`--agent cursor`) | `followup_message` |
115
+
116
+ Codex runs plugin hooks only after you trust them; it asks at the start of the
117
+ first session after install.
118
+
119
+ ## Commit the hook to the project
120
+
121
+ `hooks/stop-gate.mjs` ships in the npm package. A project that pins `stopslop`
122
+ in `devDependencies` can commit the hook to its agent settings, so every
123
+ contributor gets it without installing a plugin. This is also the way to the
124
+ hook for Gemini CLI: an extension reads hooks only from `hooks/hooks.json`,
125
+ which in this repository holds the Claude Code and Codex hook.
126
+
127
+ Gemini CLI, `.gemini/settings.json`:
128
+
129
+ ```json
130
+ {
131
+ "hooks": {
132
+ "AfterAgent": [
133
+ {
134
+ "hooks": [
135
+ {
136
+ "type": "command",
137
+ "command": "node \"$GEMINI_PROJECT_DIR/node_modules/stopslop/hooks/stop-gate.mjs\"",
138
+ "timeout": 300000
139
+ }
140
+ ]
141
+ }
142
+ ]
143
+ }
144
+ }
145
+ ```
146
+
147
+ Claude Code, `.claude/settings.json`:
148
+
149
+ ```json
150
+ {
151
+ "hooks": {
152
+ "Stop": [
153
+ {
154
+ "hooks": [
155
+ {
156
+ "type": "command",
157
+ "command": "node \"$CLAUDE_PROJECT_DIR/node_modules/stopslop/hooks/stop-gate.mjs\"",
158
+ "timeout": 300
159
+ }
160
+ ]
161
+ }
162
+ ]
163
+ }
164
+ }
165
+ ```
166
+
167
+ Codex, `.codex/hooks.json`; Codex runs the hook in the session's working
168
+ directory:
169
+
170
+ ```json
171
+ {
172
+ "hooks": {
173
+ "Stop": [
174
+ {
175
+ "hooks": [
176
+ {
177
+ "type": "command",
178
+ "command": "node node_modules/stopslop/hooks/stop-gate.mjs",
179
+ "timeout": 300
180
+ }
181
+ ]
182
+ }
183
+ ]
184
+ }
185
+ }
186
+ ```
187
+
188
+ Cursor, `.cursor/hooks.json`; project hooks run from the project root:
189
+
190
+ ```json
191
+ {
192
+ "version": 1,
193
+ "hooks": {
194
+ "stop": [{ "command": "node node_modules/stopslop/hooks/stop-gate.mjs --agent cursor" }]
195
+ }
196
+ }
197
+ ```
198
+
199
+ Agents without a plugin system or hooks get the skill alone with
200
+ `npx skills add BaryshevRS/stopslop`.
87
201
 
88
202
  ## Generate a binary Shields badge
89
203
 
package/docs/rules.md ADDED
@@ -0,0 +1,102 @@
1
+ <!-- Generated from src/report/rules.ts by `pnpm rules:generate`. Do not edit by hand. -->
2
+
3
+ # Rules
4
+
5
+ Every StopSlop finding has a `kind`. This page says, for each one, why it is a
6
+ finding and what resolves it. The same text is the rule help in GitHub Code
7
+ Scanning (`--format sarif`) and the `rules` block of `--format json`, so a
8
+ reviewer and a coding agent read the same instructions.
9
+
10
+ Every setting named below lives in `stopslop.json`; see the
11
+ [configuration reference](configuration.md). Setting a check to `false`
12
+ disables it.
13
+
14
+ ## cognitive-complexity
15
+
16
+ Function is harder to follow than the threshold allows.
17
+
18
+ **Why it matters.** Cognitive complexity counts what a reader has to hold in mind to follow a function: branches, loops, nesting, and boolean chains (SonarSource specification). Above the threshold a function is hard to review and easy to break, and it keeps growing the same way: each new case lands beside the existing ones instead of in a place of its own.
19
+
20
+ **How to fix.** Return early instead of nesting, and move each independent decision into a function whose name says what it decides. Replace a long if/else or switch over one value with a lookup table. Cutting the body into arbitrary part1/part2 helpers moves the complexity without removing it.
21
+
22
+ **Tune or disable:** `cognitiveComplexity` in `stopslop.json`.
23
+
24
+ ## god-module
25
+
26
+ Module mixes unrelated responsibilities or has outgrown one file.
27
+
28
+ **Why it matters.** Its top-level definitions fall into groups that do not use each other, or the file is too large and too complex to be one unit. Every change touches a file that does several jobs, and new code keeps landing in it because it is the file already open.
29
+
30
+ **How to fix.** Move each responsibility group to its own module; `--details` and the JSON report list the groups found in the reference graph, the ready-made split boundaries. Members listed as holding cohesion together are shared helpers: move them where every group can import them. A size-axis finding has no clean boundaries, so split it by responsibility by hand.
31
+
32
+ **Tune or disable:** `godModule` in `stopslop.json`.
33
+
34
+ ## god-class
35
+
36
+ Class mixes unrelated responsibilities or has outgrown one unit.
37
+
38
+ **Why it matters.** Its methods fall into groups that do not share state or call each other, or the class is too large and too complex to be one unit. Every change risks the unrelated behaviour next to it, and the class cannot be tested one responsibility at a time.
39
+
40
+ **How to fix.** Extract each responsibility group into its own class or set of functions, and let the original delegate to them or disappear; `--details` and the JSON report list the groups. A size-axis finding has no clean boundaries, so split it by responsibility by hand.
41
+
42
+ **Tune or disable:** `godClass` in `stopslop.json`.
43
+
44
+ ## multiple-exported-classes
45
+
46
+ File exports more than one class.
47
+
48
+ **Why it matters.** Several exported classes in one file usually mean code was appended where it was convenient instead of placed where it belongs. Readers cannot find a class by its file name, and the file drifts toward a god module.
49
+
50
+ **How to fix.** Give each exported class its own file named after it. A class that only serves another can stay in the same file without being exported.
51
+
52
+ **Tune or disable:** `multipleExportedClasses` in `stopslop.json`.
53
+
54
+ ## duplicate-code
55
+
56
+ Block of code is copied in more than one place.
57
+
58
+ **Why it matters.** Copies drift. A bug fixed in one stays in the others, and the copies start to disagree about the same computation. It is usually cheaper to write a block again than to find the helper that already does it, which is how copies accumulate.
59
+
60
+ **How to fix.** Extract the block into one function and call it from every occurrence; the finding lists all of them. Where the copies differ on purpose, make the difference a parameter. Renaming variables does not hide a copy when `duplicates.ignoreIdentifiers` is on.
61
+
62
+ **Tune or disable:** `duplicates` in `stopslop.json`.
63
+
64
+ ## unused-export
65
+
66
+ Export is not imported anywhere.
67
+
68
+ **Why it matters.** Code nothing uses still has to be read, compiled, kept type-correct, and migrated. Code is added readily and deleted reluctantly, so unused exports only accumulate.
69
+
70
+ **How to fix.** Delete it, or drop the `export` keyword when the file uses it internally. If something Knip does not know about uses it, such as a framework convention or a public package entry, declare that entry under `knip` in stopslop.json instead.
71
+
72
+ **Tune or disable:** `deadCode.exports` in `stopslop.json`.
73
+
74
+ ## unused-dependency
75
+
76
+ Dependency in package.json is not imported anywhere.
77
+
78
+ **Why it matters.** An unused package still installs, still gets audited and updated, and still widens the supply chain the project trusts.
79
+
80
+ **How to fix.** Remove it from package.json. If it is used in a way Knip cannot see, such as a binary run from a script or a plugin loaded by name, list it in `ignoreDependencies` under `knip` in stopslop.json.
81
+
82
+ **Tune or disable:** `deadCode.dependencies` in `stopslop.json`.
83
+
84
+ ## unused-file
85
+
86
+ File is not reachable from any entry point.
87
+
88
+ **Why it matters.** Often a superseded version of a module, left next to the one that replaced it. Reported as info and not scored: the result is only as good as the entry points Knip knows.
89
+
90
+ **How to fix.** Resolve the config hints in the report first; a missing entry point makes live files look unreachable. Then delete the file.
91
+
92
+ **Tune or disable:** `deadCode.files` in `stopslop.json`.
93
+
94
+ ## orphan-feature
95
+
96
+ Function or class is used only by its own test.
97
+
98
+ **Why it matters.** No production code calls it; one test is the only thing that imports it. That is a feature written together with its test and never wired in, and the green test makes it look finished.
99
+
100
+ **How to fix.** Delete the symbol and its test together: deleting only the source breaks the test, and deleting only the test leaves dead code. If the feature is meant to exist, wire it into the production code that needs it.
101
+
102
+ **Tune or disable:** `deadCode.orphans` in `stopslop.json`.
@@ -0,0 +1,156 @@
1
+ #!/usr/bin/env node
2
+ // Coding-agent hook, run when the agent is about to end its turn. Check whether
3
+ // the uncommitted changes added slop, and if they did, hand the findings back
4
+ // while the code is still in front of the agent rather than in a review later.
5
+ //
6
+ // It reports only what the changes added — findings absent from
7
+ // .stopslop-baseline.json when the project gates on one, otherwise absent from
8
+ // HEAD — so legacy never blocks a turn. Turns that changed no JS/TS source cost
9
+ // one `git status`. Analysis errors never block: the gate is CI's job, this
10
+ // hook is feedback.
11
+ //
12
+ // node stop-gate.mjs Claude Code (Stop), Codex (Stop), Gemini CLI (AfterAgent)
13
+ // node stop-gate.mjs --agent cursor Cursor (stop)
14
+
15
+ import { spawnSync } from 'node:child_process';
16
+ import { existsSync, readFileSync } from 'node:fs';
17
+ import { join } from 'node:path';
18
+
19
+ const BASELINE = '.stopslop-baseline.json';
20
+ const RULES_DOC = 'https://github.com/BaryshevRS/stopslop/blob/main/docs/rules.md';
21
+ const SOURCE = /\.[cm]?[jt]sx?$|(?:^|\/)package\.json$/;
22
+ /** Findings listed in full; the rest are counted. Keeps the feedback short. */
23
+ const MAX_LISTED = 20;
24
+
25
+ /** How each agent hands the hook its turn and takes the feedback back. */
26
+ const AGENTS = {
27
+ // Claude Code, Codex, and Gemini CLI share one protocol: the project in `cwd`,
28
+ // a flag set once a hook already sent the agent back, and a block decision
29
+ // whose reason the agent receives as its next instruction. Codex rejects any
30
+ // other output field, so this is the whole answer.
31
+ default: {
32
+ root: (input) => input.cwd || process.cwd(),
33
+ resumed: (input) => input.stop_hook_active === true,
34
+ answer: (feedback) => ({ decision: 'block', reason: feedback }),
35
+ },
36
+ // Cursor runs plugin hooks from the plugin directory and names the project in
37
+ // the environment. Only a turn that completed is checked: an aborted one was
38
+ // the user's call, a failed one has nothing finished to review.
39
+ cursor: {
40
+ root: (input) => process.env.CURSOR_PROJECT_DIR ?? input.workspace_roots?.[0],
41
+ resumed: (input) => input.status !== 'completed' || input.loop_count > 0,
42
+ answer: (feedback) => ({ followup_message: feedback }),
43
+ },
44
+ };
45
+
46
+ /** The feedback for the agent, or undefined to let it end the turn. */
47
+ function gate(root) {
48
+ if (!changesSource(root)) return undefined;
49
+
50
+ const mode = existsSync(join(root, BASELINE))
51
+ ? { args: ['--baseline', BASELINE], what: `not in ${BASELINE}` }
52
+ : { args: ['--base', 'HEAD'], what: 'added by the uncommitted changes' };
53
+ const { status, stdout } = runStopslop(root, ['.', ...mode.args, '--format', 'json']);
54
+ if (status !== 1) return undefined;
55
+
56
+ let report;
57
+ try {
58
+ report = JSON.parse(stdout);
59
+ } catch {
60
+ return undefined;
61
+ }
62
+ if (!Array.isArray(report.findings) || report.findings.length === 0) return undefined;
63
+ return feedback(report, mode);
64
+ }
65
+
66
+ /** True when Git shows a changed or new JS/TS source file or package.json. */
67
+ function changesSource(root) {
68
+ const status = spawnSync('git', ['status', '--porcelain', '-z', '--untracked-files=all'], {
69
+ cwd: root,
70
+ encoding: 'utf8',
71
+ });
72
+ if (status.status !== 0) return false; // not a Git work tree: nothing to compare with
73
+ // Each entry is "XY path"; a rename adds its old path as the next entry. Only
74
+ // the extension matters, so the status prefix needs no parsing.
75
+ return status.stdout.split('\0').some((entry) => SOURCE.test(entry));
76
+ }
77
+
78
+ function runStopslop(root, args) {
79
+ // The project's own pinned version first — the one its CI runs.
80
+ const local = join(root, 'node_modules', 'stopslop', 'dist', 'cli.js');
81
+ const [command, prefix] = existsSync(local)
82
+ ? [process.execPath, [local]]
83
+ : ['npx', ['--yes', 'stopslop@1']];
84
+ const result = spawnSync(command, [...prefix, ...args], {
85
+ cwd: root,
86
+ encoding: 'utf8',
87
+ env: { ...process.env, NO_COLOR: '1' },
88
+ maxBuffer: 64 * 1024 * 1024,
89
+ shell: command === 'npx' && process.platform === 'win32',
90
+ timeout: 280_000,
91
+ });
92
+ return { status: result.status, stdout: result.stdout ?? '' };
93
+ }
94
+
95
+ function feedback(report, mode) {
96
+ const { findings } = report;
97
+ const rules = report.rules ?? {};
98
+ const command = `npx stopslop . ${mode.args.join(' ')}`;
99
+ const lines = [`StopSlop: ${plural(findings.length, 'finding')} ${mode.what} (\`${command}\`).`];
100
+
101
+ const byKind = new Map();
102
+ for (const finding of findings.slice(0, MAX_LISTED)) {
103
+ if (!byKind.has(finding.kind)) byKind.set(finding.kind, []);
104
+ byKind.get(finding.kind).push(finding);
105
+ }
106
+ for (const [kind, group] of byKind) {
107
+ const rule = rules[kind];
108
+ lines.push('', rule ? `${kind} — ${rule.summary}` : kind);
109
+ for (const finding of group) {
110
+ lines.push(` ${finding.file}:${finding.line} ${finding.message}${alsoAt(finding)}`);
111
+ }
112
+ if (rule) lines.push(` How to fix: ${rule.fix}`);
113
+ }
114
+ if (findings.length > MAX_LISTED) {
115
+ lines.push('', `${findings.length - MAX_LISTED} more not listed; \`${command} --json\` has all.`);
116
+ }
117
+
118
+ lines.push(
119
+ '',
120
+ 'Fix these before ending the turn. If one should stay, tell the user which and why: ' +
121
+ `accepting it into the reviewed ${BASELINE} is their decision, and raised thresholds ` +
122
+ 'or ignore globs are not a fix.',
123
+ `Rule reference: ${RULES_DOC}`,
124
+ );
125
+ return lines.join('\n');
126
+ }
127
+
128
+ /** Other places a cloned block occurs: the finding's own line is the first. */
129
+ function alsoAt(finding) {
130
+ const occurrences = finding.data?.occurrences;
131
+ if (!Array.isArray(occurrences) || occurrences.length < 2) return '';
132
+ const rest = occurrences.slice(1);
133
+ const shown = rest.slice(0, 2).map((o) => `${o.file}:${o.startLine}-${o.endLine}`);
134
+ const more = rest.length > shown.length ? ` +${rest.length - shown.length} more` : '';
135
+ return ` (also ${shown.join(', ')}${more})`;
136
+ }
137
+
138
+ function plural(n, word) {
139
+ return `${n} ${word}${n === 1 ? '' : 's'}`;
140
+ }
141
+
142
+ const flag = process.argv.indexOf('--agent');
143
+ const agent = AGENTS[flag === -1 ? 'default' : process.argv[flag + 1]] ?? AGENTS.default;
144
+ let input = {};
145
+ try {
146
+ input = JSON.parse(readFileSync(0, 'utf8'));
147
+ } catch {
148
+ // No readable input: behave as a turn with nothing to check.
149
+ }
150
+ // Already sent back once this turn: one nudge, so a finding the user decides to
151
+ // keep cannot loop the session.
152
+ if (!agent.resumed(input)) {
153
+ const root = agent.root(input);
154
+ const context = typeof root === 'string' && root !== '' ? gate(root) : undefined;
155
+ if (context !== undefined) process.stdout.write(`${JSON.stringify(agent.answer(context))}\n`);
156
+ }
package/llms.txt CHANGED
@@ -6,11 +6,14 @@ Run without installing: `npx stopslop .`. Exit codes are `0` for a clear gate, `
6
6
 
7
7
  Dead-code settings live under `knip` in `stopslop.json` — a project's own `knip.json` or `package.json#knip` is never read. Nothing needs to be written up front: the run prints `config hints` from Knip naming the workspace or entry point that is missing, and dead code stays out of the score until they are resolved.
8
8
 
9
+ Coding agents: this repository is a plugin for Claude Code (`/plugin marketplace add BaryshevRS/stopslop`), Codex (`codex plugin marketplace add BaryshevRS/stopslop`), and Cursor, and an extension for Gemini CLI. It adds the `stopslop` skill and a stop hook that hands the agent what its uncommitted changes added before it ends a turn. Other agents get the skill with `npx skills add BaryshevRS/stopslop`.
10
+
9
11
  ## Start here
10
12
 
11
13
  - [Overview, installation, CLI and badge](https://raw.githubusercontent.com/BaryshevRS/stopslop/main/README.md)
12
14
  - [Configuration reference](https://raw.githubusercontent.com/BaryshevRS/stopslop/main/docs/configuration.md)
13
15
  - [CI baselines, Git comparison, SARIF and agent reports](https://raw.githubusercontent.com/BaryshevRS/stopslop/main/docs/ci-and-automation.md)
16
+ - [What each finding means and how to fix it](https://raw.githubusercontent.com/BaryshevRS/stopslop/main/docs/rules.md)
14
17
  - [Typed Node.js API](https://raw.githubusercontent.com/BaryshevRS/stopslop/main/docs/node-api.md)
15
18
 
16
19
  ## How it works
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stopslop",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Static analysis for AI-generated JavaScript and TypeScript: find cognitive complexity, god classes/modules, duplicate code, dead code and orphan features.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -29,6 +29,7 @@
29
29
  "files": [
30
30
  "dist/**/*",
31
31
  "docs/**/*.md",
32
+ "hooks/stop-gate.mjs",
32
33
  "README.md",
33
34
  "COMPATIBILITY.md",
34
35
  "LICENSE",
@@ -44,7 +45,7 @@
44
45
  "access": "public"
45
46
  },
46
47
  "scripts": {
47
- "prebuild": "pnpm schema:check",
48
+ "prebuild": "pnpm schema:check && pnpm rules:check",
48
49
  "build": "tsdown",
49
50
  "dev": "tsdown --watch",
50
51
  "test": "vitest run",
@@ -59,6 +60,8 @@
59
60
  "typecheck": "tsc --noEmit",
60
61
  "schema:generate": "tsx scripts/generate-config-schema.ts",
61
62
  "schema:check": "tsx scripts/generate-config-schema.ts --check",
63
+ "rules:generate": "tsx scripts/generate-rules-doc.ts",
64
+ "rules:check": "tsx scripts/generate-rules-doc.ts --check",
62
65
  "commitlint": "commitlint",
63
66
  "prepare": "simple-git-hooks 1>&2"
64
67
  },