Skip to content

Commit 38df5bf

Browse files
authored
Merge pull request #34 from samtkit/file-separation
Warn when providers and consumers are not in their own files
2 parents 4cbd9d4 + d3e09ea commit 38df5bf

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

semantic/src/main/kotlin/tools/samt/semantic/SemanticModelPreProcessor.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,29 @@ internal class SemanticModelPreProcessor(private val controller: DiagnosticContr
4141
}
4242
}
4343

44+
private fun reportFileSeparation(file: FileNode) {
45+
val statements = file.statements
46+
if (statements.size < 10) {
47+
return
48+
}
49+
for (provider in statements.filterIsInstance<ProviderDeclarationNode>()) {
50+
controller.getOrCreateContext(provider.location.source).warn {
51+
message("Provider declaration should be in its own file")
52+
highlight("provider declaration", provider.location, highlightBeginningOnly = true)
53+
}
54+
}
55+
for (consumer in statements.filterIsInstance<ConsumerDeclarationNode>()) {
56+
controller.getOrCreateContext(consumer.location.source).warn {
57+
message("Consumer declaration should be in its own file")
58+
highlight("consumer declaration", consumer.location, highlightBeginningOnly = true)
59+
}
60+
}
61+
}
62+
4463
fun fillPackage(samtPackage: Package, files: List<FileNode>) {
4564
for (file in files) {
65+
reportFileSeparation(file)
66+
4667
var parentPackage = samtPackage
4768
for (component in file.packageDeclaration.name.components) {
4869
var subPackage = parentPackage.subPackages.find { it.name == component.name }

semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,74 @@ class SemanticModelTest {
12441244
}
12451245
}
12461246

1247+
@Nested
1248+
inner class FileSeparation {
1249+
@Test
1250+
fun `provider in file with other types is warning`() {
1251+
val source = """
1252+
package separation
1253+
1254+
record A {}
1255+
record B {}
1256+
record C {}
1257+
record D {}
1258+
record E {}
1259+
record F {}
1260+
record G {}
1261+
record H {}
1262+
record I {}
1263+
1264+
service TestService {}
1265+
1266+
provide TestProvider {
1267+
implements TestService
1268+
1269+
transport http
1270+
}
1271+
""".trimIndent()
1272+
parseAndCheck(
1273+
source to listOf("Warning: Provider declaration should be in its own file")
1274+
)
1275+
}
1276+
1277+
@Test
1278+
fun `consumer in file with other types is warning`() {
1279+
val source = """
1280+
package separation
1281+
1282+
record A {}
1283+
record B {}
1284+
record C {}
1285+
record D {}
1286+
record E {}
1287+
record F {}
1288+
record G {}
1289+
record H {}
1290+
record I {}
1291+
1292+
service TestService {}
1293+
1294+
consume TestProvider {
1295+
uses TestService
1296+
}
1297+
""".trimIndent()
1298+
val providerSource = """
1299+
package separation
1300+
1301+
provide TestProvider {
1302+
implements TestService
1303+
1304+
transport http
1305+
}
1306+
""".trimIndent()
1307+
1308+
parseAndCheck(
1309+
source to listOf("Warning: Consumer declaration should be in its own file"),
1310+
providerSource to emptyList()
1311+
)
1312+
}
1313+
}
1314+
12471315
private fun parseAndCheck(
12481316
vararg sourceAndExpectedMessages: Pair<String, List<String>>,
12491317
): SemanticModel {

0 commit comments

Comments
 (0)