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

Feature. Ktlint rule wrapper #744

Merged
merged 14 commits into from
Feb 4, 2021
Merged

Conversation

aktsay6
Copy link
Collaborator

@aktsay6 aktsay6 commented Feb 3, 2021

Introduced DiktatRule to wrap c.p.k.c.Rule

This pull request closes #606

### What's done:
  * Wrapper made
  * Class reworking hsa begun
### What's done:
  * Refactored more classes
  * Added error message
@aktsay6 aktsay6 added the enhancement New feature or request label Feb 3, 2021
@aktsay6 aktsay6 requested review from petertrr and orchestr7 February 3, 2021 10:47
@codecov
Copy link

codecov bot commented Feb 3, 2021

Codecov Report

Merging #744 (240d4a7) into master (d65d447) will decrease coverage by 0.42%.
The diff coverage is 93.23%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #744      +/-   ##
============================================
- Coverage     80.05%   79.63%   -0.43%     
- Complexity     1997     2003       +6     
============================================
  Files            95       96       +1     
  Lines          5119     5032      -87     
  Branches       1644     1604      -40     
============================================
- Hits           4098     4007      -91     
- Misses          242      246       +4     
  Partials        779      779              
Flag Coverage Δ Complexity Δ
unittests 79.63% <93.23%> (-0.43%) 0.00 <78.00> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ Complexity Δ
.../ruleset/rules/chapter6/classes/DataClassesRule.kt 68.75% <50.00%> (-1.25%) 30.00 <2.00> (ø)
...et/rules/chapter6/classes/SingleConstructorRule.kt 71.91% <50.00%> (-0.62%) 26.00 <1.00> (ø)
.../diktat/ruleset/rules/chapter5/LambdaLengthRule.kt 80.00% <66.66%> (-1.82%) 8.00 <1.00> (ø)
...set/rules/chapter6/ExtensionFunctionsInFileRule.kt 81.81% <66.66%> (-1.52%) 9.00 <2.00> (ø)
...kotlin/org/cqfn/diktat/ruleset/rules/DiktatRule.kt 75.00% <75.00%> (ø) 6.00 <6.00> (?)
...rules/chapter3/LongNumericalValuesSeparatedRule.kt 89.39% <80.00%> (-0.32%) 24.00 <1.00> (ø)
...g/cqfn/diktat/ruleset/rules/chapter1/FileNaming.kt 85.00% <100.00%> (-1.37%) 9.00 <1.00> (ø)
.../diktat/ruleset/rules/chapter1/IdentifierNaming.kt 74.66% <100.00%> (+0.34%) 84.00 <1.00> (ø)
...qfn/diktat/ruleset/rules/chapter1/PackageNaming.kt 91.76% <100.00%> (ø) 35.00 <1.00> (ø)
...at/ruleset/rules/chapter2/comments/CommentsRule.kt 90.47% <100.00%> (-0.30%) 26.00 <1.00> (ø)
... and 57 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d65d447...240d4a7. Read the comment docs.

import org.cqfn.diktat.ruleset.utils.log
import org.jetbrains.kotlin.com.intellij.lang.ASTNode

typealias DiktatConfigRule = org.cqfn.diktat.common.config.rules.Rule
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
typealias DiktatConfigRule = org.cqfn.diktat.common.config.rules.Rule
private typealias DiktatConfigRule = org.cqfn.diktat.common.config.rules.Rule

Is it used only in this file?

else {
try {
logic(node)
} catch (e: Exception) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch (e: Exception) {
} catch (e: Throwable) {

### What's done:
  * Fixed bugs
Copy link
Member

@petertrr petertrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK to me, @akuleshov7 please take a look. I remember you had some considerations whether to terminate application on internal errors.

@aktsay6 aktsay6 force-pushed the feature/ktlint-rule-wrapper(#606) branch 2 times, most recently from 351c9b0 to 019ea91 Compare February 3, 2021 12:19
### What's done:
  * Fixed bugs
### What's done:
  * Fixed bugs
### What's done:
  * Fixed bugs
### What's done:
  * Fixed bugs
### What's done:
  * Fixed bugs
### What's done:
  * Fixed bugs
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/.\n Error: ${internalError.message}")
Copy link
Member

@orchestr7 orchestr7 Feb 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, exactly what @petertrr has mentioned. On the internal error we should stop diktat (exit 1) and SUGGEST to disable a rule where an issue happened

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

} else {
try {
logic(node)
} catch (internalError: Throwable) {
Copy link
Member

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

Copy link
Member

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.

Copy link
Member

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

Copy link
Member

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 with catch (e: Exception). So their idea is that the visitor either handles all exceptions, or they are considered internal errors.

@@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this pass diktat formatting rule?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible bug in diktat?

@Suppress("TooGenericExceptionCaught")
abstract class DiktatRule(id: String,
val configRules: List<RulesConfig>,
val rules: List<DiktatConfigRule>) : Rule(id) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please rename "rules" to "inspections". Who was writing a white paper? Me or you?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed

}

private fun areRulesEnabled(): Boolean =
rules.none { configRules.isRuleEnabled(it) }
Copy link
Member

@orchestr7 orchestr7 Feb 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct me if I'm wrong: here you check that ALL rules are enabled. And if one of them is not enabled you simply do not run the whole rule (that can contain other inspections)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test (sorry if I have missed it)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test and renamed a function

### What's done:
  * Fixed bugs
### What's done:
  * Fixed bugs
### What's done:
  * Fixed bugs
@aktsay6 aktsay6 requested review from petertrr and orchestr7 February 4, 2021 08:00
Copy link
Member

@petertrr petertrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@petertrr petertrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@orchestr7 orchestr7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@aktsay6 aktsay6 merged commit e51031c into master Feb 4, 2021
@aktsay6 aktsay6 deleted the feature/ktlint-rule-wrapper(#606) branch February 4, 2021 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce DiktatRule to wrap c.p.k.c.Rule
3 participants