From 32193544acf74965e8e4700dd61121f43db72b22 Mon Sep 17 00:00:00 2001 From: Marcel Joss Date: Mon, 29 May 2023 12:02:10 +0200 Subject: [PATCH 1/2] feat(semantic): add warning when providers and consumers are not in their own files --- .../semantic/SemanticModelPreProcessor.kt | 21 ++++++++ .../tools/samt/semantic/SemanticModelTest.kt | 48 +++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelPreProcessor.kt b/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelPreProcessor.kt index 2c9db0a7..203754d7 100644 --- a/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelPreProcessor.kt +++ b/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelPreProcessor.kt @@ -41,8 +41,29 @@ internal class SemanticModelPreProcessor(private val controller: DiagnosticContr } } + private fun reportFileSeparation(file: FileNode) { + val statements = file.statements + if (statements.size <= 1) { + return + } + for (provider in statements.filterIsInstance()) { + 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()) { + 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) { 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 } diff --git a/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt b/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt index 9a0b2156..228ffe75 100644 --- a/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt +++ b/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt @@ -1244,6 +1244,54 @@ class SemanticModelTest { } } + @Nested + inner class FileSeparation { + @Test + fun `provider in file with other types is warning`() { + val source = """ + package separation + + 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 + + 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>, ): SemanticModel { From d3e09ea95a196323dab81966b85c65c243e0befa Mon Sep 17 00:00:00 2001 From: Marcel Joss Date: Mon, 29 May 2023 12:07:24 +0200 Subject: [PATCH 2/2] feat(semantic): only warn if there are ten or more statements in a file --- .../semantic/SemanticModelPreProcessor.kt | 2 +- .../tools/samt/semantic/SemanticModelTest.kt | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelPreProcessor.kt b/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelPreProcessor.kt index 203754d7..97cd8364 100644 --- a/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelPreProcessor.kt +++ b/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelPreProcessor.kt @@ -43,7 +43,7 @@ internal class SemanticModelPreProcessor(private val controller: DiagnosticContr private fun reportFileSeparation(file: FileNode) { val statements = file.statements - if (statements.size <= 1) { + if (statements.size < 10) { return } for (provider in statements.filterIsInstance()) { diff --git a/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt b/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt index 228ffe75..7686c458 100644 --- a/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt +++ b/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt @@ -1251,6 +1251,16 @@ class SemanticModelTest { 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 { @@ -1269,6 +1279,16 @@ class SemanticModelTest { 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 {