Skip to content

Commit

Permalink
Revert "refactor: remove reference to rule.Rule in mapResolver interf…
Browse files Browse the repository at this point in the history
…ace (bazel-contrib#1873)"

This reverts commit 3ddf7ee.
  • Loading branch information
tyler-french committed Aug 28, 2024
1 parent 1a98d35 commit 268a993
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/gazelle/fix-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,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.Kind(), v.pkgRel); rslv != nil {
if rslv := mrslv.Resolver(r, 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(ruleKind string, pkgRel string) resolve.Resolver {
func (mr *metaResolver) Resolver(r *rule.Rule, pkgRel string) resolve.Resolver {
for _, mappedKind := range mr.mappedKinds[pkgRel] {
if mappedKind.KindName == ruleKind {
if mappedKind.KindName == r.Kind() {
fromKindResolver := mr.builtins[mappedKind.FromKind]
if fromKindResolver == nil {
return nil
Expand All @@ -66,7 +66,7 @@ func (mr *metaResolver) Resolver(ruleKind string, pkgRel string) resolve.Resolve
}
}
}
return mr.builtins[ruleKind]
return mr.builtins[r.Kind()]
}

// 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.Kind(), "").Resolve(c, ix, rc, r, imports[i], label.New("", tc.old.rel, r.Name()))
mrslv.Resolver(r, "").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(ruleKind, f string) resolve.Resolver {
return mr[ruleKind]
func (mr mapResolver) Resolver(r *rule.Rule, f string) resolve.Resolver {
return mr[r.Kind()]
}
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(ruleKind, f string) resolve.Resolver {
return mr[ruleKind]
func (mr mapResolver) Resolver(r *rule.Rule, f string) resolve.Resolver {
return mr[r.Kind()]
}
13 changes: 8 additions & 5 deletions resolve/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type CrossResolver interface {
// RuleIndex is a table of rules in a workspace, indexed by label and by
// import path. Used by Resolver to map import paths to labels.
type RuleIndex struct {
mrslv func(ruleKind, pkgRel string) Resolver
mrslv func(r *rule.Rule, pkgRel string) Resolver
crossResolvers []CrossResolver

// The underlying state of rules. All indexing should be reproducible from this.
Expand Down Expand Up @@ -110,6 +110,8 @@ type RuleIndex struct {

// ruleRecord contains information about a rule relevant to import indexing.
type ruleRecord struct {
rule *rule.Rule

Kind string `json:"kind"`
Label label.Label `json:"label"`

Expand All @@ -132,7 +134,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(ruleKind, pkgRel string) Resolver, exts ...interface{}) *RuleIndex {
func NewRuleIndex(mrslv func(r *rule.Rule, pkgRel string) Resolver, exts ...interface{}) *RuleIndex {
var crossResolvers []CrossResolver
for _, e := range exts {
if cr, ok := e.(CrossResolver); ok {
Expand Down Expand Up @@ -161,7 +163,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.Kind(), f.Pkg); rslv != nil {
if rslv := ix.mrslv(r, f.Pkg); rslv != nil {
lang = rslv.Name()
if passesLanguageFilter(c.Langs, lang) {
imps = rslv.Imports(c, r, f)
Expand All @@ -178,6 +180,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,
Expand Down Expand Up @@ -228,7 +231,7 @@ func (ix *RuleIndex) collectRecordEmbeds(r *ruleRecord, didCollectEmbeds map[lab
if _, ok := didCollectEmbeds[r.Label]; ok {
return
}
resolver := ix.mrslv(r.Kind, r.Pkg)
resolver := ix.mrslv(r.rule, r.Pkg)
didCollectEmbeds[r.Label] = true
ix.embeds[r.Label] = r.Embeds
for _, e := range r.Embeds {
Expand All @@ -237,7 +240,7 @@ func (ix *RuleIndex) collectRecordEmbeds(r *ruleRecord, didCollectEmbeds map[lab
continue
}
ix.collectRecordEmbeds(er, didCollectEmbeds)
erResolver := ix.mrslv(er.Kind, er.Pkg)
erResolver := ix.mrslv(er.rule, 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 268a993

Please sign in to comment.