1
+ package com.google.devtools.ksp.visitor
2
+
3
+ import com.google.devtools.ksp.symbol.*
4
+
5
+ class KSValidateVisitor (private val predicate : (KSNode , KSNode ) -> Boolean ) : KSDefaultVisitor<Unit, Boolean>() {
6
+ private fun validateDeclarations (declarationContainer : KSDeclarationContainer ): Boolean {
7
+ return ! declarationContainer.declarations.any { predicate(declarationContainer, it) && ! it.accept(this , Unit ) }
8
+ }
9
+
10
+ private fun validateTypeParameters (declaration : KSDeclaration ): Boolean {
11
+ return ! declaration.typeParameters.any { predicate(declaration, it) && ! it.accept(this , Unit ) }
12
+ }
13
+
14
+ private fun validateType (type : KSType ): Boolean {
15
+ return ! type.isError && ! type.arguments.any { it.type?.accept(this , Unit ) == false }
16
+ }
17
+
18
+ override fun defaultHandler (node : KSNode , data : Unit ): Boolean {
19
+ throw IllegalStateException (" unhandled validation condition, please file a bug at https://github.com/google/ksp/issues/new" )
20
+ }
21
+
22
+ override fun visitTypeReference (typeReference : KSTypeReference , data : Unit ): Boolean {
23
+ return validateType(typeReference.resolve())
24
+ }
25
+
26
+ override fun visitClassDeclaration (classDeclaration : KSClassDeclaration , data : Unit ): Boolean {
27
+ if (! validateTypeParameters(classDeclaration)) {
28
+ return false
29
+ }
30
+ if (classDeclaration.asStarProjectedType().isError) {
31
+ return false
32
+ }
33
+ if (classDeclaration.superTypes.any { predicate(classDeclaration, it) && ! it.accept(this , Unit ) }) {
34
+ return false
35
+ }
36
+ if (! validateDeclarations(classDeclaration)) {
37
+ return false
38
+ }
39
+ return true
40
+ }
41
+
42
+ override fun visitFunctionDeclaration (function : KSFunctionDeclaration , data : Unit ): Boolean {
43
+ if (function.returnType != null && ! (predicate(function, function.returnType!! ) && function.returnType!! .accept(this , data))) {
44
+ return false
45
+ }
46
+ if (function.parameters.any { predicate(function, it) && ! it.accept(this , Unit )}) {
47
+ return false
48
+ }
49
+ if (! validateTypeParameters(function)) {
50
+ return false
51
+ }
52
+ if (! validateDeclarations(function)) {
53
+ return false
54
+ }
55
+ return true
56
+ }
57
+
58
+ override fun visitPropertyDeclaration (property : KSPropertyDeclaration , data : Unit ): Boolean {
59
+ if (predicate(property, property.type) && property.type.resolve().isError) {
60
+ return false
61
+ }
62
+ if (! validateTypeParameters(property)) {
63
+ return false
64
+ }
65
+ return true
66
+ }
67
+
68
+ override fun visitTypeParameter (typeParameter : KSTypeParameter , data : Unit ): Boolean {
69
+ if (typeParameter.bounds.any { predicate(typeParameter, it) && ! it.accept(this , Unit ) }) {
70
+ return false
71
+ }
72
+ return true
73
+ }
74
+
75
+ override fun visitValueParameter (valueParameter : KSValueParameter , data : Unit ): Boolean {
76
+ return valueParameter.type?.accept(this , Unit ) != false
77
+ }
78
+ }
0 commit comments