From a6ee7d1ac08c46f678235cb41d309a8b1e35bb96 Mon Sep 17 00:00:00 2001 From: Eric Brown Date: Mon, 27 May 2024 20:48:44 -0700 Subject: [PATCH] Fix for traceback in plain renderer (#494) Fixes: #493 Signed-off-by: Eric Brown --- precli/renderers/detailed.py | 10 ++++------ precli/renderers/json.py | 25 +++++++++++++------------ precli/renderers/markdown.py | 8 +++++--- precli/renderers/plain.py | 33 ++++++++++++++++----------------- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/precli/renderers/detailed.py b/precli/renderers/detailed.py index 3641f407..a50dbb79 100644 --- a/precli/renderers/detailed.py +++ b/precli/renderers/detailed.py @@ -50,14 +50,12 @@ def render(self, run: Run): ) rule = Rule.get_by_id(result.rule_id) if rule: + self.console.print(f"{rule.id}: {rule.cwe.name}", style=style) + else: self.console.print( - f"{rule.id}: {rule.cwe.name}", - style=style, + f"{result.rule_id}: Parsing error", style=style ) - self.console.print( - f"{result.message}", - style=style, - ) + self.console.print(f"{result.message}", style=style) line_offset = result.location.start_line - 2 code = syntax.Syntax( diff --git a/precli/renderers/json.py b/precli/renderers/json.py index 913bf5a4..5601ae28 100644 --- a/precli/renderers/json.py +++ b/precli/renderers/json.py @@ -162,9 +162,6 @@ def render(self, run: Run): result, rules, rule_indices ) - if not rule: - continue - fixes = [] for fix in result.fixes: fixes.append(self.to_fix(result.artifact.file_name, fix)) @@ -175,24 +172,28 @@ def render(self, run: Run): ) ) - code_lines = result.snippet.splitlines(keepends=True) - code_line = code_lines[1] if len(code_lines) > 1 else code_lines[0] physical_location.region = sarif_om.Region( start_line=result.location.start_line, end_line=result.location.end_line, start_column=result.location.start_column + 1, end_column=result.location.end_column + 1, - snippet=sarif_om.ArtifactContent(text=code_line), ) - physical_location.context_region = sarif_om.Region( - start_line=result.location.start_line - 1, - end_line=result.location.end_line + 1, - snippet=sarif_om.ArtifactContent(text=result.snippet), - ) + if result.snippet: + lines = result.snippet.splitlines(keepends=True) + code_line = lines[1] if len(lines) > 1 else lines[0] + physical_location.region.snippet = sarif_om.ArtifactContent( + text=code_line + ) + + physical_location.context_region = sarif_om.Region( + start_line=result.location.start_line - 1, + end_line=result.location.end_line + 1, + snippet=sarif_om.ArtifactContent(text=result.snippet), + ) sarif_result = sarif_om.Result( - rule_id=rule.id, + rule_id=result.rule_id, rule_index=rule_index, message=sarif_om.Message(text=result.message), fixes=fixes, diff --git a/precli/renderers/markdown.py b/precli/renderers/markdown.py index fa9cd16b..870d4002 100644 --- a/precli/renderers/markdown.py +++ b/precli/renderers/markdown.py @@ -65,14 +65,16 @@ def render(self, run: Run): output += ( f"> [!{alert}]\n" f">\n" + f"> {result.rule_id}: Parsing error\n" f"on line {result.location.start_line} in {file_name}\n" f"> \n" f"> {result.message}\n" ) - output += f"> ```{result.artifact.language}\n" - output += f"{result.snippet}" - output += "> ```\n" + if result.snippet: + output += f"> ```{result.artifact.language}\n" + output += f"{result.snippet}" + output += "> ```\n" if result.fixes: output += f"> Suggested fix: {result.fixes[0].description}\n" diff --git a/precli/renderers/plain.py b/precli/renderers/plain.py index b283306b..d0a88572 100644 --- a/precli/renderers/plain.py +++ b/precli/renderers/plain.py @@ -35,34 +35,33 @@ def render(self, run: Run): file_name = result.artifact.file_name if rule: - self.console.print( - f"{rule.id}: {rule.cwe.name}", - ) + self.console.print(f"{rule.id}: {rule.cwe.name}") + else: + self.console.print(f"{result.rule_id}: Parsing error") # TODO(ericwb): replace hardcoded with actual scope self.console.print( f' File "{file_name}", line ' f"{result.location.start_line}, in ", ) - code_lines = result.snippet.splitlines(keepends=True) - code_line = code_lines[1] if len(code_lines) > 1 else code_lines[0] - underline_width = ( - result.location.end_column - result.location.start_column - ) - underline = ( - " " * result.location.start_column + "^" * underline_width - ) - self.console.print( - Padding(code_line + underline, (0, 4)), - ) + if result.snippet: + lines = result.snippet.splitlines(keepends=True) + code_line = lines[1] if len(lines) > 1 else lines[0] + underline_width = ( + result.location.end_column - result.location.start_column + ) + underline = ( + " " * result.location.start_column + "^" * underline_width + ) + self.console.print( + Padding(code_line + underline, (0, 4)), + ) self.console.print( f"{result.level.name.title()}: ", style=style, end="", ) - self.console.print( - f"{result.message}", - ) + self.console.print(f"{result.message}") self.console.print() self.console.print( f"Found {run.metrics.errors} errors, {run.metrics.warnings} "