Skip to content

Commit

Permalink
feat(linter): implement noLowerCaseEnum (biomejs#3990)
Browse files Browse the repository at this point in the history
  • Loading branch information
eMerzh committed Sep 21, 2024
1 parent 29266a0 commit e8ca1e8
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 53 deletions.
126 changes: 73 additions & 53 deletions crates/biome_configuration/src/analyzer/linter/rules.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/biome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ define_categories! {
"lint/nursery/noInvalidGridAreas": "https://biomejs.dev/linter/rules/use-consistent-grid-areas",
"lint/nursery/noInvalidPositionAtImportRule": "https://biomejs.dev/linter/rules/no-invalid-position-at-import-rule",
"lint/nursery/noIrregularWhitespace": "https://biomejs.dev/linter/rules/no-irregular-whitespace",
"lint/nursery/noLowerCaseEnum": "https://biomejs.dev/linter/rules/no-lower-case-enum",
"lint/nursery/noMissingGenericFamilyKeyword": "https://biomejs.dev/linter/rules/no-missing-generic-family-keyword",
"lint/nursery/noMissingVarFunction": "https://biomejs.dev/linter/rules/no-missing-var-function",
"lint/nursery/noOctalEscape": "https://biomejs.dev/linter/rules/no-octal-escape",
Expand Down
2 changes: 2 additions & 0 deletions crates/biome_graphql_analyze/src/lint/nursery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
use biome_analyze::declare_lint_group;

pub mod no_duplicated_fields;
pub mod no_lower_case_enum;
pub mod use_deprecated_reason;

declare_lint_group! {
pub Nursery {
name : "nursery" ,
rules : [
self :: no_duplicated_fields :: NoDuplicatedFields ,
self :: no_lower_case_enum :: NoLowerCaseEnum ,
self :: use_deprecated_reason :: UseDeprecatedReason ,
]
}
Expand Down
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."
}),
)
}
}
2 changes: 2 additions & 0 deletions crates/biome_graphql_analyze/src/options.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enum Status {
Active
}
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 {
> 2Active
^^^^^^
3}
4 │
i Change the enum value to be in all caps.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum Status {
ACTIVE
INACTIVE
}
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
}
```
5 changes: 5 additions & 0 deletions packages/@biomejs/backend-jsonrpc/src/workspace.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/@biomejs/biome/configuration_schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e8ca1e8

Please sign in to comment.