@@ -6,6 +6,7 @@ import com.apollographql.ijplugin.project.apolloProjectService
66import com.apollographql.ijplugin.refactoring.migration.v3tov4.ApolloV3ToV4MigrationProcessor
77import com.apollographql.ijplugin.telemetry.TelemetryEvent
88import com.apollographql.ijplugin.telemetry.telemetryService
9+ import com.apollographql.ijplugin.util.apollo3
910import com.apollographql.ijplugin.util.getMethodName
1011import com.apollographql.ijplugin.util.unquoted
1112import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo
@@ -22,17 +23,11 @@ import org.jetbrains.kotlin.idea.util.application.isUnitTestMode
2223import org.jetbrains.kotlin.psi.KtBinaryExpression
2324import org.jetbrains.kotlin.psi.KtCallExpression
2425import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
25- import org.jetbrains.kotlin.psi.KtLiteralStringTemplateEntry
26- import org.jetbrains.kotlin.psi.KtStringTemplateEntry
2726import org.jetbrains.kotlin.psi.KtStringTemplateExpression
28- import org.toml.lang.psi.TomlInlineTable
2927import org.toml.lang.psi.TomlLiteral
30- import org.toml.lang.psi.TomlTable
3128import org.toml.lang.psi.ext.TomlLiteralKind
3229import org.toml.lang.psi.ext.kind
3330
34- private const val apollo3 = " com.apollographql.apollo"
35-
3631class Apollo4AvailableInspection : LocalInspectionTool () {
3732 // XXX kts files are not highlighted in tests
3833 private val buildGradleFileName = if (isUnitTestMode()) " build.gradle.kt" else " build.gradle.kts"
@@ -42,8 +37,6 @@ class Apollo4AvailableInspection : LocalInspectionTool() {
4237
4338 override fun buildVisitor (holder : ProblemsHolder , isOnTheFly : Boolean ): PsiElementVisitor {
4439 return object : PsiElementVisitor () {
45- private val registeredTomlVersionValues = mutableSetOf<PsiElement >()
46-
4740 override fun visitElement (element : PsiElement ) {
4841 if (! isEnabled) return
4942 if (! element.project.apolloProjectService.apolloVersion.isAtLeastV3) return
@@ -62,97 +55,55 @@ class Apollo4AvailableInspection : LocalInspectionTool() {
6255 if (element.kind !is TomlLiteralKind .String ) return
6356 val dependencyText = element.text.unquoted()
6457 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 )
8859 }
8960 }
9061
9162 private fun visitBuildGradleKts (callExpression : KtCallExpression , holder : ProblemsHolder ) {
9263 when (callExpression.getMethodName()) {
9364 " id" -> {
9465 // id("xxx")
95- val dependencyText = callExpression.getArgumentAsStringTemplateEntries( 0 )?.getSingleEntry( ) ? : return
66+ val dependencyText = callExpression.getArgumentAsString( 0 ) ? : return
9667 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
10471 }
10572
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
11375 }
11476 }
77+ holder.registerProblem(element, ApolloBundle .message(" inspection.apollo4Available.reportText" ), Apollo4AvailableQuickFix )
11578 }
11679
11780 " implementation" , " api" , " testImplementation" , " testApi" -> {
11881 when (callExpression.valueArguments.size) {
11982 // implementation("xxx:yyy:zzz")
12083 1 -> {
121- val dependency = callExpression.getArgumentAsStringTemplateEntries( 0 )?.getSingleEntry( ) ? : return
84+ val dependency = callExpression.getArgumentAsString( 0 ) ? : return
12285 val dependencyElements = dependency.split(" :" )
123- if (dependencyElements.size != 3 ) return
12486 val groupId = dependencyElements[0 ]
12587 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 )
13089 }
13190
13291
92+ // implementation("xxx", "yyy")
13393 // implementation("xxx", "yyy", "zzz")
134- 3 -> {
135- val groupId = callExpression.getArgumentAsStringTemplateEntries( 0 )?.getSingleEntry( ) ? : return
94+ 2 , 3 -> {
95+ val groupId = callExpression.getArgumentAsString( 0 ) ? : return
13696 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 )
14198 }
14299 }
143100 }
144101 }
145102 }
146103
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()
156107 }
157108 }
158109}
0 commit comments