forked from biomejs/biome
-
Notifications
You must be signed in to change notification settings - Fork 0
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 (biomejs#3990)
- Loading branch information
Showing
11 changed files
with
213 additions
and
53 deletions.
There are no files selected for viewing
126 changes: 73 additions & 53 deletions
126
crates/biome_configuration/src/analyzer/linter/rules.rs
Large diffs are not rendered by default.
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
76 changes: 76 additions & 0 deletions
76
crates/biome_graphql_analyze/src/lint/nursery/no_lower_case_enum.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,76 @@ | ||
use biome_analyze::{context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic}; | ||
use biome_console::markup; | ||
use biome_graphql_syntax::{GraphqlEnumValueDefinition, GraphqlRoot}; | ||
use biome_rowan::{AstNode, TextRange}; | ||
|
||
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 NoLowerCaseEnum { | ||
version: "next", | ||
name: "noLowerCaseEnum", | ||
language: "graphql", | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for NoLowerCaseEnum { | ||
type Query = Ast<GraphqlRoot>; | ||
type State = TextRange; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { | ||
let node = ctx.query(); | ||
for val in node | ||
.syntax() | ||
.descendants() | ||
.filter_map(GraphqlEnumValueDefinition::cast) | ||
{ | ||
if val.text().chars().any(|c| c.is_lowercase()) { | ||
return Some(val.range()); | ||
} | ||
} | ||
|
||
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!(), | ||
state, | ||
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/noLowerCaseEnum/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 | ||
} |
28 changes: 28 additions & 0 deletions
28
crates/biome_graphql_analyze/tests/specs/nursery/noLowerCaseEnum/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,28 @@ | ||
--- | ||
source: crates/biome_graphql_analyze/tests/spec_tests.rs | ||
expression: invalid.graphql | ||
--- | ||
# Input | ||
```graphql | ||
enum Status { | ||
Active | ||
} | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.graphql:2:2 lint/nursery/noLowerCaseEnum ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! 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/noLowerCaseEnum/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/noLowerCaseEnum/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.