@blundergoat/gruff-ts 0.1.1 → 0.3.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/CHANGELOG.md +55 -19
- package/README.md +103 -35
- package/docs/README.md +2 -0
- package/docs/agent-hook.md +80 -0
- package/docs/ci-integration.md +16 -0
- package/docs/configuration.md +60 -3
- package/docs/dashboard.md +7 -0
- package/docs/output-formats.md +11 -1
- package/docs/philosophy.md +21 -0
- package/docs/releasing.md +12 -11
- package/docs/reports-and-ci.md +5 -2
- package/docs/rules.md +24 -15
- package/package.json +5 -5
- package/scripts/bump-version.sh +20 -4
- package/scripts/preflight-checks.sh +4 -41
- package/src/analyser.ts +47 -34
- package/src/blocks.ts +82 -75
- package/src/changed-regions.ts +390 -0
- package/src/check-ignore.ts +49 -0
- package/src/class-rules.ts +80 -33
- package/src/cli-program.ts +209 -51
- package/src/comment-rules.ts +4 -0
- package/src/config-load-error.ts +18 -0
- package/src/config-parse.ts +458 -0
- package/src/config-preservation.ts +64 -0
- package/src/config.ts +311 -428
- package/src/constants.ts +10 -2
- package/src/context-doc-rules.ts +2 -4
- package/src/dashboard.ts +8 -5
- package/src/dead-code-rules.ts +60 -14
- package/src/discovery.ts +88 -32
- package/src/doc-rules.ts +3 -3
- package/src/findings-helpers.ts +58 -5
- package/src/fixtures/rule-catalogue-security-doctrine.ts +28 -0
- package/src/init-config.ts +94 -31
- package/src/line-rules.ts +58 -4
- package/src/naming-pushers.ts +42 -7
- package/src/pillar-summary.ts +129 -0
- package/src/profiles.ts +232 -0
- package/src/project-config-rules.ts +5 -5
- package/src/project-rules.ts +2 -1
- package/src/report-html.ts +404 -0
- package/src/report-renderers.ts +210 -397
- package/src/rule-list.ts +85 -1
- package/src/rules.ts +28 -27
- package/src/safety-rules.ts +22 -7
- package/src/scoring.ts +78 -8
- package/src/security-flow-rules.ts +533 -0
- package/src/sensitive-data-rules.ts +138 -2
- package/src/test-block-rules.ts +140 -7
- package/src/test-fixtures.ts +59 -13
- package/src/types.ts +108 -5
package/docs/dashboard.md
CHANGED
|
@@ -22,6 +22,13 @@ The dashboard has no authentication and should stay bound to loopback unless the
|
|
|
22
22
|
network is trusted. The `/scan` endpoint analyses filesystem paths from request
|
|
23
23
|
parameters, so the bind address is the safety boundary.
|
|
24
24
|
|
|
25
|
+
## Gating threshold (none)
|
|
26
|
+
|
|
27
|
+
`dashboard` has no `--fail-on` flag and does not participate in the
|
|
28
|
+
`minimumSeverity:` config block - setting `minimumSeverity.dashboard:` in
|
|
29
|
+
`.gruff-ts.yaml` is rejected at config load. Whether the dashboard should gate
|
|
30
|
+
is a deferred design question; raise it as an issue if your workflow needs it.
|
|
31
|
+
|
|
25
32
|
## Polyglot Repos
|
|
26
33
|
|
|
27
34
|
`gruff-ts` defaults to port `8767`, `gruff-rs` defaults to `8766`, and Go, PHP,
|
package/docs/output-formats.md
CHANGED
|
@@ -14,12 +14,22 @@ Use `text` for local terminal scans:
|
|
|
14
14
|
|
|
15
15
|
## JSON
|
|
16
16
|
|
|
17
|
-
Use `json` for automation. JSON reports use `gruff.analysis.
|
|
17
|
+
Use `json` for automation. JSON reports use `gruff.analysis.v2`.
|
|
18
18
|
|
|
19
19
|
```sh
|
|
20
20
|
./bin/gruff-ts analyse src --format=json --fail-on=none > gruff-ts.json
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
+
`paths.skipped` (added in 0.3.0) lists every excluded path with its ignore
|
|
24
|
+
`source` (`config` / `gitignore` / `default`) and the matching `pattern`;
|
|
25
|
+
`paths.ignoredPaths` remains as the back-compatible `string[]` of the same paths.
|
|
26
|
+
The field is additive, so existing `gruff.analysis.v2` consumers are unaffected.
|
|
27
|
+
|
|
28
|
+
Score math clusters correlated `complexity.cognitive`,
|
|
29
|
+
`complexity.cyclomatic`, and `size.function-length` findings once per function
|
|
30
|
+
symbol. The detailed `findings` array still lists each rule finding; only pillar
|
|
31
|
+
penalties and offender scores are de-duplicated.
|
|
32
|
+
|
|
23
33
|
## HTML
|
|
24
34
|
|
|
25
35
|
Use `html` for archived human review or dashboard scan output:
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Philosophy
|
|
2
|
+
|
|
3
|
+
gruff-ts governs AI-generated code. Every rule, threshold, and report exists to serve one goal: a human who did not write the code can read, review, and trust it.
|
|
4
|
+
|
|
5
|
+
## The reviewer is not the author
|
|
6
|
+
|
|
7
|
+
A coding agent holds the full context while it writes. The person who has to sign off on the change does not. Conventional linters optimise for the author - they assume someone who already understands the code and just wants it tidy. gruff-ts optimises for the reviewer of code they did not write, which is the position every human reviewing an agent's output is in.
|
|
8
|
+
|
|
9
|
+
Used as a hook on an agent's output, gruff-ts is a forcing function, not advice. A finding is friction the agent must resolve before the change reaches a human, so what finally lands is already shaped for sign-off. Because the agent will change code to clear a finding, the direction each rule pushes matters as much as the threshold it fires at: a rule should never push the agent toward code that is harder to verify or less safe.
|
|
10
|
+
|
|
11
|
+
## Three goals
|
|
12
|
+
|
|
13
|
+
- **Verifiable.** The change can be checked by reading, not by re-deriving what the agent was thinking. The complexity, size, naming, and documentation pillars push toward code whose intent is visible on its face.
|
|
14
|
+
- **Secure where the eye slips.** Human review reliably misses a known set of unsafe patterns - disabled TLS verification, `eval` and dynamic `Function` construction, injection-shaped string building, committed secrets. The security and sensitive-data pillars catch those mechanically so the reviewer does not have to.
|
|
15
|
+
- **Honestly tested.** A test suite should raise confidence, not just coverage. The test-quality pillar flags low-signal ceremony - mock-only, snapshot-only, assertion-free, and tautological tests - so an agent cannot satisfy a "write tests" instruction with padding.
|
|
16
|
+
|
|
17
|
+
## Why documentation is mandatory, even on a private one-liner
|
|
18
|
+
|
|
19
|
+
Coding agents routinely produce code that superficially works while misunderstanding the requirement. The implementation can be plausible and still solve the wrong problem, and that failure is invisible to a reviewer reading only the code.
|
|
20
|
+
|
|
21
|
+
Forcing the agent to state intent, usage, contract, and failure behaviour in prose gives the reviewer something to check the implementation against. The doc comment is a second, independent statement of what the code is meant to do, and a mismatch between that prose and the code is itself a signal that the change needs a deeper look. That is worth the friction even on a one-line private helper, because the cost of a confidently-wrong agent change is paid by the human who trusted it.
|
package/docs/releasing.md
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
# Releasing
|
|
2
2
|
|
|
3
|
-
This checklist prepares
|
|
4
|
-
|
|
3
|
+
This checklist prepares public `@blundergoat/gruff-ts@0.3.x` patch releases.
|
|
4
|
+
Current release line: `0.3.0`.
|
|
5
5
|
|
|
6
6
|
## Bump The Version
|
|
7
7
|
|
|
8
8
|
`scripts/bump-version.sh <semver>` updates `package.json`,
|
|
9
9
|
`package-lock.json`, and `src/constants.ts` together so the CLI `--version`
|
|
10
10
|
output and the published `@blundergoat/gruff-ts` package version cannot drift
|
|
11
|
-
apart. For
|
|
12
|
-
use `--check` instead of bumping unless the
|
|
11
|
+
apart. For a no-op release (e.g. fixes folded into the current dated version),
|
|
12
|
+
the version should already match; use `--check` instead of bumping unless the
|
|
13
|
+
release version changes.
|
|
13
14
|
|
|
14
15
|
```bash
|
|
15
16
|
scripts/bump-version.sh --check
|
|
16
|
-
scripts/bump-version.sh 0.
|
|
17
|
+
scripts/bump-version.sh 0.3.1
|
|
17
18
|
scripts/bump-version.sh --check
|
|
18
19
|
```
|
|
19
20
|
|
|
@@ -26,14 +27,14 @@ update `CHANGELOG.md` and run `npm run check`.
|
|
|
26
27
|
- [ ] `CHANGELOG.md` has an entry for the new version with today's date.
|
|
27
28
|
- [ ] `README.md`, `CONTRIBUTING.md`, `SECURITY.md`, and docs under `docs/`
|
|
28
29
|
reflect any user-visible changes.
|
|
29
|
-
- [ ] `.goat-flow/tasks/0.
|
|
30
|
+
- [ ] `.goat-flow/tasks/0.3.0/` has no unresolved release blockers beyond
|
|
30
31
|
explicitly accepted `human-verification-pending` milestones.
|
|
31
32
|
- [ ] `LICENSE` is present and `package.json` `license` field matches.
|
|
32
33
|
- [ ] `npm run check` passes.
|
|
33
|
-
- [ ] `scripts/preflight-checks.sh` passes (checks version lockstep
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
- [ ] `scripts/preflight-checks.sh` passes (checks version lockstep, runs
|
|
35
|
+
`npm audit --audit-level=moderate`, `npm run check`, a full `gruff-ts`
|
|
36
|
+
self-scan, and `shellcheck` on `scripts/*.sh` when `shellcheck` is
|
|
37
|
+
installed).
|
|
37
38
|
- [ ] `npm pack --dry-run` shows only publishable runtime, docs, scripts, and
|
|
38
39
|
metadata files.
|
|
39
40
|
- [ ] Local smoke scan succeeds:
|
|
@@ -101,5 +102,5 @@ prompts before publishing.
|
|
|
101
102
|
- [ ] Run `gruff-ts summary . --fail-on=none`.
|
|
102
103
|
- [ ] Run `gruff-ts list-rules`.
|
|
103
104
|
- [ ] Verify `README.md` install instructions from a clean checkout.
|
|
104
|
-
- [ ] Tag the release in git (`git tag v0.
|
|
105
|
+
- [ ] Tag the release in git (`git tag v0.3.0 && git push --tags`) and create
|
|
105
106
|
or update public release notes from `CHANGELOG.md`.
|
package/docs/reports-and-ci.md
CHANGED
|
@@ -45,7 +45,7 @@ per-pillar counts, top rules, and top file offenders.
|
|
|
45
45
|
|
|
46
46
|
Schema strings:
|
|
47
47
|
|
|
48
|
-
- `gruff.analysis.
|
|
48
|
+
- `gruff.analysis.v2` for full analysis reports.
|
|
49
49
|
- `gruff.baseline.v1` for baselines.
|
|
50
50
|
- `gruff.hotspot.v1` for hotspot output.
|
|
51
51
|
|
|
@@ -125,7 +125,10 @@ gruff-ts report . --format=json --output gruff-report.json
|
|
|
125
125
|
```
|
|
126
126
|
|
|
127
127
|
`report` defaults to `--fail-on none`, making it suitable for local inspection
|
|
128
|
-
and scheduled reporting.
|
|
128
|
+
and scheduled reporting. `analyse` and `summary` default to `--fail-on advisory`
|
|
129
|
+
out of the box; override per-project by setting `minimumSeverity:` in
|
|
130
|
+
`.gruff-ts.yaml`. See `docs/configuration.md` and ADR-004 for the precedence
|
|
131
|
+
chain (CLI flag > config > binary default).
|
|
129
132
|
|
|
130
133
|
## Dashboard
|
|
131
134
|
|
package/docs/rules.md
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
# Rules
|
|
2
2
|
|
|
3
|
-
`gruff-ts` exposes
|
|
3
|
+
`gruff-ts` exposes 121 rules across 11 pillars. This list is generated from the
|
|
4
4
|
public rule catalogue used by `gruff-ts list-rules`; severity, confidence,
|
|
5
5
|
thresholds, and option names are the defaults before project config overrides.
|
|
6
6
|
|
|
7
|
+
The pillars are organised around one goal: making AI-generated code something a
|
|
8
|
+
human who did not write it can verify by reading, trust as secure, and rely on as
|
|
9
|
+
honestly tested rather than padded with low-signal ceremony. Complexity, size,
|
|
10
|
+
naming, and documentation serve verifiability; security and sensitive-data serve
|
|
11
|
+
safety where review is weakest; test-quality guards against coverage theatre. See
|
|
12
|
+
[Philosophy](philosophy.md) for the intent behind the catalogue, including why a
|
|
13
|
+
doc comment is expected even on a private one-liner.
|
|
14
|
+
|
|
7
15
|
Use the CLI when you need machine-readable metadata:
|
|
8
16
|
|
|
9
17
|
```bash
|
|
@@ -12,23 +20,22 @@ gruff-ts list-rules --format=json
|
|
|
12
20
|
|
|
13
21
|
## Pillar Counts
|
|
14
22
|
|
|
15
|
-
- complexity:
|
|
23
|
+
- complexity: 2
|
|
16
24
|
- dead-code: 1
|
|
17
|
-
- design:
|
|
18
|
-
- documentation:
|
|
25
|
+
- design: 5
|
|
26
|
+
- documentation: 18
|
|
19
27
|
- maintainability: 14
|
|
20
28
|
- modernisation: 14
|
|
21
29
|
- naming: 10
|
|
22
|
-
- security:
|
|
23
|
-
- sensitive-data:
|
|
24
|
-
- size:
|
|
30
|
+
- security: 29
|
|
31
|
+
- sensitive-data: 10
|
|
32
|
+
- size: 3
|
|
25
33
|
- test-quality: 15
|
|
26
34
|
|
|
27
35
|
## Complexity
|
|
28
36
|
|
|
29
37
|
- `complexity.cognitive` (warning; high confidence; threshold 15): Flags functions with high combined branch and nesting complexity.
|
|
30
38
|
- `complexity.cyclomatic` (warning; high confidence; threshold 15): Flags functions with many independent branch paths.
|
|
31
|
-
- `complexity.npath` (warning; medium confidence; threshold 200): Flags functions with high approximate NPath complexity.
|
|
32
39
|
|
|
33
40
|
## Dead Code
|
|
34
41
|
|
|
@@ -38,7 +45,6 @@ gruff-ts list-rules --format=json
|
|
|
38
45
|
|
|
39
46
|
- `design.circular-import` (warning; medium confidence): Flags simple relative import cycles inside the discovered source set.
|
|
40
47
|
- `design.deep-relative-import` (advisory; medium confidence; threshold 2): Flags relative imports that climb too many parent directories.
|
|
41
|
-
- `design.god-function` (warning; high confidence): Flags functions that are both long and complex.
|
|
42
48
|
- `design.large-module-concentration` (advisory; medium confidence; threshold 55; options: minFiles, minLines): Flags a production module that dominates project source lines.
|
|
43
49
|
- `design.package-bin-missing` (warning; high confidence): Flags package bin entries that point at missing files.
|
|
44
50
|
- `design.package-bin-not-executable` (warning; high confidence): Flags package bin targets that are not executable.
|
|
@@ -83,7 +89,7 @@ gruff-ts list-rules --format=json
|
|
|
83
89
|
## Naming
|
|
84
90
|
|
|
85
91
|
- `naming.acronym-case` (advisory; medium confidence): Flags mixed casings of a known acronym in one file.
|
|
86
|
-
- `naming.boolean-prefix` (advisory; medium confidence): Flags boolean names without intent-revealing prefixes on declarations, function parameters (typed `: boolean` or with `= true|false` default), and interface/type-literal fields.
|
|
92
|
+
- `naming.boolean-prefix` (advisory; medium confidence; allowlists: booleanPrefixes, acceptedBooleanNames): Flags boolean names without intent-revealing prefixes on declarations, function parameters (typed `: boolean` or with `= true|false` default), and interface/type-literal fields.
|
|
87
93
|
- `naming.class-file-mismatch` (advisory; medium confidence): Flags exported classes whose name differs from the file name.
|
|
88
94
|
- `naming.generic-function` (advisory; high confidence): Flags generic function names that hide intent.
|
|
89
95
|
- `naming.generic-parameter` (advisory; medium confidence; options: minCyclomatic, minLineCount, minParameters): Flags placeholder parameter names in multi-parameter, long, exported, or complex functions.
|
|
@@ -120,18 +126,22 @@ gruff-ts list-rules --format=json
|
|
|
120
126
|
- `security.ssrf-candidate` (warning; medium confidence): Flags external input sent to network request sinks.
|
|
121
127
|
- `security.string-timer` (warning; high confidence): Flags string callbacks passed to timers.
|
|
122
128
|
- `security.throw-non-error` (warning; medium confidence): Flags thrown non-Error values.
|
|
123
|
-
- `security.
|
|
129
|
+
- `security.unsafe-deserialization` (warning; medium confidence): Flags external input reaching unsafe deserialization or dynamic code-loading sinks.
|
|
130
|
+
- `security.url-dependency` (warning; medium confidence): Flags dependencies installed from URL, git, or local file specs.
|
|
124
131
|
- `security.weak-crypto` (warning; high confidence): Flags weak crypto primitives such as md5, sha1, or createCipher.
|
|
132
|
+
- `security.xxe-candidate` (warning; medium confidence): Flags external XML reaching parsers configured to expand entities.
|
|
125
133
|
|
|
126
134
|
## Sensitive Data
|
|
127
135
|
|
|
128
|
-
- `sensitive-data.api-key-pattern` (error; high confidence): Flags vendor API key patterns.
|
|
136
|
+
- `sensitive-data.api-key-pattern` (error; high confidence): Flags vendor API key patterns including GitHub, GitLab, npm, Google API, Slack, Discord, and Stripe/OpenAI-style keys.
|
|
129
137
|
- `sensitive-data.aws-access-key` (error; high confidence): Flags AWS access key looking values.
|
|
130
|
-
- `sensitive-data.database-url-password` (error; high confidence): Flags database URLs that include passwords.
|
|
138
|
+
- `sensitive-data.database-url-password` (error; high confidence): Flags database and HTTP(S) URLs that include passwords or embedded credentials.
|
|
139
|
+
- `sensitive-data.gcp-service-account-key` (error; high confidence): Flags GCP service-account key files (type service_account alongside a private key).
|
|
131
140
|
- `sensitive-data.hardcoded-env-value` (error; medium confidence; threshold 16): Flags environment-style secret values committed in text.
|
|
132
141
|
- `sensitive-data.high-entropy-string` (error; medium confidence; threshold 32): Flags high-entropy string literals that may be secrets.
|
|
133
142
|
- `sensitive-data.jwt-token` (error; high confidence): Flags JWT-looking token literals.
|
|
134
|
-
- `sensitive-data.
|
|
143
|
+
- `sensitive-data.phi-pattern` (error; high confidence): Flags PHI identifiers such as Medicare (MBI) and medical record numbers.
|
|
144
|
+
- `sensitive-data.pii-pattern` (error; high confidence): Flags PII-like identifier patterns including SSN-shaped values and Luhn-valid payment cards.
|
|
135
145
|
- `sensitive-data.private-key` (error; high confidence): Flags private key block markers.
|
|
136
146
|
|
|
137
147
|
## Size
|
|
@@ -139,7 +149,6 @@ gruff-ts list-rules --format=json
|
|
|
139
149
|
- `size.file-length` (warning; high confidence; threshold 750): Flags files longer than the configured threshold.
|
|
140
150
|
- `size.function-length` (warning; high confidence; threshold 200): Flags functions longer than the configured threshold.
|
|
141
151
|
- `size.parameter-count` (warning; high confidence; threshold 7): Flags functions with too many parameters.
|
|
142
|
-
- `size.stylesheet-length` (warning; high confidence; threshold 1500): Flags stylesheets longer than the configured threshold.
|
|
143
152
|
|
|
144
153
|
## Test Quality
|
|
145
154
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blundergoat/gruff-ts",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Governs AI-generated code so a human can verify it: a TypeScript/JavaScript static analyzer - 119 rules across 11 quality pillars, SARIF output, baselines, and a local dashboard.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Matthew Hansen (https://www.blundergoat.com/about)",
|
|
7
7
|
"homepage": "https://github.com/blundergoat/gruff-ts#readme",
|
|
@@ -46,11 +46,11 @@
|
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"commander": "^14.0.2",
|
|
49
|
-
"tsx": "^4.21.0"
|
|
49
|
+
"tsx": "^4.21.0",
|
|
50
|
+
"typescript": "^5.9.3"
|
|
50
51
|
},
|
|
51
52
|
"devDependencies": {
|
|
52
53
|
"@blundergoat/goat-flow": "^1.6.4",
|
|
53
|
-
"@types/node": "^25.0.0"
|
|
54
|
-
"typescript": "^5.9.3"
|
|
54
|
+
"@types/node": "^25.0.0"
|
|
55
55
|
}
|
|
56
56
|
}
|
package/scripts/bump-version.sh
CHANGED
|
@@ -12,10 +12,11 @@ Usage:
|
|
|
12
12
|
scripts/bump-version.sh --check
|
|
13
13
|
|
|
14
14
|
Arguments:
|
|
15
|
-
<new-version> Target semver, e.g. 0.1.1, 0.2.0,
|
|
15
|
+
<new-version> Target semver, e.g. 0.1.1, 0.2.0, 0.3.0-rc.1.
|
|
16
16
|
|
|
17
17
|
Options:
|
|
18
|
-
--check Verify package.json, package-lock.json,
|
|
18
|
+
--check Verify package.json, package-lock.json, src/constants.ts, and CHANGELOG.md
|
|
19
|
+
already agree on the same version.
|
|
19
20
|
--help, -h Show this help.
|
|
20
21
|
|
|
21
22
|
Notes:
|
|
@@ -50,6 +51,14 @@ read_package_lock_package_version() {
|
|
|
50
51
|
node -e 'const fs = require("node:fs"); const data = JSON.parse(fs.readFileSync("package-lock.json", "utf8")); process.stdout.write(String(data.packages?.[""]?.version ?? ""));'
|
|
51
52
|
}
|
|
52
53
|
|
|
54
|
+
read_changelog_latest_version() {
|
|
55
|
+
# POSIX awk: avoid gawk's 3-argument match() so this still parses under mawk/BSD awk.
|
|
56
|
+
# Matches the themed-narrative heading style `## v<semver> - YYYY-MM-DD`. The leading `v` and
|
|
57
|
+
# the digit lookahead filter out any non-version headings (e.g. a hypothetical `## Unreleased`
|
|
58
|
+
# block above the latest release).
|
|
59
|
+
awk '/^##[[:space:]]+v[0-9]/ { sub(/^##[[:space:]]+v/, ""); sub(/[[:space:]].*$/, ""); print; exit }' CHANGELOG.md
|
|
60
|
+
}
|
|
61
|
+
|
|
53
62
|
write_package_version() {
|
|
54
63
|
local next_version="$1"
|
|
55
64
|
awk -v target="$next_version" '
|
|
@@ -105,7 +114,7 @@ validate_semver() {
|
|
|
105
114
|
}
|
|
106
115
|
|
|
107
116
|
check_version_lockstep() {
|
|
108
|
-
local pkg const_ lock lock_package
|
|
117
|
+
local pkg const_ lock lock_package changelog
|
|
109
118
|
pkg="$(read_package_version)" || die "failed to read package.json version"
|
|
110
119
|
const_="$(read_constants_version)" || die "failed to read src/constants.ts VERSION"
|
|
111
120
|
lock="$(read_package_lock_version)" || die "failed to read package-lock.json root version"
|
|
@@ -118,7 +127,14 @@ check_version_lockstep() {
|
|
|
118
127
|
printf 'version surfaces disagree: package.json=%s src/constants.ts=%s package-lock.json=%s package-lock.json packages[""]=%s\n' "$pkg" "$const_" "$lock" "$lock_package" >&2
|
|
119
128
|
exit 1
|
|
120
129
|
fi
|
|
121
|
-
|
|
130
|
+
[[ -f CHANGELOG.md ]] || die "CHANGELOG.md not found"
|
|
131
|
+
changelog="$(read_changelog_latest_version)" || die "failed to read CHANGELOG.md latest version"
|
|
132
|
+
[[ -n "$changelog" ]] || die "CHANGELOG.md has no '## [version]' heading"
|
|
133
|
+
if [[ "$pkg" != "$changelog" ]]; then
|
|
134
|
+
printf 'CHANGELOG.md latest version %s does not match package.json %s; run scripts/bump-version.sh %s or add a CHANGELOG.md entry for %s\n' "$changelog" "$pkg" "$changelog" "$pkg" >&2
|
|
135
|
+
exit 1
|
|
136
|
+
fi
|
|
137
|
+
printf 'package.json, package-lock.json, src/constants.ts, and CHANGELOG.md agree on %s\n' "$pkg"
|
|
122
138
|
}
|
|
123
139
|
|
|
124
140
|
main() {
|
|
@@ -33,7 +33,6 @@ FAILED=0
|
|
|
33
33
|
FAILURES=()
|
|
34
34
|
TMP_FILES=()
|
|
35
35
|
START_TIME=$(date +%s%N)
|
|
36
|
-
NPM_REGISTRY_URL="${NPM_REGISTRY_URL:-https://registry.npmjs.org/}"
|
|
37
36
|
NPM_AUDIT_LEVEL="${NPM_AUDIT_LEVEL:-moderate}"
|
|
38
37
|
|
|
39
38
|
usage() {
|
|
@@ -42,7 +41,7 @@ Usage:
|
|
|
42
41
|
scripts/preflight-checks.sh
|
|
43
42
|
|
|
44
43
|
Runs the local preflight gate:
|
|
45
|
-
-
|
|
44
|
+
- version surfaces (package.json, package-lock.json, src/constants.ts, CHANGELOG.md) agree
|
|
46
45
|
- npm dependency audit
|
|
47
46
|
- npm run check (TypeScript compile plus unit tests)
|
|
48
47
|
- gruff-ts full-project scan
|
|
@@ -50,7 +49,6 @@ Runs the local preflight gate:
|
|
|
50
49
|
|
|
51
50
|
Environment:
|
|
52
51
|
GRUFF_TS_FAIL_ON gruff-ts severity that fails static analysis (default: advisory)
|
|
53
|
-
NPM_REGISTRY_URL npm registry used for the published-version check
|
|
54
52
|
NPM_AUDIT_LEVEL npm audit threshold (default: moderate)
|
|
55
53
|
USAGE
|
|
56
54
|
}
|
|
@@ -182,52 +180,17 @@ make_temp_file() {
|
|
|
182
180
|
printf '%s\n' "$temp_file"
|
|
183
181
|
}
|
|
184
182
|
|
|
185
|
-
|
|
186
|
-
node -p "require('./package.json').name"
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
read_package_version() {
|
|
190
|
-
node -p "require('./package.json').version"
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
npm_version_check() {
|
|
194
|
-
local package_name
|
|
195
|
-
local version
|
|
183
|
+
version_consistency_check() {
|
|
196
184
|
local output
|
|
197
185
|
local status
|
|
198
186
|
|
|
199
|
-
package_name="$(read_package_name)" || return 1
|
|
200
|
-
version="$(read_package_version)" || return 1
|
|
201
|
-
[[ -n "$package_name" ]] || {
|
|
202
|
-
printf 'package.json has no name field\n'
|
|
203
|
-
return 1
|
|
204
|
-
}
|
|
205
|
-
[[ -n "$version" ]] || {
|
|
206
|
-
printf 'package.json has no version field\n'
|
|
207
|
-
return 1
|
|
208
|
-
}
|
|
209
|
-
|
|
210
187
|
output=$(bash scripts/bump-version.sh --check 2>&1)
|
|
211
188
|
status=$?
|
|
212
189
|
if ((status != 0)); then
|
|
213
190
|
printf '%s\n' "$output"
|
|
214
191
|
return "$status"
|
|
215
192
|
fi
|
|
216
|
-
|
|
217
|
-
output=$(npm view "${package_name}@${version}" version --registry="$NPM_REGISTRY_URL" 2>&1)
|
|
218
|
-
status=$?
|
|
219
|
-
if ((status == 0)); then
|
|
220
|
-
printf '%s@%s is already published on %s; run scripts/bump-version.sh <next-version>\n' "$package_name" "$version" "$NPM_REGISTRY_URL"
|
|
221
|
-
return 1
|
|
222
|
-
fi
|
|
223
|
-
|
|
224
|
-
if printf '%s\n' "$output" | grep -Eq '(^|[[:space:]])(E404|404)([[:space:]]|$)'; then
|
|
225
|
-
printf 'lockstep ok; %s@%s is not published on %s' "$package_name" "$version" "$NPM_REGISTRY_URL"
|
|
226
|
-
return 0
|
|
227
|
-
fi
|
|
228
|
-
|
|
229
|
-
printf '%s\n' "$output"
|
|
230
|
-
return "$status"
|
|
193
|
+
printf '%s' "$output"
|
|
231
194
|
}
|
|
232
195
|
|
|
233
196
|
npm_audit_check() {
|
|
@@ -411,7 +374,7 @@ main() {
|
|
|
411
374
|
return 127
|
|
412
375
|
fi
|
|
413
376
|
|
|
414
|
-
run_step "
|
|
377
|
+
run_step "Version consistency" version_consistency_check
|
|
415
378
|
|
|
416
379
|
run_step "Dependency audit" npm_audit_check
|
|
417
380
|
|
package/src/analyser.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
// Analyser pipeline: walks discovered sources, runs every rule pass (complexity, dead-code, design,
|
|
2
2
|
// documentation, maintainability, modernisation, naming, security, sensitive-data, size, test-quality),
|
|
3
|
-
// aggregates findings into the `gruff.analysis.
|
|
3
|
+
// aggregates findings into the `gruff.analysis.v2` schema, and exposes `analyse` to the CLI shell.
|
|
4
4
|
import { existsSync, readFileSync } from "node:fs";
|
|
5
5
|
import { cwd } from "node:process";
|
|
6
6
|
import { basename, join } from "node:path";
|
|
7
7
|
import { applyBaseline, dedupeFindings, DEFAULT_BASELINE, recordHistory, writeBaseline } from "./baseline.ts";
|
|
8
|
+
import { changedRegionScope, filterChangedFindings } from "./changed-regions.ts";
|
|
8
9
|
import { loadConfig, optionNumber, ruleEnabled, ruleSeverity, threshold } from "./config.ts";
|
|
9
10
|
import { VERSION } from "./constants.ts";
|
|
10
11
|
import { absolutize, discoverSources, displayPath, type SourceFile } from "./discovery.ts";
|
|
11
12
|
import { makeFinding } from "./findings.ts";
|
|
12
|
-
import {
|
|
13
|
+
import { finding } from "./findings-helpers.ts";
|
|
13
14
|
import { commentRecords } from "./comment-scanner.ts";
|
|
14
15
|
import { analyseArchitectureRules, analyseTestAdequacyRules, buildProjectIndex, exportedSurface, isProductionSourcePath, isTestPath, type ProjectSource } from "./project-rules.ts";
|
|
15
16
|
import { analyseBlockRules, type BlockRuleContext, blockRuleContext, type FunctionBlock, functionBlocks, parameterNames } from "./blocks.ts";
|
|
@@ -18,6 +19,7 @@ import { analyseDeadCode, analyseUnreachable, analyseUnusedImports } from "./dea
|
|
|
18
19
|
import { analyseCommentQualityRules } from "./comment-rules.ts";
|
|
19
20
|
import { analyseDocRules, analyseFileOverviewDoc, analyseInterfaceDocs } from "./doc-rules.ts";
|
|
20
21
|
import { analyseLineRules } from "./line-rules.ts";
|
|
22
|
+
import { analyseSecurityFlow } from "./security-flow-rules.ts";
|
|
21
23
|
import { pushBooleanPrefixAt, pushIdentifierQualityAt, pushNegativeBooleanAt, pushShortVariableAt } from "./naming-pushers.ts";
|
|
22
24
|
import { analyseTestBlock } from "./test-block-rules.ts";
|
|
23
25
|
import { analyseGithubActionsRules } from "./github-actions-rules.ts";
|
|
@@ -25,10 +27,10 @@ import { analyseProjectConfigRules } from "./project-config-rules.ts";
|
|
|
25
27
|
import { scoreReport, summarize } from "./scoring.ts";
|
|
26
28
|
import { analyseSensitiveData } from "./sensitive-data-rules.ts";
|
|
27
29
|
import { maskNonCode, maskTemplateLiteralBodies, parseDiagnostics } from "./source-text.ts";
|
|
28
|
-
import type { AnalysisOptions, AnalysisReport, Config, Finding, RunDiagnostic } from "./types.ts";
|
|
30
|
+
import type { AnalysisOptions, AnalysisReport, Config, Finding, RunDiagnostic, SkippedPath } from "./types.ts";
|
|
29
31
|
|
|
30
32
|
/**
|
|
31
|
-
* Analyse the configured paths and return the stable gruff.analysis.
|
|
33
|
+
* Analyse the configured paths and return the stable gruff.analysis.v2 report contract.
|
|
32
34
|
*
|
|
33
35
|
* @param options Normalised analysis options from the CLI or direct callers.
|
|
34
36
|
* @returns Versioned report with fingerprinted findings, diagnostics, paths, and score data.
|
|
@@ -38,7 +40,7 @@ export function analyse(options: AnalysisOptions): AnalysisReport {
|
|
|
38
40
|
const config = loadConfig(projectRoot, options);
|
|
39
41
|
const diagnostics: RunDiagnostic[] = [];
|
|
40
42
|
const discovery = discoverSources(projectRoot, options, config);
|
|
41
|
-
|
|
43
|
+
const changedScope = changedRegionScope(options);
|
|
42
44
|
pushMissingPathDiagnostics(discovery.missingPaths, diagnostics);
|
|
43
45
|
|
|
44
46
|
const scanned = scanDiscoveredSources(discovery.files, config, diagnostics);
|
|
@@ -47,12 +49,13 @@ export function analyse(options: AnalysisOptions): AnalysisReport {
|
|
|
47
49
|
...analyseProjectIndex(scanned.projectSources, config).filter((finding) => ruleEnabled(config, finding.ruleId)),
|
|
48
50
|
]);
|
|
49
51
|
const baselineResult = applyBaselineOptions(projectRoot, options, allFindings);
|
|
52
|
+
const changedResult = filterChangedFindings(baselineResult.findings, changedScope, scanned.sources);
|
|
50
53
|
|
|
51
54
|
if (options.historyFile) {
|
|
52
|
-
recordHistory(projectRoot, options.historyFile,
|
|
55
|
+
recordHistory(projectRoot, options.historyFile, changedResult.findings, diagnostics);
|
|
53
56
|
}
|
|
54
57
|
|
|
55
|
-
return buildAnalysisReport(projectRoot, options, discovery, diagnostics, baselineResult);
|
|
58
|
+
return buildAnalysisReport(projectRoot, options, discovery, diagnostics, { ...baselineResult, findings: changedResult.findings }, changedResult.suppressedCount);
|
|
56
59
|
}
|
|
57
60
|
|
|
58
61
|
function buildAnalysisReport(
|
|
@@ -61,10 +64,11 @@ function buildAnalysisReport(
|
|
|
61
64
|
discovery: DiscoverySummary,
|
|
62
65
|
diagnostics: RunDiagnostic[],
|
|
63
66
|
baselineResult: BaselineApplication,
|
|
67
|
+
suppressedCount?: number,
|
|
64
68
|
): AnalysisReport {
|
|
65
69
|
const findings = baselineResult.findings;
|
|
66
70
|
return {
|
|
67
|
-
schemaVersion: "gruff.analysis.
|
|
71
|
+
schemaVersion: "gruff.analysis.v2",
|
|
68
72
|
tool: { name: "gruff-ts", version: VERSION },
|
|
69
73
|
run: {
|
|
70
74
|
projectRoot,
|
|
@@ -76,10 +80,12 @@ function buildAnalysisReport(
|
|
|
76
80
|
paths: {
|
|
77
81
|
analysedFiles: discovery.files.length,
|
|
78
82
|
ignoredPaths: discovery.ignoredPaths,
|
|
83
|
+
skipped: discovery.skipped,
|
|
79
84
|
missingPaths: discovery.missingPaths,
|
|
80
85
|
},
|
|
81
86
|
diagnostics,
|
|
82
87
|
findings,
|
|
88
|
+
...(suppressedCount === undefined ? {} : { suppressedCount }),
|
|
83
89
|
score: scoreReport(findings),
|
|
84
90
|
...(baselineResult.baseline ? { baseline: baselineResult.baseline } : {}),
|
|
85
91
|
};
|
|
@@ -90,6 +96,7 @@ function buildAnalysisReport(
|
|
|
90
96
|
interface DiscoverySummary {
|
|
91
97
|
files: SourceFile[];
|
|
92
98
|
ignoredPaths: string[];
|
|
99
|
+
skipped: SkippedPath[];
|
|
93
100
|
missingPaths: string[];
|
|
94
101
|
}
|
|
95
102
|
|
|
@@ -98,11 +105,12 @@ interface DiscoverySummary {
|
|
|
98
105
|
interface SourceScanResult {
|
|
99
106
|
findings: Finding[];
|
|
100
107
|
projectSources: ProjectSource[];
|
|
108
|
+
sources: Map<string, { file: SourceFile; source: string }>;
|
|
101
109
|
}
|
|
102
110
|
|
|
103
111
|
/*
|
|
104
112
|
* Result of applying a baseline (suppression) or generating a new one. The optional `baseline`
|
|
105
|
-
* matches the `gruff.analysis.
|
|
113
|
+
* matches the `gruff.analysis.v2` schema's baseline metadata - present only when a baseline file
|
|
106
114
|
* was actually used or generated, so the report stays stable across baseline-disabled runs.
|
|
107
115
|
*/
|
|
108
116
|
interface BaselineApplication {
|
|
@@ -117,16 +125,6 @@ interface BaselineSelection {
|
|
|
117
125
|
source: string;
|
|
118
126
|
}
|
|
119
127
|
|
|
120
|
-
// Mutates `discovery.files` in place to retain only paths that changed against the diff base.
|
|
121
|
-
// Required when `--diff` is set so the scan does not waste time on unchanged files; no-op otherwise.
|
|
122
|
-
function filterDiffSources(discovery: DiscoverySummary, options: AnalysisOptions): void {
|
|
123
|
-
if (!options.diff) {
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
const changed = changedFiles(options.diff);
|
|
127
|
-
discovery.files = discovery.files.filter((file) => changed.has(file.displayPath));
|
|
128
|
-
}
|
|
129
|
-
|
|
130
128
|
// Emits a `missing-path` diagnostic per path that the user requested but discovery could not
|
|
131
129
|
// resolve. Diagnostics force a non-zero exit (see `exitFor`); never throws - partial scans should still report.
|
|
132
130
|
function pushMissingPathDiagnostics(missingPaths: string[], diagnostics: RunDiagnostic[]): void {
|
|
@@ -147,9 +145,11 @@ function pushMissingPathDiagnostics(missingPaths: string[], diagnostics: RunDiag
|
|
|
147
145
|
function scanDiscoveredSources(files: SourceFile[], config: Config, diagnostics: RunDiagnostic[]): SourceScanResult {
|
|
148
146
|
const findings: Finding[] = [];
|
|
149
147
|
const projectSources: ProjectSource[] = [];
|
|
148
|
+
const sources = new Map<string, { file: SourceFile; source: string }>();
|
|
150
149
|
for (const file of files) {
|
|
151
150
|
try {
|
|
152
151
|
const source = readFileSync(file.absolutePath, "utf8");
|
|
152
|
+
sources.set(file.displayPath, { file, source });
|
|
153
153
|
if (shouldRetainProjectSource(file, source)) {
|
|
154
154
|
projectSources.push(projectSource(file, source));
|
|
155
155
|
}
|
|
@@ -164,7 +164,7 @@ function scanDiscoveredSources(files: SourceFile[], config: Config, diagnostics:
|
|
|
164
164
|
});
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
|
-
return { findings, projectSources };
|
|
167
|
+
return { findings, projectSources, sources };
|
|
168
168
|
}
|
|
169
169
|
|
|
170
170
|
// Retains production files for exported-surface checks, tests for central-suite import coverage,
|
|
@@ -289,18 +289,13 @@ function analyseProjectIndex(projectSources: ProjectSource[], config: Config): F
|
|
|
289
289
|
}
|
|
290
290
|
|
|
291
291
|
/*
|
|
292
|
-
* Per-file text-pillar rules run before script-only rules because config, workflow,
|
|
292
|
+
* Per-file text-pillar rules run before script-only rules because config, workflow, and
|
|
293
293
|
* secret surfaces are not TypeScript. The order is a stable baseline contract: reshuffling these
|
|
294
294
|
* checks changes same-line finding order for machine reports.
|
|
295
295
|
*/
|
|
296
296
|
function analyseTextRules(file: SourceFile, source: string, config: Config, findings: Finding[]): void {
|
|
297
297
|
const lines = lineCount(source);
|
|
298
|
-
if (
|
|
299
|
-
const stylesheetThreshold = threshold(config, "size.stylesheet-length", 1500);
|
|
300
|
-
if (lines > stylesheetThreshold) {
|
|
301
|
-
findings.push(finding({ ruleId: "size.stylesheet-length", message: `Stylesheet has ${lines} lines, above the threshold of ${stylesheetThreshold}.`, file, line: 1, severity: ruleSeverity(config, "size.stylesheet-length", "warning"), pillar: "size" }));
|
|
302
|
-
}
|
|
303
|
-
} else if (!isGeneratedLockfile(file.displayPath)) {
|
|
298
|
+
if (!isGeneratedLockfile(file.displayPath)) {
|
|
304
299
|
const fileLengthThreshold = threshold(config, "size.file-length", 750);
|
|
305
300
|
if (lines > fileLengthThreshold) {
|
|
306
301
|
findings.push(finding({ ruleId: "size.file-length", message: `File has ${lines} lines, above the threshold of ${fileLengthThreshold}.`, file, line: 1, severity: ruleSeverity(config, "size.file-length", "warning"), pillar: "size" }));
|
|
@@ -312,12 +307,6 @@ function analyseTextRules(file: SourceFile, source: string, config: Config, find
|
|
|
312
307
|
analyseProjectConfigRules(file, source, findings);
|
|
313
308
|
}
|
|
314
309
|
|
|
315
|
-
// CSS paths use a dedicated size rule (`size.stylesheet-length`) so stylesheets can have a
|
|
316
|
-
// different threshold and message from generic source files.
|
|
317
|
-
function isCssPath(displayPath: string): boolean {
|
|
318
|
-
return displayPath.toLowerCase().endsWith(".css");
|
|
319
|
-
}
|
|
320
|
-
|
|
321
310
|
// Counts the same logical lines as `source.split(/\r?\n/)` without allocating the full line array.
|
|
322
311
|
function lineCount(source: string): number {
|
|
323
312
|
let count = 1;
|
|
@@ -348,6 +337,7 @@ function analyseTypeScriptRules(file: SourceFile, source: string, config: Config
|
|
|
348
337
|
analyseBlocks(file, blocks, config, findings);
|
|
349
338
|
analyseUnusedImports(file, codeSource, source, findings);
|
|
350
339
|
analyseLineRules(file, source, codeSource, config, findings);
|
|
340
|
+
analyseSecurityFlow(file, source, findings);
|
|
351
341
|
analyseUnreachable(file, codeSource, findings);
|
|
352
342
|
analyseDocRules(file, source, codeSource, findings);
|
|
353
343
|
analyseInterfaceDocs(file, source, codeSource, findings);
|
|
@@ -384,7 +374,9 @@ function pushParameterNamingFindings(context: BlockRuleContext): void {
|
|
|
384
374
|
const line = context.block.declarationLine;
|
|
385
375
|
const params = parameterNames(context.block.params);
|
|
386
376
|
for (const parameter of params) {
|
|
387
|
-
|
|
377
|
+
if (!isComparatorShortParameter(context, params, parameter.name)) {
|
|
378
|
+
pushShortVariableAt(context.file, line, parameter.name, context.config, context.findings, "parameter");
|
|
379
|
+
}
|
|
388
380
|
pushIdentifierQualityAt(context.file, line, parameter.name, context.config, context.findings, "parameter");
|
|
389
381
|
if (isBooleanParameter(parameter.raw)) {
|
|
390
382
|
pushBooleanPrefixAt(context.file, line, parameter.name, context.config, context.findings, "parameter");
|
|
@@ -396,6 +388,27 @@ function pushParameterNamingFindings(context: BlockRuleContext): void {
|
|
|
396
388
|
}
|
|
397
389
|
}
|
|
398
390
|
|
|
391
|
+
// `(a, b)` is a conventional comparator pair when the callable is explicitly shaped like sorting.
|
|
392
|
+
// Keep the exemption narrow so arbitrary two-parameter helpers still need domain names.
|
|
393
|
+
function isComparatorShortParameter(context: BlockRuleContext, params: Array<{ name: string; raw: string }>, name: string): boolean {
|
|
394
|
+
const names = params.map((parameter) => parameter.name);
|
|
395
|
+
if (names.length !== 2 || names[0] !== "a" || names[1] !== "b" || (name !== "a" && name !== "b")) {
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
return isComparatorCallable(context.block.name, context.block.codeBody);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// Comparator-shaped callables are either named for ordering (`compare*`, `*Comparator`, `byName`) or
|
|
402
|
+
// their body performs the standard locale/numeric ordering expression over both parameters.
|
|
403
|
+
function isComparatorCallable(name: string, codeBody: string): boolean {
|
|
404
|
+
return (
|
|
405
|
+
/^(?:compare[A-Z0-9_]?|.*Comparator$|by[A-Z])/.test(name) ||
|
|
406
|
+
/\.localeCompare\s*\(/.test(codeBody) ||
|
|
407
|
+
/\breturn\s+a\b[\s\S]*?(?:-\s*b\b|[<>]=?\s*b\b)/.test(codeBody) ||
|
|
408
|
+
/\breturn\s+b\b[\s\S]*?(?:-\s*a\b|[<>]=?\s*a\b)/.test(codeBody)
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
|
|
399
412
|
// Generic-parameter rule is context-gated: only fires when the surrounding function is itself
|
|
400
413
|
// large enough to deserve attention (long, complex, or many parameters). Keeps noise down on
|
|
401
414
|
// trivial helpers that legitimately accept a `value` argument.
|