Skip to content

Instantly share code, notes, and snippets.

@amadeu01
Created September 14, 2019 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amadeu01/4ca8b1664bc0c815edc54b7348b0858c to your computer and use it in GitHub Desktop.
Save amadeu01/4ca8b1664bc0c815edc54b7348b0858c to your computer and use it in GitHub Desktop.
Custom Rule for ktlint
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