Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ class BasePackage(g: Global) extends AnalyzerRule(g, "basePackage") {

import global._

object SkipImports {
@tailrec def unapply(stats: List[Tree]): Some[List[Tree]] = stats match {
case Import(_, _) :: tail => unapply(tail)
case stats => Some(stats)
}
}

def analyze(unit: CompilationUnit): Unit = if (argument != null) {
val requiredBasePackage = argument

@tailrec def validate(tree: Tree): Unit = tree match {
case PackageDef(pid, _) if pid.symbol.hasPackageFlag && pid.symbol.fullName == requiredBasePackage =>
case PackageDef(_, List(stat)) => validate(stat)
case PackageDef(_, SkipImports(List(stat))) => validate(stat)
case t => report(t.pos, s"`$requiredBasePackage` must be one of the base packages in this file")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,36 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {
|""".stripMargin)
}

test("base package object with imports") {
assertNoErrors(
"""
|package com.avsystem
|
|import scala.collection.mutable.Seq
|import scala.collection.mutable.Set
|
|package object commons
|""".stripMargin)
}

test("no base package") {
assertErrors(1,
"""
|object bar
|""".stripMargin)
}

test("no base package with imports") {
assertErrors(1,
"""
|import scala.collection.mutable.Seq
|import scala.collection.mutable.Set
|
|object bar
|""".stripMargin)
}


test("wrong base package") {
assertErrors(1,
"""
Expand All @@ -68,4 +91,16 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {
|object bar
|""".stripMargin)
}

test("unchained subpackage with imports") {
assertErrors(1,
"""
|package com.avsystem.commons.core
|
|import scala.collection.mutable.Seq
|import scala.collection.mutable.Set
|
|object bar
|""".stripMargin)
}
}
2 changes: 1 addition & 1 deletion docs/Analyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Here's a list of currently supported rules:
| `discardedMonixTask` | warning | Makes sure that expressions evaluating to Monix `Task`s are not accidentally discarded by conversion to `Unit` |
| `throwableObjects` | warning | Makes sure that objects are never used as `Throwable`s (unless they have stack traces disabled) |
| `constantDeclarations` | off | Checks if constants are always declared as `final val`s with `UpperCamelCase` and no explicit type annotation for literal values |
| `basePackage | warning | Checks if all sources are within the specified base package |
| `basePackage` | warning | Checks if all sources are within the specified base package |

Rules may be enabled and disabled in `build.sbt` with Scala compiler options:

Expand Down