diff --git a/config/Sets/standard.php b/config/Sets/standard.php index 786d095..db52f6e 100644 --- a/config/Sets/standard.php +++ b/config/Sets/standard.php @@ -12,6 +12,7 @@ use Worksome\Graphlint\Inspections\NonNullableInsideListInspection; use Worksome\Graphlint\Inspections\NonNullableListInspection; use Worksome\Graphlint\Inspections\PascalCaseObjectTypeDefinitionInspection; +use Worksome\Graphlint\Inspections\UpperSnakeCaseEnumCaseDefinitionInspection; return function (ContainerConfigurator $config): void { $services = $config->services(); @@ -25,6 +26,7 @@ NonNullableListInspection::class, PascalCaseObjectTypeDefinitionInspection::class, DescriptionRequiredInspection::class, + UpperSnakeCaseEnumCaseDefinitionInspection::class, ]; foreach ($inspections as $inspection) { diff --git a/src/Fixes/UpperSnakeCaseNameFixer.php b/src/Fixes/UpperSnakeCaseNameFixer.php new file mode 100644 index 0000000..9636cac --- /dev/null +++ b/src/Fixes/UpperSnakeCaseNameFixer.php @@ -0,0 +1,41 @@ +getNode(); + + if (! $node instanceof NameNode) { + return; + } + + $name = $this->nodeNameResolver->getName($node); + + if ($name === null) { + return; + } + + $upperCase = Str::of($name) + ->replace('_', ' ') + ->title() + ->snake() + ->upper(); + + $node->value = $upperCase->__toString(); + } +} diff --git a/src/Inspections/UpperSnakeCaseEnumCaseDefinitionInspection.php b/src/Inspections/UpperSnakeCaseEnumCaseDefinitionInspection.php new file mode 100644 index 0000000..b55dc71 --- /dev/null +++ b/src/Inspections/UpperSnakeCaseEnumCaseDefinitionInspection.php @@ -0,0 +1,57 @@ +nameResolver->getName($enumValueDefinitionNode); + + if ($name === null) { + return; + } + + if ($this->apolloFederationChecker->isApolloDefinitionName($enumValueDefinitionNode->name)) { + return; + } + + $upperCase = Str::of($name)->replace('_', ' ')->title()->snake()->upper()->__toString(); + + if ($name === $upperCase) { + return; + } + + $problemsHolder->registerProblemWithDescription( + $enumValueDefinitionNode->name, + $this->definition()->getTitle(), + $this->upperCaseNameFixer, + ); + } + + public function definition(): InspectionDescription + { + return new InspectionDescription( + "Enum cases must be UPPER_CASE.", + ); + } +} diff --git a/test-resources/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest/lower_snake_case_enum_case.graphql.inc b/test-resources/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest/lower_snake_case_enum_case.graphql.inc new file mode 100644 index 0000000..f08b412 --- /dev/null +++ b/test-resources/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest/lower_snake_case_enum_case.graphql.inc @@ -0,0 +1,7 @@ +enum Status { + in_progress +} +----- +enum Status { + IN_PROGRESS +} diff --git a/test-resources/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest/pascal_case_enum_case.graphql.inc b/test-resources/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest/pascal_case_enum_case.graphql.inc new file mode 100644 index 0000000..98f8d39 --- /dev/null +++ b/test-resources/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest/pascal_case_enum_case.graphql.inc @@ -0,0 +1,7 @@ +enum Status { + Active +} +----- +enum Status { + ACTIVE +} diff --git a/test-resources/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest/upper_snake_case_enum_case.skip.graphql.inc b/test-resources/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest/upper_snake_case_enum_case.skip.graphql.inc new file mode 100644 index 0000000..404e503 --- /dev/null +++ b/test-resources/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest/upper_snake_case_enum_case.skip.graphql.inc @@ -0,0 +1,4 @@ +enum Status { + ACTIVE + IN_PROGRESS +} diff --git a/tests/Feature/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest.php b/tests/Feature/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest.php new file mode 100644 index 0000000..79d338e --- /dev/null +++ b/tests/Feature/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest.php @@ -0,0 +1,19 @@ +get(UpperSnakeCaseEnumCaseDefinitionInspection::class); + + expect($smartFileInfo) + ->toPassInspection($inspection); +})->with(yieldFixtures( + __DIR__ . '/../../../test-resources/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest' +));