rspec/rules/S6203/kotlin/rule.adoc

56 lines
1.5 KiB
Plaintext

Multi-line string literals representing a big chunk of text should not be used directly in complex expressions, as it decreases the code's readability. It is preferred to extract the string literal and store it in a variable or a field that can be accessed in the context of the complex expression.
This rule reports an issue when a multi-line string literal consists of more lines than specified in the rule's parameter and is directly used within a lambda literal.
== Noncompliant Code Example
----
listOfString
.map { str ->
str != """
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
</project>
"""
}
----
== Compliant Solution
----
val myTextBlock = """
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
</project>
"""
listOfString.map { str -> str != myTextBlock }
----
== See
* https://kotlinlang.org/spec/expressions.html#string-interpolation-expressions[8.2 String interpolation expressions] in the Kotlin language specification