1

I would like to write some custom lint rules for my company android project. I was reading a few tutorials and successfully created a sample rule.

Problem is that I was using a @Deprecated JavaScanner API. I was reading official google doc located here but it's not up to date. I tried to dig into exisiting rules, found this google git repository but it also uses deprecated APIs. So my questions are

1) Is there any up to date documentation out there, but I just simply can not find it?

2) Is there git repository of current lint rules available? So I could analise it?

Thanks!

Phate P
  • 1,572
  • 1
  • 22
  • 31

2 Answers2

1

Ok I've already found an JavaScanner replacement. It does not anwser 2 questions I've asked bellow but it solves deprecation interface problem so I have decided to post an answer.

According to this google groups - API changed twice since JavaScanner.

First change was to JavaPsiScanner but they "didn't advertise this widely, since I already knew that we wanted to switch over to UAST (which was still in development)"

Second and final change is to UastScanner. So one should use it now for Java Classes.

You can even find short documentation writen by Tor Norbye (7th comment from above)

Edit: Sample UastDetector class

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Phate P
  • 1,572
  • 1
  • 22
  • 31
0

One easy way to implement Custom Rules in Android Projects is by using the Regex-based linting tool AnyLint. It's written in Swift though and therefore it only works on macOS and Linux at the moment (Windows support is in the works). But you don't really need Swift knowledge to use it, just follow it's documentation.

It's the easiest and fastest way to implement custom rules for any language, basically and supports example validation and even autocorrection.

For example, you could write a custom rule to prevent multiple empty lines like so (supports autocorrect):

#!/usr/local/bin/swift-sh
import AnyLint // @Flinesoft ~> 0.6.0
try Lint.logSummaryAndExit(arguments: CommandLine.arguments) {
    // MARK: - Variables
    let kotlinFiles: Regex = #"^app/src/main/kotlin/.*\.kt$"#
    let javaFiles: Regex = #"^app/src/main/java/.*\.java$"#
    let xmlFiles: Regex = #"^app/src/main/res/.*\.xml$"#
    let gradleFiles: Regex = #"^.*\.gradle$"#

    // MARK: MultilineWhitespaces
    try Lint.checkFileContents(
        checkInfo: "MultilineWhitespaces: Restrict whitespace lines to a maximum of one.",
        regex: #"\n( *\n){2,}"#,
        matchingExamples: ["}\n    \n     \n\nclass", "}\n\n\nvoid"],
        nonMatchingExamples: ["}\n    \n    class"],
        includeFilters: [kotlinFiles, javaFiles, xmlFiles, gradleFiles],
        autoCorrectReplacement: "\n\n",
        autoCorrectExamples: [
            ["before": "}\n    \n     \n\n    class", "after": "}\n\n    class"],
            ["before": "}\n\n\nvoid", "after": "}\n\nvoid"],
        ]
    )
}

I hope this helps.

Jeehut
  • 20,202
  • 8
  • 59
  • 80