|
74 | 74 | build_declared_marker_views, |
75 | 75 | ) |
76 | 76 | from skillspector.state import AnalyzerNodeResponse, SkillspectorState, transitive_remaining_seconds |
| 77 | +from skillspector.surface import COMMENTS, infer_surface |
77 | 78 |
|
78 | 79 | from .common import ( |
79 | 80 | LINE_BREAK_CHARS, |
@@ -493,6 +494,13 @@ def _markdown_context_prefix( |
493 | 494 | return "" |
494 | 495 |
|
495 | 496 |
|
| 497 | +# Separators that ``str.splitlines()`` honours but CPython's own line numbering |
| 498 | +# does not: vertical tab, form feed, file/group/record separators, NEL, and the |
| 499 | +# Unicode line/paragraph separators. They make the two coordinate systems used |
| 500 | +# below disagree -- see :func:`_refined_surface_line`. |
| 501 | +_EXOTIC_LINE_BREAKS = tuple(ch for ch in LINE_BREAK_CHARS if ch not in "\r\n") |
| 502 | + |
| 503 | + |
496 | 504 | def _normalize_license_line(line: str) -> str: |
497 | 505 | return " ".join(line.casefold().split()) |
498 | 506 |
|
@@ -676,8 +684,15 @@ def _is_env_file_reference_in_docs( |
676 | 684 | def analyzer_finding_to_finding( |
677 | 685 | af: AnalyzerFinding, |
678 | 686 | get_remediation_fn: Callable[[str], str] | None = None, |
| 687 | + *, |
| 688 | + line_text: str | None = None, |
679 | 689 | ) -> Finding: |
680 | | - """Convert an AnalyzerFinding (from any analyzer) to graph-state Finding.""" |
| 690 | + """Convert an AnalyzerFinding (from any analyzer) to graph-state Finding. |
| 691 | +
|
| 692 | + ``line_text`` is the matched source line when the caller has it. It only |
| 693 | + refines a ``code``/``config`` path into the ``comments`` surface, so callers |
| 694 | + that cannot supply content still receive a path-derived classification. |
| 695 | + """ |
681 | 696 | rem_fn = get_remediation_fn or get_remediation |
682 | 697 | remediation = af.remediation or rem_fn(af.rule_id) |
683 | 698 | category = (af.tags[0] if af.tags else None) or get_category(af.rule_id) |
@@ -705,6 +720,7 @@ def analyzer_finding_to_finding( |
705 | 720 | intent=None, |
706 | 721 | evidence=dict(af.evidence), |
707 | 722 | match_fingerprint=af.match_fingerprint, |
| 723 | + surface=infer_surface(af.location.file, line_text), |
708 | 724 | ) |
709 | 725 |
|
710 | 726 |
|
@@ -959,7 +975,37 @@ def _convert_analyzer_finding( |
959 | 975 | for triage_tag in ("contextual-triage", "likely-benign-context"): |
960 | 976 | if triage_tag not in af.tags: |
961 | 977 | af.tags.append(triage_tag) |
962 | | - return analyzer_finding_to_finding(af) |
| 978 | + return analyzer_finding_to_finding( |
| 979 | + af, line_text=_refined_surface_line(af, path, content, content_lines) |
| 980 | + ) |
| 981 | + |
| 982 | + |
| 983 | +def _refined_surface_line( |
| 984 | + af: AnalyzerFinding, |
| 985 | + path: str, |
| 986 | + content: str, |
| 987 | + content_lines: list[str], |
| 988 | +) -> str | None: |
| 989 | + """The matched source line, or ``None`` when line numbering is ambiguous. |
| 990 | +
|
| 991 | + Findings from the pattern modules number lines with |
| 992 | + :func:`logical_line_starts`, which breaks on ``_EXOTIC_LINE_BREAKS`` as well |
| 993 | + as CR/LF; Python AST findings use CPython's physical ``\n`` count instead. |
| 994 | + The two agree only when the source contains none of those extra separators, |
| 995 | + so for such files the refinement is skipped rather than indexing |
| 996 | + ``content_lines`` with a line number from the other system -- which would |
| 997 | + otherwise report a neighbouring line's surface. |
| 998 | + """ |
| 999 | + if any(ch in content for ch in _EXOTIC_LINE_BREAKS): |
| 1000 | + return None |
| 1001 | + start_line = af.location.start_line |
| 1002 | + end_line = af.location.end_line or start_line |
| 1003 | + start_index = max(0, start_line - 1) |
| 1004 | + end_index = min(len(content_lines), end_line) |
| 1005 | + covered_lines = content_lines[start_index:end_index] |
| 1006 | + if covered_lines and all(infer_surface(path, text) == COMMENTS for text in covered_lines): |
| 1007 | + return covered_lines[0] |
| 1008 | + return None |
963 | 1009 |
|
964 | 1010 |
|
965 | 1011 | def _scan_path( |
|
0 commit comments