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 @@ -41,8 +41,29 @@ internal class SemanticModelPreProcessor(private val controller: DiagnosticContr
}
}

private fun reportFileSeparation(file: FileNode) {
val statements = file.statements
if (statements.size < 10) {
return
}
for (provider in statements.filterIsInstance<ProviderDeclarationNode>()) {
controller.getOrCreateContext(provider.location.source).warn {
message("Provider declaration should be in its own file")
highlight("provider declaration", provider.location, highlightBeginningOnly = true)
}
}
for (consumer in statements.filterIsInstance<ConsumerDeclarationNode>()) {
controller.getOrCreateContext(consumer.location.source).warn {
message("Consumer declaration should be in its own file")
highlight("consumer declaration", consumer.location, highlightBeginningOnly = true)
}
}
}

fun fillPackage(samtPackage: Package, files: List<FileNode>) {
for (file in files) {
reportFileSeparation(file)

var parentPackage = samtPackage
for (component in file.packageDeclaration.name.components) {
var subPackage = parentPackage.subPackages.find { it.name == component.name }
Expand Down
68 changes: 68 additions & 0 deletions semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,74 @@ class SemanticModelTest {
}
}

@Nested
inner class FileSeparation {
@Test
fun `provider in file with other types is warning`() {
val source = """
package separation

record A {}
record B {}
record C {}
record D {}
record E {}
record F {}
record G {}
record H {}
record I {}

service TestService {}

provide TestProvider {
implements TestService

transport http
}
""".trimIndent()
parseAndCheck(
source to listOf("Warning: Provider declaration should be in its own file")
)
}

@Test
fun `consumer in file with other types is warning`() {
val source = """
package separation

record A {}
record B {}
record C {}
record D {}
record E {}
record F {}
record G {}
record H {}
record I {}

service TestService {}

consume TestProvider {
uses TestService
}
""".trimIndent()
val providerSource = """
package separation

provide TestProvider {
implements TestService

transport http
}
""".trimIndent()

parseAndCheck(
source to listOf("Warning: Consumer declaration should be in its own file"),
providerSource to emptyList()
)
}
}

private fun parseAndCheck(
vararg sourceAndExpectedMessages: Pair<String, List<String>>,
): SemanticModel {
Expand Down