Skip to content

Commit 3ee3c89

Browse files
author
Roman Janusz
authored
Merge pull request #434 from AVSystem/package-analyzer-rule-fix
Analyzer rule to check for a required base package - handle imports
2 parents 854d9e2 + ceb0e54 commit 3ee3c89

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

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

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

99
import global._
1010

11+
object SkipImports {
12+
@tailrec def unapply(stats: List[Tree]): Some[List[Tree]] = stats match {
13+
case Import(_, _) :: tail => unapply(tail)
14+
case stats => Some(stats)
15+
}
16+
}
17+
1118
def analyze(unit: CompilationUnit): Unit = if (argument != null) {
1219
val requiredBasePackage = argument
1320

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

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)