https://kotlinlang.org logo
Title
e

evant

10/24/2021, 8:16 PM
And separate question. Is the expected behavior for the predicate passed to KSValidateVisitor to be returning true means to check the node and false means to skip assuming it's valid? Seeing some inconsistent behavior in it's implementation and wondering if it's a bug.
j

Jiaxiang

10/24/2021, 9:37 PM
Not really assuming valid, but just not care about it. Like you might not care about a function’s parameter type if your annotation processor only need the return type information. What is the inconsistent behavior you are observing?
e

evant

10/24/2021, 9:41 PM
There's 2 issues I've found:
visitFunctionDeclaration
will return false if the predicate returns false, so
validate()
will always say it's invalid if you choose to skip it
visitPropertyDeclaration
checks
property.type.resolve().isError
instead of visiting, which means
property.validate()
may return true while
property.type.validate()
may return false. Seems inconsistent with other places in the visitor.
Here's my custom subclass, I can send a pr when I got time
class FixedKSValidateVisitor(private val predicate: (KSNode?, KSNode) -> Boolean) : KSValidateVisitor(predicate) {
    override fun visitFunctionDeclaration(function: KSFunctionDeclaration, data: KSNode?): Boolean {
        if (!predicate(function, function.returnType!!)) {
            return true
        }
        return super.visitFunctionDeclaration(function, data)
    }

    override fun visitPropertyDeclaration(property: KSPropertyDeclaration, data: KSNode?): Boolean {
        if (predicate(property, property.type) && property.type.accept(this, data)) {
            return false
        }
        if (!this.visitDeclaration(property, data)) {
            return false
        }
        return true
    }
}
j

Jiaxiang

10/24/2021, 9:51 PM
got your point, seems something should be fixed to me. But your custom class needs some tweak before merged, like in your class when the
predicate
indicates no check for function return type, it will return
true
directly while there are other places for validation like parameter types.
e

evant

10/24/2021, 9:59 PM
yeah that makes sense