Does anyone know how you could create a custom lint rule that would fail if it catches a certain fun...
c
Does anyone know how you could create a custom lint rule that would fail if it catches a certain function/function name being used anywhere outside a specified file?
😶 2
If this is a channel for nothing but kotlin questions it should probably be renamed to something other than android 🙂 Not quite clear that it's only for kotlin related questions...
d
This one is an interesting question for this one as Android’s custom lint is to be written in Kotlin and not Java 🙂 What you’d need to do is use a extension of
Detector()
that implements
SourceCodeScanner
First, in your
override
of
getApplicableUastTypes
, you’d specify
listOf(UIMethod::class.java)
Second, in your
override
of `createUastHandler`you’ll want to implement
override fun visitMethod(node: UIMethod)
In this method, you’ll look at the
node.name
to see it has the method you do not want. The
context.getLocation(node)
will let you know where you are. You can decide to first look at the node or the location. Just be aware of the scopes you pass into the
Implementation
. By default,
TEST_SOURCES
aren’t in there, so if you want tests, you need to add them. You can find the official guide here - https://googlesamples.github.io/android-custom-lint-rules/api-guide.html If you’re not already using custom lint, I’d also suggest that you put `lintPublish. project(':customLintProject')`and
lintChecks project(':customLintProject')
in a module that all your modules are dependent on in your build script. This will force all projects to pick it up and it can bring the checks into studio.
👍 1
Also, do be aware that if you write tests, the imports in anything you load in via
lint().files
needs to resolve. E.g., if you have
Copy code
import androidx.compose.material.OutlinedButton
c
Thank you for all the information! I'll be looking into these more 🔍