-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ApolloOneOfInputCreationInspection
- Loading branch information
Showing
10 changed files
with
157 additions
and
5 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...c/main/kotlin/com/apollographql/ijplugin/inspection/ApolloOneOfInputCreationInspection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.apollographql.ijplugin.inspection | ||
|
||
import com.apollographql.ijplugin.ApolloBundle | ||
import com.apollographql.ijplugin.navigation.findInputTypeGraphQLDefinitions | ||
import com.apollographql.ijplugin.navigation.isApolloInputClassReference | ||
import com.apollographql.ijplugin.project.apolloProjectService | ||
import com.apollographql.ijplugin.util.cast | ||
import com.apollographql.ijplugin.util.type | ||
import com.intellij.codeInspection.LocalInspectionTool | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.lang.jsgraphql.psi.GraphQLInputObjectTypeDefinition | ||
import com.intellij.psi.PsiElementVisitor | ||
import com.intellij.psi.util.parentOfType | ||
import org.jetbrains.kotlin.idea.base.utils.fqname.fqName | ||
import org.jetbrains.kotlin.psi.KtCallExpression | ||
import org.jetbrains.kotlin.psi.KtNameReferenceExpression | ||
import org.jetbrains.kotlin.psi.KtVisitorVoid | ||
|
||
class ApolloOneOfInputCreationInspection : LocalInspectionTool() { | ||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { | ||
return object : KtVisitorVoid() { | ||
override fun visitCallExpression(expression: KtCallExpression) { | ||
super.visitCallExpression(expression) | ||
if (!expression.project.apolloProjectService.apolloVersion.isAtLeastV4) return | ||
val reference = (expression.calleeExpression.cast<KtNameReferenceExpression>()) | ||
if (reference?.isApolloInputClassReference() != true) return | ||
val inputTypeName = reference.text | ||
val inputTypeDefinition = findInputTypeGraphQLDefinitions(reference.project, inputTypeName).firstOrNull() | ||
?.parentOfType<GraphQLInputObjectTypeDefinition>() | ||
?: return | ||
val isOneOf = inputTypeDefinition.directives.any { it.name == "oneOf" } | ||
if (!isOneOf) return | ||
if (expression.valueArguments.size != 1) { | ||
holder.registerProblem(expression.calleeExpression!!, ApolloBundle.message("inspection.oneOfInputCreation.reportText.wrongNumberOfArgs")) | ||
return | ||
} | ||
val arg = expression.valueArguments.first() | ||
if (arg.getArgumentExpression()?.type()?.fqName?.asString() == "com.apollographql.apollo3.api.Optional.Absent") { | ||
holder.registerProblem(expression.calleeExpression!!, ApolloBundle.message("inspection.oneOfInputCreation.reportText.argIsAbsent")) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
intellij-plugin/src/main/resources/inspectionDescriptions/ApolloOneOfInputCreation.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<body> | ||
Reports invalid constructor invocations of <code>@oneOf</code> input types. | ||
<p> | ||
Exactly one field must be set, and it must be <code>Present</code>. | ||
</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...st/kotlin/com/apollographql/ijplugin/inspection/ApolloOneOfInputCreationInspectionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.apollographql.ijplugin.inspection | ||
|
||
import com.apollographql.ijplugin.ApolloTestCase | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
|
||
@RunWith(JUnit4::class) | ||
class ApolloOneOfInputCreationInspectionTest : ApolloTestCase() { | ||
@Throws(Exception::class) | ||
override fun setUp() { | ||
super.setUp() | ||
myFixture.enableInspections(ApolloOneOfInputCreationInspection()) | ||
} | ||
|
||
@Test | ||
fun testOneOfConstructorInvocations() { | ||
myFixture.configureFromTempProjectFile("src/main/kotlin/com/example/OneOf.kt") | ||
val highlightInfos = doHighlighting() | ||
assertTrue(highlightInfos.any { it.description == "@oneOf input must have exactly one field set" && it.text == "FindUserInput" && it.line == 8}) | ||
assertTrue(highlightInfos.any { it.description == "@oneOf input must have exactly one field set" && it.text == "FindUserInput" && it.line == 10}) | ||
assertTrue(highlightInfos.any { it.description == "@oneOf input argument must be Present" && it.text == "FindUserInput" && it.line == 20}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
tests/intellij-plugin-test-project/src/main/graphql/FindUserQuery.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
query FindUsersQuery($findUserInput: FindUserInput!) { | ||
findUser(findUserInput: $findUserInput) { | ||
id | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tests/intellij-plugin-test-project/src/main/kotlin/com/example/OneOf.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.example | ||
|
||
import com.apollographql.apollo3.api.Optional | ||
import com.apollographql.apollo3.api.Optional.Absent | ||
import com.example.generated.type.FindUserInput | ||
|
||
fun oneOf() { | ||
FindUserInput() | ||
|
||
FindUserInput( | ||
email = Optional.present("[email protected]"), | ||
name = Optional.present("John"), | ||
) | ||
|
||
FindUserInput( | ||
email = Optional.present("[email protected]") | ||
) | ||
|
||
val absentEmail: Absent = Optional.absent() | ||
FindUserInput( | ||
email = absentEmail | ||
) | ||
} |