@yagyeshvyas/vibeguard 0.1.0 → 1.0.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/src/suppress.js CHANGED
@@ -1,14 +1,17 @@
1
1
  'use strict';
2
2
 
3
3
  /*
4
- * Context-aware false positive suppression.
4
+ * Context-aware false positive suppression + inline suppression comments.
5
5
  *
6
- * Scans nearby lines for "safe context" patterns that indicate a finding
7
- * is a false positive. For example:
8
- * - eval() inside a test file (test.js) → suppress
9
- * - secret that looks like a placeholder/example → suppress
10
- * - HTTP URL in a comment → suppress
11
- * - Variable named "testKey" or "mockToken" → suppress
6
+ * Inline suppression syntax (two forms):
7
+ * // vibeguard-ignore-line [rule1, rule2] -> suppress on this line
8
+ * // vibeguard-ignore-next-line [rule1, ...] -> suppress on the next line
9
+ * // vibeguard-ignore[ruleId]: reason -> suppress ruleId on this line
10
+ * // vibeguard-ignore-next[ruleId]: reason -> suppress ruleId on the next line
11
+ * No rule list = suppress everything on the target line. Works with //, #, /* *.
12
+ *
13
+ * Suppressed findings are dropped from output but counted as `suppressedCount`
14
+ * on the scan result (see scanner.js). --show-suppressed re-adds them.
12
15
  *
13
16
  * This runs AFTER rule matching, BEFORE findings are returned.
14
17
  */
@@ -86,4 +89,71 @@ function suppressFindings(findings, content, relPath) {
86
89
  });
87
90
  }
88
91
 
89
- module.exports = { suppressFindings, isPlaceholder, isTestFile, isDocsFile, hasSafeContext };
92
+ // Parse inline suppression comments from file lines.
93
+ // Returns a Map: targetLine(1-based) -> Set(ruleIds) | '*'
94
+ // Also returns the raw suppressed entries for --show-suppressed.
95
+ //
96
+ // Supports both forms:
97
+ // // vibeguard-ignore-line [rule1, rule2]
98
+ // // vibeguard-ignore-next-line [rule1, ...]
99
+ // // vibeguard-ignore[ruleId]: reason
100
+ // // vibeguard-ignore-next[ruleId]: reason
101
+ function buildIgnoreMap(lines) {
102
+ const map = new Map(); // targetLine(1-based) -> Set(ruleIds) | '*'
103
+ // Old syntax: vibeguard-ignore(-next)?-line [rules]
104
+ const reOld = /(?:\/\/|#|\/\*|\*)\s*vibeguard-ignore(-next)?-line\b([^*\n]*)/i;
105
+ // New syntax: vibeguard-ignore[ruleId]: reason (or vibeguard-ignore-next[ruleId]: reason)
106
+ // Must NOT match vibeguard-ignore-line or vibeguard-ignore-next-line (old syntax).
107
+ const reNew = /(?:\/\/|#|\/\*|\*)\s*vibeguard-ignore(?:-(next))?(?!\s*-line)\[([^\]]+)\]\s*:?\s*(.*)/i;
108
+
109
+ for (let i = 0; i < lines.length; i++) {
110
+ // Check new syntax first (more specific).
111
+ const mNew = reNew.exec(lines[i]);
112
+ if (mNew) {
113
+ const target = mNew[1] === 'next' ? i + 2 : i + 1;
114
+ const ruleId = mNew[2].trim();
115
+ const existing = map.get(target);
116
+ if (existing === '*') continue;
117
+ if (ruleId === '*') { map.set(target, '*'); continue; }
118
+ map.set(target, new Set([...(existing || []), ruleId]));
119
+ continue;
120
+ }
121
+ // Check old syntax.
122
+ const mOld = reOld.exec(lines[i]);
123
+ if (mOld) {
124
+ const target = mOld[1] ? i + 2 : i + 1;
125
+ const rulesPart = (mOld[2] || '').replace(/\*\/.*/, '').trim();
126
+ // Strip surrounding brackets if present: [rule1, rule2] -> rule1, rule2
127
+ const cleaned = rulesPart.replace(/^\[|\]$/g, '').trim();
128
+ const rules = cleaned ? cleaned.split(/[\s,]+/).filter(Boolean) : '*';
129
+ const existing = map.get(target);
130
+ if (existing === '*' || rules === '*') { map.set(target, '*'); continue; }
131
+ map.set(target, new Set([...(existing || []), ...rules]));
132
+ }
133
+ }
134
+ return map;
135
+ }
136
+
137
+ // Apply inline suppressions to findings. Returns { kept, suppressed }.
138
+ // `suppressed` findings are annotated with `suppressed: true` and `suppressionReason`.
139
+ function applyInlineSuppressionsWithCount(findings, contents) {
140
+ const byFile = new Map();
141
+ for (const c of contents) byFile.set(c.rel, buildIgnoreMap(c.lines));
142
+ const kept = [];
143
+ const suppressed = [];
144
+ for (const f of findings) {
145
+ const map = byFile.get(f.file);
146
+ if (!map) { kept.push(f); continue; }
147
+ const entry = map.get(f.line);
148
+ if (!entry) { kept.push(f); continue; }
149
+ const isSuppressed = entry === '*' || entry.has(f.ruleId);
150
+ if (isSuppressed) {
151
+ suppressed.push({ ...f, suppressed: true });
152
+ } else {
153
+ kept.push(f);
154
+ }
155
+ }
156
+ return { kept, suppressed };
157
+ }
158
+
159
+ module.exports = { suppressFindings, isPlaceholder, isTestFile, isDocsFile, hasSafeContext, buildIgnoreMap, applyInlineSuppressionsWithCount };