Skip to content

Commit

Permalink
Remove check for Int.MAX and Int.MIN
Browse files Browse the repository at this point in the history
Remove corresponding Int tests
Make extension parameter non null
  • Loading branch information
atulgpt committed Jan 18, 2023
1 parent 247844f commit 2af4f64
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,19 @@ class StringShouldBeRawString(config: Config) : Rule(config) {

override fun visitStringTemplateExpression(expression: KtStringTemplateExpression) {
super.visitStringTemplateExpression(expression)
if (maxEscapedCharacterCount == Int.MAX_VALUE) {
return
}
val maxEscapedCharacterCount = this.maxEscapedCharacterCount.coerceAtLeast(0)
val expressionParent = expression.getParentExpressionAfterParenthesis()
val rootElement = expression.getRootExpression()
if (
expressionParent !is KtBinaryExpression ||
(rootElement != null && expression.isPivotElementInTheTree(rootElement))
) {
val shouldReport =
rootElement.getStringSequenceExcludingRawString().flatMap { stringTemplateExpressionText ->
REGEX_FOR_ESCAPE_CHARS.findAll(stringTemplateExpressionText).filter {
it.value !in ignoredCharacters
}
}.take(maxEscapedCharacterCount + 1).toList().size > maxEscapedCharacterCount
if (shouldReport) {
val stringSeqToProcess = rootElement?.getStringSequenceExcludingRawString() ?: sequenceOf("")
val hasNoViolations = stringSeqToProcess.flatMap { stringTemplateExpressionText ->
REGEX_FOR_ESCAPE_CHARS.findAll(stringTemplateExpressionText).filter {
it.value !in ignoredCharacters
}
}.drop(maxEscapedCharacterCount).none()
if (hasNoViolations.not()) {
report(
CodeSmell(
issue,
Expand All @@ -120,18 +116,19 @@ class StringShouldBeRawString(config: Config) : Rule(config) {
}
}

private fun KtElement?.getStringSequenceExcludingRawString(): Sequence<String> {
this ?: return sequence { yield("") }

fun KtElement?.getStringSequence(): Sequence<KtStringTemplateExpression> = sequence {
private fun KtElement.getStringSequenceExcludingRawString(): Sequence<String> {
fun KtElement.getStringSequence(): Sequence<KtStringTemplateExpression> = sequence {
if (this@getStringSequence is KtStringTemplateExpression) {
yield(this@getStringSequence)
} else if (this@getStringSequence is KtBinaryExpression) {
yieldAll(left?.deparenthesize().getStringSequence())
yieldAll(right?.deparenthesize().getStringSequence())
left?.let {
yieldAll(it.deparenthesize().getStringSequence())
}
right?.let {
yieldAll(it.deparenthesize().getStringSequence())
}
}
}

return this.getStringSequence().filter {
(it.text.startsWith("\"\"\"") && it.text.endsWith("\"\"\"")).not()
}.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ class StringShouldBeRawStringSpec {
assertThat(subject.findings).hasSize(2)
}

@Test
fun `does report in case of multiple violations in multiple expressions`() {
val code = """
fun test() {
val size1 = "\n + \n".length
val size2 = "\n + \n".length
}
""".trimIndent()
val subject = StringShouldBeRawString(TestConfig(mapOf(MAX_ESCAPED_CHARACTER_COUNT to 1)))
subject.compileAndLint(code)
assertThat(subject.findings).hasSize(2)
}

@Test
fun `does report in case of string has operator call with violations`() {
val code = """
Expand Down Expand Up @@ -302,19 +315,6 @@ class StringShouldBeRawStringSpec {
assertThat(subject.findings).hasSize(2)
}

@Test
fun `does report in case of multiple violations in multiple expressions`() {
val code = """
fun test() {
val size1 = "\n + \n".length
val size2 = "\n + \n".length
}
""".trimIndent()
val subject = StringShouldBeRawString(TestConfig(mapOf(MAX_ESCAPED_CHARACTER_COUNT to 1)))
subject.compileAndLint(code)
assertThat(subject.findings).hasSize(2)
}

@Test
fun `does not report in case of single line comment`() {
val code = """
Expand Down Expand Up @@ -369,54 +369,6 @@ class StringShouldBeRawStringSpec {
assertThat(subject.findings).isEmpty()
}

@Test
fun `does not report non violating string in case maximum violations is Int MAX`() {
val code = """
fun test() {
val size1 = "This rule is awesome".length
}
""".trimIndent()
val subject = StringShouldBeRawString(TestConfig(mapOf(MAX_ESCAPED_CHARACTER_COUNT to Int.MAX_VALUE)))
subject.compileAndLint(code)
assertThat(subject.findings).isEmpty()
}

@Test
fun `does not report one violating string in case maximum violations is Int MAX`() {
val code = """
fun test() {
val size1 = "This rule is awesome \n".length
}
""".trimIndent()
val subject = StringShouldBeRawString(TestConfig(mapOf(MAX_ESCAPED_CHARACTER_COUNT to Int.MAX_VALUE)))
subject.compileAndLint(code)
assertThat(subject.findings).isEmpty()
}

@Test
fun `does not report non violating string in case maximum violations is Int MIN`() {
val code = """
fun test() {
val size1 = "This rule is awesome".length
}
""".trimIndent()
val subject = StringShouldBeRawString(TestConfig(mapOf(MAX_ESCAPED_CHARACTER_COUNT to Int.MIN_VALUE)))
subject.compileAndLint(code)
assertThat(subject.findings).isEmpty()
}

@Test
fun `does not report one violating string in case maximum violations is Int MIN`() {
val code = """
fun test() {
val size1 = "This rule is awesome \n".length
}
""".trimIndent()
val subject = StringShouldBeRawString(TestConfig(mapOf(MAX_ESCAPED_CHARACTER_COUNT to Int.MIN_VALUE)))
subject.compileAndLint(code)
assertThat(subject.findings).hasSize(1)
}

companion object {
private const val MAX_ESCAPED_CHARACTER_COUNT = "maxEscapedCharacterCount"
private const val IGNORED_CHARACTERS = "ignoredCharacters"
Expand Down Expand Up @@ -574,7 +526,6 @@ class StringShouldBeRawStringSpec {
return Stream.of(
Arguments.of(""""\t\t\t"""", 3, listOf("\\t")),
Arguments.of(""""\\\\\\"""", 3, listOf("\\\\")),
Arguments.of(""""\n\n\n"""", 3, listOf("\\n")),
Arguments.of(""" "abc" + "\n" + "efg" + "\n" + "hij\n" """, 3, listOf("\\n")),
Arguments.of(""""\"[^\"\\\\\\n]*(?:\\\\.[^\"\\\\\\n]*)*\""""", 12, listOf("\\n", "\\\\", "\\\"")),
Arguments.of(""""In java new line char is \n"""", 1, listOf("\\n")),
Expand Down

0 comments on commit 2af4f64

Please sign in to comment.