Skip to content

Commit

Permalink
Fix for traceback in plain renderer (#494)
Browse files Browse the repository at this point in the history
Fixes: #493

Signed-off-by: Eric Brown <[email protected]>
  • Loading branch information
ericwb authored May 28, 2024
1 parent 4b426cf commit a6ee7d1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
10 changes: 4 additions & 6 deletions precli/renderers/detailed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
25 changes: 13 additions & 12 deletions precli/renderers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions precli/renderers/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 16 additions & 17 deletions precli/renderers/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <module> with actual scope
self.console.print(
f' File "{file_name}", line '
f"{result.location.start_line}, in <module>",
)
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} "
Expand Down

0 comments on commit a6ee7d1

Please sign in to comment.