Skip to content

Commit

Permalink
reporter: Report last line and last column of code snippet (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
mishaschwartz authored Sep 16, 2021
1 parent 2b62296 commit 765f423
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions python_ta/reporters/json_reporter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
from typing import Dict, List

from pylint.interfaces import IReporter
from pylint.reporters.ureports.nodes import BaseLayout

from .core import PythonTaReporter
from .core import NewMessage, PythonTaReporter


class JSONReporter(PythonTaReporter):
Expand All @@ -17,6 +18,8 @@ class JSONReporter(PythonTaReporter):

OUTPUT_FILENAME = "pyta_report.json"

messages: Dict[str, List[NewMessage]]

def display_messages(self, layout: BaseLayout) -> None:
"""Hook for displaying the messages of the reporter
Expand All @@ -30,7 +33,23 @@ def display_messages(self, layout: BaseLayout) -> None:
output = []
for k, msgs in self.messages.items():
output.append(
{"filename": k, "msgs": [msg._replace(node=None)._asdict() for msg in msgs]}
{
"filename": k,
"msgs": [
{**msg._replace(node=None)._asdict(), **self._snippet_endings(msg)}
for msg in msgs
],
}
)

self.writeln(json.dumps(output, indent=4))

def _snippet_endings(self, msg: NewMessage) -> Dict[str, int]:
"""Return a dictionary containing the index of the last line and the index of the last column of the
error reported by the message in msg.
"""
if msg.node is None:
line_end, column_end = msg.line, len(self.source_lines[msg.line - 1])
else:
line_end, column_end = msg.node.end_lineno, msg.node.end_col_offset
return {"line_end": line_end, "column_end": column_end}

0 comments on commit 765f423

Please sign in to comment.