Skip to content

Commit

Permalink
fix(analyze): corrected statistics about analyzed files
Browse files Browse the repository at this point in the history
  • Loading branch information
d-biehl committed Nov 21, 2024
1 parent c5f766f commit 18b6cb8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 32 deletions.
71 changes: 45 additions & 26 deletions packages/analyze/src/robotcode/analyze/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Flag
from pathlib import Path
from textwrap import indent
from typing import List, Optional, Set, Tuple
from typing import List, Optional, Set, Tuple, Union

import click

Expand Down Expand Up @@ -56,16 +56,49 @@ class ReturnCode(Flag):

class Statistic:
def __init__(self) -> None:
self.folders: Set[WorkspaceFolder] = set()
self.files: Set[TextDocument] = set()
self.errors = 0
self.warnings = 0
self.infos = 0
self.hints = 0
self._folders: Set[WorkspaceFolder] = set()
self._files: Set[TextDocument] = set()
self._diagnostics: List[Union[DocumentDiagnosticReport, FolderDiagnosticReport]] = []

@property
def errors(self) -> int:
return sum(
len([i for i in e.items if i.severity == DiagnosticSeverity.ERROR]) for e in self._diagnostics if e.items
)

@property
def warnings(self) -> int:
return sum(
len([i for i in e.items if i.severity == DiagnosticSeverity.WARNING]) for e in self._diagnostics if e.items
)

@property
def infos(self) -> int:
return sum(
len([i for i in e.items if i.severity == DiagnosticSeverity.INFORMATION])
for e in self._diagnostics
if e.items
)

@property
def hints(self) -> int:
return sum(
len([i for i in e.items if i.severity == DiagnosticSeverity.HINT]) for e in self._diagnostics if e.items
)

def add_diagnostics_report(
self, diagnostics_report: Union[DocumentDiagnosticReport, FolderDiagnosticReport]
) -> None:
self._diagnostics.append(diagnostics_report)

if isinstance(diagnostics_report, FolderDiagnosticReport):
self._folders.add(diagnostics_report.folder)
elif isinstance(diagnostics_report, DocumentDiagnosticReport):
self._files.add(diagnostics_report.document)

def __str__(self) -> str:
return (
f"Files: {len(self.files)}, Errors: {self.errors}, Warnings: {self.warnings}, "
f"Files: {len(self._files)}, Errors: {self.errors}, Warnings: {self.warnings}, "
f"Infos: {self.infos}, Hints: {self.hints}"
)

Expand Down Expand Up @@ -192,7 +225,6 @@ def code(
The return code is a bitwise combination of the following values:
\b
- `0`: **SUCCESS** - No issues detected.
- `1`: **ERRORS** - Critical issues found.
- `2`: **WARNINGS** - Non-critical issues detected.
Expand Down Expand Up @@ -284,20 +316,17 @@ def code(
robot_profile=robot_profile,
root_folder=root_folder,
).run(paths=paths, filter=filter):
if isinstance(e, FolderDiagnosticReport):
statistics.folders.add(e.folder)
statistics.add_diagnostics_report(e)

if isinstance(e, FolderDiagnosticReport):
if e.items:
_print_diagnostics(app, root_folder, statistics, e.items, e.folder.uri.to_path())

_print_diagnostics(app, root_folder, e.items, e.folder.uri.to_path())
elif isinstance(e, DocumentDiagnosticReport):
statistics.files.add(e.document)

doc_path = (
e.document.uri.to_path().relative_to(root_folder) if root_folder else e.document.uri.to_path()
)
if e.items:
_print_diagnostics(app, root_folder, statistics, e.items, doc_path)
_print_diagnostics(app, root_folder, e.items, doc_path)

statistics_str = str(statistics)
if statistics.errors > 0:
Expand All @@ -314,23 +343,13 @@ def code(
def _print_diagnostics(
app: Application,
root_folder: Optional[Path],
statistics: Statistic,
diagnostics: List[Diagnostic],
folder_path: Optional[Path],
print_range: bool = True,
) -> None:
for item in diagnostics:
severity = item.severity if item.severity is not None else DiagnosticSeverity.ERROR

if severity == DiagnosticSeverity.ERROR:
statistics.errors += 1
elif severity == DiagnosticSeverity.WARNING:
statistics.warnings += 1
elif severity == DiagnosticSeverity.INFORMATION:
statistics.infos += 1
elif severity == DiagnosticSeverity.HINT:
statistics.hints += 1

app.echo(
(
(
Expand Down
12 changes: 6 additions & 6 deletions packages/analyze/src/robotcode/analyze/code_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def run(
self.app.error(f"Error analyzing {folder.uri.to_path()}: {item}")
else:
diagnostics.extend(item)
if diagnostics:
yield FolderDiagnosticReport(folder, diagnostics)

yield FolderDiagnosticReport(folder, diagnostics)

documents = self.collect_documents(folder, paths=paths, filter=filter)

Expand All @@ -110,8 +110,8 @@ def run(
self.app.error(f"Error analyzing {document.uri.to_path()}: {item}")
else:
diagnostics.extend(item)
if diagnostics:
yield DocumentDiagnosticReport(document, diagnostics)

yield DocumentDiagnosticReport(document, diagnostics)

self.app.verbose(f"Collect Diagnostics for {len(documents)} documents")
for document in documents:
Expand All @@ -125,8 +125,8 @@ def run(
self.app.error(f"Error collecting diagnostics for {document.uri.to_path()}: {item}")
else:
diagnostics.extend(item)
if diagnostics:
yield DocumentDiagnosticReport(document, diagnostics)

yield DocumentDiagnosticReport(document, diagnostics)

def collect_documents(
self, folder: WorkspaceFolder, paths: Iterable[Path] = {}, filter: Iterable[str] = {}
Expand Down

0 comments on commit 18b6cb8

Please sign in to comment.