@@ -712,6 +712,21 @@ def _join_call_hint(aliases: dict[str, str]) -> re.Pattern[str]:
712712 return re .compile (r"\b(?:" + "|" .join (sorted (re .escape (name ) for name in names )) + r")\s*\(" )
713713
714714
715+ def _fragment_whole_file_line_offset (fragment : str , whole_content : str ) -> int | None :
716+ """Return how many whole-file lines precede a windowed view fragment.
717+
718+ Windowed view fragments are contiguous slices of the scanned file, so a
719+ whole-file AST line number maps onto a fragment-relative line by
720+ subtracting this offset. Return ``None`` when the fragment is not a
721+ slice of the whole file (for example a normalized view), so callers keep
722+ the standalone-parse fallback.
723+ """
724+ start = whole_content .find (fragment )
725+ if start < 0 :
726+ return None
727+ return whole_content .count ("\n " , 0 , start )
728+
729+
715730def _constructed_sensitive_paths (
716731 content : str ,
717732 file_path : str ,
@@ -728,45 +743,77 @@ def _constructed_sensitive_paths(
728743 without reparsing tricks. The scan's shared parse is reused whenever this
729744 runs inside the runner (the cache key published by node()); standalone
730745 callers get a single on-demand parse. Windowed view fragments under a scan
731- miss the whole-file cache entry and are parsed directly behind the same
732- textual join-or-import gate, so large files keep their findings instead of
733- silently dropping them. On such a fragment the whole file's import-alias
746+ miss the whole-file cache entry; behind the same textual join-or-import
747+ gate they are evaluated against the whole file's cached tree instead of
748+ being parsed standalone, so a fragment starting mid-block (for example
749+ inside a function body, which is not a valid module on its own) keeps its
750+ findings instead of silently dropping them. The whole file's import-alias
734751 map is carried in from the scan cache for the join gate and call
735752 resolution, so a renamed ``os.path.join`` spelling (``from os.path import
736753 join as j``) is recognized even when the import lives in an earlier window
737- than the call. Only fully-literal positional argument lists are resolved;
738- anything dynamic is left to the existing pattern loop. Unparseable content
739- simply yields no findings here.
754+ than the call. Call spans map onto fragment-relative lines and only
755+ calls starting inside the fragment are owned by it, preserving the
756+ runner's source-coordinate and dedupe behavior. When the whole-file tree
757+ is unavailable, or the fragment is not a slice of the whole file
758+ (normalized views), the fragment is parsed directly as before. Only
759+ fully-literal positional argument lists are resolved; anything dynamic is
760+ left to the existing pattern loop. Unparseable content simply yields no
761+ findings here.
740762 """
741763 whole_file_aliases : dict [str , str ] | None = None
764+ whole_file_tree : ast .Module | None = None
765+ whole_file_source : str | None = None
766+ whole_file_line_offset : int | None = None
742767 if python_ast is None :
743768 cache_key = _scan_python_ast_cache_key .get ()
744769 if cache_key is not None :
745770 python_ast = peek_python_ast (cache_key , content , file_path )
746771 if python_ast is None :
747772 # A windowed view fragment, not the scan's whole file: the
748773 # shared tree does not cover this slice. Carry the whole
749- # file's import-alias map from the scan cache first (a
750- # fragment is a slice of the same scanned file), so the
774+ # file's import-alias map and tree from the scan cache first
775+ # (a fragment is a slice of the same scanned file), so the
751776 # textual gate below also fires on renamed call spellings
752777 # like ``j(`` when the ``from os.path import join as j``
753778 # import lives in an earlier window. Fragments without a
754779 # plausible join call or join import never parse.
755780 whole_file = peek_python_ast_any_content (cache_key , file_path )
756781 if whole_file is not None and whole_file .tree is not None :
757782 whole_file_aliases = whole_file .import_aliases
783+ whole_file_tree = whole_file .tree
784+ whole_file_source = whole_file .content
758785 if _join_call_hint (whole_file_aliases or {}).search (
759786 content
760787 ) or _JOIN_IMPORT_HINT .search (content ):
761- python_ast = parse_python_source (content , file_path )
788+ if whole_file_tree is not None and whole_file_source is not None :
789+ # Evaluate the whole-file tree rather than parsing the
790+ # fragment standalone: a slice starting mid-block is
791+ # not a valid module, so its standalone parse fails
792+ # and the finding would be silently dropped.
793+ whole_file_line_offset = _fragment_whole_file_line_offset (
794+ content , whole_file_source
795+ )
796+ if whole_file_line_offset is None :
797+ python_ast = parse_python_source (content , file_path )
762798 else :
763799 python_ast = parse_python_source (content , file_path )
764- if python_ast is None :
765- return []
766- tree = python_ast .tree
767- if tree is None :
768- return []
769- aliases = whole_file_aliases if whole_file_aliases is not None else python_ast .import_aliases
800+ if whole_file_tree is not None and whole_file_line_offset is not None :
801+ tree = whole_file_tree
802+ aliases = whole_file_aliases or {}
803+ owned_span : tuple [int , int ] | None = (
804+ whole_file_line_offset ,
805+ whole_file_line_offset + len (content .splitlines ()),
806+ )
807+ else :
808+ if python_ast is None :
809+ return []
810+ tree = python_ast .tree
811+ if tree is None :
812+ return []
813+ aliases = (
814+ whole_file_aliases if whole_file_aliases is not None else python_ast .import_aliases
815+ )
816+ owned_span = None
770817 if not _join_call_hint (aliases ).search (content ):
771818 return []
772819 resolved : list [tuple [int , int , str , float ]] = []
@@ -781,9 +828,19 @@ def _constructed_sensitive_paths(
781828 if len (parts ) != len (node .args ) or not all (isinstance (part , str ) for part in parts ):
782829 continue
783830 value = posixpath .join (* parts )
831+ start_line = node .lineno
832+ end_line = node .end_lineno or node .lineno
833+ if owned_span is not None :
834+ # Only calls starting inside this fragment are owned by it; the
835+ # runner's own owned-range filter and cross-window dedupe keep
836+ # exactly one finding per call at whole-file coordinates.
837+ if not owned_span [0 ] < start_line <= owned_span [1 ]:
838+ continue
839+ start_line -= owned_span [0 ]
840+ end_line -= owned_span [0 ]
784841 for pattern , confidence in PE3_PATTERNS :
785842 if re .search (pattern , value , re .IGNORECASE ):
786- resolved .append ((node . lineno , node . end_lineno or node . lineno , value , confidence ))
843+ resolved .append ((start_line , end_line , value , confidence ))
787844 break
788845 return resolved
789846
0 commit comments