Skip to content

Commit

Permalink
[IJ Plugin] Suppress GraphQLDuplicateDirective for certain directives (
Browse files Browse the repository at this point in the history
…apollographql#5910)

* Suppress GraphQLDuplicateDirective for certain directives

* Update intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/inspection/GraphQLUnresolvedReferenceInspectionSuppressor.kt

Co-authored-by: Martin Bonnin <[email protected]>

* Fix bad rebase

---------

Co-authored-by: Martin Bonnin <[email protected]>
  • Loading branch information
BoD and martinbonnin committed Jul 1, 2024
1 parent cd8d948 commit a351432
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.apollographql.ijplugin.inspection

import com.apollographql.apollo3.ast.GQLDirectiveDefinition
import com.apollographql.apollo3.ast.linkDefinitions
import com.apollographql.ijplugin.util.KOTLIN_LABS_DEFINITIONS
import com.apollographql.ijplugin.util.NULLABILITY_DEFINITIONS
import com.apollographql.ijplugin.util.NULLABILITY_URL
import com.apollographql.ijplugin.util.directives
Expand All @@ -15,13 +16,14 @@ import com.intellij.lang.jsgraphql.psi.GraphQLDirectivesAware
import com.intellij.psi.PsiElement

private val KNOWN_DIRECTIVES: List<GQLDirectiveDefinition> by lazy {
linkDefinitions().directives() + NULLABILITY_DEFINITIONS.directives()
linkDefinitions().directives() + NULLABILITY_DEFINITIONS.directives() + KOTLIN_LABS_DEFINITIONS.directives()
}

/**
* Do not highlight certain known directives as unresolved references.
*
* TODO: remove this once https://github.com/JetBrains/js-graphql-intellij-plugin/pull/698 is merged.
* Note: we'll need this workaround until there is a way for a plugin to provide their own known definitions to the GraphQL plugin.
* See https://github.com/JetBrains/js-graphql-intellij-plugin/issues/697.
*/
class GraphQLUnresolvedReferenceInspectionSuppressor : InspectionSuppressor {
override fun isSuppressedFor(element: PsiElement, toolId: String): Boolean {
Expand All @@ -31,8 +33,9 @@ class GraphQLUnresolvedReferenceInspectionSuppressor : InspectionSuppressor {

"GraphQLMissingType" -> element is GraphQLDirectivesAware && element.directives.all { it.isKnownDirective() }

// We need to suppress this one too because the plugin doesn't know that @link is repeatable
"GraphQLDuplicateDirective" -> element is GraphQLDirective && element.name == "link"
// We need to suppress this one too because the plugin doesn't know that certain directives (e.g. @link) are repeatable
"GraphQLDuplicateDirective" -> element is GraphQLDirective &&
KNOWN_DIRECTIVES.any { it.name == element.name && it.repeatable }

else -> false
}
Expand All @@ -48,5 +51,6 @@ private fun PsiElement.isKnownDirective(): Boolean {
private fun PsiElement.isKnownDirectiveArgument(): Boolean {
return this is GraphQLArgument &&
parent?.parent?.isKnownDirective() == true &&
name in KNOWN_DIRECTIVES.firstOrNull { it.name == (parent.parent as GraphQLDirective).nameWithoutPrefix }?.arguments?.map { it.name }.orEmpty()
name in KNOWN_DIRECTIVES.firstOrNull { it.name == (parent.parent as GraphQLDirective).nameWithoutPrefix }?.arguments?.map { it.name }
.orEmpty()
}

0 comments on commit a351432

Please sign in to comment.