Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

94 lines
2.8 KiB
Go

package resolver
import (
"strings"
"github.com/zzet/gortex/internal/graph"
)
// UIKit directory-convention name-resolution. Like the SwiftUI pass, this is a
// fallback for the references sourcekit-lsp leaves unresolved: a `*ViewController`
// binds to /ViewControllers/ or /Controllers/, a `*Cell` to /Cells/,
// /TableViewCells/ or /CollectionViewCells/, and a `*Delegate` / `*DataSource`
// to a /Delegates/ or /DataSources/ directory (or, via the same-dir tier, the
// owning controller's own directory). Swift and Objective-C references are
// considered.
var (
uikitVCDirs = []string{"/ViewControllers/", "/Controllers/", "/ViewController/", "/Controller/"}
uikitCellDirs = []string{"/Cells/", "/TableViewCells/", "/CollectionViewCells/", "/Cell/"}
uikitDelegateDirs = []string{"/Delegates/", "/DataSources/", "/Delegate/", "/DataSource/"}
)
// ResolveUIKitRefs binds residual unresolved UIKit references to their
// directory-located definitions. Returns the count bound.
func ResolveUIKitRefs(g graph.Store) int {
if g == nil {
return 0
}
resolved := 0
var reindex []graph.EdgeReindex
for _, kind := range []graph.EdgeKind{graph.EdgeInstantiates, graph.EdgeReferences, graph.EdgeTypedAs, graph.EdgeCalls} {
for e := range g.EdgesByKind(kind) {
if e == nil || !graph.IsUnresolvedTarget(e.To) {
continue
}
name := graph.UnresolvedName(e.To)
if name == "" || strings.ContainsRune(name, '.') {
continue
}
dirs, ok := uikitDirsFor(name)
if !ok {
continue
}
fromFile := ""
if n := g.GetNode(e.From); n != nil {
fromFile = n.FilePath
}
if !isAppleSourceFile(fromFile) {
continue
}
targetID, conf := ResolveByConvention(g, name, "", dirs, fromFile)
if targetID == "" {
continue
}
oldTo := e.To
e.To = targetID
e.Origin = graph.OriginASTInferred
e.Confidence = conf
e.ConfidenceLabel = graph.ConfidenceLabelFor(e.Kind, conf)
StampSynthesized(e, SynthUIKitResolve)
reindex = append(reindex, graph.EdgeReindex{Edge: e, OldTo: oldTo})
resolved++
}
}
if len(reindex) > 0 {
g.ReindexEdges(reindex)
}
return resolved
}
// uikitDirsFor classifies a UIKit reference name into its convention dirs.
func uikitDirsFor(name string) ([]string, bool) {
switch {
case strings.HasSuffix(name, "ViewController"):
return uikitVCDirs, true
case strings.HasSuffix(name, "Cell"):
return uikitCellDirs, true
case strings.HasSuffix(name, "Delegate"), strings.HasSuffix(name, "DataSource"):
return uikitDelegateDirs, true
}
return nil, false
}
// isAppleSourceFile reports whether a path is a Swift or Objective-C source
// file — the only files the UIKit pass binds references from.
func isAppleSourceFile(p string) bool {
switch {
case strings.HasSuffix(p, ".swift"), strings.HasSuffix(p, ".m"),
strings.HasSuffix(p, ".mm"), strings.HasSuffix(p, ".h"):
return true
}
return false
}