Skip to content

Commit f0dfebc

Browse files
committed
refactor(bug-detectors): share suppression state
Reuse a small IgnoreList helper so code injection and path traversal store suppression rules consistently without duplicating array management. This keeps the detector APIs unchanged while making future suppression changes easier to apply safely.
1 parent b0dd643 commit f0dfebc

4 files changed

Lines changed: 33 additions & 11 deletions

File tree

packages/bug-detectors/internal/code-injection.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import {
2929
buildGenericSuppressionSnippet,
3030
captureStack,
3131
getRelevantStackLines,
32+
IgnoreList,
3233
type IgnoreRule,
33-
matchesIgnoreRules,
3434
type OriginFrame,
3535
parseOriginFrame,
3636
} from "../shared/finding-suppression";
@@ -70,8 +70,8 @@ export interface CodeInjectionConfig {
7070
class CodeInjectionConfigImpl implements CodeInjectionConfig {
7171
private _reportAccess = true;
7272
private _reportInvocation = true;
73-
private readonly _ignoredAccessRules: IgnoreRule[] = [];
74-
private readonly _ignoredInvocationRules: IgnoreRule[] = [];
73+
private readonly _ignoredAccessRules = new IgnoreList();
74+
private readonly _ignoredInvocationRules = new IgnoreList();
7575

7676
disableAccessReporting(): this {
7777
this._reportAccess = false;
@@ -84,12 +84,12 @@ class CodeInjectionConfigImpl implements CodeInjectionConfig {
8484
}
8585

8686
ignoreAccess(rule: IgnoreRule): this {
87-
this._ignoredAccessRules.push(rule);
87+
this._ignoredAccessRules.add(rule);
8888
return this;
8989
}
9090

9191
ignoreInvocation(rule: IgnoreRule): this {
92-
this._ignoredInvocationRules.push(rule);
92+
this._ignoredInvocationRules.add(rule);
9393
return this;
9494
}
9595

@@ -99,7 +99,7 @@ class CodeInjectionConfigImpl implements CodeInjectionConfig {
9999
): boolean {
100100
return (
101101
this._reportAccess &&
102-
!matchesIgnoreRules(this._ignoredAccessRules, originFrame, stack)
102+
!this._ignoredAccessRules.matches(originFrame, stack)
103103
);
104104
}
105105

@@ -109,7 +109,7 @@ class CodeInjectionConfigImpl implements CodeInjectionConfig {
109109
): boolean {
110110
return (
111111
this._reportInvocation &&
112-
!matchesIgnoreRules(this._ignoredInvocationRules, originFrame, stack)
112+
!this._ignoredInvocationRules.matches(originFrame, stack)
113113
);
114114
}
115115
}

packages/bug-detectors/internal/path-traversal.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import {
2626
buildGenericSuppressionSnippet,
2727
captureStack,
2828
getRelevantStackLines,
29+
IgnoreList,
2930
type IgnoreRule,
30-
matchesIgnoreRules,
3131
type OriginFrame,
3232
parseOriginFrame,
3333
} from "../shared/finding-suppression";
@@ -55,15 +55,15 @@ export interface PathTraversalConfig {
5555
}
5656

5757
class PathTraversalConfigImpl implements PathTraversalConfig {
58-
private readonly _ignoredRules: IgnoreRule[] = [];
58+
private readonly _ignoredRules = new IgnoreList();
5959

6060
ignore(rule: IgnoreRule): this {
61-
this._ignoredRules.push(rule);
61+
this._ignoredRules.add(rule);
6262
return this;
6363
}
6464

6565
shouldReport(originFrame: OriginFrame | undefined, stack: string): boolean {
66-
return !matchesIgnoreRules(this._ignoredRules, originFrame, stack);
66+
return !this._ignoredRules.matches(originFrame, stack);
6767
}
6868
}
6969

packages/bug-detectors/shared/finding-suppression.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import {
1818
buildGenericSuppressionSnippet,
1919
getNormalizedRelevantStack,
20+
IgnoreList,
2021
matchesIgnoreRules,
2122
parseOriginFrame,
2223
} from "./finding-suppression";
@@ -69,6 +70,15 @@ describe("finding suppression", () => {
6970
).toBe(true);
7071
});
7172

73+
test("IgnoreList stores and matches suppression rules", () => {
74+
const originFrame = parseOriginFrame(stack);
75+
const ignoreList = new IgnoreList();
76+
77+
ignoreList.add({ stackPattern: "sample.test.js:10" });
78+
79+
expect(ignoreList.matches(originFrame, stack)).toBe(true);
80+
});
81+
7282
test("prints generic example snippets with optional chaining", () => {
7383
expect(
7484
buildGenericSuppressionSnippet("code-injection", "ignoreInvocation"),

packages/bug-detectors/shared/finding-suppression.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ export type OriginFrame = {
5454
filePath?: string;
5555
};
5656

57+
export class IgnoreList {
58+
private readonly _rules: IgnoreRule[] = [];
59+
60+
add(rule: IgnoreRule): void {
61+
this._rules.push(rule);
62+
}
63+
64+
matches(originFrame: OriginFrame | undefined, stack: string): boolean {
65+
return matchesIgnoreRules(this._rules, originFrame, stack);
66+
}
67+
}
68+
5769
export function matchesIgnoreRules(
5870
rules: IgnoreRule[],
5971
originFrame: OriginFrame | undefined,

0 commit comments

Comments
 (0)