1 /*
2  * Copyright 2024 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.lifecycle.lint
18 
19 import androidx.compose.lint.Name
20 import androidx.compose.lint.Package
21 import androidx.compose.lint.inheritsFrom
22 import androidx.compose.lint.isInvokedWithinComposable
23 import com.android.tools.lint.client.api.UElementHandler
24 import com.android.tools.lint.detector.api.Category
25 import com.android.tools.lint.detector.api.Detector
26 import com.android.tools.lint.detector.api.Implementation
27 import com.android.tools.lint.detector.api.Issue
28 import com.android.tools.lint.detector.api.JavaContext
29 import com.android.tools.lint.detector.api.Scope
30 import com.android.tools.lint.detector.api.Severity
31 import com.android.tools.lint.detector.api.SourceCodeScanner
32 import java.util.EnumSet
33 import org.jetbrains.uast.UCallExpression
34 import org.jetbrains.uast.UElement
35 import org.jetbrains.uast.util.isConstructorCall
36 
37 /** [Detector] that checks if a view model is being constructed directly in a composable. */
38 class ViewModelConstructorInComposableDetector : Detector(), SourceCodeScanner {
getApplicableUastTypesnull39     override fun getApplicableUastTypes(): List<Class<out UElement>> {
40         return listOf(UCallExpression::class.java)
41     }
42 
createUastHandlernull43     override fun createUastHandler(context: JavaContext): UElementHandler {
44         return object : UElementHandler() {
45             override fun visitCallExpression(node: UCallExpression) {
46                 if (!node.isInvokedWithinComposable()) return
47                 if (!node.isConstructorCall()) return
48 
49                 val containingClass = node.resolve()?.containingClass ?: return
50                 if (containingClass.inheritsFrom(FqViewModelName)) {
51                     context.report(
52                         ISSUE,
53                         node,
54                         context.getNameLocation(node),
55                         "Constructing a view model in a composable"
56                     )
57                 }
58             }
59         }
60     }
61 
62     companion object {
63         private val FqViewModelName = Name(Package("androidx.lifecycle"), "ViewModel")
64 
65         val ISSUE =
66             Issue.create(
67                 "ViewModelConstructorInComposable",
68                 "Constructing a view model in a composable",
69                 "View models should not be constructed directly inside composable" +
70                     " functions. Instead you should use the lifecycle viewmodel extension" +
71                     "functions e.g. viewModel<MyViewModel>()",
72                 Category.CORRECTNESS,
73                 3,
74                 Severity.ERROR,
75                 Implementation(
76                     ViewModelConstructorInComposableDetector::class.java,
77                     EnumSet.of(Scope.JAVA_FILE, Scope.TEST_SOURCES)
78                 )
79             )
80     }
81 }
82