-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow @tags only to accept lowercase alphanumeric and _
- Loading branch information
Showing
4 changed files
with
51 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...plugin/extension-processor/src/main/kotlin/com/typewritermc/verification/TagsValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,48 @@ | ||
package com.typewritermc.verification | ||
|
||
import com.google.devtools.ksp.KspExperimental | ||
import com.google.devtools.ksp.getAnnotationsByType | ||
import com.google.devtools.ksp.processing.Resolver | ||
import com.google.devtools.ksp.processing.SymbolProcessor | ||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment | ||
import com.google.devtools.ksp.processing.SymbolProcessorProvider | ||
import com.google.devtools.ksp.symbol.KSAnnotated | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
import com.typewritermc.core.extension.annotations.Tags | ||
import com.typewritermc.processors.fullName | ||
|
||
private val regex = Regex("^[a-z0-9_]+$") | ||
|
||
class TagsValidator : SymbolProcessor { | ||
@OptIn(KspExperimental::class) | ||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
val entries = resolver.getSymbolsWithAnnotation(Tags::class.qualifiedName!!) | ||
val invalidEntries = entries | ||
.map { it to it.getAnnotationsByType(Tags::class).first() } | ||
.flatMap { (entry, annotation) -> | ||
annotation.tags.filter { !it.matches(regex) }.map { tag -> | ||
if (entry !is KSClassDeclaration) return@map tag | ||
"${entry.fullName}: $tag" | ||
} | ||
} | ||
.toList() | ||
|
||
if (invalidEntries.isEmpty()) return emptyList() | ||
throw InvalidTagsException(invalidEntries) | ||
} | ||
} | ||
|
||
class InvalidTagsException(tags: List<String>) : Exception( | ||
""" | ||
|Tags can only contain lowercase letters, numbers and underscores. | ||
|The following tags are invalid: | ||
| - ${tags.joinToString("\n - ")} | ||
| | ||
""".trimMargin() | ||
) | ||
|
||
class TagsValidatorProvider : SymbolProcessorProvider { | ||
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor { | ||
return TagsValidator() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters