Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IJ Plugin] Suppress GraphQLDuplicateDirective for certain directives #5910

Merged
merged 3 commits into from
May 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
Loading