Skip to content

Commit

Permalink
refactor: remove reference to rule.Rule in resolver interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Aug 19, 2024
1 parent 9ada797 commit 46ece3a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmd/gazelle/fix-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/gazelle/metaresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions language/go/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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]
}
4 changes: 2 additions & 2 deletions language/proto/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
14 changes: 7 additions & 7 deletions resolve/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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]...)
Expand Down

0 comments on commit 46ece3a

Please sign in to comment.