Skip to content

Commit

Permalink
remove unnecessary deep directives (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcj authored Nov 27, 2023
1 parent 8a8b31c commit 1593ce0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
10 changes: 6 additions & 4 deletions language/scala/existing_scala_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ func (s *existingScalaRule) Resolve(rctx *scalarule.ResolveContext, importsRaw i

// part 1a: deps

depLabels := sc.cleanDeps(r.Attr("deps"))
mergeDeps(r.Kind(), depLabels, imports.Deps(sc.maybeRewrite(r.Kind(), rctx.From)))
newImports := imports.Deps(sc.maybeRewrite(r.Kind(), rctx.From))
depLabels := sc.cleanDeps(rctx.From, r.Attr("deps"), newImports)
mergeDeps(r.Kind(), depLabels, newImports)
if len(depLabels.List) > 0 {
r.SetAttr("deps", depLabels)
} else {
Expand All @@ -141,8 +142,9 @@ func (s *existingScalaRule) Resolve(rctx *scalarule.ResolveContext, importsRaw i

// part 1b: exports
if strings.HasSuffix(r.Kind(), "_library") {
exportLabels := sc.cleanExports(r.Attr("exports"))
mergeDeps(r.Kind(), exportLabels, exports.Deps(sc.maybeRewrite(r.Kind(), rctx.From)))
newExports := exports.Deps(sc.maybeRewrite(r.Kind(), rctx.From))
exportLabels := sc.cleanExports(rctx.From, r.Attr("exports"), newExports)
mergeDeps(r.Kind(), exportLabels, newExports)
if len(exportLabels.List) > 0 {
r.SetAttr("exports", exportLabels)
} else {
Expand Down
24 changes: 22 additions & 2 deletions language/scala/scala_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,22 @@ func mergeDeps(kind string, target *build.ListExpr, deps []label.Label) {

// cleanExports takes the given list of exports and removes those that are expected to
// be provided again.
func (c *scalaConfig) cleanExports(current build.Expr) *build.ListExpr {
func (c *scalaConfig) cleanExports(from label.Label, current build.Expr, newExports []label.Label) *build.ListExpr {
incoming := make(map[label.Label]bool)
for _, l := range newExports {
incoming[l] = true
}

exports := &build.ListExpr{}
if current != nil {
if listExpr, ok := current.(*build.ListExpr); ok {
for _, expr := range listExpr.List {
if c.shouldKeepExport(expr) {
dep := labelFromDepExpr(expr)
if rule.ShouldKeep(expr) && incoming[dep] {
log.Printf(`%v: in attr 'exports', "%v" does not need a '# keep' directive (fixed)`, from, dep)
continue
}
exports.List = append(exports.List, expr)
}
}
Expand All @@ -369,12 +379,22 @@ func (c *scalaConfig) cleanExports(current build.Expr) *build.ListExpr {

// cleanDeps takes the given list of deps and removes those that are expected to
// be provided again.
func (c *scalaConfig) cleanDeps(current build.Expr) *build.ListExpr {
func (c *scalaConfig) cleanDeps(from label.Label, current build.Expr, newImports []label.Label) *build.ListExpr {
incoming := make(map[label.Label]bool)
for _, l := range newImports {
incoming[l] = true
}

deps := &build.ListExpr{}
if current != nil {
if listExpr, ok := current.(*build.ListExpr); ok {
for _, expr := range listExpr.List {
if c.shouldKeepDep(expr) {
dep := labelFromDepExpr(expr)
if rule.ShouldKeep(expr) && incoming[dep] {
log.Printf(`%v: in attr 'deps', "%v" does not need a '# keep' directive (fixed)`, from, dep)
continue
}
deps.List = append(deps.List, expr)
}
}
Expand Down
4 changes: 4 additions & 0 deletions language/scala/testdata/override_provider/BUILD.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ load("@io_bazel_rules_scala//scala:scala.bzl", "scala_library")
scala_library(
name = "app",
srcs = ["Main.scala"],
deps = [
# demonstrate that scala-gazelle removes un-necessary 'keep' directives
"@maven//:com_typesafe_scala_logging_scala_logging_2_12", # keep
],
)

0 comments on commit 1593ce0

Please sign in to comment.