@boringparts/verule 0.0.0-stage → 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.
@@ -0,0 +1,297 @@
1
+ import { normalizeLocation, } from "tssae";
2
+ export const INLINE_IGNORE_RULE_ID = "verule/inline-ignore";
3
+ const DIRECTIVE_PREFIX = "verule-ignore-next-code-line";
4
+ function isWhitespaceChar(ch) {
5
+ return /\s/u.test(ch);
6
+ }
7
+ function splitReasonSeparator(rest) {
8
+ if (rest.startsWith("--")) {
9
+ const after = rest.slice(2);
10
+ if (after === "" || isWhitespaceChar(after[0])) {
11
+ return ["", after];
12
+ }
13
+ return undefined;
14
+ }
15
+ let searchFrom = 0;
16
+ while (true) {
17
+ const idx = rest.indexOf("--", searchFrom);
18
+ if (idx === -1) {
19
+ return undefined;
20
+ }
21
+ const before = rest.slice(0, idx);
22
+ const after = rest.slice(idx + 2);
23
+ const leftOk = before.length > 0 && isWhitespaceChar(before.at(-1));
24
+ const rightOk = after === "" || isWhitespaceChar(after[0]);
25
+ if (leftOk && rightOk) {
26
+ return [before, after];
27
+ }
28
+ searchFrom = idx + 1;
29
+ }
30
+ }
31
+ /** Parses one `verule-ignore-next-code-line` directive from a TSSAE standalone comment. */
32
+ export function parseDirectiveFromComment(comment) {
33
+ const trimmed = comment.text.trim();
34
+ if (!trimmed.startsWith(DIRECTIVE_PREFIX)) {
35
+ return undefined;
36
+ }
37
+ const afterPrefix = trimmed.slice(DIRECTIVE_PREFIX.length);
38
+ if (afterPrefix.length > 0 && !isWhitespaceChar(afterPrefix[0])) {
39
+ return undefined;
40
+ }
41
+ const rest = afterPrefix.trim();
42
+ if (rest === "") {
43
+ return {
44
+ kind: "malformed",
45
+ message: 'Missing rule ID and reason; expected "verule-ignore-next-code-line <ruleId> -- <reason>".',
46
+ at: comment.at,
47
+ };
48
+ }
49
+ const separated = splitReasonSeparator(rest);
50
+ if (!separated) {
51
+ return {
52
+ kind: "malformed",
53
+ message: 'Missing "-- <reason>" separator; expected "verule-ignore-next-code-line <ruleId> -- <reason>".',
54
+ at: comment.at,
55
+ };
56
+ }
57
+ const [rawIdPart, rawReasonPart] = separated;
58
+ const idTokens = rawIdPart.trim().split(/\s+/u).filter(Boolean);
59
+ if (idTokens.length === 0) {
60
+ return {
61
+ kind: "malformed",
62
+ message: 'Missing rule ID before "--"; expected "verule-ignore-next-code-line <ruleId> -- <reason>".',
63
+ at: comment.at,
64
+ };
65
+ }
66
+ if (idTokens.length > 1 || idTokens[0].includes(",")) {
67
+ return {
68
+ kind: "malformed",
69
+ message: 'Expected a single rule ID before "--".',
70
+ at: comment.at,
71
+ };
72
+ }
73
+ const reason = rawReasonPart.trim();
74
+ if (reason === "") {
75
+ return {
76
+ kind: "malformed",
77
+ message: 'Missing non-empty reason after "--".',
78
+ at: comment.at,
79
+ };
80
+ }
81
+ return {
82
+ kind: "valid",
83
+ ruleId: idTokens[0],
84
+ reason,
85
+ at: comment.at,
86
+ targetLine: comment.nextCodeLine,
87
+ };
88
+ }
89
+ /** Filters and parses all Verule inline ignore directives from scanned snapshot comments. */
90
+ export function parseInlineIgnoreDirectives(comments) {
91
+ const directives = [];
92
+ for (const comment of comments) {
93
+ const parsed = parseDirectiveFromComment(comment);
94
+ if (parsed !== undefined) {
95
+ directives.push(parsed);
96
+ }
97
+ }
98
+ return directives;
99
+ }
100
+ function normalizeFilePath(file) {
101
+ return file.replaceAll("\\", "/");
102
+ }
103
+ function primaryRangeKey(loc) {
104
+ return `${normalizeFilePath(loc.file)}\0${loc.start}\0${loc.end}`;
105
+ }
106
+ function compareLocations(a, b) {
107
+ if (a.file < b.file)
108
+ return -1;
109
+ if (a.file > b.file)
110
+ return 1;
111
+ if (a.start !== b.start)
112
+ return a.start - b.start;
113
+ if (a.end !== b.end)
114
+ return a.end - b.end;
115
+ if (a.line !== b.line)
116
+ return a.line - b.line;
117
+ return a.column - b.column;
118
+ }
119
+ /**
120
+ * Resolves `verule-ignore-next-code-line` directives against collected rule diagnostics.
121
+ *
122
+ * Rules run normally and keep all gaps and evidence; suppression only hides diagnostics whose
123
+ * `(ruleId, file, start, end)` primary location uniquely matches the target code line.
124
+ */
125
+ export function resolveInlineIgnores(params) {
126
+ if (params.directives.length === 0) {
127
+ return {
128
+ diagnostics: params.collected,
129
+ suppressions: [],
130
+ entries: [],
131
+ };
132
+ }
133
+ const fileProjects = new Map();
134
+ for (const project of params.projects) {
135
+ for (const file of project.sourceFiles) {
136
+ const normalized = normalizeFilePath(file);
137
+ const list = fileProjects.get(normalized);
138
+ if (list) {
139
+ if (!list.includes(project.projectId)) {
140
+ list.push(project.projectId);
141
+ }
142
+ }
143
+ else {
144
+ fileProjects.set(normalized, [project.projectId]);
145
+ }
146
+ }
147
+ }
148
+ const byRuleFileLine = new Map();
149
+ for (const diag of params.collected) {
150
+ const file = normalizeFilePath(diag.at.file);
151
+ const key = `${diag.ruleId}\0${file}\0${diag.at.line}`;
152
+ const bucket = byRuleFileLine.get(key);
153
+ if (bucket) {
154
+ bucket.push(diag);
155
+ }
156
+ else {
157
+ byRuleFileLine.set(key, [diag]);
158
+ }
159
+ }
160
+ const suppressedKeys = new Set();
161
+ const suppressions = [];
162
+ const directiveDiagnostics = [];
163
+ const entries = [];
164
+ const emitDirectiveDiagnostic = (directiveAt, findingKey, message, evidence = []) => {
165
+ const projectIds = fileProjects.get(directiveAt.file) ?? [
166
+ params.projects[0]?.projectId ?? "default",
167
+ ];
168
+ for (const projectId of projectIds) {
169
+ directiveDiagnostics.push({
170
+ ruleId: INLINE_IGNORE_RULE_ID,
171
+ projectId,
172
+ at: directiveAt,
173
+ message,
174
+ findingKey,
175
+ evidence,
176
+ });
177
+ }
178
+ };
179
+ for (const rawDirective of params.directives) {
180
+ const directiveAt = normalizeLocation(rawDirective.at);
181
+ if (rawDirective.kind === "malformed") {
182
+ const message = `Malformed inline ignore directive: ${rawDirective.message}`;
183
+ emitDirectiveDiagnostic(directiveAt, "inline-ignore-malformed", message);
184
+ entries.push({
185
+ status: "malformed",
186
+ at: directiveAt,
187
+ message,
188
+ });
189
+ continue;
190
+ }
191
+ if (!params.isPartialRuleSelection &&
192
+ !params.configuredRuleIds.has(rawDirective.ruleId)) {
193
+ const message = `Unknown rule ID "${rawDirective.ruleId}" in inline ignore directive.`;
194
+ emitDirectiveDiagnostic(directiveAt, "inline-ignore-unknown-rule", message);
195
+ entries.push({
196
+ status: "unknown_rule",
197
+ at: directiveAt,
198
+ ...(rawDirective.targetLine !== null
199
+ ? { targetLine: rawDirective.targetLine }
200
+ : {}),
201
+ ruleId: rawDirective.ruleId,
202
+ reason: rawDirective.reason,
203
+ message,
204
+ });
205
+ continue;
206
+ }
207
+ const matches = rawDirective.targetLine !== null
208
+ ? (byRuleFileLine.get(`${rawDirective.ruleId}\0${directiveAt.file}\0${rawDirective.targetLine}`) ?? [])
209
+ : [];
210
+ const uniqueByRange = new Map();
211
+ for (const match of matches) {
212
+ const key = primaryRangeKey(match.at);
213
+ if (!uniqueByRange.has(key)) {
214
+ uniqueByRange.set(key, normalizeLocation(match.at));
215
+ }
216
+ }
217
+ const uniqueLocations = [...uniqueByRange.values()].toSorted(compareLocations);
218
+ const isRuleComplete = params.executedRuleIds.has(rawDirective.ruleId);
219
+ if (uniqueLocations.length >= 2) {
220
+ const locLabels = uniqueLocations
221
+ .map((loc) => `${loc.file}:${loc.line}:${loc.column}`)
222
+ .join(", ");
223
+ const message = `Ambiguous inline ignore directive for rule "${rawDirective.ruleId}": matched ${uniqueLocations.length} distinct primary locations on line ${rawDirective.targetLine} (${locLabels}); all original diagnostics are retained.`;
224
+ const evidence = uniqueLocations.map((loc) => ({
225
+ kind: "source",
226
+ message: `Matched primary location ${loc.file}:${loc.line}:${loc.column} (${loc.start}..${loc.end})`,
227
+ at: loc,
228
+ }));
229
+ emitDirectiveDiagnostic(directiveAt, "inline-ignore-ambiguous", message, evidence);
230
+ entries.push({
231
+ status: "ambiguous",
232
+ at: directiveAt,
233
+ ...(rawDirective.targetLine !== null
234
+ ? { targetLine: rawDirective.targetLine }
235
+ : {}),
236
+ ruleId: rawDirective.ruleId,
237
+ reason: rawDirective.reason,
238
+ matchedLocations: uniqueLocations,
239
+ message,
240
+ });
241
+ continue;
242
+ }
243
+ if (!isRuleComplete) {
244
+ entries.push({
245
+ status: "undetermined",
246
+ at: directiveAt,
247
+ ...(rawDirective.targetLine !== null
248
+ ? { targetLine: rawDirective.targetLine }
249
+ : {}),
250
+ ruleId: rawDirective.ruleId,
251
+ reason: rawDirective.reason,
252
+ });
253
+ continue;
254
+ }
255
+ if (uniqueLocations.length === 0) {
256
+ const message = rawDirective.targetLine === null
257
+ ? `Unused inline ignore directive for rule "${rawDirective.ruleId}": no code line immediately follows the comment (blank line or end of file).`
258
+ : `Unused inline ignore directive for rule "${rawDirective.ruleId}": no matching diagnostic on line ${rawDirective.targetLine}.`;
259
+ emitDirectiveDiagnostic(directiveAt, "inline-ignore-unused", message);
260
+ entries.push({
261
+ status: "unused",
262
+ at: directiveAt,
263
+ ...(rawDirective.targetLine !== null
264
+ ? { targetLine: rawDirective.targetLine }
265
+ : {}),
266
+ ruleId: rawDirective.ruleId,
267
+ reason: rawDirective.reason,
268
+ message,
269
+ });
270
+ continue;
271
+ }
272
+ const matchedLoc = uniqueLocations[0];
273
+ suppressedKeys.add(`${rawDirective.ruleId}\0${primaryRangeKey(matchedLoc)}`);
274
+ suppressions.push({
275
+ ruleId: rawDirective.ruleId,
276
+ at: matchedLoc,
277
+ directiveAt,
278
+ reason: rawDirective.reason,
279
+ });
280
+ entries.push({
281
+ status: "suppressed",
282
+ at: directiveAt,
283
+ ...(rawDirective.targetLine !== null
284
+ ? { targetLine: rawDirective.targetLine }
285
+ : {}),
286
+ ruleId: rawDirective.ruleId,
287
+ reason: rawDirective.reason,
288
+ matchedLocations: [matchedLoc],
289
+ });
290
+ }
291
+ const surviving = params.collected.filter((diag) => !suppressedKeys.has(`${diag.ruleId}\0${primaryRangeKey(diag.at)}`));
292
+ return {
293
+ diagnostics: [...surviving, ...directiveDiagnostics],
294
+ suppressions,
295
+ entries,
296
+ };
297
+ }