Created
September 14, 2019 16:28
-
-
Save amadeu01/4ca8b1664bc0c815edc54b7348b0858c to your computer and use it in GitHub Desktop.
Custom Rule for ktlint
This file contains hidden or 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
class CustomRuleSetProvider : RuleSetProvider { | |
override fun get() = RuleSet("rules", | |
NoInternalImportRule(), | |
NoVarRule() | |
) | |
} | |
class NoInternalImportRule : Rule("no-internal-import") { | |
override fun visit( | |
node: ASTNode, autoCorrect: Boolean, | |
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit | |
) { | |
if (node.elementType == KtStubElementTypes.IMPORT_DIRECTIVE) { | |
val importDirective = node.psi as KtImportDirective | |
val path = importDirective.importPath?.pathStr | |
if (path?.contains("internal") == true) { | |
emit(node.startOffset, "Importing '$path' which is an internal import.", false) | |
} | |
} | |
} | |
} | |
class NoVarRule : Rule("no-var") { | |
override fun visit( | |
node: ASTNode, | |
autoCorrect: Boolean, | |
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit | |
) { | |
if (node.elementType == VAR_KEYWORD) { | |
emit(node.startOffset, "😱 Unexpected var, use val instead 🏄", false) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment