Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove reference to rule.Rule in mapResolver interface #1873

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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(r *rule.Rule, pkgRel string) Resolver
mrslv func(ruleKind, pkgRel string) Resolver
crossResolvers []CrossResolver

// The underlying state of rules. All indexing should be reproducible from this.
Expand Down Expand Up @@ -107,7 +107,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 @@ -129,7 +129,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 {
fmeum marked this conversation as resolved.
Show resolved Hide resolved
var crossResolvers []CrossResolver
for _, e := range exts {
if cr, ok := e.(CrossResolver); ok {
Expand All @@ -154,7 +154,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 @@ -171,7 +171,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 @@ -219,7 +219,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 @@ -228,7 +228,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