Skip to content

Commit 32b1112

Browse files
committed
Showing extended preview
1 parent 9e3a126 commit 32b1112

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

semgrep/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18
1+
19

semgrep/service/al_run.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,14 @@ def _process_results(
186186
for result in results:
187187
line_start, line_end = result["start"]["line"], result["end"]["line"]
188188
if (line_start, line_end) not in lines_by_rule[result["check_id"]]:
189-
line_no.add((line_start, line_end))
190189
result_by_rule[result["check_id"]].append(result)
191190
lines_by_rule[result["check_id"]].add((line_start, line_end))
191+
extended_preview = self.metadata_cache.get(result["check_id"], {}).get(
192+
"extend_preview", 0
193+
)
194+
if extended_preview:
195+
line_start = max(line_start - extended_preview, first_line_no)
196+
line_no.add((line_start, line_end))
192197

193198
lines = dict()
194199
if line_no:
@@ -207,6 +212,9 @@ def _process_results(
207212
metadata = self.metadata_cache.get(rule_id, {})
208213
title = metadata.get("title", metadata.get("name", message[:100]))
209214
attack_id = metadata.get("attack_id")
215+
extend_preview = metadata.get("extend_preview", 0)
216+
if not isinstance(extend_preview, int):
217+
extend_preview = 0
210218

211219
section = ResultTextSection(
212220
title,
@@ -216,7 +224,8 @@ def _process_results(
216224
section.set_heuristic(heuristic, signature=rule_id, attack_id=attack_id)
217225
for match in matches:
218226
line_start, line_end = match["start"]["line"], match["end"]["line"]
219-
line = lines.get((line_start, line_end), "")
227+
228+
line = lines.get((max(line_start - extend_preview, first_line_no), line_end), "")
220229
code_hash = self._get_code_hash(line)
221230

222231
if first_line_no == 0:
@@ -225,6 +234,8 @@ def _process_results(
225234
title = f"Match at lines {line_start} - {line_end}"
226235
if line_start == line_end:
227236
title = f"Match at line {line_start}"
237+
if extend_preview:
238+
title = f"{title} (+{extend_preview} lines before)"
228239
ResultMemoryDumpSection(
229240
title,
230241
body=line[:MAX_LINE_SIZE],

0 commit comments

Comments
 (0)