|
| 1 | +/* |
| 2 | + * Copyright 2026 Code Intelligence GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +const JAZZER_INTERNAL_STACK_MARKERS = [ |
| 18 | + "/@jazzer.js/", |
| 19 | + "/jazzer.js/packages/", |
| 20 | + "/jazzer.js/core/", |
| 21 | + "/jazzer.js-commercial/packages/", |
| 22 | + "/jazzer.js-commercial/core/", |
| 23 | + "../../packages/", |
| 24 | +]; |
| 25 | + |
| 26 | +/** |
| 27 | + * Defines a suppression rule for bug-detector findings. |
| 28 | + */ |
| 29 | +export interface IgnoreRule { |
| 30 | + /** |
| 31 | + * A string or regular expression matching the stack excerpt shown in the |
| 32 | + * finding after removing the leading Error line and Jazzer.js frames. |
| 33 | + * @example "src/templates.js:41" |
| 34 | + * @example /renderTemplate.*handleRequest/s |
| 35 | + */ |
| 36 | + stackPattern?: string | RegExp; |
| 37 | +} |
| 38 | + |
| 39 | +export class IgnoreList { |
| 40 | + private readonly _rules: IgnoreRule[] = []; |
| 41 | + |
| 42 | + add(rule: IgnoreRule): void { |
| 43 | + this._rules.push(rule); |
| 44 | + } |
| 45 | + |
| 46 | + matches(stack: string): boolean { |
| 47 | + return matchesIgnoreRules(this._rules, stack); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export function matchesIgnoreRules( |
| 52 | + rules: IgnoreRule[], |
| 53 | + stack: string, |
| 54 | +): boolean { |
| 55 | + return rules.some((rule) => matchesIgnoreRule(rule, stack)); |
| 56 | +} |
| 57 | + |
| 58 | +export function buildGenericSuppressionSnippet( |
| 59 | + detectorName: string, |
| 60 | + suppressionMethod: string, |
| 61 | +): string { |
| 62 | + return [ |
| 63 | + 'const { getBugDetectorConfiguration } = require("@jazzer.js/bug-detectors");', |
| 64 | + "", |
| 65 | + "// Example only: adapt `stackPattern` to the shown stack above.", |
| 66 | + `getBugDetectorConfiguration("${detectorName}")`, |
| 67 | + ` ?.${suppressionMethod}({`, |
| 68 | + ' stackPattern: "test.js:10",', |
| 69 | + " });", |
| 70 | + ].join("\n"); |
| 71 | +} |
| 72 | + |
| 73 | +export function captureStack(): string { |
| 74 | + return new Error().stack ?? ""; |
| 75 | +} |
| 76 | + |
| 77 | +export function getUserFacingStack(stack: string): string { |
| 78 | + return getUserFacingStackLines(stack).join("\n"); |
| 79 | +} |
| 80 | + |
| 81 | +export function getUserFacingStackLines(stack: string): string[] { |
| 82 | + return stack |
| 83 | + .split("\n") |
| 84 | + .slice(1) |
| 85 | + .filter((line) => line !== "") |
| 86 | + .filter((line) => !isJazzerInternalStackLine(line)); |
| 87 | +} |
| 88 | + |
| 89 | +function matchesIgnoreRule(rule: IgnoreRule, stack: string): boolean { |
| 90 | + return Boolean( |
| 91 | + rule.stackPattern && |
| 92 | + matchesStackPattern(rule.stackPattern, getUserFacingStack(stack)), |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +function isJazzerInternalStackLine(line: string): boolean { |
| 97 | + const normalizedLine = line.replace(/\\/g, "/"); |
| 98 | + return JAZZER_INTERNAL_STACK_MARKERS.some((marker) => |
| 99 | + normalizedLine.includes(marker), |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +function matchesPattern(pattern: RegExp, value: string): boolean { |
| 104 | + pattern.lastIndex = 0; |
| 105 | + return pattern.test(value); |
| 106 | +} |
| 107 | + |
| 108 | +function matchesStackPattern(pattern: string | RegExp, value: string): boolean { |
| 109 | + if (typeof pattern === "string") { |
| 110 | + return value.includes(pattern); |
| 111 | + } |
| 112 | + return matchesPattern(pattern, value); |
| 113 | +} |
0 commit comments