@@ -3,77 +3,88 @@ 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
+ return true
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 this .visitAnnotated(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 visitAnnotated ( annotated : KSAnnotated , data : KSNode ? ): Boolean {
34
+ return ! predicate(data, annotated) || annotated.annotations.all{ it.accept( this , annotated) }
21
35
}
22
36
23
- override fun visitTypeReference (typeReference : KSTypeReference , data : Unit ): Boolean {
37
+ override fun visitAnnotation (annotation : KSAnnotation , data : KSNode ? ): Boolean {
38
+ return ! predicate(data, annotation) || annotation.annotationType.accept(this , annotation)
39
+ }
40
+
41
+ override fun visitTypeReference (typeReference : KSTypeReference , data : KSNode ? ): Boolean {
24
42
return validateType(typeReference.resolve())
25
43
}
26
44
27
- override fun visitClassDeclaration (classDeclaration : KSClassDeclaration , data : Unit ): Boolean {
28
- if (! validateTypeParameters( classDeclaration) ) {
45
+ override fun visitClassDeclaration (classDeclaration : KSClassDeclaration , data : KSNode ? ): Boolean {
46
+ if (classDeclaration.asStarProjectedType().isError ) {
29
47
return false
30
48
}
31
- if (classDeclaration.asStarProjectedType().isError ) {
49
+ if (! classDeclaration.superTypes.all{ it.accept( this , classDeclaration) } ) {
32
50
return false
33
51
}
34
- if (classDeclaration.superTypes.any { predicate (classDeclaration, it) && ! it.accept( this , Unit ) } ) {
52
+ if (! this .visitDeclaration (classDeclaration, data) ) {
35
53
return false
36
54
}
37
- if (! validateDeclarations (classDeclaration)) {
55
+ if (! this .visitDeclarationContainer (classDeclaration, data )) {
38
56
return false
39
57
}
40
58
return true
41
59
}
42
60
43
- override fun visitFunctionDeclaration (function : KSFunctionDeclaration , data : Unit ): Boolean {
61
+ override fun visitFunctionDeclaration (function : KSFunctionDeclaration , data : KSNode ? ): Boolean {
44
62
if (function.returnType != null && ! (predicate(function, function.returnType!! ) && function.returnType!! .accept(this , data))) {
45
63
return false
46
64
}
47
- if (function.parameters.any { predicate(function, it) && ! it .accept(this , Unit ) }) {
65
+ if (! function.parameters.all{ it .accept(this , function) }) {
48
66
return false
49
67
}
50
- if (! validateTypeParameters (function)) {
68
+ if (! this .visitDeclaration (function, data )) {
51
69
return false
52
70
}
53
- if (! validateDeclarations (function)) {
71
+ if (! this .visitDeclarationContainer (function, data )) {
54
72
return false
55
73
}
56
74
return true
57
75
}
58
76
59
- override fun visitPropertyDeclaration (property : KSPropertyDeclaration , data : Unit ): Boolean {
77
+ override fun visitPropertyDeclaration (property : KSPropertyDeclaration , data : KSNode ? ): Boolean {
60
78
if (predicate(property, property.type) && property.type.resolve().isError) {
61
79
return false
62
80
}
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 ) }) {
81
+ if (! this .visitDeclaration(property, data)) {
71
82
return false
72
83
}
73
84
return true
74
85
}
75
86
76
- override fun visitValueParameter (valueParameter : KSValueParameter , data : Unit ): Boolean {
77
- return valueParameter.type? .accept(this , Unit ) != false
87
+ override fun visitValueParameter (valueParameter : KSValueParameter , data : KSNode ? ): Boolean {
88
+ return valueParameter.type.accept(this , valueParameter)
78
89
}
79
90
}
0 commit comments