Skip to content

Commit

Permalink
Merge pull request #34 from samtkit/file-separation
Browse files Browse the repository at this point in the history
Warn when providers and consumers are not in their own files
  • Loading branch information
mjossdev authored May 30, 2023
2 parents 4cbd9d4 + d3e09ea commit 38df5bf
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
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

0 comments on commit 38df5bf

Please sign in to comment.