From 46ece3ab2cb10baf1cc065bf50d29100f0edaf89 Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Thu, 15 Aug 2024 21:35:43 -0700 Subject: [PATCH] refactor: remove reference to rule.Rule in resolver interface --- cmd/gazelle/fix-update.go | 2 +- cmd/gazelle/metaresolver.go | 6 +++--- language/go/resolve_test.go | 6 +++--- language/proto/resolve_test.go | 4 ++-- resolve/index.go | 14 +++++++------- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/cmd/gazelle/fix-update.go b/cmd/gazelle/fix-update.go index a4c3b32fe..93d4b0885 100644 --- a/cmd/gazelle/fix-update.go +++ b/cmd/gazelle/fix-update.go @@ -488,7 +488,7 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) { for _, v := range visits { for i, r := range v.rules { from := label.New(c.RepoName, v.pkgRel, r.Name()) - if rslv := mrslv.Resolver(r, v.pkgRel); rslv != nil { + if rslv := mrslv.Resolver(r.Kind(), v.pkgRel); rslv != nil { rslv.Resolve(v.c, ruleIndex, rc, r, v.imports[i], from) } } diff --git a/cmd/gazelle/metaresolver.go b/cmd/gazelle/metaresolver.go index 86d5e44e4..31329b553 100644 --- a/cmd/gazelle/metaresolver.go +++ b/cmd/gazelle/metaresolver.go @@ -53,9 +53,9 @@ func (mr *metaResolver) MappedKind(pkgRel string, kind config.MappedKind) { // Resolver returns a resolver for the given rule and package, and a bool // indicating whether one was found. Empty string may be passed for pkgRel, // which results in consulting the builtin kinds only. -func (mr *metaResolver) Resolver(r *rule.Rule, pkgRel string) resolve.Resolver { +func (mr *metaResolver) Resolver(ruleKind string, pkgRel string) resolve.Resolver { for _, mappedKind := range mr.mappedKinds[pkgRel] { - if mappedKind.KindName == r.Kind() { + if mappedKind.KindName == ruleKind { fromKindResolver := mr.builtins[mappedKind.FromKind] if fromKindResolver == nil { return nil @@ -66,7 +66,7 @@ func (mr *metaResolver) Resolver(r *rule.Rule, pkgRel string) resolve.Resolver { } } } - return mr.builtins[r.Kind()] + return mr.builtins[ruleKind] } // inverseMapKindResolver applies an inverse of the map_kind diff --git a/language/go/resolve_test.go b/language/go/resolve_test.go index c489007a3..981e135a4 100644 --- a/language/go/resolve_test.go +++ b/language/go/resolve_test.go @@ -973,7 +973,7 @@ go_proto_library( } ix.Finish() for i, r := range f.Rules { - mrslv.Resolver(r, "").Resolve(c, ix, rc, r, imports[i], label.New("", tc.old.rel, r.Name())) + mrslv.Resolver(r.Kind(), "").Resolve(c, ix, rc, r, imports[i], label.New("", tc.old.rel, r.Name())) } f.Sync() got := strings.TrimSpace(string(bzl.Format(f.File))) @@ -1319,6 +1319,6 @@ func convertImportsAttr(r *rule.Rule) interface{} { type mapResolver map[string]resolve.Resolver -func (mr mapResolver) Resolver(r *rule.Rule, f string) resolve.Resolver { - return mr[r.Kind()] +func (mr mapResolver) Resolver(ruleKind, f string) resolve.Resolver { + return mr[ruleKind] } diff --git a/language/proto/resolve_test.go b/language/proto/resolve_test.go index ec9700e73..4b60d9638 100644 --- a/language/proto/resolve_test.go +++ b/language/proto/resolve_test.go @@ -511,6 +511,6 @@ func convertImportsAttr(r *rule.Rule) interface{} { type mapResolver map[string]resolve.Resolver -func (mr mapResolver) Resolver(r *rule.Rule, f string) resolve.Resolver { - return mr[r.Kind()] +func (mr mapResolver) Resolver(ruleKind, f string) resolve.Resolver { + return mr[ruleKind] } diff --git a/resolve/index.go b/resolve/index.go index 745d8645a..8111d6512 100644 --- a/resolve/index.go +++ b/resolve/index.go @@ -76,7 +76,7 @@ type RuleIndex struct { rules []*ruleRecord labelMap map[label.Label]*ruleRecord importMap map[ImportSpec][]*ruleRecord - mrslv func(r *rule.Rule, pkgRel string) Resolver + mrslv func(ruleKind, pkgRel string) Resolver crossResolvers []CrossResolver // Whether another rule of the same language embeds this rule. @@ -96,7 +96,7 @@ type RuleIndex struct { // ruleRecord contains information about a rule relevant to import indexing. type ruleRecord struct { - rule *rule.Rule + kind string label label.Label pkg string @@ -118,7 +118,7 @@ type ruleRecord struct { // // kindToResolver is a map from rule kinds (for example, "go_library") to // Resolvers that support those kinds. -func NewRuleIndex(mrslv func(r *rule.Rule, pkgRel string) Resolver, exts ...interface{}) *RuleIndex { +func NewRuleIndex(mrslv func(ruleKind, pkgRel string) Resolver, exts ...interface{}) *RuleIndex { var crossResolvers []CrossResolver for _, e := range exts { if cr, ok := e.(CrossResolver); ok { @@ -144,7 +144,7 @@ func (ix *RuleIndex) AddRule(c *config.Config, r *rule.Rule, f *rule.File) { l := label.New(c.RepoName, f.Pkg, r.Name()) - if rslv := ix.mrslv(r, f.Pkg); rslv != nil { + if rslv := ix.mrslv(r.Kind(), f.Pkg); rslv != nil { lang = rslv.Name() if passesLanguageFilter(c.Langs, lang) { imps = rslv.Imports(c, r, f) @@ -161,7 +161,7 @@ func (ix *RuleIndex) AddRule(c *config.Config, r *rule.Rule, f *rule.File) { } record := &ruleRecord{ - rule: r, + kind: r.Kind(), pkg: f.Pkg, label: l, importedAs: imps, @@ -206,7 +206,7 @@ func (ix *RuleIndex) collectRecordEmbeds(r *ruleRecord, didCollectEmbeds map[lab if _, ok := didCollectEmbeds[r.label]; ok { return } - resolver := ix.mrslv(r.rule, r.pkg) + resolver := ix.mrslv(r.kind, r.pkg) didCollectEmbeds[r.label] = true ix.embeds[r.label] = r.embeds for _, e := range r.embeds { @@ -215,7 +215,7 @@ func (ix *RuleIndex) collectRecordEmbeds(r *ruleRecord, didCollectEmbeds map[lab continue } ix.collectRecordEmbeds(er, didCollectEmbeds) - erResolver := ix.mrslv(er.rule, er.pkg) + erResolver := ix.mrslv(er.kind, er.pkg) if resolver.Name() == erResolver.Name() { ix.embedded[er.label] = struct{}{} ix.embeds[r.label] = append(ix.embeds[r.label], ix.embeds[er.label]...)