github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/language/bazel/visibility/lang.go (about)

     1  /* Copyright 2023 The Bazel Authors. All rights reserved.
     2  
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7     http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package visibility
    17  
    18  import (
    19  	"github.com/bazelbuild/bazel-gazelle/config"
    20  	"github.com/bazelbuild/bazel-gazelle/language"
    21  	"github.com/bazelbuild/bazel-gazelle/rule"
    22  )
    23  
    24  type visibilityExtension struct{}
    25  
    26  // NewLanguage constructs a new language.Language modifying visibility.
    27  func NewLanguage() language.Language {
    28  	return &visibilityExtension{}
    29  }
    30  
    31  // Kinds instructs gazelle to match any 'package' rule as BUILD files can only have one.
    32  func (*visibilityExtension) Kinds() map[string]rule.KindInfo {
    33  	return map[string]rule.KindInfo{
    34  		"package": {
    35  			MatchAny: true,
    36  			MergeableAttrs: map[string]bool{
    37  				"default_visibility": true,
    38  			},
    39  		},
    40  	}
    41  }
    42  
    43  func (*visibilityExtension) Loads() []rule.LoadInfo {
    44  	panic("ApparentLoads should be called instead")
    45  }
    46  
    47  // ApparentLoads noops because there are no imports to add
    48  func (*visibilityExtension) ApparentLoads(func(string) string) []rule.LoadInfo {
    49  	return nil
    50  }
    51  
    52  // GenerateRules does the hard work of setting the default_visibility if a config exists.
    53  func (*visibilityExtension) GenerateRules(args language.GenerateArgs) language.GenerateResult {
    54  	res := language.GenerateResult{}
    55  	cfg := getVisConfig(args.Config)
    56  
    57  	if len(cfg.visibilityTargets) == 0 {
    58  		return res
    59  	}
    60  
    61  	if args.File == nil {
    62  		// No need to create a visibility if we're not in a visible directory.
    63  		return res
    64  	}
    65  
    66  	r := rule.NewRule("package", "")
    67  	r.SetAttr("default_visibility", cfg.visibilityTargets)
    68  
    69  	res.Gen = append(res.Gen, r)
    70  	// we have to add a nil to Imports because there is length-matching validation with Gen.
    71  	res.Imports = append(res.Imports, nil)
    72  	return res
    73  }
    74  
    75  // Fix noop because there is nothing out there to fix yet
    76  func (*visibilityExtension) Fix(c *config.Config, f *rule.File) {}