-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): implement noLowerCaseEnum (#4031)
Co-authored-by: Emanuele Stoppa <[email protected]>
- Loading branch information
Showing
12 changed files
with
174 additions
and
9 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
74 changes: 74 additions & 0 deletions
74
crates/biome_graphql_analyze/src/lint/nursery/use_naming_convention.rs
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,74 @@ | ||
use biome_analyze::RuleSource; | ||
use biome_analyze::RuleSourceKind; | ||
use biome_analyze::{context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic}; | ||
use biome_console::markup; | ||
use biome_graphql_syntax::GraphqlEnumValueDefinition; | ||
use biome_rowan::AstNode; | ||
|
||
declare_lint_rule! { | ||
/// Validates that all enum values are capitalized. | ||
/// | ||
/// By convention in GraphQL, enum values are all caps. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```graphql,expect_diagnostic | ||
/// enum MyEnum { | ||
/// value | ||
/// } | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```graphql | ||
/// enum MyEnum { | ||
/// VALUE | ||
/// } | ||
/// ``` | ||
/// | ||
pub UseNamingConvention { | ||
version: "next", | ||
name: "useNamingConvention", | ||
language: "graphql", | ||
recommended: false, | ||
sources: &[RuleSource::EslintGraphqlSchemaLinter("enum-values-all-caps")], | ||
source_kind: RuleSourceKind::Inspired, | ||
} | ||
} | ||
|
||
impl Rule for UseNamingConvention { | ||
type Query = Ast<GraphqlEnumValueDefinition>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { | ||
let node = ctx.query(); | ||
if node.text().chars().any(|c| c.is_lowercase()) { | ||
return Some(()); | ||
} | ||
|
||
None | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
// | ||
// Read our guidelines to write great diagnostics: | ||
// https://docs.rs/biome_analyze/latest/biome_analyze/#what-a-rule-should-say-to-the-user | ||
// | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
ctx.query().range(), | ||
markup! { | ||
"Enum values should be in all caps." | ||
}, | ||
) | ||
.note(markup! { | ||
"Change the enum value to be in all caps." | ||
}), | ||
) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
crates/biome_graphql_analyze/tests/specs/nursery/useNamingConvention/invalid.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,3 @@ | ||
enum Status { | ||
Active | ||
} |
29 changes: 29 additions & 0 deletions
29
crates/biome_graphql_analyze/tests/specs/nursery/useNamingConvention/invalid.graphql.snap
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,29 @@ | ||
--- | ||
source: crates/biome_graphql_analyze/tests/spec_tests.rs | ||
expression: invalid.graphql | ||
snapshot_kind: text | ||
--- | ||
# Input | ||
```graphql | ||
enum Status { | ||
Active | ||
} | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.graphql:2:2 lint/nursery/useNamingConvention ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Enum values should be in all caps. | ||
1 │ enum Status { | ||
> 2 │ Active | ||
│ ^^^^^^ | ||
3 │ } | ||
4 │ | ||
i Change the enum value to be in all caps. | ||
``` |
4 changes: 4 additions & 0 deletions
4
crates/biome_graphql_analyze/tests/specs/nursery/useNamingConvention/valid.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,4 @@ | ||
enum Status { | ||
ACTIVE | ||
INACTIVE | ||
} |
12 changes: 12 additions & 0 deletions
12
crates/biome_graphql_analyze/tests/specs/nursery/useNamingConvention/valid.graphql.snap
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,12 @@ | ||
--- | ||
source: crates/biome_graphql_analyze/tests/spec_tests.rs | ||
expression: valid.graphql | ||
--- | ||
# Input | ||
```graphql | ||
enum Status { | ||
ACTIVE | ||
INACTIVE | ||
} | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.