1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.lint
18 
19 import com.intellij.psi.PsiClass
20 import com.intellij.psi.PsiClassOwner
21 import com.intellij.psi.PsiMember
22 import com.intellij.psi.PsiMethod
23 import com.intellij.psi.PsiType
24 import com.intellij.psi.util.InheritanceUtil
25 
26 /** Returns whether [this] has [packageName] as its package name. */
isInPackageNamenull27 fun PsiMember.isInPackageName(packageName: PackageName): Boolean {
28     val actual = (containingFile as? PsiClassOwner)?.packageName
29     return packageName.javaPackageName == actual
30 }
31 
32 /** Whether this [PsiMethod] returns Unit */
33 val PsiMethod.returnsUnit
34     get() = returnType.isVoidOrUnit
35 
36 /**
37  * Whether this [PsiType] is `void` or [Unit]
38  *
39  * In Kotlin 1.6 some expressions now explicitly return [Unit] instead of just being [PsiType.VOID],
40  * so this returns whether this type is either.
41  */
42 val PsiType?.isVoidOrUnit
43     get() = this == PsiType.VOID || this?.canonicalText == "kotlin.Unit"
44 
45 /** @return whether [this] inherits from [name]. Returns `true` if [this] _is_ directly [name]. */
inheritsFromnull46 fun PsiType.inheritsFrom(name: Name) = InheritanceUtil.isInheritor(this, name.javaFqn)
47 
48 /** @return whether [this] inherits from [name]. Returns `true` if [this] _is_ directly [name]. */
49 fun PsiClass.inheritsFrom(name: Name) = InheritanceUtil.isInheritor(this, name.javaFqn)
50