Skip to content

Commit 9ec4d1b

Browse files
committed
Handle imports when checking required base package
1 parent 854d9e2 commit 9ec4d1b

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

commons-analyzer/src/main/scala/com/avsystem/commons/analyzer/BasePackage.scala

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,25 @@ class BasePackage(g: Global) extends AnalyzerRule(g, "basePackage") {
88

99
import global._
1010

11+
object ImportsList {
12+
@tailrec def unapply(stats: List[Tree]): Option[Tree] =
13+
stats match {
14+
case Nil => None
15+
case List(stat) => Some(stat)
16+
case head :: next =>
17+
head match {
18+
case Import(_, _) => unapply(next)
19+
case _ => None
20+
}
21+
}
22+
}
23+
1124
def analyze(unit: CompilationUnit): Unit = if (argument != null) {
1225
val requiredBasePackage = argument
1326

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

commons-analyzer/src/test/scala/com/avsystem/commons/analyzer/BasePackageTest.scala

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,36 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {
4444
|""".stripMargin)
4545
}
4646

47+
test("base package object with imports") {
48+
assertNoErrors(
49+
"""
50+
|package com.avsystem
51+
|
52+
|import scala.collection.mutable.Seq
53+
|import scala.collection.mutable.Set
54+
|
55+
|package object commons
56+
|""".stripMargin)
57+
}
58+
4759
test("no base package") {
4860
assertErrors(1,
4961
"""
5062
|object bar
5163
|""".stripMargin)
5264
}
5365

66+
test("no base package with imports") {
67+
assertErrors(1,
68+
"""
69+
|import scala.collection.mutable.Seq
70+
|import scala.collection.mutable.Set
71+
|
72+
|object bar
73+
|""".stripMargin)
74+
}
75+
76+
5477
test("wrong base package") {
5578
assertErrors(1,
5679
"""
@@ -68,4 +91,16 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {
6891
|object bar
6992
|""".stripMargin)
7093
}
94+
95+
test("unchained subpackage with imports") {
96+
assertErrors(1,
97+
"""
98+
|package com.avsystem.commons.core
99+
|
100+
|import scala.collection.mutable.Seq
101+
|import scala.collection.mutable.Set
102+
|
103+
|object bar
104+
|""".stripMargin)
105+
}
71106
}

docs/Analyzer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Here's a list of currently supported rules:
3030
| `discardedMonixTask` | warning | Makes sure that expressions evaluating to Monix `Task`s are not accidentally discarded by conversion to `Unit` |
3131
| `throwableObjects` | warning | Makes sure that objects are never used as `Throwable`s (unless they have stack traces disabled) |
3232
| `constantDeclarations` | off | Checks if constants are always declared as `final val`s with `UpperCamelCase` and no explicit type annotation for literal values |
33-
| `basePackage | warning | Checks if all sources are within the specified base package |
33+
| `basePackage` | warning | Checks if all sources are within the specified base package |
3434

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

0 commit comments

Comments
 (0)