@@ -6,6 +6,7 @@ import com.apollographql.ijplugin.project.apolloProjectService
6
6
import com.apollographql.ijplugin.refactoring.migration.v3tov4.ApolloV3ToV4MigrationProcessor
7
7
import com.apollographql.ijplugin.telemetry.TelemetryEvent
8
8
import com.apollographql.ijplugin.telemetry.telemetryService
9
+ import com.apollographql.ijplugin.util.apollo3
9
10
import com.apollographql.ijplugin.util.getMethodName
10
11
import com.apollographql.ijplugin.util.unquoted
11
12
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo
@@ -22,17 +23,11 @@ import org.jetbrains.kotlin.idea.util.application.isUnitTestMode
22
23
import org.jetbrains.kotlin.psi.KtBinaryExpression
23
24
import org.jetbrains.kotlin.psi.KtCallExpression
24
25
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
25
- import org.jetbrains.kotlin.psi.KtLiteralStringTemplateEntry
26
- import org.jetbrains.kotlin.psi.KtStringTemplateEntry
27
26
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
28
- import org.toml.lang.psi.TomlInlineTable
29
27
import org.toml.lang.psi.TomlLiteral
30
- import org.toml.lang.psi.TomlTable
31
28
import org.toml.lang.psi.ext.TomlLiteralKind
32
29
import org.toml.lang.psi.ext.kind
33
30
34
- private const val apollo3 = " com.apollographql.apollo"
35
-
36
31
class Apollo4AvailableInspection : LocalInspectionTool () {
37
32
// XXX kts files are not highlighted in tests
38
33
private val buildGradleFileName = if (isUnitTestMode()) " build.gradle.kt" else " build.gradle.kts"
@@ -42,8 +37,6 @@ class Apollo4AvailableInspection : LocalInspectionTool() {
42
37
43
38
override fun buildVisitor (holder : ProblemsHolder , isOnTheFly : Boolean ): PsiElementVisitor {
44
39
return object : PsiElementVisitor () {
45
- private val registeredTomlVersionValues = mutableSetOf<PsiElement >()
46
-
47
40
override fun visitElement (element : PsiElement ) {
48
41
if (! isEnabled) return
49
42
if (! element.project.apolloProjectService.apolloVersion.isAtLeastV3) return
@@ -62,97 +55,55 @@ class Apollo4AvailableInspection : LocalInspectionTool() {
62
55
if (element.kind !is TomlLiteralKind .String ) return
63
56
val dependencyText = element.text.unquoted()
64
57
if (dependencyText == apollo3 || dependencyText.startsWith(" $apollo3 :" )) {
65
- // Find the associated version
66
- val versionEntry = (element.parent.parent as ? TomlInlineTable )?.entries
67
- ?.first { it.key.text == " version" || it.key.text == " version.ref" } ? : return
68
- if (versionEntry.key.text == " version" ) {
69
- val version = versionEntry.value?.firstChild?.text?.unquoted() ? : return
70
- if (! version.startsWith(" 4" )) {
71
- holder.registerProblem(element.parent.parent.parent, ApolloBundle .message(" inspection.apollo4Available.reportText" ), Apollo4AvailableQuickFix )
72
- }
73
- } else {
74
- // Resolve the reference
75
- val versionsTable = element.containingFile.children.filterIsInstance<TomlTable >()
76
- .firstOrNull { it.header.key?.text == " versions" } ? : return
77
- val versionRefKey = versionEntry.value?.text?.unquoted()
78
- val refTarget = versionsTable.entries.firstOrNull { it.key.text == versionRefKey } ? : return
79
- val version = refTarget.value?.firstChild?.text?.unquoted() ? : return
80
- if (! version.startsWith(" 4" )) {
81
- // Do not highlight the same element several times
82
- if (refTarget.value!! !in registeredTomlVersionValues) {
83
- holder.registerProblem(refTarget.value!! , ApolloBundle .message(" inspection.apollo4Available.reportText" ), Apollo4AvailableQuickFix )
84
- registeredTomlVersionValues.add(refTarget.value!! )
85
- }
86
- }
87
- }
58
+ holder.registerProblem(element.parent.parent.parent, ApolloBundle .message(" inspection.apollo4Available.reportText" ), Apollo4AvailableQuickFix )
88
59
}
89
60
}
90
61
91
62
private fun visitBuildGradleKts (callExpression : KtCallExpression , holder : ProblemsHolder ) {
92
63
when (callExpression.getMethodName()) {
93
64
" id" -> {
94
65
// id("xxx")
95
- val dependencyText = callExpression.getArgumentAsStringTemplateEntries( 0 )?.getSingleEntry( ) ? : return
66
+ val dependencyText = callExpression.getArgumentAsString( 0 ) ? : return
96
67
if (dependencyText != apollo3) return
97
- when (val element = callExpression.parent) {
98
- is KtBinaryExpression -> {
99
- // id("xxx") version yyy
100
- val version = (element.right as ? KtStringTemplateExpression )?.entries?.getSingleEntry() ? : return
101
- if (! version.startsWith(" 4" )) {
102
- holder.registerProblem(element, ApolloBundle .message(" inspection.apollo4Available.reportText" ), Apollo4AvailableQuickFix )
103
- }
68
+ val element = when (val parent = callExpression.parent) {
69
+ is KtBinaryExpression , is KtDotQualifiedExpression -> {
70
+ parent
104
71
}
105
72
106
- is KtDotQualifiedExpression -> {
107
- // id("xxx").version(yyy)
108
- val versionCallExpression = element.selectorExpression as ? KtCallExpression
109
- val version = versionCallExpression?.getArgumentAsStringTemplateEntries(0 )?.getSingleEntry() ? : return
110
- if (! version.startsWith(" 4" )) {
111
- holder.registerProblem(element, ApolloBundle .message(" inspection.apollo4Available.reportText" ), Apollo4AvailableQuickFix )
112
- }
73
+ else -> {
74
+ callExpression
113
75
}
114
76
}
77
+ holder.registerProblem(element, ApolloBundle .message(" inspection.apollo4Available.reportText" ), Apollo4AvailableQuickFix )
115
78
}
116
79
117
80
" implementation" , " api" , " testImplementation" , " testApi" -> {
118
81
when (callExpression.valueArguments.size) {
119
82
// implementation("xxx:yyy:zzz")
120
83
1 -> {
121
- val dependency = callExpression.getArgumentAsStringTemplateEntries( 0 )?.getSingleEntry( ) ? : return
84
+ val dependency = callExpression.getArgumentAsString( 0 ) ? : return
122
85
val dependencyElements = dependency.split(" :" )
123
- if (dependencyElements.size != 3 ) return
124
86
val groupId = dependencyElements[0 ]
125
87
if (groupId != apollo3) return
126
- val version = dependencyElements[2 ]
127
- if (! version.startsWith(" 4" )) {
128
- holder.registerProblem(callExpression, ApolloBundle .message(" inspection.apollo4Available.reportText" ), Apollo4AvailableQuickFix )
129
- }
88
+ holder.registerProblem(callExpression, ApolloBundle .message(" inspection.apollo4Available.reportText" ), Apollo4AvailableQuickFix )
130
89
}
131
90
132
91
92
+ // implementation("xxx", "yyy")
133
93
// implementation("xxx", "yyy", "zzz")
134
- 3 -> {
135
- val groupId = callExpression.getArgumentAsStringTemplateEntries( 0 )?.getSingleEntry( ) ? : return
94
+ 2 , 3 -> {
95
+ val groupId = callExpression.getArgumentAsString( 0 ) ? : return
136
96
if (groupId != apollo3) return
137
- val version = callExpression.getArgumentAsStringTemplateEntries(2 )?.getSingleEntry() ? : return
138
- if (! version.startsWith(" 4" )) {
139
- holder.registerProblem(callExpression, ApolloBundle .message(" inspection.apollo4Available.reportText" ), Apollo4AvailableQuickFix )
140
- }
97
+ holder.registerProblem(callExpression, ApolloBundle .message(" inspection.apollo4Available.reportText" ), Apollo4AvailableQuickFix )
141
98
}
142
99
}
143
100
}
144
101
}
145
102
}
146
103
147
- private fun KtCallExpression.getArgumentAsStringTemplateEntries (index : Int ): Array <KtStringTemplateEntry >? =
148
- (valueArgumentList?.arguments?.getOrNull(index)
149
- ?.children?.firstOrNull() as ? KtStringTemplateExpression )?.entries
150
-
151
- // Only consider simple strings (no templates)
152
- private fun Array<KtStringTemplateEntry>.getSingleEntry (): String? {
153
- if (size != 1 || this [0 ] !is KtLiteralStringTemplateEntry ) return null
154
- return this [0 ].text.unquoted()
155
- }
104
+ private fun KtCallExpression.getArgumentAsString (index : Int ): String? =
105
+ (valueArgumentList?.arguments?.getOrNull(index)
106
+ ?.children?.firstOrNull() as ? KtStringTemplateExpression )?.text?.unquoted()
156
107
}
157
108
}
158
109
}
0 commit comments