Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add annotation validation to validate API #161

Merged
merged 1 commit into from
Nov 20, 2020

Conversation

neetopia
Copy link
Contributor

Annotation argument values are left out for check for now.

@neetopia neetopia requested a review from ting-yuan November 20, 2020 00:14
@@ -40,6 +54,9 @@ class KSValidateVisitor(private val predicate: (KSNode, KSNode) -> Boolean) : KS
}
Copy link
Collaborator

@ting-yuan ting-yuan Nov 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have noticed previously: Since this visitor extends KSDefaultVisitor, why not delegating to it? I.e., replace calls to validateAnnotations(), validateTypeParameters(), validateDeclarations() and validateDeclaration() with a single return super.validateClassDeclaration(). Then you don't need private fun validateBlabla; Just put them in visitBlabla(). For example,

override fun visitClassDeclaration(c: KSClassDeclaration, parent: KSNode) {
  if (predicate(parent, c) == false)
    return true
  // Only validate super types here
  if ( c.superTypes.any { it.accept(this, c) } == false )
    return false
  // This in turn calls visitDeclaration and visitDeclarationContainer
  return super.visitClassDeclaration()
}

override fun visitDeclaration(decl: KSDeclaration) {
  if (predicate(parent, c) == false)
    return true
  if (decl.typeParameters.any { it.accept(this, decl) } == false)
    return false
  return super.visitDeclaration()
}

override fun visitTypeParameter() {
  if (predicate(parent, c) == false)
    return true
  // validate a type parameter.
}

override fun visitDeclarationContainer(container: KSDeclarationContainer, ...) {
  if (predicate(parent, c) == false)
    return true
  return container.declarations.all { it.accept(this, container) }
}

and so on.

@neetopia neetopia force-pushed the validate-annotations branch 2 times, most recently from d050c49 to ff99438 Compare November 20, 2020 10:11
@neetopia neetopia force-pushed the validate-annotations branch from ff99438 to 87b5a27 Compare November 20, 2020 11:37
@ting-yuan
Copy link
Collaborator

ting-yuan commented Nov 20, 2020

Actually d050c49 looks better. May I know the reason why you decided to go with the latter approach? Besides the default handler.

@neetopia
Copy link
Contributor Author

Actually d050c49 looks better. May I know the reason why you decided to go with the latter approach? Besides the default handler.

Because if we simply delegate to super, there is no control for us to let super return our desired result, all return result will be from defaultHandler and things will break, like the call to common logic won't affect the return result of super.visitClassDeclaration()

@neetopia neetopia merged commit 96a7e1f into google:master Nov 20, 2020
@neetopia neetopia deleted the validate-annotations branch November 20, 2020 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants