Skip to content

Commit

Permalink
improve companion object checking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
neetopia committed Feb 28, 2024
1 parent c56396b commit a726254
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import org.jetbrains.kotlin.analysis.api.components.buildTypeParameterType
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.descriptors.java.JavaVisibilities
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtObjectDeclaration

class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSymbol: KtClassOrObjectSymbol) :
KSClassDeclaration,
Expand Down Expand Up @@ -99,7 +98,7 @@ class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSym
}

override val isCompanionObject: Boolean by lazy {
(ktClassOrObjectSymbol.psi as? KtObjectDeclaration)?.isCompanion() ?: false
ktClassOrObjectSymbol.classKind == KtClassKind.COMPANION_OBJECT
}

override fun getSealedSubclasses(): Sequence<KSClassDeclaration> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.google.devtools.ksp.processor

import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
Expand All @@ -29,6 +30,7 @@ class ClassWithCompanionProcessor : AbstractTestProcessor() {

override fun process(resolver: Resolver): List<KSAnnotated> {
resolver.getNewFiles().forEach { it.accept(CompanionVisitor(), Unit) }
resolver.getClassDeclarationByName("A")?.accept(CompanionVisitor(), Unit)
return emptyList()
}

Expand Down
9 changes: 9 additions & 0 deletions test-utils/testData/api/companion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@
// companion:false
// obj:false
// K:true
// A:false
// Companion:true
// END

// MODULE: lib
// FILE: a.kt
class A {
companion object {}
}

// MODULE: main(lib)
class Foo {
object companion {}
object obj {}
Expand Down

0 comments on commit a726254

Please sign in to comment.