Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new rule for debug print #1307

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ enum class Warnings(
FILE_NAME_MATCH_CLASS(true, "3.1.2", "file name is incorrect - it should match with the class described in it if there is the only one class declared"),
COLLAPSE_IF_STATEMENTS(true, "3.16.1", "avoid using redundant nested if-statements, which could be collapsed into a single one"),
CONVENTIONAL_RANGE(true, "3.17.1", "use conventional rule for range case"),
DEBUG_PRINT(false, "3.18.1", "avoid print/println for debug logging"),

// ======== chapter 4 ========
NULLABLE_PROPERTY_TYPE(true, "4.3.1", "try to avoid use of nullable types"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import org.jetbrains.kotlin.com.intellij.lang.ASTNode
* Dummy warning used for testing and debug purposes.
* Can be used in manual testing.
*/
class DummyWarning(configRules: List<RulesConfig>) : DiktatRule(
class DummyWarning(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
"dummy-rule",
configRules,
listOf(
Warnings.FILE_NAME_INCORRECT,
Warnings.FILE_NAME_MATCH_CLASS
)
),
prevId
) {
@Suppress("UNUSED")
private lateinit var filePath: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ abstract class DiktatRule(
id: String,
val configRules: List<RulesConfig>,
private val inspections: List<DiktatConfigRule>,
visitorModifiers: Set<VisitorModifier> = emptySet(),
) : Rule(id, visitorModifiers) {
prevId: String?,
) : Rule(id, visitorModifiers = prevId?.let { setOf(VisitorModifier.RunAfterRule(it)) } ?: emptySet()) {
/**
* Default value is false
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cqfn.diktat.ruleset.rules

import com.pinterest.ktlint.core.Rule
import org.cqfn.diktat.common.config.rules.DIKTAT_COMMON
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.RulesConfigReader
Expand Down Expand Up @@ -85,6 +86,7 @@ import org.jetbrains.kotlin.org.jline.utils.Levenshtein
import org.slf4j.LoggerFactory

import java.io.File
import kotlin.reflect.KFunction

/**
* this constant will be used everywhere in the code to mark usage of Diktat ruleset
Expand Down Expand Up @@ -221,15 +223,17 @@ class DiktatRuleSetProvider(private var diktatConfigFile: String = DIKTAT_ANALYS
::NewlinesRule, // newlines need to be inserted right before fixing indentation
::WhiteSpaceRule, // this rule should be after other rules that can cause wrong spacing
::IndentationRule, // indentation rule should be the last because it fixes formatting after all the changes done by previous rules

)
.map {
it.invoke(configRules)
}
.toTypedArray()
var prevRuleId: String? = null
val createdRules: List<Rule> = rules.map { function ->
val result: Rule = function.invoke(configRules, prevRuleId)
prevRuleId = result.id
return RuleSet(result.id)
}
val rulesArray: Array<Rule> = createdRules.toTypedArray()
return RuleSet(
DIKTAT_RULE_SET_ID,
*rules
rules = rulesArray
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import java.io.File
* Aggressive: In case file contains only one class on upper level - it should be named with the same name
*/
@Suppress("ForbiddenComment")
class FileNaming(configRules: List<RulesConfig>) : DiktatRule(
class FileNaming(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(FILE_NAME_INCORRECT, FILE_NAME_MATCH_CLASS)
listOf(FILE_NAME_INCORRECT, FILE_NAME_MATCH_CLASS),
prevId
) {
private lateinit var filePath: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ import java.util.Locale
* // FixMe: because it fixes only declaration without the usages
*/
@Suppress("ForbiddenComment", "MISSING_KDOC_CLASS_ELEMENTS")
class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
class IdentifierNaming(configRules: List<RulesConfig>, prevId: String? = null) : DiktatRule(
NAME_ID,
configRules,
listOf(BACKTICKS_PROHIBITED, VARIABLE_NAME_INCORRECT, VARIABLE_NAME_INCORRECT_FORMAT, CONSTANT_UPPERCASE,
VARIABLE_HAS_PREFIX, CONFUSING_IDENTIFIER_NAMING, GENERIC_NAME, CLASS_NAME_INCORRECT,
ENUM_VALUE, EXCEPTION_SUFFIX, FUNCTION_BOOLEAN_PREFIX, FUNCTION_NAME_INCORRECT_CASE,
IDENTIFIER_LENGTH, OBJECT_NAME_INCORRECT, TYPEALIAS_NAME_INCORRECT_CASE)
IDENTIFIER_LENGTH, OBJECT_NAME_INCORRECT, TYPEALIAS_NAME_INCORRECT_CASE),
prevId
) {
private val allMethodPrefixes by lazy {
if (configuration.allowedBooleanPrefixes.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ import java.util.concurrent.atomic.AtomicInteger
* package a.b.c.D -> then class D should be placed in a/b/c/ directories
*/
@Suppress("ForbiddenComment", "TOO_MANY_LINES_IN_LAMBDA")
class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
class PackageNaming(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(INCORRECT_PACKAGE_SEPARATOR, PACKAGE_NAME_INCORRECT_CASE, PACKAGE_NAME_MISSING,
PACKAGE_NAME_INCORRECT_PATH, PACKAGE_NAME_INCORRECT_PREFIX, PACKAGE_NAME_INCORRECT_SYMBOLS),
prevId
) {
private lateinit var domainName: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import org.slf4j.LoggerFactory
* No commented out code is allowed, including imports.
*/
@Suppress("ForbiddenComment")
class CommentsRule(configRules: List<RulesConfig>) : DiktatRule(
class CommentsRule(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(COMMENTED_OUT_CODE)
listOf(COMMENTED_OUT_CODE),
prevId
) {
private lateinit var ktPsiFactory: KtPsiFactory

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ import java.time.LocalDate
* 4) Ensure files with many or zero classes have proper description
*/
@Suppress("ForbiddenComment")
class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
class HeaderCommentRule(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE, HEADER_MISSING_OR_WRONG_COPYRIGHT, HEADER_NOT_BEFORE_PACKAGE,
HEADER_NOT_BEFORE_PACKAGE, HEADER_WRONG_FORMAT, WRONG_COPYRIGHT_YEAR),
prevId
) {
override fun logic(node: ASTNode) {
if (node.elementType == FILE && !node.getFilePath().isGradleScript()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
* * Leave one single space between the comment on the right side of the code and the code.
* * Comments in if else should be inside code blocks. Exception: General if comment
*/
class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
class CommentsFormatting(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(COMMENT_WHITE_SPACE, FIRST_COMMENT_NO_BLANK_LINE,
IF_ELSE_COMMENTS, WRONG_NEWLINES_AROUND_KDOC),
prevId
) {
/**
* @param node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ import org.jetbrains.kotlin.psi.psiUtil.siblings
* 2) All internal elements in class like class, property or function should be documented with KDoc
* 3) All properties declared in the primary constructor are documented using `@property` tag in class KDoc
*/
class KdocComments(configRules: List<RulesConfig>) : DiktatRule(
class KdocComments(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(KDOC_EXTRA_PROPERTY, KDOC_NO_CONSTRUCTOR_PROPERTY,
KDOC_NO_CONSTRUCTOR_PROPERTY_WITH_COMMENT, MISSING_KDOC_CLASS_ELEMENTS, MISSING_KDOC_TOP_LEVEL)
KDOC_NO_CONSTRUCTOR_PROPERTY_WITH_COMMENT, MISSING_KDOC_CLASS_ELEMENTS, MISSING_KDOC_TOP_LEVEL),
prevId
) {
private val config by lazy { configRules.getCommonConfiguration() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ import java.time.temporal.ChronoField
* 7) ensuring @since tag contains only versions and not dates
*/
@Suppress("ForbiddenComment")
class KdocFormatting(configRules: List<RulesConfig>) : DiktatRule(
class KdocFormatting(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(KDOC_CONTAINS_DATE_OR_AUTHOR, KDOC_EMPTY_KDOC, KDOC_NEWLINES_BEFORE_BASIC_TAGS, KDOC_NO_DEPRECATED_TAG,
KDOC_NO_EMPTY_TAGS, KDOC_NO_NEWLINES_BETWEEN_BASIC_TAGS, KDOC_NO_NEWLINE_AFTER_SPECIAL_TAGS,
KDOC_WRONG_SPACES_AFTER_TAG, KDOC_WRONG_TAGS_ORDER),
prevId
) {
private val basicTagsList = listOf(KDocKnownTag.PARAM, KDocKnownTag.RETURN, KDocKnownTag.THROWS)
private val specialTagNames = setOf("implSpec", "implNote", "apiNote")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ import org.jetbrains.kotlin.psi.psiUtil.referenceExpression
* Currently only `throw` keyword from this methods body is supported for `@throws` check.
*/
@Suppress("ForbiddenComment")
class KdocMethods(configRules: List<RulesConfig>) : DiktatRule(
class KdocMethods(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(KDOC_TRIVIAL_KDOC_ON_FUNCTION, KDOC_WITHOUT_PARAM_TAG, KDOC_WITHOUT_RETURN_TAG,
KDOC_WITHOUT_THROWS_TAG, MISSING_KDOC_ON_FUNCTION)
KDOC_WITHOUT_THROWS_TAG, MISSING_KDOC_ON_FUNCTION),
prevId
) {
/**
* @param node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
/**
* This rule makes each annotation applied to a class, method or constructor is on its own line. Except: if first annotation of constructor, class or method
*/
class AnnotationNewLineRule(configRules: List<RulesConfig>) : DiktatRule(
class AnnotationNewLineRule(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(ANNOTATION_NEW_LINE)
listOf(ANNOTATION_NEW_LINE),
prevId
) {
override fun logic(node: ASTNode) {
when (node.elementType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ import org.jetbrains.kotlin.psi.KtTryExpression
* - opening brace of lambda
* - braces around `else`/`catch`/`finally`/`while` (in `do-while` loop)
*/
class BlockStructureBraces(configRules: List<RulesConfig>) : DiktatRule(
class BlockStructureBraces(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(BRACES_BLOCK_STRUCTURE_ERROR),
prevId
) {
override fun logic(node: ASTNode) {
val configuration = BlockStructureBracesConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ import java.lang.RuntimeException
/**
* Rule that checks if the boolean expression can be simplified.
*/
class BooleanExpressionsRule(configRules: List<RulesConfig>) : DiktatRule(
class BooleanExpressionsRule(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(COMPLEX_BOOLEAN_EXPRESSION)
listOf(COMPLEX_BOOLEAN_EXPRESSION),
prevId
) {
override fun logic(node: ASTNode) {
if (node.elementType == CONDITION) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ import org.jetbrains.kotlin.psi.psiUtil.children
/**
* Rule that checks that all conditionals and loops have braces.
*/
class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatRule(
class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
listOf(NO_BRACES_IN_CONDITIONALS_AND_LOOPS),
prevId
) {
override fun logic(node: ASTNode) {
when (node.elementType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ import org.jetbrains.kotlin.psi.psiUtil.siblings
/**
* Rule that checks order of declarations inside classes, interfaces and objects.
*/
class ClassLikeStructuresOrderRule(configRules: List<RulesConfig>) : DiktatRule(
class ClassLikeStructuresOrderRule(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(BLANK_LINE_BETWEEN_PROPERTIES, WRONG_ORDER_IN_CLASS_LIKE_STRUCTURES)
listOf(BLANK_LINE_BETWEEN_PROPERTIES, WRONG_ORDER_IN_CLASS_LIKE_STRUCTURES),
prevId
) {
override fun logic(node: ASTNode) {
if (node.elementType == CLASS_BODY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ typealias PlaceOfWarningForCurrentNode = Pair<Int, ASTNode>
/**
* Rule for redundant nested if-statements, which could be collapsed into a single one
*/
class CollapseIfStatementsRule(configRules: List<RulesConfig>) : DiktatRule(
class CollapseIfStatementsRule(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(
Warnings.COLLAPSE_IF_STATEMENTS
)
),
prevId
) {
private val configuration by lazy {
CollapseIfStatementsConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafElement
* 2) If saveInitialFormattingForEnums is true then white spaces in enums will not be affected
*
*/
class ConsecutiveSpacesRule(configRules: List<RulesConfig>) : DiktatRule(
class ConsecutiveSpacesRule(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(TOO_MANY_CONSECUTIVE_SPACES),
prevId
) {
override fun logic(node: ASTNode) {
val configuration = TooManySpacesRuleConfiguration(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.cqfn.diktat.ruleset.rules.chapter3

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode

/**
* Rule checks that print or println are not in used (assumption: it's a debug logging)
*/
class DebugPrintRule(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(NAME_ID, configRules, listOf(), prevId) {

companion object {
const val NAME_ID = "aan-empty-block-structure"
}

override fun logic(node: ASTNode) {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import org.jetbrains.kotlin.psi.psiUtil.parents
/**
* Rule that checks if empty code blocks (`{ }`) are used and checks their formatting.
*/
class EmptyBlock(configRules: List<RulesConfig>) : DiktatRule(
class EmptyBlock(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(EMPTY_BLOCK_STRUCTURE_ERROR)
listOf(EMPTY_BLOCK_STRUCTURE_ERROR),
prevId
) {
override fun logic(node: ASTNode) {
val configuration = EmptyBlockStyleConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
/**
* Rule that checks enum classes formatting
*/
class EnumsSeparated(configRules: List<RulesConfig>) : DiktatRule(
class EnumsSeparated(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(ENUMS_SEPARATED),
prevId
) {
override fun logic(node: ASTNode) {
if (node.elementType == CLASS && node.hasChildOfType(CLASS_BODY) && node.isClassEnum()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ import java.net.URL
* Rule can fix long binary expressions in condition inside `if` and in property declarations and one line functions
*/
@Suppress("ForbiddenComment")
class LineLength(configRules: List<RulesConfig>) : DiktatRule(
class LineLength(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(LONG_LINE)
listOf(LONG_LINE),
prevId
) {
private val configuration by lazy {
LineLengthConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import java.lang.StringBuilder
/**
* Rule that checks if numerical separators (`_`) are used for long numerical literals
*/
class LongNumericalValuesSeparatedRule(configRules: List<RulesConfig>) : DiktatRule(
class LongNumericalValuesSeparatedRule(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(LONG_NUMERICAL_VALUES_SEPARATED)
listOf(LONG_NUMERICAL_VALUES_SEPARATED),
prevId
) {
override fun logic(node: ASTNode) {
val configuration = LongNumericalValuesConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import org.jetbrains.kotlin.psi.psiUtil.parents
/**
* Rule for magic number
*/
class MagicNumberRule(configRules: List<RulesConfig>) : DiktatRule(
class MagicNumberRule(configRules: List<RulesConfig>, prevId: String?) : DiktatRule(
NAME_ID,
configRules,
listOf(MAGIC_NUMBER)
listOf(MAGIC_NUMBER),
prevId
) {
private val configuration by lazy {
MagicNumberConfiguration(
Expand Down
Loading