Skip to content

Commit

Permalink
Backend: Add the regex 101 link when a pattern fails (hannibal002#3034)
Browse files Browse the repository at this point in the history
  • Loading branch information
CalMWolfs authored Dec 8, 2024
1 parent 0a3991e commit 18afe80
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 7 additions & 0 deletions detekt/src/main/kotlin/RepoPatternElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyDelegate
import org.jetbrains.kotlin.psi.KtStringTemplateEntryWithExpression
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import java.net.URLEncoder

class RepoPatternElement private constructor(
val variableName: String,
Expand All @@ -18,6 +19,12 @@ class RepoPatternElement private constructor(

val pattern by lazy { rawPattern.toPattern() }

val regex101Url: String by lazy {
val encodedPattern = URLEncoder.encode(rawPattern, "UTF-8")
val encodedTests = regexTests.joinToString("\n") { URLEncoder.encode(it, "UTF-8") }
"https://regex101.com/?regex=$encodedPattern&testString=$encodedTests"
}

companion object {
fun KtPropertyDelegate.asRepoPatternElement(): RepoPatternElement? {
val expression = this.expression as? KtDotQualifiedExpression ?: return null
Expand Down
6 changes: 4 additions & 2 deletions detekt/src/main/kotlin/repo/RepoPatternRegexTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ class RepoPatternRegexTest(config: Config) : SkyHanniRule(config) {

repoPatternElement.regexTests.forEach { test ->
if (!repoPatternElement.pattern.matcher(test).find()) {
delegate.reportIssue("Repo pattern `$variableName` failed regex test: `$test` pattern: `$rawPattern`.")
delegate.reportIssue("Repo pattern `$variableName` failed regex test: `$test` pattern: `$rawPattern`. " +
"[View on Regex101](${repoPatternElement.regex101Url})")
}
}

repoPatternElement.failingRegexTests.forEach { test ->
if (repoPatternElement.pattern.matcher(test).find()) {
delegate.reportIssue("Repo pattern `$variableName` passed regex test: `$test` pattern: `$rawPattern` even though it was set to fail.")
delegate.reportIssue("Repo pattern `$variableName` passed regex test: `$test` pattern: `$rawPattern` " +
"even though it was set to fail. [View on Regex101](${repoPatternElement.regex101Url})")
}
}
}
Expand Down

0 comments on commit 18afe80

Please sign in to comment.