@blundergoat/gruff-ts 0.1.0 → 0.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/CHANGELOG.md +35 -11
- package/CONTRIBUTING.md +3 -3
- package/README.md +141 -183
- package/docs/README.md +23 -0
- package/docs/ci-integration.md +63 -0
- package/docs/{CONFIGURATION.md → configuration.md} +41 -20
- package/docs/dashboard.md +36 -0
- package/docs/output-formats.md +65 -0
- package/docs/{RELEASING.md → releasing.md} +17 -14
- package/docs/{REPORTS_AND_CI.md → reports-and-ci.md} +5 -2
- package/docs/rules.md +177 -0
- package/package.json +2 -2
- package/scripts/bump-version.sh +83 -18
- package/scripts/dependency-install.sh +96 -0
- package/scripts/dependency-update.sh +177 -0
- package/scripts/preflight-checks.sh +38 -0
- package/src/analyser.ts +9 -24
- package/src/blocks.ts +66 -12
- package/src/class-rules.ts +5 -6
- package/src/cli-program.ts +216 -39
- package/src/cli.ts +4 -1
- package/src/comment-rules.ts +2 -0
- package/src/config-load-error.ts +18 -0
- package/src/config-preservation.ts +64 -0
- package/src/config.ts +155 -29
- package/src/constants.ts +10 -2
- package/src/dashboard.ts +1 -1
- package/src/dead-code-rules.ts +62 -16
- package/src/doc-rules.ts +3 -3
- package/src/findings-helpers.ts +17 -0
- package/src/init-config.ts +308 -0
- package/src/line-rules.ts +64 -12
- package/src/naming-pushers.ts +7 -32
- package/src/pillar-summary.ts +129 -0
- package/src/project-config-rules.ts +1 -1
- package/src/report-html.ts +404 -0
- package/src/report-renderers.ts +210 -406
- package/src/rule-list.ts +65 -1
- package/src/rules.ts +36 -37
- package/src/safety-rules.ts +9 -4
- package/src/scoring.ts +41 -9
- package/src/test-block-rules.ts +43 -1
- package/src/test-fixtures.ts +13 -7
- package/src/text-scans.ts +1 -152
- package/src/types.ts +32 -6
package/src/text-scans.ts
CHANGED
|
@@ -26,157 +26,6 @@ function firstLine(source: string, pattern: RegExp): number {
|
|
|
26
26
|
return source.split(/\r?\n/).findIndex((line) => pattern.test(line)) + 1 || 1;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
// Mutable lexer state threaded through the task-marker scanner. `quote === "\`"` survives across
|
|
30
|
-
// newlines because template literals can span lines; ordinary quotes reset at end of line.
|
|
31
|
-
interface TodoMarkerScanState {
|
|
32
|
-
isInBlockComment: boolean;
|
|
33
|
-
quote: string | undefined;
|
|
34
|
-
isEscaped: boolean;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// One scanner step yields the comment bytes for this position, how many extra characters to skip
|
|
38
|
-
// (e.g., to consume `*/`), and whether the rest of the line is comment text (no need to keep scanning).
|
|
39
|
-
interface CommentScanStep {
|
|
40
|
-
comment: string;
|
|
41
|
-
skip: number;
|
|
42
|
-
isDone: boolean;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Task-marker counter consumed by the density rule. Skipping strings is mandatory - markers inside
|
|
46
|
-
// string literals must not inflate the count, or every fixture string containing a task keyword
|
|
47
|
-
// would trip. `isScript` flips `#` line-comment recognition for shell-like config files.
|
|
48
|
-
function todoMarkerSummary(source: string, isScript: boolean): { count: number; firstLine: number } {
|
|
49
|
-
if (!source.includes("TODO") && !source.includes("FIXME")) {
|
|
50
|
-
return { count: 0, firstLine: 1 };
|
|
51
|
-
}
|
|
52
|
-
let count = 0;
|
|
53
|
-
let firstLine = 0;
|
|
54
|
-
const state: TodoMarkerScanState = { isInBlockComment: false, quote: undefined, isEscaped: false };
|
|
55
|
-
|
|
56
|
-
source.split(/\r?\n/).forEach((line, index) => {
|
|
57
|
-
const markerCount = countMatches(commentTextForLine(line, state, isScript), /\b(?:TODO|FIXME)\b/g);
|
|
58
|
-
if (markerCount === 0) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
count += markerCount;
|
|
62
|
-
if (firstLine === 0) {
|
|
63
|
-
firstLine = index + 1;
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
return { count, firstLine: firstLine || 1 };
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Returns only the comment portions of `line` - strings, regex literals, and code are dropped.
|
|
71
|
-
// Mutates `state` so block comments and template literals can carry across line boundaries.
|
|
72
|
-
function commentTextForLine(line: string, state: TodoMarkerScanState, isScript: boolean): string {
|
|
73
|
-
let comment = "";
|
|
74
|
-
for (let index = 0; index < line.length; index += 1) {
|
|
75
|
-
const step = commentScanStep(line, index, state, isScript);
|
|
76
|
-
comment += step.comment;
|
|
77
|
-
index += step.skip;
|
|
78
|
-
if (step.isDone) {
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if (state.quote !== "`") {
|
|
83
|
-
state.quote = undefined;
|
|
84
|
-
state.isEscaped = false;
|
|
85
|
-
}
|
|
86
|
-
return comment;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Three-state dispatcher: in a block comment, in a quoted string, or in executable code. Each
|
|
90
|
-
// branch must be exhaustive - falling through would let a task keyword inside a string get counted.
|
|
91
|
-
function commentScanStep(line: string, index: number, state: TodoMarkerScanState, isScript: boolean): CommentScanStep {
|
|
92
|
-
const character = line[index] ?? "";
|
|
93
|
-
const next = line[index + 1] ?? "";
|
|
94
|
-
if (state.isInBlockComment) {
|
|
95
|
-
return blockCommentScanStep(character, next, state);
|
|
96
|
-
}
|
|
97
|
-
if (state.quote) {
|
|
98
|
-
return quotedScanStep(character, state);
|
|
99
|
-
}
|
|
100
|
-
return openCodeCommentScanStep(line, index, state, isScript);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Priority order: quote → block opener → line comment. Quotes must come first because `/* ... */`
|
|
104
|
-
// inside a string is just text, and `// foo` inside a string would otherwise prematurely end scanning.
|
|
105
|
-
function openCodeCommentScanStep(line: string, index: number, state: TodoMarkerScanState, isScript: boolean): CommentScanStep {
|
|
106
|
-
const character = line[index] ?? "";
|
|
107
|
-
const next = line[index + 1] ?? "";
|
|
108
|
-
const quoteStep = quoteStartScanStep(character, state);
|
|
109
|
-
if (quoteStep) {
|
|
110
|
-
return quoteStep;
|
|
111
|
-
}
|
|
112
|
-
const blockStep = blockStartScanStep(character, next, state);
|
|
113
|
-
if (blockStep) {
|
|
114
|
-
return blockStep;
|
|
115
|
-
}
|
|
116
|
-
return lineCommentScanStep(line, index, character, next, isScript);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Detects opening quote of a string or template literal and mutates `state.quote` so subsequent
|
|
120
|
-
// characters are routed through `quotedScanStep` rather than the code branch.
|
|
121
|
-
function quoteStartScanStep(character: string, state: TodoMarkerScanState): CommentScanStep | undefined {
|
|
122
|
-
if (character === "\"" || character === "'" || character === "`") {
|
|
123
|
-
state.quote = character;
|
|
124
|
-
return emptyCommentScanStep();
|
|
125
|
-
}
|
|
126
|
-
return undefined;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// Detects `/*` opener. Returns `skip: 1` so the caller advances past `*` and does not re-enter on
|
|
130
|
-
// the next character as if `*` were code.
|
|
131
|
-
function blockStartScanStep(character: string, next: string, state: TodoMarkerScanState): CommentScanStep | undefined {
|
|
132
|
-
if (character === "/" && next === "*") {
|
|
133
|
-
state.isInBlockComment = true;
|
|
134
|
-
return { comment: "", skip: 1, isDone: false };
|
|
135
|
-
}
|
|
136
|
-
return undefined;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// Slices the comment payload after `//` (or `#` in config files) and signals `isDone: true` so the
|
|
140
|
-
// caller stops walking the line - everything to the right is comment text.
|
|
141
|
-
function lineCommentScanStep(line: string, index: number, character: string, next: string, isScript: boolean): CommentScanStep {
|
|
142
|
-
if (character === "/" && next === "/") {
|
|
143
|
-
return { comment: line.slice(index + 2), skip: line.length, isDone: true };
|
|
144
|
-
}
|
|
145
|
-
if (!isScript && character === "#") {
|
|
146
|
-
return { comment: line.slice(index + 1), skip: line.length, isDone: true };
|
|
147
|
-
}
|
|
148
|
-
return emptyCommentScanStep();
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Inside a block comment until `*/` is seen. Each character is treated as comment text so task
|
|
152
|
-
// markers inside the block contribute to the count.
|
|
153
|
-
function blockCommentScanStep(character: string, next: string, state: TodoMarkerScanState): CommentScanStep {
|
|
154
|
-
if (character === "*" && next === "/") {
|
|
155
|
-
state.isInBlockComment = false;
|
|
156
|
-
return { comment: "", skip: 1, isDone: false };
|
|
157
|
-
}
|
|
158
|
-
return { comment: character, skip: 0, isDone: false };
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Consumes the body of a string/template literal. `\` toggles `isEscaped` so the next character
|
|
162
|
-
// is not interpreted as the closing quote - necessary for sequences like `"\""` and `"\\\""`.
|
|
163
|
-
function quotedScanStep(character: string, state: TodoMarkerScanState): CommentScanStep {
|
|
164
|
-
if (state.isEscaped) {
|
|
165
|
-
state.isEscaped = false;
|
|
166
|
-
} else if (character === "\\") {
|
|
167
|
-
state.isEscaped = true;
|
|
168
|
-
} else if (character === state.quote) {
|
|
169
|
-
state.quote = undefined;
|
|
170
|
-
}
|
|
171
|
-
return emptyCommentScanStep();
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// No-op step used by branches that consumed a character but produced no comment text - keeps the
|
|
175
|
-
// caller loop branchless at the cost of one allocation per non-comment character.
|
|
176
|
-
function emptyCommentScanStep(): CommentScanStep {
|
|
177
|
-
return { comment: "", skip: 0, isDone: false };
|
|
178
|
-
}
|
|
179
|
-
|
|
180
29
|
// One-based line number containing byte offset `index`. Used to anchor findings extracted from
|
|
181
30
|
// regex match indices; off-by-one would shift every reported line in the resulting reports.
|
|
182
31
|
function byteLine(source: string, index: number): number {
|
|
@@ -190,4 +39,4 @@ function byteLine(source: string, index: number): number {
|
|
|
190
39
|
return line;
|
|
191
40
|
}
|
|
192
41
|
|
|
193
|
-
export { byteLine, countMatches, firstLine
|
|
42
|
+
export { byteLine, countMatches, firstLine };
|
package/src/types.ts
CHANGED
|
@@ -6,7 +6,7 @@ export type Pillar =
|
|
|
6
6
|
| "size"
|
|
7
7
|
| "complexity"
|
|
8
8
|
| "dead-code"
|
|
9
|
-
| "
|
|
9
|
+
| "maintainability"
|
|
10
10
|
| "naming"
|
|
11
11
|
| "documentation"
|
|
12
12
|
| "modernisation"
|
|
@@ -39,8 +39,21 @@ export interface AnalysisOptions {
|
|
|
39
39
|
shouldSkipBaseline: boolean;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
/**
|
|
42
|
+
/** Command keys that participate in the `minimumSeverity:` config block. `dashboard` is omitted on purpose - it has no `--fail-on` flag and accepting it as a key would silently no-op. See ADR-004. */
|
|
43
|
+
export type MinimumSeverityCommand = "analyse" | "summary" | "report";
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Loaded analyzer configuration derived from optional gruff config files. The schema invariant is
|
|
47
|
+
* that `schemaVersion` is fixed at the supported version and every field has a defined default in
|
|
48
|
+
* `defaultConfig()` so consumers can rely on the shape without per-field guards.
|
|
49
|
+
*/
|
|
43
50
|
export interface Config {
|
|
51
|
+
/**
|
|
52
|
+
* Required top-level config-schema version. Pre-1.0 break: every `.gruff-ts.yaml` must declare
|
|
53
|
+
* `schemaVersion: gruff-ts.config.v0.1` or loading throws. Lives in the `gruff-ts.config.*`
|
|
54
|
+
* namespace, distinct from the output schemas (`gruff.analysis.v2`, etc.). See ADR-004.
|
|
55
|
+
*/
|
|
56
|
+
schemaVersion: "gruff-ts.config.v0.1";
|
|
44
57
|
ignoredPaths: string[];
|
|
45
58
|
acceptedAbbreviations: Set<string>;
|
|
46
59
|
secretPreviews: Set<string>;
|
|
@@ -48,9 +61,14 @@ export interface Config {
|
|
|
48
61
|
booleanPrefixes: Set<string>;
|
|
49
62
|
hungarianPrefixes: Set<string>;
|
|
50
63
|
placeholderNames: Set<string>;
|
|
51
|
-
abbreviationDenylist: Set<string>;
|
|
52
64
|
negativeBooleanAllowed: Set<string>;
|
|
53
65
|
knownAcronyms: Set<string>;
|
|
66
|
+
/**
|
|
67
|
+
* Per-command default for `--fail-on`. Precedence: CLI flag > this map > binary default.
|
|
68
|
+
* `dashboard` is intentionally not a valid key (no `--fail-on` flag exists for it); the parser
|
|
69
|
+
* rejects `dashboard` with a documented error. See ADR-004.
|
|
70
|
+
*/
|
|
71
|
+
minimumSeverity: Map<MinimumSeverityCommand, FailThreshold>;
|
|
54
72
|
rules: Map<string, { enabled?: boolean; threshold?: number; severity?: Severity; options: Map<string, number> }>;
|
|
55
73
|
}
|
|
56
74
|
|
|
@@ -81,9 +99,9 @@ export interface RunDiagnostic {
|
|
|
81
99
|
line?: number;
|
|
82
100
|
}
|
|
83
101
|
|
|
84
|
-
/** Stable gruff.analysis.
|
|
102
|
+
/** Stable gruff.analysis.v2 report schema returned by analyse and JSON report commands. */
|
|
85
103
|
export interface AnalysisReport {
|
|
86
|
-
schemaVersion: "gruff.analysis.
|
|
104
|
+
schemaVersion: "gruff.analysis.v2";
|
|
87
105
|
tool: { name: "gruff-ts"; version: string };
|
|
88
106
|
run: { projectRoot: string; format: OutputFormat; failOn: FailThreshold; generatedAt: string };
|
|
89
107
|
summary: { advisory: number; warning: number; error: number; total: number };
|
|
@@ -93,7 +111,7 @@ export interface AnalysisReport {
|
|
|
93
111
|
score: {
|
|
94
112
|
composite: number;
|
|
95
113
|
grade: string;
|
|
96
|
-
pillars: Array<{ pillar: Pillar; score: number; findings: number }>;
|
|
114
|
+
pillars: Array<{ pillar: Pillar; score: number; penalty: number; findings: number }>;
|
|
97
115
|
topOffenders: Array<{ filePath: string; score: number; findings: number }>;
|
|
98
116
|
};
|
|
99
117
|
baseline?: { path: string; source: string; suppressed: number; generated: boolean };
|
|
@@ -109,5 +127,13 @@ export interface RuleDescriptor {
|
|
|
109
127
|
remediation: string;
|
|
110
128
|
threshold?: number;
|
|
111
129
|
optionKeys?: readonly string[];
|
|
130
|
+
/**
|
|
131
|
+
* Names of `allowlists.*` keys in `.gruff-ts.yaml` that override or extend the rule's
|
|
132
|
+
* behaviour. Mirrors the `optionKeys` shape but targets the cross-pillar allowlist surface
|
|
133
|
+
* (e.g. `naming.boolean-prefix` consults `allowlists.booleanPrefixes`). Surfaced in the
|
|
134
|
+
* `list-rules --format=json` payload and the per-rule remediation text so operators don't
|
|
135
|
+
* have to grep `.gruff-ts.yaml` and `src/config.ts` to find the override knob.
|
|
136
|
+
*/
|
|
137
|
+
allowlistKeys?: readonly string[];
|
|
112
138
|
fixtureExemption?: string;
|
|
113
139
|
}
|