-
Notifications
You must be signed in to change notification settings - Fork 39
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
Feature. Ktlint rule wrapper #744
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bd6e597
feature/ktlint-rule-wrapper(#606)
aktsay6 84cf9d4
* feature/ktlint-rule-wrapper(#606)
aktsay6 019ea91
* feature/ktlint-rule-wrapper(#606)
aktsay6 a5d178e
feature/ktlint-rule-wrapper(#606)
aktsay6 988ed05
feature/ktlint-rule-wrapper(#606)
aktsay6 6ab619b
feature/ktlint-rule-wrapper(#606)
aktsay6 d72624f
feature/ktlint-rule-wrapper(#606)
aktsay6 b79fd2e
feature/ktlint-rule-wrapper(#606)
aktsay6 59b993d
feature/ktlint-rule-wrapper(#606)
aktsay6 b847b98
feature/ktlint-rule-wrapper(#606)
aktsay6 004a54b
feature/ktlint-rule-wrapper(#606)
aktsay6 aeb3242
feature/ktlint-rule-wrapper(#606)
aktsay6 66663ec
Merge branch 'master' into feature/ktlint-rule-wrapper(#606)
aktsay6 240d4a7
feature/ktlint-rule-wrapper(#606)
aktsay6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.cqfn.diktat.ruleset.rules | ||
|
||
import org.cqfn.diktat.common.config.rules.RulesConfig | ||
import org.cqfn.diktat.common.config.rules.isRuleEnabled | ||
import org.cqfn.diktat.ruleset.constants.EmitType | ||
import org.cqfn.diktat.ruleset.utils.log | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import kotlin.system.exitProcess | ||
|
||
private typealias DiktatConfigRule = org.cqfn.diktat.common.config.rules.Rule | ||
|
||
/** | ||
* This is a wrapper around Ktlint Rule | ||
* | ||
* @param id id of the rule | ||
* @property configRules all rules from configuration | ||
* @property inspections warnings that are used in the rule's code | ||
*/ | ||
@Suppress("TooGenericExceptionCaught") | ||
abstract class DiktatRule(id: String, | ||
val configRules: List<RulesConfig>, | ||
val inspections: List<DiktatConfigRule>) : Rule(id) { | ||
/** | ||
* Default value is false | ||
*/ | ||
var isFixMode: Boolean = false | ||
|
||
/** | ||
* Will be initialized in visit | ||
*/ | ||
lateinit var emitWarn: EmitType | ||
|
||
override fun visit(node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: EmitType) { | ||
emitWarn = emit | ||
isFixMode = autoCorrect | ||
|
||
if (areInspectionsDisabled()) { | ||
return | ||
} else { | ||
try { | ||
logic(node) | ||
} catch (internalError: Throwable) { | ||
log.error("Internal error has occurred in $id. Please make an issue on this bug at https://github.com/cqfn/diKTat/.", internalError) | ||
log.error("As a workaround you can disable these inspections in yml config: $inspections") | ||
exitProcess(1) | ||
} | ||
} | ||
} | ||
|
||
private fun areInspectionsDisabled(): Boolean = | ||
inspections.none { configRules.isRuleEnabled(it) } | ||
|
||
/** | ||
* Logic of the rule | ||
* | ||
* @param node node that are coming from visit | ||
*/ | ||
abstract fun logic(node: ASTNode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ package org.cqfn.diktat.ruleset.rules.chapter1 | |
import org.cqfn.diktat.common.config.rules.RuleConfiguration | ||
import org.cqfn.diktat.common.config.rules.RulesConfig | ||
import org.cqfn.diktat.common.config.rules.getRuleConfig | ||
import org.cqfn.diktat.ruleset.constants.EmitType | ||
import org.cqfn.diktat.ruleset.constants.Warnings.BACKTICKS_PROHIBITED | ||
import org.cqfn.diktat.ruleset.constants.Warnings.CLASS_NAME_INCORRECT | ||
import org.cqfn.diktat.ruleset.constants.Warnings.CONFUSING_IDENTIFIER_NAMING | ||
|
@@ -18,6 +17,7 @@ import org.cqfn.diktat.ruleset.constants.Warnings.OBJECT_NAME_INCORRECT | |
import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_HAS_PREFIX | ||
import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_NAME_INCORRECT | ||
import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_NAME_INCORRECT_FORMAT | ||
import org.cqfn.diktat.ruleset.rules.DiktatRule | ||
import org.cqfn.diktat.ruleset.utils.Style | ||
import org.cqfn.diktat.ruleset.utils.containsOneLetterOrZero | ||
import org.cqfn.diktat.ruleset.utils.findChildAfter | ||
|
@@ -42,7 +42,6 @@ import org.cqfn.diktat.ruleset.utils.toLowerCamelCase | |
import org.cqfn.diktat.ruleset.utils.toPascalCase | ||
import org.cqfn.diktat.ruleset.utils.toUpperSnakeCase | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.ast.ElementType | ||
import com.pinterest.ktlint.core.ast.ElementType.CATCH | ||
import com.pinterest.ktlint.core.ast.ElementType.CATCH_KEYWORD | ||
|
@@ -80,7 +79,11 @@ import org.jetbrains.kotlin.psi.psiUtil.parents | |
* // FixMe: because it fixes only declaration without the usages | ||
*/ | ||
@Suppress("ForbiddenComment", "MISSING_KDOC_CLASS_ELEMENTS") | ||
class IdentifierNaming(private val configRules: List<RulesConfig>) : Rule("identifier-naming") { | ||
class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule("identifier-naming", configRules, | ||
listOf(BACKTICKS_PROHIBITED, VARIABLE_NAME_INCORRECT, VARIABLE_NAME_INCORRECT_FORMAT, CONSTANT_UPPERCASE, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this pass diktat formatting rule? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. possible bug in diktat? |
||
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)) { | ||
private val allMethodPrefixes by lazy { | ||
if (configuration.allowedBooleanPrefixes.isNullOrEmpty()) { | ||
booleanMethodPrefixes | ||
|
@@ -93,17 +96,10 @@ class IdentifierNaming(private val configRules: List<RulesConfig>) : Rule("ident | |
this.configRules.getRuleConfig(FUNCTION_BOOLEAN_PREFIX)?.configuration ?: emptyMap() | ||
) | ||
} | ||
private var isFixMode: Boolean = false | ||
private lateinit var emitWarn: EmitType | ||
|
||
override fun visit( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: EmitType | ||
override fun logic( | ||
node: ASTNode | ||
) { | ||
isFixMode = autoCorrect | ||
emitWarn = emit | ||
|
||
// backticks are prohibited for identifier declarations everywhere except test methods that are marked with @Test annotation | ||
if (isIdentifierWithBackticks(node)) { | ||
return | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
damn, catching all exceptions? I do not know if it is a good idea, @petertrr think about this here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, isn't this what we were trying to achieve? Otherwise these exceptions are caught by ktlint and they just log a warning as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am saying that simply doing catch (Exception e) is a bad practice. May be there are different types of exceptions in ktlint to handle BEFORE we will catch all remaining exeptions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything that is thrown from
rule.visit
is catched in Ktlint.kt withcatch (e: Exception)
. So their idea is that the visitor either handles all exceptions, or they are considered internal errors.