Skip to content

Commit

Permalink
Add inspection suppressor to allow suppression on fields
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD committed Aug 17, 2023
1 parent 8af7b9b commit 1fed6c6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.apollographql.ijplugin.inspection

import com.apollographql.ijplugin.ApolloBundle
import com.intellij.codeInspection.InspectionSuppressor
import com.intellij.codeInspection.SuppressQuickFix
import com.intellij.codeInspection.SuppressionUtil
import com.intellij.lang.jsgraphql.ide.validation.fixes.GraphQLSuppressByCommentFix
import com.intellij.lang.jsgraphql.psi.GraphQLField
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import java.util.regex.Pattern

private val SUPPRESS_IN_LINE_COMMENT_PATTERN = Pattern.compile("#" + SuppressionUtil.COMMON_SUPPRESS_REGEXP)

class GraphQLInspectionSuppressor : InspectionSuppressor {
override fun isSuppressedFor(element: PsiElement, toolId: String): Boolean {
return element.isSuppressedOnSelfOrParent(toolId, SUPPRESS_IN_LINE_COMMENT_PATTERN)
}

override fun getSuppressActions(
element: PsiElement?,
toolId: String,
): Array<SuppressQuickFix> {
return if (element == null) {
SuppressQuickFix.EMPTY_ARRAY
} else {
arrayOf(
GraphQLSuppressByCommentFix(toolId, GraphQLField::class.java, ApolloBundle.message("inspection.suppress.field")),
)
}
}
}

fun PsiElement.isSuppressedOnSelfOrParent(
toolId: String,
suppressInLineCommentPattern: Pattern,
): Boolean {
var element: PsiElement? = this
while (element != null) {
val prev = PsiTreeUtil.skipWhitespacesBackward(element)
if (prev is PsiComment) {
val text = prev.getText()
val matcher = suppressInLineCommentPattern.matcher(text)
if (matcher.matches() && SuppressionUtil.isInspectionToolIdMentioned(matcher.group(1), toolId)) {
return true
}
}
element = element.parent
}
return false
}
6 changes: 6 additions & 0 deletions intellij-plugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@
level="INFO"
/>

<!-- Suppression of inspections on individual fields -->
<lang.inspectionSuppressor
language="GraphQL"
implementationClass="com.apollographql.ijplugin.inspection.GraphQLInspectionSuppressor"
/>

<!-- Fields insights service (fetch and cache data) -->
<projectService
serviceInterface="com.apollographql.ijplugin.studio.fieldinsights.FieldInsightsService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,6 @@ inspection.endpointNotConfigured.displayName=GraphQL endpoint not configured
inspection.endpointNotConfigured.reportText=GraphQL endpoint not configured
inspection.endpointNotConfigured.quickFix=Add introspection block

inspection.suppress.field=Suppress for field

notification.group.apollo=Apollo
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class ApolloUnusedFieldInspectionTest : ApolloTestCase() {
assertTrue(highlightInfos.none { it.description == "Unused field" && it.text == "name" })
// id inside the fragment is used
assertTrue(highlightInfos.none { it.description == "Unused field" && it.text == "id" && it.line > 3 })
// barkVolume is unused, but the inspection is suppressed
assertTrue(highlightInfos.none { it.description == "Unused field" && it.text == "barkVolume"})

moveCaret("id")

Expand All @@ -39,6 +41,7 @@ class ApolloUnusedFieldInspectionTest : ApolloTestCase() {
}
... on dog {
id
# noinspection ApolloUnusedField
barkVolume
fieldOnDogAndCat
}
Expand Down Expand Up @@ -72,6 +75,7 @@ class ApolloUnusedFieldInspectionTest : ApolloTestCase() {
name
... on dog {
id
# noinspection ApolloUnusedField
barkVolume
fieldOnDogAndCat
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ query animals {
}
... on dog {
id
# noinspection ApolloUnusedField
barkVolume
fieldOnDogAndCat
}
Expand Down

0 comments on commit 1fed6c6

Please sign in to comment.