-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from worksome/feature/upper-snake-case-enum-cases
feat: add rule for upper-snake case enum cases
- Loading branch information
Showing
7 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
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
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
57
src/Inspections/UpperSnakeCaseEnumCaseDefinitionInspection.php
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,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.", | ||
); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...ons/UpperSnakeCaseEnumCaseDefinitionInspectionTest/lower_snake_case_enum_case.graphql.inc
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,7 @@ | ||
enum Status { | ||
in_progress | ||
} | ||
----- | ||
enum Status { | ||
IN_PROGRESS | ||
} |
7 changes: 7 additions & 0 deletions
7
...pections/UpperSnakeCaseEnumCaseDefinitionInspectionTest/pascal_case_enum_case.graphql.inc
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,7 @@ | ||
enum Status { | ||
Active | ||
} | ||
----- | ||
enum Status { | ||
ACTIVE | ||
} |
4 changes: 4 additions & 0 deletions
4
...pperSnakeCaseEnumCaseDefinitionInspectionTest/upper_snake_case_enum_case.skip.graphql.inc
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,4 @@ | ||
enum Status { | ||
ACTIVE | ||
IN_PROGRESS | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/Feature/Inspections/UpperSnakeCaseEnumCaseDefinitionInspectionTest.php
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,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' | ||
)); |