forked from bazelbuild/bazel-gazelle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.go
55 lines (44 loc) · 1.46 KB
/
lang.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package visibility
import (
"flag"
"strings"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/language"
"github.com/bazelbuild/bazel-gazelle/rule"
)
type visibilityExtension struct{}
// NewLanguage constructs a new language.Language modifying visibility.
func NewLanguage() language.Language {
return &visibilityExtension{}
}
// Kinds instructs gazelle to match any 'package' rule as BUILD files can only have one.
func (*visibilityExtension) Kinds() map[string]rule.KindInfo {
return map[string]rule.KindInfo{
"package": {
MatchAny: true,
MergeableAttrs: map[string]bool{
"default_visibility": true,
},
},
}
}
// Loads noops because there are no imports to add
func (*visibilityExtension) Loads() []rule.LoadInfo {
return nil
}
// GenerateRules does the hard work of setting the default_visibility if a config exists.
func (*visibilityExtension) GenerateRules(args language.GenerateArgs) language.GenerateResult {
res := language.GenerateResult{}
cfg := getVisConfig(args.Config)
if len(cfg.visibilityTargets) == 0 {
return res
}
r := rule.NewRule("package", "")
r.SetAttr("default_visibility", cfg.visibilityTargets)
res.Gen = append(res.Gen, r)
// we have to add a nil to Imports because there is length-matching validation with Gen.
res.Imports = append(res.Imports, nil)
return res
}
// Fix noop because there is nothing out there to fix yet
func (*visibilityExtension) Fix(c *config.Config, f *rule.File) {}