-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Insert suppression for class-signature, function-signature and parame…
…ter-list-wrapping at a class or function level Closes #2588
- Loading branch information
1 parent
41ae540
commit 1188519
Showing
4 changed files
with
145 additions
and
8 deletions.
There are no files selected for viewing
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
52 changes: 52 additions & 0 deletions
52
...ain/kotlin/com/pinterest/ktlint/rule/engine/internal/KtlintSuppressionTargetNodeFinder.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,52 @@ | ||
package com.pinterest.ktlint.rule.engine.internal | ||
|
||
import com.pinterest.ktlint.rule.engine.api.KtlintSuppression | ||
import com.pinterest.ktlint.rule.engine.core.api.ElementType | ||
import com.pinterest.ktlint.rule.engine.core.api.RuleId | ||
import com.pinterest.ktlint.rule.engine.core.api.parent | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
|
||
private class DefaultSuppressionTargetNodeFinder : SuppressionTargetNodeFinder { | ||
override fun findSuppressionTargetNode(astNode: ASTNode): ASTNode = astNode | ||
} | ||
|
||
private val defaultSuppressionTargetNodeFinder = DefaultSuppressionTargetNodeFinder() | ||
|
||
private class FunctionSuppressionTargetNodeFinder : SuppressionTargetNodeFinder { | ||
override fun findSuppressionTargetNode(astNode: ASTNode) = | ||
if (astNode.elementType == ElementType.FUN) { | ||
astNode | ||
} else { | ||
astNode.parent { it.elementType == ElementType.FUN } | ||
} | ||
} | ||
|
||
private val functionSuppressionTargetNodeFinder = FunctionSuppressionTargetNodeFinder() | ||
|
||
private class ClassSuppressionTargetNodeFinder : SuppressionTargetNodeFinder { | ||
override fun findSuppressionTargetNode(astNode: ASTNode) = | ||
if (astNode.elementType == ElementType.CLASS) { | ||
astNode | ||
} else { | ||
astNode.parent { it.elementType == ElementType.CLASS } | ||
} | ||
} | ||
|
||
private val classSuppressionTargetNodeFinder = ClassSuppressionTargetNodeFinder() | ||
|
||
// TODO: Decide in Ktlint 2.x whether it is worth to move the SuppressionTargetNodeFinder into the Rule class. The KtlintRuleEngine should | ||
// not have any knowledge about how to suppress specific rules. | ||
private val ruleSuppressionTargetNodeFinder: Map<RuleId, SuppressionTargetNodeFinder> = | ||
mapOf( | ||
RuleId("standard:class-signature") to classSuppressionTargetNodeFinder, | ||
RuleId("standard:function-signature") to functionSuppressionTargetNodeFinder, | ||
RuleId("standard:parameter-list-wrapping") to functionSuppressionTargetNodeFinder, | ||
) | ||
|
||
internal fun KtlintSuppression.findSuppressionTargetNodeFinder() = | ||
ruleSuppressionTargetNodeFinder[ruleId] | ||
?: defaultSuppressionTargetNodeFinder | ||
|
||
internal interface SuppressionTargetNodeFinder { | ||
fun findSuppressionTargetNode(astNode: ASTNode): 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