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