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