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