Skip to content

Commit

Permalink
Add checkstyle suppressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchallen committed Oct 30, 2022
1 parent 229d820 commit 0df9b41
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ allprojects {
}
subprojects {
group = "com.github.cs125-illinois.jeed"
version = "2022.10.6"
version = "2022.10.7"
tasks.withType<Test> {
useJUnitPlatform()
enableAssertions = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2022.10.6
version=2022.10.7
25 changes: 13 additions & 12 deletions core/src/main/kotlin/Checkstyle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import javax.xml.xpath.XPathFactory
data class CheckstyleArguments(
val sources: Set<String>? = null,
val failOnError: Boolean = false,
val skipUnmapped: Boolean = true
val skipUnmapped: Boolean = true,
val suppressions: Set<String> = setOf()
)

@JsonClass(generateAdapter = true)
Expand Down Expand Up @@ -164,38 +165,38 @@ suspend fun Source.checkstyle(
sources.filter {
names.contains(it.key)
}
).values.flatten().mapNotNull {
).values.flatten().mapNotNull { error ->
val mappedLocation = try {
mapLocation(it.location)
mapLocation(error.location)
} catch (e: SourceMappingException) {
if (!checkstyleArguments.skipUnmapped) {
throw e
}
null
}
if (mappedLocation != null) {
val message = if (it.key?.startsWith("indentation") == true) {
if (mappedLocation != null && error.key !in checkstyleArguments.suppressions) {
val message = if (error.key?.startsWith("indentation") == true) {
@Suppress("TooGenericExceptionCaught")
try {
val addedIndent = leadingIndentation(it.location)
val addedIndent = leadingIndentation(error.location)
val (incorrectMessage, incorrectAmount) =
incorrectLevelRegex.find(it.message)?.groups?.let { match ->
incorrectLevelRegex.find(error.message)?.groups?.let { match ->
Pair(match[0]?.value, match[1]?.value?.toInt())
} ?: error("Couldn't parse indentation error")
val (expectedMessage, expectedAmount) =
expectedLevelRegex.find(it.message)?.groups?.let { match ->
expectedLevelRegex.find(error.message)?.groups?.let { match ->
Pair(match[0]?.value, match[1]?.value?.toInt())
} ?: error("Couldn't parse indentation error")
it.message
error.message
.replace(incorrectMessage!!, "incorrect indentation level ${incorrectAmount!! - addedIndent}")
.replace(expectedMessage!!, "expected level should be ${expectedAmount!! - addedIndent}")
} catch (_: Exception) {
it.message
error.message
}
} else {
it.message
error.message
}
CheckstyleError(it.severity, it.key, mappedLocation, message)
CheckstyleError(error.severity, error.key, mappedLocation, message)
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2022.10.6
version=2022.10.7
9 changes: 9 additions & 0 deletions core/src/test/kotlin/TestCheckstyle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,15 @@ public int add(int a, int b) {
it shouldNot haveCheckstyleErrors()
}
}
"should allow suppressions" {
Source.fromSnippet(
"""
for (int i = 0; i < 10; i++);
""".trim()
).checkstyle(CheckstyleArguments(suppressions = setOf("empty.statement"))).also {
it shouldNot haveCheckstyleErrors()
}
}
})

fun haveCheckstyleErrors() = object : Matcher<CheckstyleResults> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2022.10.6
version=2022.10.7
6 changes: 6 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
rootProject.name = "jeed"
include("core", "server", "containerrunner", "leaktest")
pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}
}

0 comments on commit 0df9b41

Please sign in to comment.