Skip to content

Commit

Permalink
Merge pull request #28 from worksome/feature/upper-snake-case-enum-cases
Browse files Browse the repository at this point in the history
feat: add rule for upper-snake case enum cases
  • Loading branch information
owenvoke authored Feb 10, 2023
2 parents 05e0205 + f312560 commit a6cee1b
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/Sets/standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -25,6 +26,7 @@
NonNullableListInspection::class,
PascalCaseObjectTypeDefinitionInspection::class,
DescriptionRequiredInspection::class,
UpperSnakeCaseEnumCaseDefinitionInspection::class,
];

foreach ($inspections as $inspection) {
Expand Down
41 changes: 41 additions & 0 deletions src/Fixes/UpperSnakeCaseNameFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Worksome\Graphlint\Fixes;

use GraphQL\Language\AST\NameNode;
use Illuminate\Support\Str;
use Worksome\Graphlint\ProblemDescriptor;
use Worksome\Graphlint\Utils\NodeNameResolver;

class UpperSnakeCaseNameFixer extends Fixer
{
public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
) {
}

public function fix(ProblemDescriptor $problemDescriptor): void
{
$node = $problemDescriptor->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();
}
}
57 changes: 57 additions & 0 deletions src/Inspections/UpperSnakeCaseEnumCaseDefinitionInspection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Worksome\Graphlint\Inspections;

use GraphQL\Language\AST\EnumValueDefinitionNode;
use Illuminate\Support\Str;
use Worksome\Graphlint\Fixes\UpperSnakeCaseNameFixer;
use Worksome\Graphlint\InspectionDescription;
use Worksome\Graphlint\ProblemsHolder;
use Worksome\Graphlint\Utils\ApolloFederationChecker;
use Worksome\Graphlint\Utils\NodeNameResolver;

class UpperSnakeCaseEnumCaseDefinitionInspection extends Inspection
{
public function __construct(
private readonly NodeNameResolver $nameResolver,
private readonly UpperSnakeCaseNameFixer $upperCaseNameFixer,
private readonly ApolloFederationChecker $apolloFederationChecker,
) {
}

public function visitEnumValueDefinition(
ProblemsHolder $problemsHolder,
EnumValueDefinitionNode $enumValueDefinitionNode,
): void {
$name = $this->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.",
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum Status {
in_progress
}
-----
enum Status {
IN_PROGRESS
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum Status {
Active
}
-----
enum Status {
ACTIVE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum Status {
ACTIVE
IN_PROGRESS
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Worksome\Graphlint\Tests\Feature\Inspections;

use Symplify\SmartFileSystem\SmartFileInfo;
use Worksome\Graphlint\Inspections\UpperSnakeCaseEnumCaseDefinitionInspection;
use function Worksome\Graphlint\Tests\app;
use function Worksome\Graphlint\Tests\yieldFixtures;

it('can rename type', function (SmartFileInfo $smartFileInfo) {
$inspection = app()->get(UpperSnakeCaseEnumCaseDefinitionInspection::class);

expect($smartFileInfo)
->toPassInspection($inspection);
})->with(yieldFixtures(
__DIR__ . '/../../../test-resources/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest'
));

0 comments on commit a6cee1b

Please sign in to comment.