Skip to content

Commit 18b6cb8

Browse files
committed
fix(analyze): corrected statistics about analyzed files
1 parent c5f766f commit 18b6cb8

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

packages/analyze/src/robotcode/analyze/cli.py

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from enum import Flag
22
from pathlib import Path
33
from textwrap import indent
4-
from typing import List, Optional, Set, Tuple
4+
from typing import List, Optional, Set, Tuple, Union
55

66
import click
77

@@ -56,16 +56,49 @@ class ReturnCode(Flag):
5656

5757
class Statistic:
5858
def __init__(self) -> None:
59-
self.folders: Set[WorkspaceFolder] = set()
60-
self.files: Set[TextDocument] = set()
61-
self.errors = 0
62-
self.warnings = 0
63-
self.infos = 0
64-
self.hints = 0
59+
self._folders: Set[WorkspaceFolder] = set()
60+
self._files: Set[TextDocument] = set()
61+
self._diagnostics: List[Union[DocumentDiagnosticReport, FolderDiagnosticReport]] = []
62+
63+
@property
64+
def errors(self) -> int:
65+
return sum(
66+
len([i for i in e.items if i.severity == DiagnosticSeverity.ERROR]) for e in self._diagnostics if e.items
67+
)
68+
69+
@property
70+
def warnings(self) -> int:
71+
return sum(
72+
len([i for i in e.items if i.severity == DiagnosticSeverity.WARNING]) for e in self._diagnostics if e.items
73+
)
74+
75+
@property
76+
def infos(self) -> int:
77+
return sum(
78+
len([i for i in e.items if i.severity == DiagnosticSeverity.INFORMATION])
79+
for e in self._diagnostics
80+
if e.items
81+
)
82+
83+
@property
84+
def hints(self) -> int:
85+
return sum(
86+
len([i for i in e.items if i.severity == DiagnosticSeverity.HINT]) for e in self._diagnostics if e.items
87+
)
88+
89+
def add_diagnostics_report(
90+
self, diagnostics_report: Union[DocumentDiagnosticReport, FolderDiagnosticReport]
91+
) -> None:
92+
self._diagnostics.append(diagnostics_report)
93+
94+
if isinstance(diagnostics_report, FolderDiagnosticReport):
95+
self._folders.add(diagnostics_report.folder)
96+
elif isinstance(diagnostics_report, DocumentDiagnosticReport):
97+
self._files.add(diagnostics_report.document)
6598

6699
def __str__(self) -> str:
67100
return (
68-
f"Files: {len(self.files)}, Errors: {self.errors}, Warnings: {self.warnings}, "
101+
f"Files: {len(self._files)}, Errors: {self.errors}, Warnings: {self.warnings}, "
69102
f"Infos: {self.infos}, Hints: {self.hints}"
70103
)
71104

@@ -192,7 +225,6 @@ def code(
192225
193226
The return code is a bitwise combination of the following values:
194227
195-
\b
196228
- `0`: **SUCCESS** - No issues detected.
197229
- `1`: **ERRORS** - Critical issues found.
198230
- `2`: **WARNINGS** - Non-critical issues detected.
@@ -284,20 +316,17 @@ def code(
284316
robot_profile=robot_profile,
285317
root_folder=root_folder,
286318
).run(paths=paths, filter=filter):
287-
if isinstance(e, FolderDiagnosticReport):
288-
statistics.folders.add(e.folder)
319+
statistics.add_diagnostics_report(e)
289320

321+
if isinstance(e, FolderDiagnosticReport):
290322
if e.items:
291-
_print_diagnostics(app, root_folder, statistics, e.items, e.folder.uri.to_path())
292-
323+
_print_diagnostics(app, root_folder, e.items, e.folder.uri.to_path())
293324
elif isinstance(e, DocumentDiagnosticReport):
294-
statistics.files.add(e.document)
295-
296325
doc_path = (
297326
e.document.uri.to_path().relative_to(root_folder) if root_folder else e.document.uri.to_path()
298327
)
299328
if e.items:
300-
_print_diagnostics(app, root_folder, statistics, e.items, doc_path)
329+
_print_diagnostics(app, root_folder, e.items, doc_path)
301330

302331
statistics_str = str(statistics)
303332
if statistics.errors > 0:
@@ -314,23 +343,13 @@ def code(
314343
def _print_diagnostics(
315344
app: Application,
316345
root_folder: Optional[Path],
317-
statistics: Statistic,
318346
diagnostics: List[Diagnostic],
319347
folder_path: Optional[Path],
320348
print_range: bool = True,
321349
) -> None:
322350
for item in diagnostics:
323351
severity = item.severity if item.severity is not None else DiagnosticSeverity.ERROR
324352

325-
if severity == DiagnosticSeverity.ERROR:
326-
statistics.errors += 1
327-
elif severity == DiagnosticSeverity.WARNING:
328-
statistics.warnings += 1
329-
elif severity == DiagnosticSeverity.INFORMATION:
330-
statistics.infos += 1
331-
elif severity == DiagnosticSeverity.HINT:
332-
statistics.hints += 1
333-
334353
app.echo(
335354
(
336355
(

packages/analyze/src/robotcode/analyze/code_analyzer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def run(
9393
self.app.error(f"Error analyzing {folder.uri.to_path()}: {item}")
9494
else:
9595
diagnostics.extend(item)
96-
if diagnostics:
97-
yield FolderDiagnosticReport(folder, diagnostics)
96+
97+
yield FolderDiagnosticReport(folder, diagnostics)
9898

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

@@ -110,8 +110,8 @@ def run(
110110
self.app.error(f"Error analyzing {document.uri.to_path()}: {item}")
111111
else:
112112
diagnostics.extend(item)
113-
if diagnostics:
114-
yield DocumentDiagnosticReport(document, diagnostics)
113+
114+
yield DocumentDiagnosticReport(document, diagnostics)
115115

116116
self.app.verbose(f"Collect Diagnostics for {len(documents)} documents")
117117
for document in documents:
@@ -125,8 +125,8 @@ def run(
125125
self.app.error(f"Error collecting diagnostics for {document.uri.to_path()}: {item}")
126126
else:
127127
diagnostics.extend(item)
128-
if diagnostics:
129-
yield DocumentDiagnosticReport(document, diagnostics)
128+
129+
yield DocumentDiagnosticReport(document, diagnostics)
130130

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

0 commit comments

Comments
 (0)